From 3ceb85bd93fa87a5be3682ab8995abea82a63ea3 Mon Sep 17 00:00:00 2001 From: Carlos Maniero Date: Thu, 20 Apr 2023 13:32:36 -0300 Subject: parser: Stop exiting on parser error Previously, when an error occurred during parsing, the application would exit, making it difficult to test the parser and limiting the compiler's extensibility. This commit improves the parser's error handling by allowing for continued execution after an error, enabling easier testing and increased flexibility. The parser is prepared to handle multiples errors, although the current implementation always returns a single error, it may be useful given multiples functions where we can show errors by context. Signed-off-by: Carlos Maniero Reviwed-by: Johnny Richard --- src/pipac.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'src/pipac.c') diff --git a/src/pipac.c b/src/pipac.c index 41294c4..2b7b1f5 100644 --- a/src/pipac.c +++ b/src/pipac.c @@ -47,6 +47,20 @@ print_tokens(lexer_t *lexer) { } } +void +parser_print_errors(parser_t *parser) { + for (int i=0; i < parser->errors_len; i++) { + parser_error_t error = parser->errors[i]; + + fprintf( + stderr, + "%s:%d:%d: [ERROR]: %s\n", + error.token.filepath, error.token.row + 1, error.token.col + 1, + error.message + ); + } +} + int main(int argc, char **argv) { @@ -64,7 +78,11 @@ main(int argc, char **argv) parser_init(&parser, &lexer); ast_node_t* func = ast_node_new(); - parser_parse_function_declaration(&parser, func); + + if (!parser_parse_function_declaration(&parser, func)) { + parser_print_errors(&parser); + return EXIT_FAILURE; + } generate_gas_x86_64_linux(func); -- cgit v1.2.3