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;
48pm_rich_error_compare(
const void *a,
const void *b) {
52 if (left->line_index != right->line_index) {
53 return (left->line_index < right->line_index) ? -1 : 1;
56 if (left->column_start != right->column_start) {
57 return (left->column_start < right->column_start) ? -1 : 1;
60 if (left->idx != right->idx) {
61 return (left->idx < right->idx) ? 1 : -1;
72pm_rich_errors_final_line(
const pm_parser_t *parser) {
74 uint32_t final_line = (uint32_t) (line_offsets->
size - 1);
76 if (final_line > 0 && (parser->start + line_offsets->
offsets[final_line]) == parser->end) {
90pm_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;
105 uint32_t final_line = pm_rich_errors_final_line(parser);
112 size_t checked_index = 0;
114 for (
size_t idx = 0; idx < nerrors; idx++) {
117 pm_rich_error_context(rich_errors[idx].line_index, final_line, &first_line, &last_line);
119 size_t start_index = (size_t) first_line;
120 size_t end_index = (size_t) last_line;
122 if (start_index < checked_index) start_index = checked_index;
123 if (start_index > end_index)
continue;
125 if (!lines_are_utf8(parser, start_index, end_index))
return false;
126 checked_index = end_index + 1;
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"
163pm_error_line_digits(int32_t line,
char digits[11]) {
166 uint32_t magnitude = (line < 0) ? (0U - (uint32_t) line) : (uint32_t) line;
170 digits[10 - length++] = (char) (
'0' + (magnitude % 10));
172 }
while (magnitude > 0);
174 if (line < 0) digits[10 - length++] =
'-';
183pm_error_append_line_number(
pm_buffer_t *buffer, int32_t line, uint8_t width) {
185 uint8_t length = pm_error_line_digits(line, digits);
187 if (width > length) pm_buffer_append_repeated(buffer,
' ', (
size_t) (width - length));
188 pm_buffer_append_string(buffer, digits +
sizeof(digits) - length, length);
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);
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;
208 .width = (first_width > last_width) ? first_width : last_width
219 pm_error_append_line_number(buffer, line, format->
width);
220 PM_BUFFER_APPEND_LITERAL(buffer,
" | ");
231 pm_buffer_append_repeated(buffer,
' ', (
size_t) format->
width + 1);
232 PM_BUFFER_APPEND_LITERAL(buffer,
"| ");
243 PM_BUFFER_APPEND_LITERAL(buffer,
" ");
244 pm_buffer_append_repeated(buffer,
'~', (
size_t) format->
width + 4);
246 pm_buffer_append_byte(buffer,
'\n');
257pm_error_advance_columns(
const pm_parser_t *parser,
const uint8_t *start,
size_t *column,
size_t limit) {
260 while (*column < limit) {
261 const uint8_t *cursor = start + *column;
262 size_t char_width = 1;
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;
271 *column += char_width;
281 PRISM_ASSERT(index < parser->line_offsets.
size);
283 const uint8_t *start = parser->start + parser->line_offsets.
offsets[index];
286 if (index >= parser->line_offsets.
size - 1) {
289 end = parser->start + parser->line_offsets.
offsets[index + 1];
292 pm_error_line_format_number(format, buffer, pm_line_offset_list_line_number(line, parser->start_line));
296 bool truncate_start = column_start >= TRUNCATE;
297 const uint8_t *display_start = truncate_start ? (start + column_start) : start;
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;
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);
313 if (char_width == 0)
break;
317 if (cursor + char_width > end_candidate) {
318 end_candidate = cursor;
322 cursor += char_width;
329 if (truncate_start) PM_BUFFER_APPEND_LITERAL(buffer,
"... ");
330 pm_buffer_append_string(buffer, (
const char *) display_start, (
size_t) (end - display_start));
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');
345 PM_BUFFER_APPEND_LITERAL(buffer,
" ");
346 pm_error_format_line(parser, buffer, format, line, 0, WINDOW);
351 uint32_t final_line = pm_rich_errors_final_line(parser);
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);
362 pm_error_line_format_init(&format, format_type, parser, first_displayed_line, last_displayed_line);
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;
380 for (
size_t idx = 0; idx < nerrors; idx++) {
385 uint32_t first_context = (rich_error->line_index > 2) ? (rich_error->line_index - 2) : 0;
387 if (rich_error->line_index > last_line && rich_error->line_index - last_line > 3) {
388 pm_error_line_format_divider(&format, buffer);
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;
394 for (uint32_t line = first_context; line < rich_error->line_index; line++) {
395 pm_error_format_context_line(parser, buffer, &format, line);
401 if (idx == window_end_idx) {
402 switch (format_type) {
404 PM_BUFFER_APPEND_LITERAL(buffer,
"> ");
407 PM_BUFFER_APPEND_LITERAL(buffer, COLOR_BOLD
"> " COLOR_RESET);
410 PM_BUFFER_APPEND_LITERAL(buffer, COLOR_RED
"> " COLOR_RESET);
416 window_column_start = (rich_error->column_start >= TRUNCATE) ? rich_error->column_start : 0;
417 uint32_t window_limit = window_column_start + WINDOW;
421 window_column_end = rich_error->column_end;
422 for (window_end_idx = idx + 1; window_end_idx < nerrors; 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;
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);
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');
444 PM_BUFFER_APPEND_LITERAL(buffer,
" ");
445 pm_error_line_format_blank(&format, buffer);
448 if (window_column_start > 0) {
449 PM_BUFFER_APPEND_LITERAL(buffer,
" ");
450 column = window_column_start;
453 pm_buffer_append_repeated(buffer,
' ', pm_error_advance_columns(parser, start, &column, rich_error->column_start));
455 switch (format_type) {
457 pm_buffer_append_byte(buffer,
'^');
460 PM_BUFFER_APPEND_LITERAL(buffer, COLOR_BOLD
"^");
463 PM_BUFFER_APPEND_LITERAL(buffer, COLOR_RED
"^");
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));
474 PM_BUFFER_APPEND_LITERAL(buffer, COLOR_RESET);
477 if (inline_messages) {
478 const char *message = pm_diagnostic_message(rich_error->diagnostic);
480 pm_buffer_append_byte(buffer,
' ');
481 pm_buffer_append_string(buffer, message, strlen(message));
484 pm_buffer_append_byte(buffer,
'\n');
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;
496 for (uint32_t count = 0; count < 2 && last_line < last_context; count++) {
497 pm_error_format_context_line(parser, buffer, &format, ++last_line);
505 pm_line_column_t start_line_column = pm_line_offset_list_line_column(line_offsets, location->
start, 0);
509 if (start_line_column.
line == end_line_column.
line) {
510 column_end = end_line_column.
column;
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);
516 if (start_line_column.
column == column_end) column_end++;
519 .diagnostic = diagnostic,
520 .line_index = (uint32_t) start_line_column.
line,
521 .column_start = start_line_column.
column,
522 .column_end = column_end
531pm_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));
541 size_t nerrors = parser->error_list.size;
542 PRISM_ASSERT(nerrors > 0);
545 if (rich_errors_size /
sizeof(
pm_rich_error_t) != nerrors) PRISM_OOM();
548 if (rich_errors == NULL) PRISM_OOM();
550 size_t rich_errors_idx = 0;
555 switch (pm_diagnostic_error_level(diagnostic)) {
557 pm_rich_error_init(parser, &rich_errors[rich_errors_idx], diagnostic, &location);
558 rich_errors[rich_errors_idx].idx = rich_errors_idx;
566 pm_rich_error_init(parser, rich_errors, diagnostic, &location);
567 pm_error_append_summary(parser, buffer, rich_errors[0].line_index, pm_diagnostic_message(diagnostic));
569 if (rich_errors_are_utf8(parser, 1, rich_errors)) {
570 pm_buffer_append_byte(buffer,
'\n');
571 pm_rich_errors_format(parser, buffer, format_type, 1, rich_errors,
false);
574 xfree_sized(rich_errors, rich_errors_size);
580 const char *message = pm_diagnostic_message(diagnostic);
581 pm_buffer_append_string(buffer, message, strlen(message));
583 xfree_sized(rich_errors, rich_errors_size);
592 pm_rich_error_init(parser, rich_errors, diagnostic, &location);
593 pm_error_append_summary(parser, buffer, rich_errors[0].line_index, pm_diagnostic_message(diagnostic));
595 if (rich_errors_are_utf8(parser, 1, rich_errors)) {
596 pm_buffer_append_byte(buffer,
'\n');
597 pm_rich_errors_format(parser, buffer, format_type, 1, rich_errors,
false);
600 xfree_sized(rich_errors, rich_errors_size);
606 PRISM_ASSERT(rich_errors_idx == nerrors);
610 pm_error_append_summary(parser, buffer, rich_errors[0].line_index, (nerrors > 1) ?
"syntax errors found\n" :
"syntax error found\n");
615 qsort(rich_errors, nerrors,
sizeof(
pm_rich_error_t), pm_rich_error_compare);
617 if (rich_errors_are_utf8(parser, nerrors, rich_errors)) {
621 pm_rich_errors_format(parser, buffer, format_type, nerrors, rich_errors,
true);
628 for (
size_t idx = 0; idx < nerrors; idx++) {
629 if (idx > 0) pm_buffer_append_byte(buffer,
'\n');
630 pm_error_append_summary(parser, buffer, rich_errors[idx].line_index, pm_diagnostic_message(rich_errors[idx].diagnostic));
634 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_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.