From f2bc5274bb55bc2b180efae274ada107ea4716d1 Mon Sep 17 00:00:00 2001 From: Johnny Richard Date: Fri, 14 Apr 2023 23:38:43 +0200 Subject: parser: Show filepath row and col when parsing fails In order to find out where a parsing error occurred, this patch introduces the exactly location following the format 'file:row:col'. Signed-off-by: Johnny Richard --- src/lexer.c | 8 ++++++++ src/lexer.h | 1 + src/parser.c | 14 ++++++++++++-- 3 files changed, 21 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/lexer.c b/src/lexer.c index 0327ad5..f58b0ea 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -57,6 +57,7 @@ lexer_next_token(lexer_t *lexer, token_t *token) token->kind = TOKEN_NUMBER; token->value = (char *) malloc(sizeof(char) * (lexer->cur - begin + 1)); strncpy(token->value, lexer->src + begin, lexer->cur - begin); + token->filepath = lexer->filepath; token->row = lexer->row; token->col = begin - lexer->bol; return; @@ -71,6 +72,7 @@ lexer_next_token(lexer_t *lexer, token_t *token) token->kind = TOKEN_NAME; token->value = (char *) malloc(sizeof(char) * (lexer->cur - begin + 1)); strncpy(token->value, lexer->src + begin, lexer->cur - begin); + token->filepath = lexer->filepath; token->row = lexer->row; token->col = begin - lexer->bol; return; @@ -80,6 +82,7 @@ lexer_next_token(lexer_t *lexer, token_t *token) token->kind = TOKEN_OPAREN; token->value = (char *) malloc(sizeof(char) + 1); strcpy(token->value, "("); + token->filepath = lexer->filepath; token->row = lexer->row; token->col = lexer->cur - lexer->bol; lexer_drop_char(lexer); @@ -90,6 +93,7 @@ lexer_next_token(lexer_t *lexer, token_t *token) token->kind = TOKEN_CPAREN; token->value = (char *) malloc(sizeof(char) + 1); strcpy(token->value, ")"); + token->filepath = lexer->filepath; token->row = lexer->row; token->col = lexer->cur - lexer->bol; lexer_drop_char(lexer); @@ -100,6 +104,7 @@ lexer_next_token(lexer_t *lexer, token_t *token) token->kind = TOKEN_COLON; token->value = (char *) malloc(sizeof(char) + 1); strcpy(token->value, ":"); + token->filepath = lexer->filepath; token->row = lexer->row; token->col = lexer->cur - lexer->bol; lexer_drop_char(lexer); @@ -110,6 +115,7 @@ lexer_next_token(lexer_t *lexer, token_t *token) token->kind = TOKEN_SEMICOLON; token->value = (char *) malloc(sizeof(char) + 1); strcpy(token->value, ";"); + token->filepath = lexer->filepath; token->row = lexer->row; token->col = lexer->cur - lexer->bol; lexer_drop_char(lexer); @@ -120,6 +126,7 @@ lexer_next_token(lexer_t *lexer, token_t *token) token->kind = TOKEN_OCURLY; token->value = (char *) malloc(sizeof(char) + 1); strcpy(token->value, "{"); + token->filepath = lexer->filepath; token->row = lexer->row; token->col = lexer->cur - lexer->bol; lexer_drop_char(lexer); @@ -130,6 +137,7 @@ lexer_next_token(lexer_t *lexer, token_t *token) token->kind = TOKEN_CCURLY; token->value = (char *) malloc(sizeof(char) + 1); strcpy(token->value, "}"); + token->filepath = lexer->filepath; token->row = lexer->row; token->col = lexer->cur - lexer->bol; lexer_drop_char(lexer); diff --git a/src/lexer.h b/src/lexer.h index f5333a0..472ee10 100644 --- a/src/lexer.h +++ b/src/lexer.h @@ -36,6 +36,7 @@ typedef enum { typedef struct token_t { token_kind_t kind; char *value; + char *filepath; uint32_t row; uint32_t col; } token_t; diff --git a/src/parser.c b/src/parser.c index 5d2a734..4867201 100644 --- a/src/parser.c +++ b/src/parser.c @@ -38,12 +38,22 @@ expected_token(parser_t *parser, token_kind_t kind) lexer_next_token(parser->lexer, &token); if (token.kind == TOKEN_EOF) { - fprintf(stderr, "[ERROR]: expected '%s' but got end of file\n", token_kind_to_str(kind)); + fprintf( + stderr, + "%s:%d:%d: [ERROR]: expected '%s' but got end of file\n", + token.filepath, token.row + 1, token.col + 1, + token_kind_to_str(kind) + ); exit(EXIT_FAILURE); } if (token.kind != kind) { - fprintf(stderr, "[ERROR]: expected '%s' but got '%s'\n", token_kind_to_str(kind), token_kind_to_str(token.kind)); + fprintf( + stderr, + "%s:%d:%d: [ERROR]: expected '%s' but got '%s'\n", + token.filepath, token.row + 1, token.col + 1, + token_kind_to_str(kind), token_kind_to_str(token.kind) + ); exit(EXIT_FAILURE); } -- cgit v1.2.3