From c714caa27aba5c838faca1509f41f98311073f47 Mon Sep 17 00:00:00 2001 From: Martin Griffin Date: Wed, 4 Dec 2024 18:22:35 +0000 Subject: [PATCH] trainerproc: Fix showing incorrect error context (#5769) --- tools/trainerproc/main.c | 98 ++++++++++++++++++++++------------------ 1 file changed, 53 insertions(+), 45 deletions(-) diff --git a/tools/trainerproc/main.c b/tools/trainerproc/main.c index 8b939a955f..f1bcf1bfa4 100644 --- a/tools/trainerproc/main.c +++ b/tools/trainerproc/main.c @@ -251,51 +251,6 @@ static bool set_parse_error(struct Parser *p, struct SourceLocation location, co return false; } -static bool show_parse_error(struct Parser *p) -{ - // Print error message. - int n = fprintf(stderr, "%s:%d: ", p->source->path, p->error_location.line); - fprintf(stderr, "error: %s\n", p->error); - - // Seek to the line. - int line, begin, end; - for (line = 1, begin = 0; begin < p->source->buffer_n; begin++) - { - if (p->error_location.line == line) - break; - if (p->source->buffer[begin] == '\n') - line++; - } - for (end = begin; end < p->source->buffer_n; end++) - { - if (p->source->buffer[end] == '\n') - break; - } - - // Print the source line. - fprintf(stderr, "%s:%d: %.*s\n", p->source->path, p->error_location.line, end - begin, &p->source->buffer[begin]); - - // Print caret pointing at the column. - fprintf(stderr, "%*s", n, ""); - for (int column = 1; column < p->error_location.column && begin + column < end; column++) - { - unsigned char c = p->source->buffer[begin + column]; - fputc(c == '\t' ? c : ' ', stderr); - } - fprintf(stderr, "^\n"); - - p->error = NULL; - p->fatal_error = true; - - return false; -} - -static bool set_show_parse_error(struct Parser *p, struct SourceLocation location, const char *error) -{ - set_parse_error(p, location, error); - return show_parse_error(p); -} - __attribute__((warn_unused_result)) static bool peek_char(struct Parser *p, unsigned char *c) { @@ -618,6 +573,59 @@ static bool match_move_identifier(struct Parser *p, struct Token *t) return true; } +static bool show_parse_error(struct Parser *p) +{ + // Print error message. + int n = fprintf(stderr, "%s:%d: ", p->source->path, p->error_location.line); + fprintf(stderr, "error: %s\n", p->error); + + struct Parser p_ = { + .source = p->source, + .location = { .line = 1, .column = 1 }, + .offset = 0, + }; + + for (;;) { + if (p->error_location.line == p_.location.line) + break; + if (!match_empty_line(&p_)) + skip_line(&p_); + if (match_eof(&p_)) + assert(false); + } + + int begin = p_.offset; + int end; + for (end = begin; end < p->source->buffer_n; end++) + { + if (p->source->buffer[end] == '\n') + break; + } + + // Print the source line. + fprintf(stderr, "%s:%d: %.*s\n", p->source->path, p->error_location.line, end - begin, &p->source->buffer[begin]); + + // Print caret pointing at the column. + fprintf(stderr, "%*s", n, ""); + for (int column = 1; column < p->error_location.column && begin + column < end; column++) + { + unsigned char c = p->source->buffer[begin + column]; + fputc(c == '\t' ? c : ' ', stderr); + } + fprintf(stderr, "^\n"); + + p->error = NULL; + p->fatal_error = true; + + return false; +} + +static bool set_show_parse_error(struct Parser *p, struct SourceLocation location, const char *error) +{ + set_parse_error(p, location, error); + return show_parse_error(p); +} + __attribute__((warn_unused_result)) static bool parse_section(struct Parser *p, struct Token *section) {