backburn 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/Makefile +50 -0
- data/README.md +520 -0
- data/docs/cli-and-outputs.md +226 -0
- data/docs/technical-spec.md +656 -0
- data/exe/backburn +8 -0
- data/ext/backburn/extconf.rb +31 -0
- data/ext/backburn/prism/COMMIT +1 -0
- data/ext/backburn/prism/LICENSE.md +7 -0
- data/ext/backburn/prism/Makefile +213 -0
- data/ext/backburn/prism/include/prism/arena.h +45 -0
- data/ext/backburn/prism/include/prism/arena.hpp +42 -0
- data/ext/backburn/prism/include/prism/ast.h +10395 -0
- data/ext/backburn/prism/include/prism/ast.hpp +641 -0
- data/ext/backburn/prism/include/prism/buffer.h +60 -0
- data/ext/backburn/prism/include/prism/buffer.hpp +55 -0
- data/ext/backburn/prism/include/prism/comments.h +72 -0
- data/ext/backburn/prism/include/prism/compiler/accel.h +26 -0
- data/ext/backburn/prism/include/prism/compiler/align.h +36 -0
- data/ext/backburn/prism/include/prism/compiler/assume.h +25 -0
- data/ext/backburn/prism/include/prism/compiler/branch.h +26 -0
- data/ext/backburn/prism/include/prism/compiler/bswap.h +46 -0
- data/ext/backburn/prism/include/prism/compiler/cplusplus.hpp +16 -0
- data/ext/backburn/prism/include/prism/compiler/endianness.h +34 -0
- data/ext/backburn/prism/include/prism/compiler/exceptions.hpp +17 -0
- data/ext/backburn/prism/include/prism/compiler/exported.h +24 -0
- data/ext/backburn/prism/include/prism/compiler/fallthrough.h +22 -0
- data/ext/backburn/prism/include/prism/compiler/filesystem.h +32 -0
- data/ext/backburn/prism/include/prism/compiler/flex_array.h +19 -0
- data/ext/backburn/prism/include/prism/compiler/force_inline.h +21 -0
- data/ext/backburn/prism/include/prism/compiler/format.h +27 -0
- data/ext/backburn/prism/include/prism/compiler/inline.h +17 -0
- data/ext/backburn/prism/include/prism/compiler/lifetimebound.hpp +25 -0
- data/ext/backburn/prism/include/prism/compiler/nodiscard.h +22 -0
- data/ext/backburn/prism/include/prism/compiler/nonnull.h +18 -0
- data/ext/backburn/prism/include/prism/compiler/noreturn.h +24 -0
- data/ext/backburn/prism/include/prism/compiler/stack.h +60 -0
- data/ext/backburn/prism/include/prism/compiler/unused.h +18 -0
- data/ext/backburn/prism/include/prism/constant_pool.h +112 -0
- data/ext/backburn/prism/include/prism/diagnostic.h +121 -0
- data/ext/backburn/prism/include/prism/errors_format.h +63 -0
- data/ext/backburn/prism/include/prism/excludes.h +29 -0
- data/ext/backburn/prism/include/prism/integer.h +79 -0
- data/ext/backburn/prism/include/prism/internal/allocator.h +68 -0
- data/ext/backburn/prism/include/prism/internal/allocator_debug.h +88 -0
- data/ext/backburn/prism/include/prism/internal/arena.h +223 -0
- data/ext/backburn/prism/include/prism/internal/assert.h +14 -0
- data/ext/backburn/prism/include/prism/internal/bignum.h +239 -0
- data/ext/backburn/prism/include/prism/internal/bit.h +66 -0
- data/ext/backburn/prism/include/prism/internal/buffer.h +104 -0
- data/ext/backburn/prism/include/prism/internal/char.h +145 -0
- data/ext/backburn/prism/include/prism/internal/comments.h +54 -0
- data/ext/backburn/prism/include/prism/internal/constant_pool.h +155 -0
- data/ext/backburn/prism/include/prism/internal/diagnostic.h +392 -0
- data/ext/backburn/prism/include/prism/internal/double.h +19 -0
- data/ext/backburn/prism/include/prism/internal/encoding.h +326 -0
- data/ext/backburn/prism/include/prism/internal/integer.h +66 -0
- data/ext/backburn/prism/include/prism/internal/isinf.h +16 -0
- data/ext/backburn/prism/include/prism/internal/line_offset_list.h +50 -0
- data/ext/backburn/prism/include/prism/internal/list.h +62 -0
- data/ext/backburn/prism/include/prism/internal/magic_comments.h +23 -0
- data/ext/backburn/prism/include/prism/internal/memchr.h +15 -0
- data/ext/backburn/prism/include/prism/internal/node.h +197 -0
- data/ext/backburn/prism/include/prism/internal/oom.h +16 -0
- data/ext/backburn/prism/include/prism/internal/options.h +248 -0
- data/ext/backburn/prism/include/prism/internal/parser.h +1170 -0
- data/ext/backburn/prism/include/prism/internal/regexp.h +70 -0
- data/ext/backburn/prism/include/prism/internal/serialize.h +39 -0
- data/ext/backburn/prism/include/prism/internal/source.h +72 -0
- data/ext/backburn/prism/include/prism/internal/static_literals.h +60 -0
- data/ext/backburn/prism/include/prism/internal/stringy.h +37 -0
- data/ext/backburn/prism/include/prism/internal/strncasecmp.h +18 -0
- data/ext/backburn/prism/include/prism/internal/strpbrk.h +61 -0
- data/ext/backburn/prism/include/prism/internal/tokens.h +11 -0
- data/ext/backburn/prism/include/prism/internal/xxh3.h +16 -0
- data/ext/backburn/prism/include/prism/json.h +40 -0
- data/ext/backburn/prism/include/prism/line_offset_list.h +69 -0
- data/ext/backburn/prism/include/prism/magic_comments.h +53 -0
- data/ext/backburn/prism/include/prism/node.h +139 -0
- data/ext/backburn/prism/include/prism/node.hpp +272 -0
- data/ext/backburn/prism/include/prism/node_type.hpp +38 -0
- data/ext/backburn/prism/include/prism/options.h +393 -0
- data/ext/backburn/prism/include/prism/options.hpp +43 -0
- data/ext/backburn/prism/include/prism/parser.h +433 -0
- data/ext/backburn/prism/include/prism/parser.hpp +274 -0
- data/ext/backburn/prism/include/prism/prettyprint.h +39 -0
- data/ext/backburn/prism/include/prism/serialize.h +123 -0
- data/ext/backburn/prism/include/prism/source.h +163 -0
- data/ext/backburn/prism/include/prism/stream.h +36 -0
- data/ext/backburn/prism/include/prism/string_query.h +97 -0
- data/ext/backburn/prism/include/prism/stringy.h +61 -0
- data/ext/backburn/prism/include/prism/stringy.hpp +31 -0
- data/ext/backburn/prism/include/prism/version.h +46 -0
- data/ext/backburn/prism/include/prism.h +123 -0
- data/ext/backburn/prism/include/prism.hpp +24 -0
- data/ext/backburn/prism/src/arena.c +200 -0
- data/ext/backburn/prism/src/bignum.c +399 -0
- data/ext/backburn/prism/src/buffer.c +405 -0
- data/ext/backburn/prism/src/char.c +289 -0
- data/ext/backburn/prism/src/comments.c +455 -0
- data/ext/backburn/prism/src/constant_pool.c +410 -0
- data/ext/backburn/prism/src/diagnostic.c +918 -0
- data/ext/backburn/prism/src/double.c +394 -0
- data/ext/backburn/prism/src/encoding.c +1701 -0
- data/ext/backburn/prism/src/errors_format.c +650 -0
- data/ext/backburn/prism/src/integer.c +675 -0
- data/ext/backburn/prism/src/json.c +5745 -0
- data/ext/backburn/prism/src/line_offset_list.c +99 -0
- data/ext/backburn/prism/src/list.c +24 -0
- data/ext/backburn/prism/src/memchr.c +37 -0
- data/ext/backburn/prism/src/node.c +4464 -0
- data/ext/backburn/prism/src/options.c +475 -0
- data/ext/backburn/prism/src/parser.c +371 -0
- data/ext/backburn/prism/src/prettyprint.c +9038 -0
- data/ext/backburn/prism/src/prism.c +23615 -0
- data/ext/backburn/prism/src/regexp.c +3833 -0
- data/ext/backburn/prism/src/serialize.c +2656 -0
- data/ext/backburn/prism/src/source.c +525 -0
- data/ext/backburn/prism/src/static_literals.c +562 -0
- data/ext/backburn/prism/src/string_query.c +195 -0
- data/ext/backburn/prism/src/stringy.c +77 -0
- data/ext/backburn/prism/src/strncasecmp.c +37 -0
- data/ext/backburn/prism/src/strpbrk.c +465 -0
- data/ext/backburn/prism/src/tokens.c +724 -0
- data/ext/backburn/prism/src/xxh3.c +292 -0
- data/lib/backburn/version.rb +7 -0
- data/presets/devise.roots +11 -0
- data/presets/fast-mcp.roots +5 -0
- data/presets/graphql.roots +6 -0
- data/presets/phlex.roots +7 -0
- data/presets/pundit.roots +3 -0
- data/presets/rails-framework.roots +10 -0
- data/presets/rails.roots +83 -0
- data/presets/sidekiq.roots +4 -0
- data/presets/thor.roots +3 -0
- data/rails.roots +99 -0
- data/scripts/embed-presets.sh +13 -0
- data/scripts/version-h.sh +7 -0
- data/src/analysis/analysis.c +95 -0
- data/src/analysis/analysis.h +24 -0
- data/src/analysis/batches.c +127 -0
- data/src/analysis/batches.h +17 -0
- data/src/analysis/graph.c +238 -0
- data/src/analysis/graph.h +34 -0
- data/src/analysis/mark.c +300 -0
- data/src/analysis/mark.h +21 -0
- data/src/analysis/roots.c +229 -0
- data/src/analysis/roots.h +29 -0
- data/src/cli/cli.c +237 -0
- data/src/cli/cli.h +9 -0
- data/src/cli/config.c +112 -0
- data/src/cli/config.h +14 -0
- data/src/cli/options.c +17 -0
- data/src/cli/options.h +35 -0
- data/src/commands/commands.h +11 -0
- data/src/commands/explain.c +255 -0
- data/src/commands/gemfile.c +126 -0
- data/src/commands/gemfile.h +27 -0
- data/src/commands/gems.c +408 -0
- data/src/commands/init.c +92 -0
- data/src/commands/roots.c +82 -0
- data/src/commands/scan.c +91 -0
- data/src/core/intern.c +94 -0
- data/src/core/intern.h +40 -0
- data/src/core/json.c +17 -0
- data/src/core/json.h +9 -0
- data/src/core/model.c +25 -0
- data/src/core/model.h +85 -0
- data/src/core/util.c +116 -0
- data/src/core/util.h +48 -0
- data/src/index/erb.c +40 -0
- data/src/index/erb.h +9 -0
- data/src/index/haml.c +334 -0
- data/src/index/haml.h +10 -0
- data/src/index/indexer.c +702 -0
- data/src/index/indexer.h +19 -0
- data/src/index/walk.c +144 -0
- data/src/index/walk.h +14 -0
- data/src/main.c +81 -0
- data/src/output/baseline.c +101 -0
- data/src/output/baseline.h +15 -0
- data/src/output/report.c +440 -0
- data/src/output/report.h +28 -0
- metadata +231 -0
|
@@ -0,0 +1,650 @@
|
|
|
1
|
+
#include "prism/errors_format.h"
|
|
2
|
+
|
|
3
|
+
#include "prism/internal/allocator.h"
|
|
4
|
+
#include "prism/internal/assert.h"
|
|
5
|
+
#include "prism/internal/buffer.h"
|
|
6
|
+
#include "prism/internal/diagnostic.h"
|
|
7
|
+
#include "prism/internal/encoding.h"
|
|
8
|
+
#include "prism/internal/line_offset_list.h"
|
|
9
|
+
#include "prism/internal/oom.h"
|
|
10
|
+
#include "prism/internal/parser.h"
|
|
11
|
+
|
|
12
|
+
#include <inttypes.h>
|
|
13
|
+
#include <stdbool.h>
|
|
14
|
+
#include <stdlib.h>
|
|
15
|
+
#include <string.h>
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Check if the source in the given range of lines is valid UTF-8. The indices
|
|
19
|
+
* are zero-based indices into the line offsets list, and the range is
|
|
20
|
+
* inclusive on both ends.
|
|
21
|
+
*/
|
|
22
|
+
static bool
|
|
23
|
+
lines_are_utf8(const pm_parser_t *parser, size_t start_index, size_t end_index) {
|
|
24
|
+
const pm_line_offset_list_t *line_offsets = &parser->line_offsets;
|
|
25
|
+
const uint8_t *cursor = parser->start + line_offsets->offsets[start_index];
|
|
26
|
+
const uint8_t *end = (end_index + 1 == line_offsets->size) ? parser->end : (parser->start + line_offsets->offsets[end_index + 1]);
|
|
27
|
+
|
|
28
|
+
while (cursor < end) {
|
|
29
|
+
size_t width = pm_encoding_utf_8_char_width(cursor, end - cursor);
|
|
30
|
+
if (width == 0) return false;
|
|
31
|
+
|
|
32
|
+
cursor += width;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
typedef struct {
|
|
39
|
+
pm_diagnostic_t *diagnostic;
|
|
40
|
+
/* Zero-based line indices preserve source order across line-number wraparound. */
|
|
41
|
+
uint32_t line_index;
|
|
42
|
+
uint32_t column_start;
|
|
43
|
+
uint32_t column_end;
|
|
44
|
+
size_t idx;
|
|
45
|
+
} pm_rich_error_t;
|
|
46
|
+
|
|
47
|
+
static int
|
|
48
|
+
pm_rich_error_compare(const void *a, const void *b) {
|
|
49
|
+
const pm_rich_error_t *left = (const pm_rich_error_t *) a;
|
|
50
|
+
const pm_rich_error_t *right = (const pm_rich_error_t *) b;
|
|
51
|
+
|
|
52
|
+
if (left->line_index != right->line_index) {
|
|
53
|
+
return (left->line_index < right->line_index) ? -1 : 1;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (left->column_start != right->column_start) {
|
|
57
|
+
return (left->column_start < right->column_start) ? -1 : 1;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (left->idx != right->idx) {
|
|
61
|
+
return (left->idx < right->idx) ? 1 : -1;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return 0;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Return the zero-based index of the last line of the source that can be displayed. When
|
|
69
|
+
* the source ends with a newline, the empty line after it is not displayed.
|
|
70
|
+
*/
|
|
71
|
+
static uint32_t
|
|
72
|
+
pm_rich_errors_final_line(const pm_parser_t *parser) {
|
|
73
|
+
const pm_line_offset_list_t *line_offsets = &parser->line_offsets;
|
|
74
|
+
uint32_t final_line = (uint32_t) (line_offsets->size - 1);
|
|
75
|
+
|
|
76
|
+
if (final_line > 0 && (parser->start + line_offsets->offsets[final_line]) == parser->end) {
|
|
77
|
+
final_line--;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return final_line;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Find the range of lines that may be displayed around an error on the given
|
|
85
|
+
* line: up to two lines of context on either side of it, limited to the lines
|
|
86
|
+
* in the source. The error's own line is always included, even when it is the
|
|
87
|
+
* empty line after a trailing newline.
|
|
88
|
+
*/
|
|
89
|
+
static void
|
|
90
|
+
pm_rich_error_context(uint32_t line, uint32_t final_line, uint32_t *first, uint32_t *last) {
|
|
91
|
+
*first = (line > 2) ? (line - 2) : 0;
|
|
92
|
+
*last = (final_line > line && final_line - line > 2) ? (line + 2) : final_line;
|
|
93
|
+
if (*last < line) *last = line;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Check if the source that will be displayed for the given rich errors is
|
|
98
|
+
* valid UTF-8. That is each error's line and the lines of context around it.
|
|
99
|
+
* The errors must be sorted by their line. Each line is checked at most once,
|
|
100
|
+
* so that many errors on the same long line cost time proportional to the
|
|
101
|
+
* line, not to the line times the number of errors.
|
|
102
|
+
*/
|
|
103
|
+
static bool
|
|
104
|
+
rich_errors_are_utf8(const pm_parser_t *parser, size_t nerrors, const pm_rich_error_t *rich_errors) {
|
|
105
|
+
uint32_t final_line = pm_rich_errors_final_line(parser);
|
|
106
|
+
|
|
107
|
+
/* Every line from the first line of context of the most recently visited
|
|
108
|
+
* error up to (but not including) this index has already been checked.
|
|
109
|
+
* Because the errors are sorted, the first line of context never
|
|
110
|
+
* decreases, so any later error only needs the lines from this index
|
|
111
|
+
* onward. */
|
|
112
|
+
size_t checked_index = 0;
|
|
113
|
+
|
|
114
|
+
for (size_t idx = 0; idx < nerrors; idx++) {
|
|
115
|
+
uint32_t first_line;
|
|
116
|
+
uint32_t last_line;
|
|
117
|
+
pm_rich_error_context(rich_errors[idx].line_index, final_line, &first_line, &last_line);
|
|
118
|
+
|
|
119
|
+
size_t start_index = (size_t) first_line;
|
|
120
|
+
size_t end_index = (size_t) last_line;
|
|
121
|
+
|
|
122
|
+
if (start_index < checked_index) start_index = checked_index;
|
|
123
|
+
if (start_index > end_index) continue;
|
|
124
|
+
|
|
125
|
+
if (!lines_are_utf8(parser, start_index, end_index)) return false;
|
|
126
|
+
checked_index = end_index + 1;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
#define COLOR_BOLD "\033[1m"
|
|
133
|
+
#define COLOR_GRAY "\033[2m"
|
|
134
|
+
#define COLOR_RED "\033[1;31m"
|
|
135
|
+
#define COLOR_RESET "\033[m"
|
|
136
|
+
#define TRUNCATE 30
|
|
137
|
+
|
|
138
|
+
/* The maximum number of columns of a line that errors are displayed beneath
|
|
139
|
+
* before the line is displayed again. This must be at least TRUNCATE so that
|
|
140
|
+
* every window after the first on a line starts with a truncated line. Lines
|
|
141
|
+
* of context are cut off at the same width. */
|
|
142
|
+
#define WINDOW 80
|
|
143
|
+
|
|
144
|
+
typedef struct {
|
|
145
|
+
/** The type of formatting to use when formatting the errors. */
|
|
146
|
+
pm_errors_format_type_t type;
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* The number of columns taken up by the widest line number that will be
|
|
150
|
+
* displayed, including the sign of a negative line number. The widest
|
|
151
|
+
* possible is the 11 columns of -2147483648.
|
|
152
|
+
*/
|
|
153
|
+
uint8_t width;
|
|
154
|
+
} pm_error_line_format_t;
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Write the decimal digits of the given line number, including the sign if it
|
|
158
|
+
* is negative, so that they end at the end of the given buffer. The buffer is
|
|
159
|
+
* large enough for the widest possible line number, -2147483648. Returns the
|
|
160
|
+
* number of characters written.
|
|
161
|
+
*/
|
|
162
|
+
static uint8_t
|
|
163
|
+
pm_error_line_digits(int32_t line, char digits[11]) {
|
|
164
|
+
/* The magnitude is computed in unsigned arithmetic so that negating the
|
|
165
|
+
* minimum value of a 32-bit integer is well-defined. */
|
|
166
|
+
uint32_t magnitude = (line < 0) ? (0U - (uint32_t) line) : (uint32_t) line;
|
|
167
|
+
uint8_t length = 0;
|
|
168
|
+
|
|
169
|
+
do {
|
|
170
|
+
digits[10 - length++] = (char) ('0' + (magnitude % 10));
|
|
171
|
+
magnitude /= 10;
|
|
172
|
+
} while (magnitude > 0);
|
|
173
|
+
|
|
174
|
+
if (line < 0) digits[10 - length++] = '-';
|
|
175
|
+
return length;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Append the given line number to the buffer, right aligned within the given
|
|
180
|
+
* width.
|
|
181
|
+
*/
|
|
182
|
+
static void
|
|
183
|
+
pm_error_append_line_number(pm_buffer_t *buffer, int32_t line, uint8_t width) {
|
|
184
|
+
char digits[11];
|
|
185
|
+
uint8_t length = pm_error_line_digits(line, digits);
|
|
186
|
+
|
|
187
|
+
if (width > length) pm_buffer_append_repeated(buffer, ' ', (size_t) (width - length));
|
|
188
|
+
pm_buffer_append_string(buffer, digits + sizeof(digits) - length, length);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Initialize the given line format struct based on whether or not to highlight
|
|
193
|
+
* the line and the first and last line indices that will be displayed. The
|
|
194
|
+
* widest number is at an endpoint or at the signed 32-bit wraparound.
|
|
195
|
+
*/
|
|
196
|
+
static void
|
|
197
|
+
pm_error_line_format_init(pm_error_line_format_t *format, pm_errors_format_type_t format_type, const pm_parser_t *parser, uint32_t first_line, uint32_t last_line) {
|
|
198
|
+
char digits[11];
|
|
199
|
+
uint8_t first_width = pm_error_line_digits(pm_line_offset_list_line_number(first_line, parser->start_line), digits);
|
|
200
|
+
uint8_t last_width = pm_error_line_digits(pm_line_offset_list_line_number(last_line, parser->start_line), digits);
|
|
201
|
+
|
|
202
|
+
/* The range may cross the signed 32-bit display boundary. */
|
|
203
|
+
uint32_t wrap_index = (uint32_t) INT32_MAX - (uint32_t) parser->start_line;
|
|
204
|
+
if (first_line <= wrap_index && last_line > wrap_index) last_width = 11;
|
|
205
|
+
|
|
206
|
+
*format = (pm_error_line_format_t) {
|
|
207
|
+
.type = format_type,
|
|
208
|
+
.width = (first_width > last_width) ? first_width : last_width
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Append the prefix for a line of source, which displays the line number right
|
|
214
|
+
* aligned within the width of the widest line number.
|
|
215
|
+
*/
|
|
216
|
+
static void
|
|
217
|
+
pm_error_line_format_number(const pm_error_line_format_t *format, pm_buffer_t *buffer, int32_t line) {
|
|
218
|
+
if (format->type > PM_ERRORS_FORMAT_PLAIN) PM_BUFFER_APPEND_LITERAL(buffer, COLOR_GRAY);
|
|
219
|
+
pm_error_append_line_number(buffer, line, format->width);
|
|
220
|
+
PM_BUFFER_APPEND_LITERAL(buffer, " | ");
|
|
221
|
+
if (format->type > PM_ERRORS_FORMAT_PLAIN) PM_BUFFER_APPEND_LITERAL(buffer, COLOR_RESET);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Append the prefix for a line beneath the source that displays an error, which
|
|
226
|
+
* is blank where the line number would be so that the columns line up.
|
|
227
|
+
*/
|
|
228
|
+
static void
|
|
229
|
+
pm_error_line_format_blank(const pm_error_line_format_t *format, pm_buffer_t *buffer) {
|
|
230
|
+
if (format->type > PM_ERRORS_FORMAT_PLAIN) PM_BUFFER_APPEND_LITERAL(buffer, COLOR_GRAY);
|
|
231
|
+
pm_buffer_append_repeated(buffer, ' ', (size_t) format->width + 1);
|
|
232
|
+
PM_BUFFER_APPEND_LITERAL(buffer, "| ");
|
|
233
|
+
if (format->type > PM_ERRORS_FORMAT_PLAIN) PM_BUFFER_APPEND_LITERAL(buffer, COLOR_RESET);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Append the divider that marks lines of source that are skipped between two
|
|
238
|
+
* displayed snippets. It extends just past the line number prefix.
|
|
239
|
+
*/
|
|
240
|
+
static void
|
|
241
|
+
pm_error_line_format_divider(const pm_error_line_format_t *format, pm_buffer_t *buffer) {
|
|
242
|
+
if (format->type > PM_ERRORS_FORMAT_PLAIN) PM_BUFFER_APPEND_LITERAL(buffer, COLOR_GRAY);
|
|
243
|
+
PM_BUFFER_APPEND_LITERAL(buffer, " ");
|
|
244
|
+
pm_buffer_append_repeated(buffer, '~', (size_t) format->width + 4);
|
|
245
|
+
if (format->type > PM_ERRORS_FORMAT_PLAIN) PM_BUFFER_APPEND_LITERAL(buffer, COLOR_RESET);
|
|
246
|
+
pm_buffer_append_byte(buffer, '\n');
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Advance the given column of the line that begins at start by whole
|
|
251
|
+
* characters until it reaches the given limit, and return the number of
|
|
252
|
+
* characters passed over. Each character is displayed in one column. A byte
|
|
253
|
+
* that does not begin a valid character, or that is past the end of the
|
|
254
|
+
* source, counts as one character.
|
|
255
|
+
*/
|
|
256
|
+
static size_t
|
|
257
|
+
pm_error_advance_columns(const pm_parser_t *parser, const uint8_t *start, size_t *column, size_t limit) {
|
|
258
|
+
size_t count = 0;
|
|
259
|
+
|
|
260
|
+
while (*column < limit) {
|
|
261
|
+
const uint8_t *cursor = start + *column;
|
|
262
|
+
size_t char_width = 1;
|
|
263
|
+
|
|
264
|
+
/* Every supported encoding is ASCII-compatible, so a byte below 0x80
|
|
265
|
+
* at the start of a character is a character by itself. */
|
|
266
|
+
if ((cursor < parser->end) && (*cursor >= 0x80)) {
|
|
267
|
+
char_width = pm_parser_encoding_char_width(parser, cursor, parser->end - cursor);
|
|
268
|
+
if (char_width == 0) char_width = 1;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
*column += char_width;
|
|
272
|
+
count++;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
return count;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
static void
|
|
279
|
+
pm_error_format_line(const pm_parser_t *parser, pm_buffer_t *buffer, const pm_error_line_format_t *format, uint32_t line, uint32_t column_start, uint32_t column_end) {
|
|
280
|
+
size_t index = line;
|
|
281
|
+
PRISM_ASSERT(index < parser->line_offsets.size);
|
|
282
|
+
|
|
283
|
+
const uint8_t *start = parser->start + parser->line_offsets.offsets[index];
|
|
284
|
+
const uint8_t *end;
|
|
285
|
+
|
|
286
|
+
if (index >= parser->line_offsets.size - 1) {
|
|
287
|
+
end = parser->end;
|
|
288
|
+
} else {
|
|
289
|
+
end = parser->start + parser->line_offsets.offsets[index + 1];
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
pm_error_line_format_number(format, buffer, pm_line_offset_list_line_number(line, parser->start_line));
|
|
293
|
+
|
|
294
|
+
/* Here we determine if we should truncate the start of the line. The
|
|
295
|
+
* column is the start of a diagnostic, so it is on a character boundary. */
|
|
296
|
+
bool truncate_start = column_start >= TRUNCATE;
|
|
297
|
+
const uint8_t *display_start = truncate_start ? (start + column_start) : start;
|
|
298
|
+
|
|
299
|
+
/* Here we determine if we should truncate the end of the line. Note that
|
|
300
|
+
* this is written to avoid computing start + column_end, which could be
|
|
301
|
+
* more than one past the end of the source when the error is at the end
|
|
302
|
+
* of the input. The walk to find a character boundary begins at the
|
|
303
|
+
* displayed start so that its cost is bounded by the displayed width. */
|
|
304
|
+
bool truncate_end = false;
|
|
305
|
+
if ((column_end != 0) && ((end - start) - ((ptrdiff_t) column_end) >= TRUNCATE)) {
|
|
306
|
+
const uint8_t *end_candidate = start + column_end + TRUNCATE;
|
|
307
|
+
|
|
308
|
+
for (const uint8_t *cursor = display_start; cursor < end_candidate;) {
|
|
309
|
+
size_t char_width = pm_parser_encoding_char_width(parser, cursor, parser->end - cursor);
|
|
310
|
+
|
|
311
|
+
/* If we failed to decode a character, then just bail out and
|
|
312
|
+
* truncate at the fixed width. */
|
|
313
|
+
if (char_width == 0) break;
|
|
314
|
+
|
|
315
|
+
/* If this next character would go past the end candidate,
|
|
316
|
+
* then we need to truncate before it. */
|
|
317
|
+
if (cursor + char_width > end_candidate) {
|
|
318
|
+
end_candidate = cursor;
|
|
319
|
+
break;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
cursor += char_width;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
end = end_candidate;
|
|
326
|
+
truncate_end = true;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
if (truncate_start) PM_BUFFER_APPEND_LITERAL(buffer, "... ");
|
|
330
|
+
pm_buffer_append_string(buffer, (const char *) display_start, (size_t) (end - display_start));
|
|
331
|
+
|
|
332
|
+
if (truncate_end) {
|
|
333
|
+
PM_BUFFER_APPEND_LITERAL(buffer, " ...\n");
|
|
334
|
+
} else if (end == parser->end && end > parser->start && end[-1] != '\n') {
|
|
335
|
+
pm_buffer_append_byte(buffer, '\n');
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Display a line of context around an error, which is cut off at the same width
|
|
341
|
+
* as a window so that a long line does not dominate the output.
|
|
342
|
+
*/
|
|
343
|
+
static void
|
|
344
|
+
pm_error_format_context_line(const pm_parser_t *parser, pm_buffer_t *buffer, const pm_error_line_format_t *format, uint32_t line) {
|
|
345
|
+
PM_BUFFER_APPEND_LITERAL(buffer, " ");
|
|
346
|
+
pm_error_format_line(parser, buffer, format, line, 0, WINDOW);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
static void
|
|
350
|
+
pm_rich_errors_format(const pm_parser_t *parser, pm_buffer_t *buffer, pm_errors_format_type_t format_type, size_t nerrors, pm_rich_error_t *rich_errors, bool inline_messages) {
|
|
351
|
+
uint32_t final_line = pm_rich_errors_final_line(parser);
|
|
352
|
+
|
|
353
|
+
/* The line numbers that are displayed range from the first line of context
|
|
354
|
+
* of the first error to the last line of context of the last error. */
|
|
355
|
+
uint32_t first_displayed_line;
|
|
356
|
+
uint32_t last_displayed_line;
|
|
357
|
+
uint32_t unused_line;
|
|
358
|
+
pm_rich_error_context(rich_errors[0].line_index, final_line, &first_displayed_line, &unused_line);
|
|
359
|
+
pm_rich_error_context(rich_errors[nerrors - 1].line_index, final_line, &unused_line, &last_displayed_line);
|
|
360
|
+
|
|
361
|
+
pm_error_line_format_t format;
|
|
362
|
+
pm_error_line_format_init(&format, format_type, parser, first_displayed_line, last_displayed_line);
|
|
363
|
+
|
|
364
|
+
/* We are going to iterate through every error in our error list and display
|
|
365
|
+
* it. While we are iterating, we will display some padding lines of the
|
|
366
|
+
* source before the error to give some context. We will be careful not to
|
|
367
|
+
* display the same line twice in case the errors are close enough in the
|
|
368
|
+
* source.
|
|
369
|
+
*
|
|
370
|
+
* The errors on a line are split into windows of at most WINDOW columns.
|
|
371
|
+
* Each window displays its own slice of the line, and the markers beneath
|
|
372
|
+
* it are padded relative to the start of that slice and cut off at its
|
|
373
|
+
* end. This bounds the length of every marker line, so the output grows
|
|
374
|
+
* linearly with the number of errors no matter how long the line is. */
|
|
375
|
+
uint32_t last_line = 0;
|
|
376
|
+
uint32_t window_column_start = 0;
|
|
377
|
+
uint32_t window_column_end = 0;
|
|
378
|
+
size_t window_end_idx = 0;
|
|
379
|
+
|
|
380
|
+
for (size_t idx = 0; idx < nerrors; idx++) {
|
|
381
|
+
pm_rich_error_t *rich_error = rich_errors + idx;
|
|
382
|
+
|
|
383
|
+
/* Here we determine how many lines of padding of the source to display,
|
|
384
|
+
* based on the difference from the last line that was displayed. */
|
|
385
|
+
uint32_t first_context = (rich_error->line_index > 2) ? (rich_error->line_index - 2) : 0;
|
|
386
|
+
if (idx != 0) {
|
|
387
|
+
if (rich_error->line_index > last_line && rich_error->line_index - last_line > 3) {
|
|
388
|
+
pm_error_line_format_divider(&format, buffer);
|
|
389
|
+
}
|
|
390
|
+
if (last_line >= rich_error->line_index) first_context = rich_error->line_index;
|
|
391
|
+
else if (first_context <= last_line) first_context = last_line + 1;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
for (uint32_t line = first_context; line < rich_error->line_index; line++) {
|
|
395
|
+
pm_error_format_context_line(parser, buffer, &format, line);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/* If this error is not in the current window (because it is the first
|
|
399
|
+
* error, it is on a new line, or it is too far to the right), then we
|
|
400
|
+
* start a new window and display the line that has the error in it. */
|
|
401
|
+
if (idx == window_end_idx) {
|
|
402
|
+
switch (format_type) {
|
|
403
|
+
case PM_ERRORS_FORMAT_PLAIN:
|
|
404
|
+
PM_BUFFER_APPEND_LITERAL(buffer, "> ");
|
|
405
|
+
break;
|
|
406
|
+
case PM_ERRORS_FORMAT_STYLE:
|
|
407
|
+
PM_BUFFER_APPEND_LITERAL(buffer, COLOR_BOLD "> " COLOR_RESET);
|
|
408
|
+
break;
|
|
409
|
+
case PM_ERRORS_FORMAT_COLOR:
|
|
410
|
+
PM_BUFFER_APPEND_LITERAL(buffer, COLOR_RED "> " COLOR_RESET);
|
|
411
|
+
break;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/* The displayed slice of the line starts at this error, unless it
|
|
415
|
+
* is close enough to the start of the line to show it all. */
|
|
416
|
+
window_column_start = (rich_error->column_start >= TRUNCATE) ? rich_error->column_start : 0;
|
|
417
|
+
uint32_t window_limit = window_column_start + WINDOW;
|
|
418
|
+
|
|
419
|
+
/* Find the errors on this line that start within the window, and
|
|
420
|
+
* the maximum column end of all of them, capped at the window. */
|
|
421
|
+
window_column_end = rich_error->column_end;
|
|
422
|
+
for (window_end_idx = idx + 1; window_end_idx < nerrors; window_end_idx++) {
|
|
423
|
+
const pm_rich_error_t *next_error = &rich_errors[window_end_idx];
|
|
424
|
+
if ((next_error->line_index != rich_error->line_index) || (next_error->column_start >= window_limit)) break;
|
|
425
|
+
if (next_error->column_end > window_column_end) window_column_end = next_error->column_end;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
if (window_column_end > window_limit) window_column_end = window_limit;
|
|
429
|
+
pm_error_format_line(parser, buffer, &format, rich_error->line_index, rich_error->column_start, window_column_end);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
const uint8_t *start = parser->start + parser->line_offsets.offsets[rich_error->line_index];
|
|
433
|
+
if (start == parser->end) pm_buffer_append_byte(buffer, '\n');
|
|
434
|
+
|
|
435
|
+
/* Now we will display the actual error message. We will do this by
|
|
436
|
+
* first putting the prefix to the line, then a bunch of blank spaces
|
|
437
|
+
* depending on the column, then as many tildes as we need to display
|
|
438
|
+
* the width of the error, then the error message itself.
|
|
439
|
+
*
|
|
440
|
+
* Note that this doesn't take into account the width of the actual
|
|
441
|
+
* character when displayed in the terminal. For some east-asian
|
|
442
|
+
* languages or emoji, this means it can be thrown off pretty badly. We
|
|
443
|
+
* will need to solve this eventually. */
|
|
444
|
+
PM_BUFFER_APPEND_LITERAL(buffer, " ");
|
|
445
|
+
pm_error_line_format_blank(&format, buffer);
|
|
446
|
+
|
|
447
|
+
size_t column = 0;
|
|
448
|
+
if (window_column_start > 0) {
|
|
449
|
+
PM_BUFFER_APPEND_LITERAL(buffer, " ");
|
|
450
|
+
column = window_column_start;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
pm_buffer_append_repeated(buffer, ' ', pm_error_advance_columns(parser, start, &column, rich_error->column_start));
|
|
454
|
+
|
|
455
|
+
switch (format_type) {
|
|
456
|
+
case PM_ERRORS_FORMAT_PLAIN:
|
|
457
|
+
pm_buffer_append_byte(buffer, '^');
|
|
458
|
+
break;
|
|
459
|
+
case PM_ERRORS_FORMAT_STYLE:
|
|
460
|
+
PM_BUFFER_APPEND_LITERAL(buffer, COLOR_BOLD "^");
|
|
461
|
+
break;
|
|
462
|
+
case PM_ERRORS_FORMAT_COLOR:
|
|
463
|
+
PM_BUFFER_APPEND_LITERAL(buffer, COLOR_RED "^");
|
|
464
|
+
break;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/* The caret marks the first character of the error, and the tildes
|
|
468
|
+
* mark the rest of it, up to the end of the window. */
|
|
469
|
+
pm_error_advance_columns(parser, start, &column, column + 1);
|
|
470
|
+
size_t tilde_limit = (rich_error->column_end < window_column_end) ? rich_error->column_end : window_column_end;
|
|
471
|
+
pm_buffer_append_repeated(buffer, '~', pm_error_advance_columns(parser, start, &column, tilde_limit));
|
|
472
|
+
|
|
473
|
+
if (format_type != PM_ERRORS_FORMAT_PLAIN) {
|
|
474
|
+
PM_BUFFER_APPEND_LITERAL(buffer, COLOR_RESET);
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
if (inline_messages) {
|
|
478
|
+
const char *message = pm_diagnostic_message(rich_error->diagnostic);
|
|
479
|
+
|
|
480
|
+
pm_buffer_append_byte(buffer, ' ');
|
|
481
|
+
pm_buffer_append_string(buffer, message, strlen(message));
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
pm_buffer_append_byte(buffer, '\n');
|
|
485
|
+
|
|
486
|
+
/* Here we determine how many lines of padding to display after the
|
|
487
|
+
* diagnostic, depending on where the next diagnostic is in source. The
|
|
488
|
+
* context after the last error is limited to the source. */
|
|
489
|
+
last_line = rich_error->line_index;
|
|
490
|
+
uint32_t last_context = final_line;
|
|
491
|
+
if (idx + 1 < nerrors) {
|
|
492
|
+
uint32_t next_line = rich_errors[idx + 1].line_index;
|
|
493
|
+
last_context = (next_line > last_line) ? next_line - 1 : last_line;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
for (uint32_t count = 0; count < 2 && last_line < last_context; count++) {
|
|
497
|
+
pm_error_format_context_line(parser, buffer, &format, ++last_line);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
static void
|
|
503
|
+
pm_rich_error_init(const pm_parser_t *parser, pm_rich_error_t *rich_error, pm_diagnostic_t *diagnostic, pm_location_t *location) {
|
|
504
|
+
const pm_line_offset_list_t *line_offsets = &parser->line_offsets;
|
|
505
|
+
pm_line_column_t start_line_column = pm_line_offset_list_line_column(line_offsets, location->start, 0);
|
|
506
|
+
pm_line_column_t end_line_column = pm_line_offset_list_line_column(line_offsets, location->start + location->length, 0);
|
|
507
|
+
|
|
508
|
+
uint32_t column_end;
|
|
509
|
+
if (start_line_column.line == end_line_column.line) {
|
|
510
|
+
column_end = end_line_column.column;
|
|
511
|
+
} else {
|
|
512
|
+
column_end = (uint32_t) (line_offsets->offsets[(uint32_t) start_line_column.line + 1] - line_offsets->offsets[(uint32_t) start_line_column.line] - 1);
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/* Ensure we have at least one column of error. */
|
|
516
|
+
if (start_line_column.column == column_end) column_end++;
|
|
517
|
+
|
|
518
|
+
*rich_error = (pm_rich_error_t) {
|
|
519
|
+
.diagnostic = diagnostic,
|
|
520
|
+
.line_index = (uint32_t) start_line_column.line,
|
|
521
|
+
.column_start = start_line_column.column,
|
|
522
|
+
.column_end = column_end
|
|
523
|
+
};
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* Append a line that names the file and line of an error along with its
|
|
528
|
+
* message, which is how an error is displayed without a source snippet.
|
|
529
|
+
*/
|
|
530
|
+
static void
|
|
531
|
+
pm_error_append_summary(const pm_parser_t *parser, pm_buffer_t *buffer, uint32_t line, const char *message) {
|
|
532
|
+
pm_buffer_append_string(buffer, (const char *) pm_string_source(&parser->filepath), pm_string_length(&parser->filepath));
|
|
533
|
+
pm_buffer_append_byte(buffer, ':');
|
|
534
|
+
pm_error_append_line_number(buffer, pm_line_offset_list_line_number(line, parser->start_line), 0);
|
|
535
|
+
PM_BUFFER_APPEND_LITERAL(buffer, ": ");
|
|
536
|
+
pm_buffer_append_string(buffer, message, strlen(message));
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
pm_error_level_t
|
|
540
|
+
pm_errors_format(const pm_parser_t *parser, pm_buffer_t *buffer, pm_errors_format_type_t format_type) {
|
|
541
|
+
size_t nerrors = parser->error_list.size;
|
|
542
|
+
PRISM_ASSERT(nerrors > 0);
|
|
543
|
+
|
|
544
|
+
size_t rich_errors_size = nerrors * sizeof(pm_rich_error_t);
|
|
545
|
+
if (rich_errors_size / sizeof(pm_rich_error_t) != nerrors) PRISM_OOM();
|
|
546
|
+
|
|
547
|
+
pm_rich_error_t *rich_errors = xmalloc(rich_errors_size);
|
|
548
|
+
if (rich_errors == NULL) PRISM_OOM();
|
|
549
|
+
|
|
550
|
+
size_t rich_errors_idx = 0;
|
|
551
|
+
const pm_diagnostic_t *encoding_error = NULL;
|
|
552
|
+
|
|
553
|
+
for (pm_diagnostic_t *diagnostic = (pm_diagnostic_t *) parser->error_list.head; diagnostic != NULL; diagnostic = (pm_diagnostic_t *) diagnostic->node.next) {
|
|
554
|
+
pm_location_t location = pm_diagnostic_location(diagnostic);
|
|
555
|
+
|
|
556
|
+
switch (pm_diagnostic_error_level(diagnostic)) {
|
|
557
|
+
case PM_ERROR_LEVEL_SYNTAX: {
|
|
558
|
+
pm_rich_error_init(parser, &rich_errors[rich_errors_idx], diagnostic, &location);
|
|
559
|
+
rich_errors[rich_errors_idx].idx = rich_errors_idx;
|
|
560
|
+
rich_errors_idx++;
|
|
561
|
+
break;
|
|
562
|
+
}
|
|
563
|
+
case PM_ERROR_LEVEL_ARGUMENT: {
|
|
564
|
+
/* If we get an argument error, we want to format it and return it
|
|
565
|
+
* immediately. They should include a snippet of the source if
|
|
566
|
+
* possible. */
|
|
567
|
+
pm_rich_error_init(parser, rich_errors, diagnostic, &location);
|
|
568
|
+
pm_error_append_summary(parser, buffer, rich_errors[0].line_index, pm_diagnostic_message(diagnostic));
|
|
569
|
+
|
|
570
|
+
if (rich_errors_are_utf8(parser, 1, rich_errors)) {
|
|
571
|
+
pm_buffer_append_byte(buffer, '\n');
|
|
572
|
+
pm_rich_errors_format(parser, buffer, format_type, 1, rich_errors, false);
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
xfree_sized(rich_errors, rich_errors_size);
|
|
576
|
+
return PM_ERROR_LEVEL_ARGUMENT;
|
|
577
|
+
}
|
|
578
|
+
case PM_ERROR_LEVEL_LOAD: {
|
|
579
|
+
/* If we get a load error, we want to format it and return it
|
|
580
|
+
* immediately. It should only include the diagnostic message. */
|
|
581
|
+
const char *message = pm_diagnostic_message(diagnostic);
|
|
582
|
+
pm_buffer_append_string(buffer, message, strlen(message));
|
|
583
|
+
|
|
584
|
+
xfree_sized(rich_errors, rich_errors_size);
|
|
585
|
+
return PM_ERROR_LEVEL_LOAD;
|
|
586
|
+
}
|
|
587
|
+
case PM_ERROR_LEVEL_STACK: {
|
|
588
|
+
/* If we get a stack error, we want to format it and return it
|
|
589
|
+
* immediately. Once the stack is exhausted, the syntax errors
|
|
590
|
+
* around it come from recovering from that point rather than from
|
|
591
|
+
* the source, so they are left out. It should include a snippet of
|
|
592
|
+
* the source if possible. */
|
|
593
|
+
pm_rich_error_init(parser, rich_errors, diagnostic, &location);
|
|
594
|
+
pm_error_append_summary(parser, buffer, rich_errors[0].line_index, pm_diagnostic_message(diagnostic));
|
|
595
|
+
|
|
596
|
+
if (rich_errors_are_utf8(parser, 1, rich_errors)) {
|
|
597
|
+
pm_buffer_append_byte(buffer, '\n');
|
|
598
|
+
pm_rich_errors_format(parser, buffer, format_type, 1, rich_errors, false);
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
xfree_sized(rich_errors, rich_errors_size);
|
|
602
|
+
return PM_ERROR_LEVEL_STACK;
|
|
603
|
+
}
|
|
604
|
+
case PM_ERROR_LEVEL_ENCODING_COMPATIBILITY:
|
|
605
|
+
if (encoding_error == NULL) encoding_error = diagnostic;
|
|
606
|
+
break;
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/* The compiler would encounter a syntax error before joining any strings. */
|
|
611
|
+
if (rich_errors_idx == 0) {
|
|
612
|
+
PRISM_ASSERT(encoding_error != NULL);
|
|
613
|
+
const char *message = pm_diagnostic_message(encoding_error);
|
|
614
|
+
pm_buffer_append_string(buffer, message, strlen(message));
|
|
615
|
+
|
|
616
|
+
xfree_sized(rich_errors, rich_errors_size);
|
|
617
|
+
return PM_ERROR_LEVEL_ENCODING_COMPATIBILITY;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
nerrors = rich_errors_idx;
|
|
621
|
+
|
|
622
|
+
/* The header displays the line of the first error in parse order, which
|
|
623
|
+
* is the first entry in the array because it has not been sorted yet. */
|
|
624
|
+
pm_error_append_summary(parser, buffer, rich_errors[0].line_index, (nerrors > 1) ? "syntax errors found\n" : "syntax error found\n");
|
|
625
|
+
|
|
626
|
+
/* Sort the errors so that they are in the order that they appear in the
|
|
627
|
+
* source. This is the order they are displayed in, and it also lets the
|
|
628
|
+
* UTF-8 check visit each line only once. */
|
|
629
|
+
qsort(rich_errors, nerrors, sizeof(pm_rich_error_t), pm_rich_error_compare);
|
|
630
|
+
|
|
631
|
+
if (rich_errors_are_utf8(parser, nerrors, rich_errors)) {
|
|
632
|
+
/* In here, we have all of the rich diagnostic information and we know
|
|
633
|
+
* the snippets are valid UTF-8, so we format them all together with
|
|
634
|
+
* the source code snippets. */
|
|
635
|
+
pm_rich_errors_format(parser, buffer, format_type, nerrors, rich_errors, true);
|
|
636
|
+
} else {
|
|
637
|
+
/* In here, we have content in the source that is not valid UTF-8. In
|
|
638
|
+
* that case, we do not want to attempt to format the source code
|
|
639
|
+
* snippets because we do not know how the terminal will handle it. The
|
|
640
|
+
* diagnostics are displayed in the order that they appear in the
|
|
641
|
+
* source, as they are when the snippets are displayed. */
|
|
642
|
+
for (size_t idx = 0; idx < nerrors; idx++) {
|
|
643
|
+
if (idx > 0) pm_buffer_append_byte(buffer, '\n');
|
|
644
|
+
pm_error_append_summary(parser, buffer, rich_errors[idx].line_index, pm_diagnostic_message(rich_errors[idx].diagnostic));
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
xfree_sized(rich_errors, rich_errors_size);
|
|
649
|
+
return PM_ERROR_LEVEL_SYNTAX;
|
|
650
|
+
}
|