fix bug causing parser to miss new lines after let.
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
30
test.ar
30
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")
|
||||
|
||||
|
||||
|
||||
|
||||
if (x) term.log("hello world")
|
||||
Reference in New Issue
Block a user