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"
23lines_are_utf8(
const pm_parser_t *parser,
size_t start_index,
size_t end_index) {
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]);
28 while (cursor < end) {
29 size_t width = pm_encoding_utf_8_char_width(cursor, end - cursor);
30 if (width == 0)
return false;
42 uint32_t column_start;
51pm_rich_errors_final_line(
const pm_parser_t *parser) {
53 uint32_t final_line = (uint32_t) (line_offsets->
size - 1);
55 if (final_line > 0 && (parser->start + line_offsets->
offsets[final_line]) == parser->end) {
69pm_rich_error_context(uint32_t line, uint32_t final_line, uint32_t *first, uint32_t *last) {
70 *first = (line > 2) ? (line - 2) : 0;
71 *last = (final_line > line && final_line - line > 2) ? (line + 2) : final_line;
72 if (*last < line) *last = line;
84 uint32_t final_line = pm_rich_errors_final_line(parser);
91 size_t checked_index = 0;
93 for (
size_t idx = 0; idx < nerrors; idx++) {
96 pm_rich_error_context(rich_errors[idx].line_index, final_line, &first_line, &last_line);
98 size_t start_index = (size_t) first_line;
99 size_t end_index = (size_t) last_line;
101 if (start_index < checked_index) start_index = checked_index;
102 if (start_index > end_index)
continue;
104 if (!lines_are_utf8(parser, start_index, end_index))
return false;
105 checked_index = end_index + 1;
111#define COLOR_BOLD "\033[1m"
112#define COLOR_GRAY "\033[2m"
113#define COLOR_RED "\033[1;31m"
114#define COLOR_RESET "\033[m"
142 uint8_t first_width = pm_buffer_line_number_width(pm_line_offset_list_line_number(first_line, parser->start_line));
143 uint8_t last_width = pm_buffer_line_number_width(pm_line_offset_list_line_number(last_line, parser->start_line));
146 uint32_t wrap_index = (uint32_t) INT32_MAX - (uint32_t) parser->start_line;
147 if (first_line <= wrap_index && last_line > wrap_index) last_width = 11;
151 .width = (first_width > last_width) ? first_width : last_width
162 pm_buffer_append_line_number(buffer, line, format->
width);
163 PM_BUFFER_APPEND_LITERAL(buffer,
" | ");
174 pm_buffer_append_repeated(buffer,
' ', (
size_t) format->
width + 1);
175 PM_BUFFER_APPEND_LITERAL(buffer,
"| ");
186 PM_BUFFER_APPEND_LITERAL(buffer,
" ");
187 pm_buffer_append_repeated(buffer,
'~', (
size_t) format->
width + 4);
189 pm_buffer_append_byte(buffer,
'\n');
200pm_error_advance_columns(
const pm_parser_t *parser,
const uint8_t *start,
size_t *column,
size_t limit) {
203 while (*column < limit) {
204 const uint8_t *cursor = start + *column;
205 size_t char_width = 1;
209 if ((cursor < parser->end) && (*cursor >= 0x80)) {
210 char_width = pm_parser_encoding_char_width(parser, cursor, parser->end - cursor);
211 if (char_width == 0) char_width = 1;
214 *column += char_width;
224 PRISM_ASSERT(index < parser->line_offsets.
size);
226 const uint8_t *start = parser->start + parser->line_offsets.
offsets[index];
229 if (index >= parser->line_offsets.
size - 1) {
232 end = parser->start + parser->line_offsets.
offsets[index + 1];
235 pm_error_line_format_number(format, buffer, pm_line_offset_list_line_number(line, parser->start_line));
239 bool truncate_start = column_start >= TRUNCATE;
240 const uint8_t *display_start = truncate_start ? (start + column_start) : start;
247 bool truncate_end =
false;
248 if ((column_end != 0) && ((end - start) - ((ptrdiff_t) column_end) >= TRUNCATE)) {
249 const uint8_t *end_candidate = start + column_end + TRUNCATE;
251 for (
const uint8_t *cursor = display_start; cursor < end_candidate;) {
252 size_t char_width = pm_parser_encoding_char_width(parser, cursor, parser->end - cursor);
256 if (char_width == 0)
break;
260 if (cursor + char_width > end_candidate) {
261 end_candidate = cursor;
265 cursor += char_width;
272 if (truncate_start) PM_BUFFER_APPEND_LITERAL(buffer,
"... ");
273 pm_buffer_append_string(buffer, (
const char *) display_start, (
size_t) (end - display_start));
276 PM_BUFFER_APPEND_LITERAL(buffer,
" ...\n");
277 }
else if (end == parser->end && end > parser->start && end[-1] !=
'\n') {
278 pm_buffer_append_byte(buffer,
'\n');
288 PM_BUFFER_APPEND_LITERAL(buffer,
" ");
289 pm_error_format_line(parser, buffer, format, line, 0, WINDOW);
294 uint32_t final_line = pm_rich_errors_final_line(parser);
298 uint32_t first_displayed_line;
299 uint32_t last_displayed_line;
300 uint32_t unused_line;
301 pm_rich_error_context(rich_errors[0].line_index, final_line, &first_displayed_line, &unused_line);
302 pm_rich_error_context(rich_errors[nerrors - 1].line_index, final_line, &unused_line, &last_displayed_line);
305 pm_error_line_format_init(&format, format_type, parser, first_displayed_line, last_displayed_line);
318 uint32_t last_line = 0;
319 uint32_t window_column_start = 0;
320 uint32_t window_column_end = 0;
321 size_t window_end_idx = 0;
323 for (
size_t idx = 0; idx < nerrors; idx++) {
328 uint32_t first_context = (rich_error->line_index > 2) ? (rich_error->line_index - 2) : 0;
330 if (rich_error->line_index > last_line && rich_error->line_index - last_line > 3) {
331 pm_error_line_format_divider(&format, buffer);
333 if (last_line >= rich_error->line_index) first_context = rich_error->line_index;
334 else if (first_context <= last_line) first_context = last_line + 1;
337 for (uint32_t line = first_context; line < rich_error->line_index; line++) {
338 pm_error_format_context_line(parser, buffer, &format, line);
344 if (idx == window_end_idx) {
345 switch (format_type) {
347 PM_BUFFER_APPEND_LITERAL(buffer,
"> ");
350 PM_BUFFER_APPEND_LITERAL(buffer, COLOR_BOLD
"> " COLOR_RESET);
353 PM_BUFFER_APPEND_LITERAL(buffer, COLOR_RED
"> " COLOR_RESET);
359 window_column_start = (rich_error->column_start >= TRUNCATE) ? rich_error->column_start : 0;
360 uint32_t window_limit = window_column_start + WINDOW;
364 window_column_end = rich_error->column_end;
365 for (window_end_idx = idx + 1; window_end_idx < nerrors; window_end_idx++) {
367 if ((next_error->line_index != rich_error->line_index) || (next_error->column_start >= window_limit))
break;
368 if (next_error->column_end > window_column_end) window_column_end = next_error->column_end;
371 if (window_column_end > window_limit) window_column_end = window_limit;
372 pm_error_format_line(parser, buffer, &format, rich_error->line_index, rich_error->column_start, window_column_end);
375 const uint8_t *start = parser->start + parser->line_offsets.
offsets[rich_error->line_index];
376 if (start == parser->end) pm_buffer_append_byte(buffer,
'\n');
387 PM_BUFFER_APPEND_LITERAL(buffer,
" ");
388 pm_error_line_format_blank(&format, buffer);
391 if (window_column_start > 0) {
392 PM_BUFFER_APPEND_LITERAL(buffer,
" ");
393 column = window_column_start;
396 pm_buffer_append_repeated(buffer,
' ', pm_error_advance_columns(parser, start, &column, rich_error->column_start));
398 switch (format_type) {
400 pm_buffer_append_byte(buffer,
'^');
403 PM_BUFFER_APPEND_LITERAL(buffer, COLOR_BOLD
"^");
406 PM_BUFFER_APPEND_LITERAL(buffer, COLOR_RED
"^");
412 pm_error_advance_columns(parser, start, &column, column + 1);
413 size_t tilde_limit = (rich_error->column_end < window_column_end) ? rich_error->column_end : window_column_end;
414 pm_buffer_append_repeated(buffer,
'~', pm_error_advance_columns(parser, start, &column, tilde_limit));
417 PM_BUFFER_APPEND_LITERAL(buffer, COLOR_RESET);
420 if (inline_messages) {
421 const char *message = pm_diagnostic_message(rich_error->diagnostic);
423 pm_buffer_append_byte(buffer,
' ');
424 pm_buffer_append_string(buffer, message, strlen(message));
427 pm_buffer_append_byte(buffer,
'\n');
432 last_line = rich_error->line_index;
433 uint32_t last_context = final_line;
434 if (idx + 1 < nerrors) {
435 uint32_t next_line = rich_errors[idx + 1].line_index;
436 last_context = (next_line > last_line) ? next_line - 1 : last_line;
439 for (uint32_t count = 0; count < 2 && last_line < last_context; count++) {
440 pm_error_format_context_line(parser, buffer, &format, ++last_line);
448 pm_line_column_t start_line_column = pm_line_offset_list_line_column(line_offsets, location->
start, 0);
452 if (start_line_column.
line == end_line_column.
line) {
453 column_end = end_line_column.
column;
455 column_end = (uint32_t) (line_offsets->
offsets[(uint32_t) start_line_column.
line + 1] - line_offsets->
offsets[(uint32_t) start_line_column.
line] - 1);
459 if (start_line_column.
column == column_end) column_end++;
462 .diagnostic = diagnostic,
463 .line_index = (uint32_t) start_line_column.
line,
464 .column_start = start_line_column.
column,
465 .column_end = column_end
474pm_error_append_summary(
const pm_parser_t *parser,
pm_buffer_t *buffer, uint32_t line,
const char *message) {
475 pm_buffer_append_string(buffer, (
const char *) pm_string_source(&parser->filepath), pm_string_length(&parser->filepath));
476 pm_buffer_append_byte(buffer,
':');
477 pm_buffer_append_line_number(buffer, pm_line_offset_list_line_number(line, parser->start_line), 0);
478 PM_BUFFER_APPEND_LITERAL(buffer,
": ");
479 pm_buffer_append_string(buffer, message, strlen(message));
484 size_t nerrors = parser->error_list.size;
485 PRISM_ASSERT(nerrors > 0);
488 if (rich_errors_size /
sizeof(
pm_rich_error_t) != nerrors) PRISM_OOM();
491 if (rich_errors == NULL) PRISM_OOM();
493 size_t rich_errors_idx = 0;
494 uint32_t first_syntax_line_index = 0;
500 switch (pm_diagnostic_error_level(diagnostic)) {
503 size_t position = rich_errors_idx;
504 while (position > 0 && rich_errors[position - 1].diagnostic->location.
start >= location.
start) {
505 rich_errors[position] = rich_errors[position - 1];
509 pm_rich_error_init(parser, &rich_errors[position], diagnostic, &location);
510 if (rich_errors_idx == 0) first_syntax_line_index = rich_errors[position].line_index;
518 pm_rich_error_init(parser, rich_errors, diagnostic, &location);
519 pm_error_append_summary(parser, buffer, rich_errors[0].line_index, pm_diagnostic_message(diagnostic));
521 if (rich_errors_are_utf8(parser, 1, rich_errors)) {
522 pm_buffer_append_byte(buffer,
'\n');
523 pm_rich_errors_format(parser, buffer, format_type, 1, rich_errors,
false);
526 xfree_sized(rich_errors, rich_errors_size);
532 const char *message = pm_diagnostic_message(diagnostic);
533 pm_buffer_append_string(buffer, message, strlen(message));
535 xfree_sized(rich_errors, rich_errors_size);
544 pm_rich_error_init(parser, rich_errors, diagnostic, &location);
545 pm_error_append_summary(parser, buffer, rich_errors[0].line_index, pm_diagnostic_message(diagnostic));
547 if (rich_errors_are_utf8(parser, 1, rich_errors)) {
548 pm_buffer_append_byte(buffer,
'\n');
549 pm_rich_errors_format(parser, buffer, format_type, 1, rich_errors,
false);
552 xfree_sized(rich_errors, rich_errors_size);
556 if (encoding_error == NULL) encoding_error = diagnostic;
562 if (rich_errors_idx == 0) {
563 PRISM_ASSERT(encoding_error != NULL);
564 const char *message = pm_diagnostic_message(encoding_error);
565 pm_buffer_append_string(buffer, message, strlen(message));
567 xfree_sized(rich_errors, rich_errors_size);
571 nerrors = rich_errors_idx;
574 pm_error_append_summary(parser, buffer, first_syntax_line_index, (nerrors > 1) ?
"syntax errors found\n" :
"syntax error found\n");
576 if (rich_errors_are_utf8(parser, nerrors, rich_errors)) {
580 pm_rich_errors_format(parser, buffer, format_type, nerrors, rich_errors,
true);
587 for (
size_t idx = 0; idx < nerrors; idx++) {
588 if (idx > 0) pm_buffer_append_byte(buffer,
'\n');
589 pm_error_append_summary(parser, buffer, rich_errors[idx].line_index, pm_diagnostic_message(rich_errors[idx].diagnostic));
593 xfree_sized(rich_errors, rich_errors_size);
pm_error_level_t
The levels of errors generated during parsing.
@ PM_ERROR_LEVEL_ARGUMENT
For errors that should raise an argument error.
@ PM_ERROR_LEVEL_LOAD
For errors that should raise a load error.
@ PM_ERROR_LEVEL_STACK
For errors that should raise a system stack error.
@ PM_ERROR_LEVEL_ENCODING_COMPATIBILITY
For errors that should raise an encoding compatibility error.
@ PM_ERROR_LEVEL_SYNTAX
For errors that should raise a syntax error.
#define xmalloc
Old name of ruby_xmalloc.
C99 shim for <stdbool.h>.
A line and column in a string.
uint32_t column
The column in bytes.
int32_t line
The line number.
A list of offsets of the start of lines in a string.
uint32_t * offsets
The list of offsets.
size_t size
The number of offsets in the list.
This struct represents a slice in the source code, defined by an offset and a length.
uint32_t start
The offset of the location from the start of the source.
uint32_t length
The length of the location.