• 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

65.55
/src/arkreactor/Exceptions.cpp
1
#include <Ark/Exceptions.hpp>
2

3
#include <cassert>
4
#include <sstream>
5
#include <algorithm>
6
#include <fmt/core.h>
7
#include <fmt/color.h>
8
#include <fmt/ostream.h>
9

10
#include <Ark/Constants.hpp>
11
#include <Ark/Utils.hpp>
12
#include <Ark/Files.hpp>
13
#include <Ark/Literals.hpp>
14
#include <Ark/Compiler/AST/Node.hpp>
15

16
namespace Ark::Diagnostics
17
{
18
    struct LineColorContextCounts
218✔
19
    {
20
        int open_parentheses = 0;
218✔
21
        int open_square_braces = 0;
218✔
22
        int open_curly_braces = 0;
218✔
23
    };
24

25
    inline bool isPairableChar(const char c)
×
26
    {
×
UNCOV
27
        return c == '(' || c == ')' || c == '[' || c == ']' || c == '{' || c == '}';
×
28
    }
29

30
    void colorizeLine(const std::string& line, LineColorContextCounts& line_color_context_counts, std::ostream& ss)
×
UNCOV
31
    {
×
32
        // clang-format off
UNCOV
33
        constexpr std::array pairing_color {
×
34
            fmt::color::light_blue,
35
            fmt::color::light_green,
36
            fmt::color::light_salmon,
37
            fmt::color::light_yellow,
38
            fmt::color::light_cyan,
39
            fmt::color::light_coral
40
        };
41
        // clang-format on
UNCOV
42
        constexpr std::size_t pairing_color_size = pairing_color.size();
×
43

UNCOV
44
        for (const char& c : line)
×
45
        {
UNCOV
46
            if (isPairableChar(c))
×
47
            {
UNCOV
48
                std::size_t pairing_color_index = 0;
×
49

50
                switch (c)
×
UNCOV
51
                {
×
52
                    case '(':
53
                        pairing_color_index = static_cast<std::size_t>(std::abs(line_color_context_counts.open_parentheses)) % pairing_color_size;
×
54
                        line_color_context_counts.open_parentheses++;
×
UNCOV
55
                        break;
×
56
                    case ')':
57
                        line_color_context_counts.open_parentheses--;
×
58
                        pairing_color_index = static_cast<std::size_t>(std::abs(line_color_context_counts.open_parentheses)) % pairing_color_size;
×
UNCOV
59
                        break;
×
60
                    case '[':
61
                        pairing_color_index = static_cast<std::size_t>(std::abs(line_color_context_counts.open_square_braces)) % pairing_color_size;
×
62
                        line_color_context_counts.open_square_braces++;
×
UNCOV
63
                        break;
×
64
                    case ']':
65
                        line_color_context_counts.open_square_braces--;
×
66
                        pairing_color_index = static_cast<std::size_t>(std::abs(line_color_context_counts.open_square_braces)) % pairing_color_size;
×
UNCOV
67
                        break;
×
68
                    case '{':
69
                        pairing_color_index = static_cast<std::size_t>(std::abs(line_color_context_counts.open_curly_braces)) % pairing_color_size;
×
70
                        line_color_context_counts.open_curly_braces++;
×
UNCOV
71
                        break;
×
72
                    case '}':
73
                        line_color_context_counts.open_curly_braces--;
×
74
                        pairing_color_index = static_cast<std::size_t>(std::abs(line_color_context_counts.open_curly_braces)) % pairing_color_size;
×
UNCOV
75
                        break;
×
76
                    default:
77
                        break;
×
UNCOV
78
                }
×
79

80
                fmt::print(ss, "{}", fmt::styled(c, fmt::fg(pairing_color[pairing_color_index])));
×
UNCOV
81
            }
×
82
            else
83
                fmt::print(ss, "{}", c);
×
84
        }
×
UNCOV
85
    }
×
86

87
    void makeContext(
219✔
88
        std::ostream& os,
89
        const std::string& filename,
90
        const std::optional<std::string>& expr,
91
        const std::size_t sym_size,
92
        const std::size_t target_line,
93
        const std::size_t col_start,
94
        const std::optional<CodeErrorContext>& maybe_context,  // can not be populated at runtime, only compile time
95
        const bool whole_line,
96
        const bool colorize)
97
    {
219✔
98
        assert(!(maybe_context && whole_line) && "Can not create error context when a context is given AND the whole line has to be underlined");
219✔
99

100
        using namespace Ark::literals;
101

102
        auto show_file_location = [&] {
438✔
103
            if (filename != ARK_NO_NAME_FILE)
219✔
104
                fmt::print(os, "In file {}:{}\n", filename, target_line + 1);
219✔
105
            if (expr)
219✔
106
                fmt::print(os, "At {} @ {}:{}\n", expr.value(), target_line + 1, col_start);
117✔
107
        };
219✔
108

109
        auto compute_start_end_window = [](const std::size_t center_of_window, const std::size_t line_count) {
437✔
110
            std::size_t start = center_of_window >= 3 ? center_of_window - 3 : 0;
218✔
111
            std::size_t end = center_of_window + 3 <= line_count ? center_of_window + 3 : line_count;
218✔
112
            return std::make_pair(start, end);
218✔
113
        };
218✔
114

115
        auto print_line = [&os, colorize](const std::size_t i, const std::vector<std::string>& lines, LineColorContextCounts& color_context) {
760✔
116
            // show current line with its number
117
            fmt::print(os, "{: >5} |{}", i + 1, !lines[i].empty() ? " " : "");
541✔
118
            if (colorize)
541✔
UNCOV
119
                colorizeLine(lines[i], color_context, os);
×
120
            else
121
                fmt::print(os, "{}", lines[i]);
541✔
122
            fmt::print(os, "\n");
541✔
123
        };
541✔
124

125
        const std::string line_no_num = "      |";
219✔
126

127
        auto print_context_hint = [&os, &maybe_context, &line_no_num, colorize]() mutable {
227✔
128
            if (!maybe_context)
8✔
UNCOV
129
                return;
×
130

131
            fmt::print(os, "{}", line_no_num);
8✔
132
            fmt::print(
24✔
133
                os,
8✔
134
                "{: <{}}{}\n",
8✔
135
                // padding os spaces
136
                " ",
137
                std::max(1_z, maybe_context->col),  // fixing padding when the error is on the first character
8✔
138
                // underline the parent of the error in red
139
                fmt::styled(
16✔
140
                    maybe_context->is_macro_expansion ? "^ macro expansion started here" : "^ expression started here",
8✔
141
                    colorize ? fmt::fg(fmt::color::red) : fmt::text_style()));
8✔
142
        };
8✔
143

144
        const std::string code = filename == ARK_NO_NAME_FILE ? "" : Utils::readFile(filename);
219✔
145
        const std::vector<std::string> lines = Utils::splitString(code, '\n');
219✔
146
        if (target_line >= lines.size() || code.empty())
219✔
147
        {
148
            // show the "in file..." before early return
149
            show_file_location();
1✔
150
            return;
1✔
151
        }
152

153
        auto [first_line, last_line] = compute_start_end_window(target_line, lines.size());
1,236✔
154
        // number of characters that are on more lines below
155
        std::size_t overflow = (col_start + sym_size < lines[target_line].size()) ? 0 : col_start + sym_size - lines[target_line].size();
218✔
156

157
        const bool ctx_same_file = maybe_context && maybe_context->filename == filename;
218✔
158
        const bool ctx_in_window = ctx_same_file && maybe_context &&
232✔
159
            maybe_context->line >= first_line &&
25✔
160
            maybe_context->line < last_line;
11✔
161

162
        std::size_t start_line_skipping_at = 0;
218✔
163
        std::size_t stop_line_skipping_at = first_line;
436✔
164
        if (ctx_same_file && !ctx_in_window)
218✔
165
        {
166
            // showing the context will require an ellipsis, to avoid showing too many lines in the error message
167
            if (maybe_context->line + 3 < first_line)
3✔
168
                start_line_skipping_at = maybe_context->line + 3;
3✔
169
            else
UNCOV
170
                stop_line_skipping_at = start_line_skipping_at;
×
171

172
            // due to how context works, if it points to the same file,
173
            // we are guaranteed it will be before our error
174
            first_line = maybe_context->line >= 3 ? maybe_context->line - 3 : 0;
3✔
175
        }
3✔
176
        else if (maybe_context && !ctx_same_file && !maybe_context->filename.empty())
215✔
177
        {
178
            // show the location of the parent of our error first
179
            fmt::print(os, "Error originated from file {}:{}\n", maybe_context->filename, maybe_context->line + 1);
×
180

181
            const std::vector<std::string> ctx_source_lines = Utils::splitString(Utils::readFile(maybe_context->filename), '\n');
×
UNCOV
182
            auto [ctx_first_line, ctx_last_line] = compute_start_end_window(maybe_context->line, ctx_source_lines.size());
×
183
            LineColorContextCounts line_color_context_counts;
×
184

185
            for (auto i = ctx_first_line; i < ctx_last_line; ++i)
×
186
            {
187
                print_line(i, ctx_source_lines, line_color_context_counts);
×
188
                if (i == maybe_context->line)
×
UNCOV
189
                    print_context_hint();
×
190
            }
×
191

UNCOV
192
            fmt::print(os, "\n");
×
UNCOV
193
        }
×
194

195
        show_file_location();
218✔
196
        LineColorContextCounts line_color_context_counts;
218✔
197

198
        for (auto i = first_line; i < last_line; ++i)
990✔
199
        {
200
            if (i >= start_line_skipping_at && i < stop_line_skipping_at)
554✔
201
                continue;
13✔
202
            print_line(i, lines, line_color_context_counts);
541✔
203

204
            // if the error context is in the current file, point to it as the parent of our error
205
            if (maybe_context && i == maybe_context->line && i != target_line)
541✔
206
                print_context_hint();
8✔
207

208
            // if the next line number wants us to skip line, and start != stop (meaning they got adjusted),
209
            // display an ellipsis
210
            if (i + 1 == start_line_skipping_at && i + 1 != stop_line_skipping_at)
541✔
211
                fmt::print(os, "  ... |\n");
3✔
212

213
            // show where the error occurred (do not mark empty lines as being part of the error when we have overflow)
214
            if (i == target_line || (i > target_line && overflow > 0 && !lines[i].empty()))
541✔
215
            {
216
                fmt::print(os, "{}", line_no_num);
220✔
217

218
                if (!whole_line)
220✔
219
                {
220
                    // if we have an overflow then we start at the beginning of the line
221
                    const std::size_t curr_col_start = (overflow == 0) ? col_start : 0;
118✔
222
                    // if we have an overflow, it is used as the end of the line
223
                    const std::size_t col_end = (i == target_line) ? std::min<std::size_t>(col_start + sym_size, lines[target_line].size())
120✔
224
                                                                   : std::min<std::size_t>(overflow, lines[i].size());
2✔
225
                    // update the overflow to avoid going here again if not needed
226
                    overflow = (overflow > lines[i].size()) ? overflow - lines[i].size() : 0;
118✔
227

228
                    // show the error where it's at, using the normal process, if there is no context OR if the context line is different from the error line
229
                    if (!maybe_context || maybe_context->line != target_line)
118✔
230
                        fmt::print(
324✔
231
                            os,
108✔
232
                            "{: <{}}{:~<{}}\n",
108✔
233
                            // padding of spaces
234
                            " ",
235
                            std::max(1_z, curr_col_start),  // fixing padding when the error is on the first character
108✔
236
                            // underline the error in red
237
                            fmt::styled("^", colorize ? fmt::fg(fmt::color::red) : fmt::text_style()),
108✔
238
                            col_end - curr_col_start);
108✔
239
                    else if (i == target_line)  // maybe_context has a value, i == target_line to avoid having to deal with overflow
10✔
240
                    {
241
                        const auto padding_size = std::max(1_z, maybe_context->col);
10✔
242

243
                        fmt::print(
20✔
244
                            os,
10✔
245
                            "{: <{}}{}{}{}\n",
10✔
246
                            // padding of spaces
247
                            " ",
248
                            padding_size,
249
                            // indicate where the parent is, with color
250
                            fmt::styled("│", colorize ? fmt::fg(fmt::color::red) : fmt::text_style()),
10✔
251
                            // yet another padding of spaces between the parent and error column (if need be)
252
                            // -2 to account for the │ and then └
253
                            (col_start - maybe_context->col <= 2) ? "" : fmt::format("{: <{}}", " ", col_start - maybe_context->col - 2),
10✔
254
                            // underline the error in red
255
                            fmt::styled("└─ error", colorize ? fmt::fg(fmt::color::red) : fmt::text_style()));
10✔
256
                        // new line, some spacing between the error and the parent
257
                        fmt::print(os, "{}{: <{}}{}\n", line_no_num, " ", padding_size, fmt::styled("│", colorize ? fmt::fg(fmt::color::red) : fmt::text_style()));
10✔
258
                        // new line, now show the "expression started here for the source"
259
                        fmt::print(
20✔
260
                            os,
10✔
261
                            "{}{: <{}}{}\n",
10✔
262
                            line_no_num,
263
                            // padding of spaces
264
                            " ",
265
                            padding_size,
266
                            fmt::styled(
20✔
267
                                maybe_context->is_macro_expansion ? "└─ macro expansion started here" : "└─ expression started here",
10✔
268
                                colorize ? fmt::fg(fmt::color::red) : fmt::text_style()));
10✔
269
                    }
10✔
270
                }
118✔
271
                else
272
                {
273
                    // first non-whitespace character of the line
274
                    // +1 for the leading whitespace after `    |` before the code
275
                    const std::size_t curr_col_start = lines[i].find_first_not_of(" \t\v") + 1;
102✔
276

277
                    // highlight the current line but skip any leading whitespace
278
                    fmt::print(
204✔
279
                        os,
102✔
280
                        "{: <{}}{:~<{}}\n",
102✔
281
                        // padding of spaces
282
                        " ",
283
                        curr_col_start,
284
                        // underline the whole line in red
285
                        fmt::styled("^", colorize ? fmt::fg(fmt::color::red) : fmt::text_style()),
102✔
286
                        lines[target_line].size() - curr_col_start);
102✔
287
                }
102✔
288
            }
220✔
289
        }
541✔
290
    }
219✔
291

292
    void helper(std::ostream& os, const std::string& message, const bool colorize,
117✔
293
                const std::string& filename,
294
                const std::optional<std::string>& expr, const std::size_t sym_size,
295
                const std::size_t line, const std::size_t column,
296
                const std::optional<CodeErrorContext>& maybe_context = std::nullopt)
297
    {
117✔
298
        makeContext(os, filename, expr, sym_size, line, column, maybe_context, /* whole_line= */ false, colorize);
117✔
299

300
        const auto message_lines = Utils::splitString(message, '\n');
117✔
301
        for (const auto& text : message_lines)
234✔
302
            fmt::print(os, "        {}\n", text);
117✔
303
    }
117✔
304

UNCOV
305
    std::string makeContextWithNode(const std::string& message, const internal::Node& node)
×
UNCOV
306
    {
×
UNCOV
307
        std::stringstream ss;
×
308

UNCOV
309
        std::size_t size = 3;
×
UNCOV
310
        if (node.isStringLike())
×
UNCOV
311
            size = node.string().size();
×
312

UNCOV
313
        helper(
×
UNCOV
314
            ss,
×
UNCOV
315
            message,
×
316
            true,
UNCOV
317
            node.filename(),
×
UNCOV
318
            node.repr(),
×
UNCOV
319
            size,
×
UNCOV
320
            node.line(),
×
UNCOV
321
            node.col());
×
322

UNCOV
323
        return ss.str();
×
UNCOV
324
    }
×
325

326
    void generate(const CodeError& e, std::ostream& os, bool colorize)
117✔
327
    {
117✔
328
        if (const char* nocolor = std::getenv("NOCOLOR"); nocolor != nullptr)
117✔
UNCOV
329
            colorize = false;
×
330

331
        std::string escaped_symbol;
117✔
332
        if (e.context.symbol.has_value())
117✔
333
        {
334
            switch (e.context.symbol.value().codepoint())
42✔
UNCOV
335
            {
×
UNCOV
336
                case '\n': escaped_symbol = "'\\n'"; break;
×
UNCOV
337
                case '\r': escaped_symbol = "'\\r'"; break;
×
UNCOV
338
                case '\t': escaped_symbol = "'\\t'"; break;
×
339
                case '\v': escaped_symbol = "'\\v'"; break;
11✔
340
                case '\0': escaped_symbol = "EOF"; break;
13✔
341
                case ' ': escaped_symbol = "' '"; break;
31✔
342
                default:
343
                    escaped_symbol = e.context.symbol.value().c_str();
29✔
344
            }
42✔
345
        }
42✔
346
        else
347
            escaped_symbol = e.context.expr;
75✔
348

349
        helper(
351✔
350
            os,
117✔
351
            e.what(),
117✔
352
            colorize,
117✔
353
            e.context.filename,
117✔
354
            escaped_symbol,
117✔
355
            e.context.expr.size(),
117✔
356
            e.context.line,
117✔
357
            e.context.col,
117✔
358
            e.additional_context);
117✔
359
    }
117✔
360
}
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