From efa510e6b58ce13c53e94f13a5be0007240e9dcc Mon Sep 17 00:00:00 2001 From: Mel Date: Wed, 9 Jul 2025 04:46:58 +0200 Subject: Display human-readable, informative parser error messages w/ source snippet Signed-off-by: Mel --- boot/catboot.c | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) (limited to 'boot/catboot.c') diff --git a/boot/catboot.c b/boot/catboot.c index 53b2bed..ed5831e 100644 --- a/boot/catboot.c +++ b/boot/catboot.c @@ -95,24 +95,23 @@ debug_lex_pass(struct String source) } integer -debug_parse_pass(struct String source) +debug_parse_pass(struct Source_File source_file) { struct Lexer lexer; - lexer_new(&lexer, source); + lexer_new(&lexer, source_file.source); struct Parser parser; - parser_new(&parser, &lexer); + parser_new(&parser, &lexer, source_file); - struct Parser_Error parser_error = parser_error_none(); - struct Tree tree = parser_do_your_thing(&parser, &parser_error); - if (!parser_error_is_none(&parser_error)) { - fprintf(stderr, "parser error: %s\n", parser_error_to_string(&parser_error)); - return 1; + struct Tree tree; + int parser_result = parser_do_your_thing(&parser, &tree); + if (parser_result != 0) { + fprintf(stderr, "parser finished with errors\n"); } tree_printer(&tree); - return 0; + return parser_result; } enum Command_Result @@ -155,8 +154,12 @@ enum Command_Result test_parse_command(struct Command_Arguments* arguments) { struct String source = read_file(arguments->input); + struct Source_File source_file = { + .source = source, + .path = string_from_static_c_string(arguments->input), + }; - if (debug_parse_pass(source)) return COMMAND_FAIL; + if (debug_parse_pass(source_file)) return COMMAND_FAIL; return COMMAND_OK; } @@ -165,10 +168,14 @@ enum Command_Result default_command(struct Command_Arguments* arguments) { struct String source = read_file(arguments->input); + struct Source_File source_file = { + .source = source, + .path = string_from_static_c_string(arguments->input), + }; debug_lex_pass(source); printf("\n"); - if (debug_parse_pass(source)) return COMMAND_FAIL; + if (debug_parse_pass(source_file)) return COMMAND_FAIL; printf("\n"); return COMMAND_OK; -- cgit 1.4.1