• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ArkScript-lang / Ark / 15006215616

13 May 2025 08:34PM UTC coverage: 86.726% (+0.3%) from 86.474%
15006215616

push

github

SuperFola
feat(macro processor, error): adding better error messages when a macro fails, to show the macro we were expanding and what failed

60 of 60 new or added lines in 8 files covered. (100.0%)

83 existing lines in 6 files now uncovered.

7017 of 8091 relevant lines covered (86.73%)

79023.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.97
/include/Ark/Exceptions.hpp
1
/**
2
 * @file Exceptions.hpp
3
 * @author Alexandre Plateau (lexplt.dev@gmail.com), Max (madstk1@pm.me)
4
 * @brief ArkScript homemade exceptions
5
 * @date 2020-10-27
6
 *
7
 * @copyright Copyright (c) 2020-2025
8
 *
9
 */
10

11
#ifndef INCLUDE_ARK_EXCEPTIONS_HPP
12
#define INCLUDE_ARK_EXCEPTIONS_HPP
13

14
#include <string>
15
#include <utility>
16
#include <vector>
17
#include <stdexcept>
18
#include <optional>
19
#include <ostream>
20
#include <iostream>
21

22
#include <Ark/Compiler/AST/utf8_char.hpp>
23
#include <Ark/Platform.hpp>
24

25
namespace Ark
26
{
27
    namespace internal
28
    {
29
        class Node;
30
    }
31

32
    class ARK_API Error : public std::runtime_error
288✔
33
    {
34
    public:
35
        explicit Error(const std::string& message) :
288✔
36
            std::runtime_error(message)
288✔
37
        {}
576✔
38

39
        [[nodiscard]] virtual std::string details(bool colorize [[maybe_unused]]) const
×
40
        {
×
41
            return what();
×
UNCOV
42
        }
×
43
    };
44

45
    /**
46
     * @brief A type error triggered when types don't match
47
     *
48
     */
UNCOV
49
    class ARK_API TypeError final : public Error
×
50
    {
51
    public:
52
        explicit TypeError(const std::string& message) :
×
53
            Error(message)
×
UNCOV
54
        {}
×
55
    };
56

57
    /**
58
     * @brief An assertion error, only triggered from ArkScript code through (assert expr error-message)
59
     *
60
     */
UNCOV
61
    class ARK_API AssertionFailed final : public Error
×
62
    {
63
    public:
64
        explicit AssertionFailed(const std::string& message) :
×
65
            Error("AssertionFailed: " + message)
×
UNCOV
66
        {}
×
67
    };
68

69
    class ARK_API NestedError final : public Error
102✔
70
    {
71
    public:
72
        NestedError(const Error& e, const std::string& details) :
69✔
73
            Error("NestedError"),
69✔
74
            m_details(e.details(/* colorize= */ false))
69✔
75
        {
138✔
76
            if (!m_details.empty() && m_details.back() != '\n')
69✔
UNCOV
77
                m_details += '\n';
×
78
            m_details += "\n" + details;
69✔
79
        }
69✔
80

81
        NestedError(const std::exception& e, const std::string& details) :
33✔
82
            Error("NestedError"),
33✔
83
            m_details(e.what())
33✔
84
        {
66✔
85
            if (!m_details.empty() && m_details.back() != '\n')
33✔
86
                m_details += '\n';
10✔
87
            m_details += "\n" + details;
33✔
88
        }
33✔
89

90
        [[nodiscard]] const char* what() const noexcept override
102✔
91
        {
102✔
92
            return m_details.c_str();
102✔
93
        }
94

95
    private:
96
        std::string m_details;
97
    };
98

99
    struct ARK_API CodeErrorContext final
250,438✔
100
    {
101
        const std::string filename;
102
        const std::size_t line;
103
        const std::size_t col;
104
        const std::string expr;
105
        const std::optional<internal::utf8_char_t> symbol;
106
        const bool is_macro_expansion = false;
217,587✔
107

108
        CodeErrorContext(std::string filename_, const std::size_t lineNum, const std::size_t column, std::string expression, const std::optional<internal::utf8_char_t> maybe_symbol = std::nullopt) :
217,587✔
109
            filename(std::move(filename_)),
217,587✔
110
            line(lineNum),
217,587✔
111
            col(column),
217,587✔
112
            expr(std::move(expression)),
217,587✔
113
            symbol(maybe_symbol)
217,587✔
114
        {}
435,174✔
115

116
        CodeErrorContext(std::string filename_, const std::size_t lineNum, const std::size_t column, std::string expression, const bool from_macro_expansion) :
11✔
117
            filename(std::move(filename_)),
11✔
118
            line(lineNum),
11✔
119
            col(column),
11✔
120
            expr(std::move(expression)),
11✔
121
            symbol(std::nullopt),
11✔
122
            is_macro_expansion(from_macro_expansion)
11✔
123
        {}
22✔
124
    };
125

126
    /**
127
     * @brief CodeError thrown by the compiler (parser, macro processor, optimizer, and compiler itself)
128
     *
129
     */
130
    struct ARK_API CodeError final : Error
117✔
131
    {
132
        const CodeErrorContext context;
133
        const std::optional<CodeErrorContext> additional_context;
134

135
        CodeError(const std::string& what, CodeErrorContext ctx, std::optional<CodeErrorContext> maybe_more_context = std::nullopt) :
117✔
136
            Error(what),
117✔
137
            context(std::move(ctx)),
117✔
138
            additional_context(std::move(maybe_more_context))
117✔
139
        {}
351✔
140
    };
141

142
    namespace Diagnostics
143
    {
144
        /**
145
         * @brief Helper to create a colorized context to report errors to the user
146
         *
147
         * @param os stream in which the error will be written
148
         * @param filename path to the file in which the error is
149
         * @param expr optional expression causing the error
150
         * @param sym_size length of expression to underline (can be 0)
151
         * @param target_line line where the error is
152
         * @param col_start where the error starts on the given line
153
         * @param maybe_context optional context, parent of the error
154
         * @param whole_line when true, underline the whole line, disregarding col_start and sym_size
155
         * @param colorize generate colors or not
156
         */
157
        ARK_API void makeContext(
158
            std::ostream& os,
159
            const std::string& filename,
160
            const std::optional<std::string>& expr,
161
            std::size_t sym_size,
162
            std::size_t target_line,
163
            std::size_t col_start,
164
            const std::optional<CodeErrorContext>& maybe_context,
165
            bool whole_line,
166
            bool colorize);
167

168
        /**
169
         * @brief Helper used by the compiler to generate a colorized context from a node
170
         *
171
         * @param message error message to be included in the context
172
         * @param node AST node with the error
173
         * @return std::string
174
         */
175
        ARK_API std::string makeContextWithNode(const std::string& message, const internal::Node& node);
176

177
        /**
178
         * @brief Generate a diagnostic from an error and print it to the standard output
179
         *
180
         * @param e code error
181
         * @param os output stream
182
         * @param colorize generate colors or not
183
         */
184
        ARK_API void generate(const CodeError& e, std::ostream& os = std::cout, bool colorize = true);
185
    }
186
}
187

188
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc