From d941b88b7005bb39c7ab09e6c30f396bfe8eae35 Mon Sep 17 00:00:00 2001 From: William Bell Date: Thu, 5 Jun 2025 23:18:25 +0100 Subject: [PATCH] fix bug causing parser to miss new lines after let. --- src/parser/declaration/declaration.c | 6 ++++-- src/parser/parser.c | 8 ++++++-- src/parser/parser.h | 2 +- test.ar | 30 +++++++++++++++++++++++----- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/parser/declaration/declaration.c b/src/parser/declaration/declaration.c index 5767df9..14122cf 100644 --- a/src/parser/declaration/declaration.c +++ b/src/parser/declaration/declaration.c @@ -100,12 +100,14 @@ ParsedValue *parse_declaration(char *file, DArray *tokens, size_t *index) { break; token = darray_get(tokens, *index); } - skip_newlines_and_indents(tokens, index); + size_t count = skip_newlines_and_indents(tokens, index); if ((*index) >= tokens->size) break; token = darray_get(tokens, *index); - if (token->type != TOKEN_COMMA) + if (token->type != TOKEN_COMMA) { + (*index)-=count; break; + } (*index)++; error_if_finished(file, tokens, index); skip_newlines_and_indents(tokens, index); diff --git a/src/parser/parser.c b/src/parser/parser.c index 36885df..51251b1 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -30,19 +30,22 @@ void error_if_finished(char *file, DArray *tokens, size_t *index) { } } -void skip_newlines_and_indents(DArray *tokens, size_t *index) { +size_t skip_newlines_and_indents(DArray *tokens, size_t *index) { bool passed = false; + size_t count = 0; while (!passed && (*index) < tokens->size) { Token *token = darray_get(tokens, *index); switch (token->type) { case TOKEN_NEW_LINE: case TOKEN_INDENT: + count++; (*index)++; break; default: passed = true; } } + return count; } ParsedValue *parse_token(char *file, DArray *tokens, size_t *index, @@ -140,10 +143,11 @@ void parser(char *file, DArray *parsed, DArray *tokens, bool inline_flag) { size_t index = 0; bool expecting_new_line = false; while (index < tokens->size) { + size_t old_index = index; ParsedValue *parsed_code = parse_token(file, tokens, &index, inline_flag); if (parsed_code) { if (expecting_new_line) { - Token *token = darray_get(tokens, index-1); + Token *token = darray_get(tokens, old_index); fprintf(stderr, "%s:%zu:%zu error: syntax error\n", file, token->line, token->column); exit(EXIT_FAILURE); diff --git a/src/parser/parser.h b/src/parser/parser.h index c23e0cd..5806e3f 100644 --- a/src/parser/parser.h +++ b/src/parser/parser.h @@ -37,6 +37,6 @@ void free_parsed(void *ptr); void error_if_finished(char *file,DArray *tokens, size_t *index); -void skip_newlines_and_indents(DArray *tokens, size_t *index); +size_t skip_newlines_and_indents(DArray *tokens, size_t *index); #endif // PARSER_H \ No newline at end of file diff --git a/test.ar b/test.ar index dc3865b..0256c80 100644 --- a/test.ar +++ b/test.ar @@ -1,6 +1,26 @@ -x = do - do - test - test +let a, + b = 1, + c, + d = 42, + temp_result, + compute_area(radius) = 3.1415, + identity(x) = x, + f(x), + g(y, z), + result, + z = 0, + extremely_long_variable_name_to_test_limits, + cache_value = compute_area(5), + placeholder_fn_with_no_body(arg1, arg2, arg3), + total = identity(100), + deeply_nested_args_function(arg1, arg2, arg3, arg4, arg5), + sum = a, + another, + another_function(), + just_null_here -term.log("hello world") \ No newline at end of file + + + + +if (x) term.log("hello world") \ No newline at end of file