From c3ab2585e1007e57499d13a09fdc62e166d7d070 Mon Sep 17 00:00:00 2001 From: Matteo Flebus Date: Tue, 20 Jan 2026 19:54:29 +0100 Subject: [PATCH] feat(parser): adapted to new lexer without static var --- src/parser/parser.c | 8 +++++--- src/parser/parser.h | 2 +- src/parser/parsing_utils.c | 20 ++++++++++---------- src/parser/parsing_utils.h | 30 ++++++++++++++---------------- 4 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/parser/parser.c b/src/parser/parser.c index cf7b052..d1fd70a 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -17,22 +17,23 @@ struct ast *get_ast() { + struct lexer_context *ctx = calloc(1, sizeof(struct lexer_context)); struct token *token = PEEK_TOKEN(); struct ast *res; if (token->type == TOKEN_EOF) { - token = pop_token(); + token = pop_token(ctx); return ast_create_end(); } else if (token->type == TOKEN_NEWLINE) { - token = pop_token(); + token = pop_token(ctx); return ast_create_void(); } else // TOKEN WORD { - res = parse_list(); + res = parse_list(ctx); } /* @@ -43,6 +44,7 @@ struct ast *get_ast() return NULL; } */ + destroy_lexer_context(&ctx); return res; } diff --git a/src/parser/parser.h b/src/parser/parser.h index d10cae8..5089212 100644 --- a/src/parser/parser.h +++ b/src/parser/parser.h @@ -10,7 +10,7 @@ * * @warning NOT IMPLEMENTED */ -struct ast *get_ast(); +struct ast *get_ast(void); /* @brief Builds the AST representation of the given command string. * diff --git a/src/parser/parsing_utils.c b/src/parser/parsing_utils.c index ea1b113..12b0f9b 100644 --- a/src/parser/parsing_utils.c +++ b/src/parser/parsing_utils.c @@ -54,12 +54,12 @@ static bool is_end_of_list(struct token *token) // === Functions -struct ast *parse_input(void) +struct ast *parse_input(struct lexer_context *ctx) { return parse_list(); } -struct ast *parse_list(void) +struct ast *parse_list(struct lexer_context *ctx) { struct list *result_list = NULL; struct ast *current_node = NULL; @@ -95,17 +95,17 @@ struct ast *parse_list(void) return ast_create_list(result_list); } -struct ast *parse_and_or(void) +struct ast *parse_and_or(struct lexer_context *ctx) { return parse_pipeline(); } -struct ast *parse_pipeline(void) +struct ast *parse_pipeline(struct lexer_context *ctx) { return parse_command(); } -struct ast *parse_command(void) +struct ast *parse_command(struct lexer_context *ctx) { struct token *token = PEEK_TOKEN(); @@ -123,7 +123,7 @@ struct ast *parse_command(void) } } -struct ast *parse_simple_command(void) +struct ast *parse_simple_command(struct lexer_context *ctx) { struct list *command_elements = NULL; struct token *token = PEEK_TOKEN(); @@ -166,12 +166,12 @@ struct ast *parse_simple_command(void) return result; } -struct ast *parse_shell_command(void) +struct ast *parse_shell_command(struct lexer_context *ctx) { return parse_if_rule(); } -struct ast *parse_if_rule(void) +struct ast *parse_if_rule(struct lexer_context *ctx) { // If keyword struct token *token = POP_TOKEN(); @@ -236,7 +236,7 @@ struct ast *parse_if_rule(void) return result; } -struct ast *parse_compound_list(void) +struct ast *parse_compound_list(struct lexer_context *ctx) { struct list *result_list = NULL; // ast* list struct ast *current_cmd = NULL; @@ -291,7 +291,7 @@ struct ast *parse_compound_list(void) return result; } -struct ast *parse_else_clause(void) +struct ast *parse_else_clause(struct lexer_context *ctx) { struct token *token = PEEK_TOKEN(); diff --git a/src/parser/parsing_utils.h b/src/parser/parsing_utils.h index 5464981..5b32490 100644 --- a/src/parser/parsing_utils.h +++ b/src/parser/parsing_utils.h @@ -4,7 +4,7 @@ // === Macros #define PEEK_TOKEN() \ - peek_token(); \ + peek_token(ctx); \ if (token == NULL) \ { \ puts("Internal error: cannot get the following token"); \ @@ -12,7 +12,7 @@ } #define POP_TOKEN() \ - pop_token(); \ + pop_token(ctx); \ if (token == NULL) \ { \ puts("Internal error: cannot get the following token"); \ @@ -27,26 +27,26 @@ * | EOF * ; */ -struct ast* parse_input(void); +struct ast *parse_input(struct lexer_context *ctx); /* @brief: parses a list of [and_or] rules separated by semicolons and that * ends by a newline * * @code list = and_or { ';' and_or } [ ';' ] ; */ -struct ast *parse_list(void); +struct ast *parse_list(struct lexer_context *ctx); /* @brief Only parses a pipeline rule for the moment * * @code and_or = pipeline ; */ -struct ast *parse_and_or(void); +struct ast *parse_and_or(struct lexer_context *ctx); /* @brief Only parses a command rule for the moment * * @code pipeline = command ; */ -struct ast* parse_pipeline(void); +struct ast *parse_pipeline(struct lexer_context *ctx); /* @brief Parses a simple command rule or a shell command rule depending on * the first token. @@ -58,36 +58,34 @@ struct ast* parse_pipeline(void); * | shell_command * ; */ -struct ast* parse_command(void); +struct ast *parse_command(struct lexer_context *ctx); /* @brief Parses a simple list of words (command and arguments) * ending by a separator * * @code simple_command = WORD { element } ; */ -struct ast *parse_simple_command(void); - +struct ast *parse_simple_command(struct lexer_context *ctx); /* @brief Only parses if rules for the moment * * @code shell_command = if_rule ; */ -struct ast *parse_shell_command(void); - +struct ast *parse_shell_command(struct lexer_context *ctx); /* @brief Parses a if rule (condition, then-clause, elif-clause, else-clause) * * @code if_rule = 'if' compound_list 'then' compound_list [else_clause] 'fi' ; */ -struct ast *parse_if_rule(void); - +struct ast *parse_if_rule(struct lexer_context *ctx); /* @brief parses commands inside if/else clauses and returns the corresponding * AST list * - * @code compound_list = {'\n'} and_or { ( ';' | '\n' ) {'\n'} and_or } [';'] {'\n'} ; + * @code compound_list = {'\n'} and_or { ( ';' | '\n' ) {'\n'} and_or } [';'] + * {'\n'} ; */ -struct ast* parse_compound_list(void); +struct ast *parse_compound_list(struct lexer_context *ctx); /* @brief * @@ -95,6 +93,6 @@ struct ast* parse_compound_list(void); * | 'elif' compound_list 'then' compound_list [else_clause] * ; */ -struct ast *parse_else_clause(void); +struct ast *parse_else_clause(struct lexer_context *ctx); #endif /* ! PARSING_UTILS_H */