From 6f187a71cbe3aa4ebb32ba287c75562d96c7a3f4 Mon Sep 17 00:00:00 2001 From: Carlos Maniero Date: Wed, 10 May 2023 17:49:36 -0300 Subject: tests: Replace parse function with parse ns for error handling It was necessary to test function calls errors. Signed-off-by: Carlos Maniero --- src/parser.c | 10 ++++++++++ test/parser_test.c | 11 ++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/parser.c b/src/parser.c index 578514c..377ff73 100644 --- a/src/parser.c +++ b/src/parser.c @@ -790,7 +790,17 @@ parser_parse_ns(parser_t *parser) } vector_push_back(nodes, node); + continue; } + + parser_error_t error; + + lexer_peek_next_token(parser->lexer, &error.token); + sprintf(error.message, "Unexpected token '" SVFMT "'", SVARG(&error.token.value)); + parser->errors[parser->errors_len++] = error; + + ast_node_destroy_vector(nodes); + return NULL; } return ast_node_new_namespace(nodes); diff --git a/test/parser_test.c b/test/parser_test.c index 78e2e23..ebb917c 100644 --- a/test/parser_test.c +++ b/test/parser_test.c @@ -48,9 +48,9 @@ assert_parser_error(char *src, char *error_msg) make_lexer_from_static_src(&lexer, src); parser_init(&parser, &lexer, scope); - ast_node_t *ast_function = parser_parse_function_declaration(&parser); + ast_node_t *ast_ns = parser_parse_ns(&parser); - assert_false(ast_function != NULL); + assert_false(ast_ns != NULL); assert_int(1, ==, parser.errors_len); assert_string_equal(error_msg, parser.errors[0].message); @@ -394,7 +394,7 @@ assert_string_view_equal(char *expected, string_view_t actual) static MunitResult test_parse_basic_syntax_errors(const MunitParameter params[], void *user_data_or_fixture) { - assert_parser_error("main(): i32 { return 42; }", "expected 'fn' but got 'TOKEN_NAME'"); + assert_parser_error("main(): i32 { return 42; }", "Unexpected token 'main'"); assert_parser_error("fn (): i32 { return 42; }", "expected 'TOKEN_NAME' but got '('"); assert_parser_error("fn main): i32 { return 42; }", "expected '(' but got ')'"); assert_parser_error("fn main(: i32 { return 42; }", "expected ')' but got ':'"); @@ -427,8 +427,9 @@ test_parse_basic_syntax_errors(const MunitParameter params[], void *user_data_or "incompatible types: expected 'bool', actual 'i32'."); assert_parser_error("fn main(): i32 { if true { return true; } \nreturn 42;\n }", "incompatible types: expected 'i32', actual 'bool'."); - // FIXME: once function calls are implemented, this error should inform that - // neither a variable or function call was found. + assert_parser_error("fn main(): i32 { return fn_404(); }", "identifier 'fn_404' not defined"); + assert_parser_error("fn my_bool_fn(): bool { return true; } fn main(): i32 { return my_bool_fn(); }", + "incompatible types: expected 'i32', actual 'bool'."); assert_parser_error("fn main(): i32 { oxi 42; }", "unexpected token 'TOKEN_NAME' value='oxi'"); return MUNIT_OK; -- cgit v1.2.3