From 813f14a3d4a6e80bbdbd313ffbc4e31cee5e60cd Mon Sep 17 00:00:00 2001 From: SimonFJ20 Date: Wed, 22 Mar 2023 10:57:50 +0100 Subject: [PATCH] add parser error format --- src/parser.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/parser.c b/src/parser.c index 062daa1..95b2e77 100644 --- a/src/parser.c +++ b/src/parser.c @@ -6,8 +6,6 @@ #include #include -#define PARSER_ERROR_BUFFER_SIZE 512 - Expr* error_expr(Position pos, const char* message) { Expr* node = malloc(sizeof(Expr)); @@ -100,7 +98,7 @@ Expr* parser_error(Parser* parser, const char* message) ++i) line_width = i; - char line[PARSER_ERROR_BUFFER_SIZE] = { 0 }; + char line[512] = { 0 }; char underline_indent[512] = { 0 }; if (line_width > 0) @@ -109,10 +107,10 @@ Expr* parser_error(Parser* parser, const char* message) char underline[512] = { '^', 0 }; memset(underline, '^', parser->current.length); - char formatted[PARSER_ERROR_BUFFER_SIZE]; + char formatted[512 * 4]; snprintf( formatted, - PARSER_ERROR_BUFFER_SIZE, + 512 * 4, "error: %s\n |\n %-4d|%s\n |%s%s\n", message, parser->current.line, @@ -123,15 +121,25 @@ Expr* parser_error(Parser* parser, const char* message) return error_expr(parser_pos(parser), formatted); } -Expr* parser_unknown_token_error(Parser* parser) { } +Expr* parser_unknown_token_error(Parser* parser) +{ + char buffer[128] = { 0 }; + snprintf( + buffer, 128, "unknown char '%c'", parser->text[parser->current.index] + ); + return parser_error(parser, buffer); +} Expr* parser_operand(Parser* parser) { if (parser_current_is(parser, TokenTypeInt)) { return NULL; + } else if (parser_current_is(parser, TokenTypeEof)) { + return parser_error(parser, "unexpected end-of-file"); } else { + Expr* error = parser_unknown_token_error(parser); parser_step(parser); - return parser_error(parser, "invalid token"); + return error; } }