feat(parser): adapted to new lexer without static var

This commit is contained in:
Matteo Flebus 2026-01-20 19:54:29 +01:00
parent f1955f0532
commit c3ab2585e1
4 changed files with 30 additions and 30 deletions

View file

@ -17,22 +17,23 @@
struct ast *get_ast() struct ast *get_ast()
{ {
struct lexer_context *ctx = calloc(1, sizeof(struct lexer_context));
struct token *token = PEEK_TOKEN(); struct token *token = PEEK_TOKEN();
struct ast *res; struct ast *res;
if (token->type == TOKEN_EOF) if (token->type == TOKEN_EOF)
{ {
token = pop_token(); token = pop_token(ctx);
return ast_create_end(); return ast_create_end();
} }
else if (token->type == TOKEN_NEWLINE) else if (token->type == TOKEN_NEWLINE)
{ {
token = pop_token(); token = pop_token(ctx);
return ast_create_void(); return ast_create_void();
} }
else // TOKEN WORD else // TOKEN WORD
{ {
res = parse_list(); res = parse_list(ctx);
} }
/* /*
@ -43,6 +44,7 @@ struct ast *get_ast()
return NULL; return NULL;
} }
*/ */
destroy_lexer_context(&ctx);
return res; return res;
} }

View file

@ -10,7 +10,7 @@
* *
* @warning NOT IMPLEMENTED * @warning NOT IMPLEMENTED
*/ */
struct ast *get_ast(); struct ast *get_ast(void);
/* @brief Builds the AST representation of the given command string. /* @brief Builds the AST representation of the given command string.
* *

View file

@ -54,12 +54,12 @@ static bool is_end_of_list(struct token *token)
// === Functions // === Functions
struct ast *parse_input(void) struct ast *parse_input(struct lexer_context *ctx)
{ {
return parse_list(); return parse_list();
} }
struct ast *parse_list(void) struct ast *parse_list(struct lexer_context *ctx)
{ {
struct list *result_list = NULL; struct list *result_list = NULL;
struct ast *current_node = NULL; struct ast *current_node = NULL;
@ -95,17 +95,17 @@ struct ast *parse_list(void)
return ast_create_list(result_list); return ast_create_list(result_list);
} }
struct ast *parse_and_or(void) struct ast *parse_and_or(struct lexer_context *ctx)
{ {
return parse_pipeline(); return parse_pipeline();
} }
struct ast *parse_pipeline(void) struct ast *parse_pipeline(struct lexer_context *ctx)
{ {
return parse_command(); return parse_command();
} }
struct ast *parse_command(void) struct ast *parse_command(struct lexer_context *ctx)
{ {
struct token *token = PEEK_TOKEN(); 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 list *command_elements = NULL;
struct token *token = PEEK_TOKEN(); struct token *token = PEEK_TOKEN();
@ -166,12 +166,12 @@ struct ast *parse_simple_command(void)
return result; return result;
} }
struct ast *parse_shell_command(void) struct ast *parse_shell_command(struct lexer_context *ctx)
{ {
return parse_if_rule(); return parse_if_rule();
} }
struct ast *parse_if_rule(void) struct ast *parse_if_rule(struct lexer_context *ctx)
{ {
// If keyword // If keyword
struct token *token = POP_TOKEN(); struct token *token = POP_TOKEN();
@ -236,7 +236,7 @@ struct ast *parse_if_rule(void)
return result; 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 list *result_list = NULL; // ast* list
struct ast *current_cmd = NULL; struct ast *current_cmd = NULL;
@ -291,7 +291,7 @@ struct ast *parse_compound_list(void)
return result; return result;
} }
struct ast *parse_else_clause(void) struct ast *parse_else_clause(struct lexer_context *ctx)
{ {
struct token *token = PEEK_TOKEN(); struct token *token = PEEK_TOKEN();

View file

@ -4,7 +4,7 @@
// === Macros // === Macros
#define PEEK_TOKEN() \ #define PEEK_TOKEN() \
peek_token(); \ peek_token(ctx); \
if (token == NULL) \ if (token == NULL) \
{ \ { \
puts("Internal error: cannot get the following token"); \ puts("Internal error: cannot get the following token"); \
@ -12,7 +12,7 @@
} }
#define POP_TOKEN() \ #define POP_TOKEN() \
pop_token(); \ pop_token(ctx); \
if (token == NULL) \ if (token == NULL) \
{ \ { \
puts("Internal error: cannot get the following token"); \ puts("Internal error: cannot get the following token"); \
@ -27,26 +27,26 @@
* | EOF * | 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 /* @brief: parses a list of [and_or] rules separated by semicolons and that
* ends by a newline * ends by a newline
* *
* @code list = and_or { ';' and_or } [ ';' ] ; * @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 /* @brief Only parses a pipeline rule for the moment
* *
* @code and_or = pipeline ; * @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 /* @brief Only parses a command rule for the moment
* *
* @code pipeline = command ; * @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 /* @brief Parses a simple command rule or a shell command rule depending on
* the first token. * the first token.
@ -58,36 +58,34 @@ struct ast* parse_pipeline(void);
* | shell_command * | 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) /* @brief Parses a simple list of words (command and arguments)
* ending by a separator * ending by a separator
* *
* @code simple_command = WORD { element } ; * @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 /* @brief Only parses if rules for the moment
* *
* @code shell_command = if_rule ; * @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) /* @brief Parses a if rule (condition, then-clause, elif-clause, else-clause)
* *
* @code if_rule = 'if' compound_list 'then' compound_list [else_clause] 'fi' ; * @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 /* @brief parses commands inside if/else clauses and returns the corresponding
* AST list * 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 /* @brief
* *
@ -95,6 +93,6 @@ struct ast* parse_compound_list(void);
* | 'elif' compound_list 'then' compound_list [else_clause] * | '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 */ #endif /* ! PARSING_UTILS_H */