From 787b1aed35ef083ab2da5d260c001f89d92de190 Mon Sep 17 00:00:00 2001 From: "Gu://em_" Date: Sat, 24 Jan 2026 16:48:21 +0100 Subject: [PATCH] fix: random fixes --- src/parser/grammar.c | 27 ++++++++++++++++++++++++++- src/parser/grammar_advanced.c | 5 ----- src/parser/grammar_basic.c | 11 +++++++++-- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/parser/grammar.c b/src/parser/grammar.c index 982a744..be26dfc 100644 --- a/src/parser/grammar.c +++ b/src/parser/grammar.c @@ -166,5 +166,30 @@ bool is_first(struct token token, enum rule rule) struct ast *parse_input(struct lexer_context *ctx) { - return parse_list(ctx); + struct token *token = PEEK_TOKEN(); + + if (token->type == TOKEN_EOF) + return ast_create_list(NULL); + + if (token->type == TOKEN_NEWLINE) + { + POP_TOKEN(); + return ast_create_list(NULL); + } + + struct ast *ast = parse_list(ctx); + if (ast == NULL) + return NULL; + + token = PEEK_TOKEN(); + if (token->type == TOKEN_NEWLINE || token->type == TOKEN_EOF) + { + if (token->type == TOKEN_NEWLINE) + POP_TOKEN(); + return ast; + } + + puts("Syntax error: expected newline or EOF after list"); + ast_free(&ast); + return NULL; } diff --git a/src/parser/grammar_advanced.c b/src/parser/grammar_advanced.c index 1608d5c..aac5742 100644 --- a/src/parser/grammar_advanced.c +++ b/src/parser/grammar_advanced.c @@ -9,11 +9,6 @@ struct ast *parse_redirection(struct lexer_context *ctx) { struct token *token = PEEK_TOKEN(); - if (token->type == TOKEN_IONUMBER) - { - // TODO - } - int io_number = -1; if (token->type == TOKEN_IONUMBER) { diff --git a/src/parser/grammar_basic.c b/src/parser/grammar_basic.c index 78b3b3d..a0e0868 100644 --- a/src/parser/grammar_basic.c +++ b/src/parser/grammar_basic.c @@ -48,7 +48,11 @@ struct ast *parse_list(struct lexer_context *ctx) { current_node = parse_and_or(ctx); if (current_node == NULL) + { + struct ast *tmp = ast_create_list(result_list); + ast_free(&tmp); return NULL; + } result_list = list_append(result_list, current_node); token = PEEK_TOKEN(); } @@ -60,9 +64,11 @@ struct ast *parse_list(struct lexer_context *ctx) struct ast *parse_and_or(struct lexer_context *ctx) { struct ast *result = parse_pipeline(ctx); + if (result == NULL) + return NULL; struct token *token = PEEK_TOKEN(); - if (token->type == TOKEN_AND || token->type == TOKEN_OR) + while (token->type == TOKEN_AND || token->type == TOKEN_OR) { // Set left part @@ -117,7 +123,8 @@ struct ast *parse_command(struct lexer_context *ctx) } else { - return ast_create_void(); // TODO not sure what to do + puts("Syntax error: expected command"); + return NULL; } }