Skip to content

Commit

Permalink
Improve parser error for missing }
Browse files Browse the repository at this point in the history
  • Loading branch information
LesleyLai committed Jan 18, 2025
1 parent f422755 commit fd9edc6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/frontend/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ typedef struct Parser {

static SourceRange token_source_range(Token token)
{
return (SourceRange){.begin = token.start, .end = token.start + token.size};
const uint32_t begin =
(token.type == TOKEN_EOF) ? token.start - 1 : token.start;

return (SourceRange){.begin = begin, .end = token.start + token.size};
}

static SourceRange source_range_union(SourceRange lhs, SourceRange rhs)
Expand Down Expand Up @@ -342,7 +345,8 @@ static CompoundStmt parse_compound_stmt(Parser* parser)

bool has_error = false;

while (parser_current_token(parser).type != TOKEN_RIGHT_BRACE) {
while (parser_current_token(parser).type != TOKEN_RIGHT_BRACE &&
parser_current_token(parser).type != TOKEN_EOF) {
Stmt stmt;
parse_stmt(parser, &stmt);

Expand All @@ -365,7 +369,7 @@ static CompoundStmt parse_compound_stmt(Parser* parser)
parse_advance(parser);
}
} else {
parse_consume(parser, TOKEN_RIGHT_BRACE, "Expect }");
parse_consume(parser, TOKEN_RIGHT_BRACE, "Expect `}`");
}

Stmt* stmts =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
{{base_dir}}/failed_parsing/unclosed_brace.c:3:1: Error: Expect statement
{{base_dir}}/failed_parsing/unclosed_brace.c:2:14: Error: Expect `}`
2 | return 0;
| ^

0 comments on commit fd9edc6

Please sign in to comment.