trainerproc: Fix showing incorrect error context (#5769)
This commit is contained in:
parent
93e733820c
commit
c714caa27a
1 changed files with 53 additions and 45 deletions
|
@ -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)
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue