From 51158ec4f9829e79b0bf58cde214ba87988735ce Mon Sep 17 00:00:00 2001 From: matteo Date: Thu, 29 Jan 2026 18:53:37 +0100 Subject: [PATCH] fix(lexer): merge conflicts --- src/lexer/lexer.c | 55 +++++++++++++++++++++-------------- src/lexer/lexer_utils.c | 33 ++++++++++++++++----- src/lexer/lexer_utils.h | 26 ++++++++--------- src/parser/grammar_advanced.c | 5 ++-- src/parser/grammar_basic.c | 2 +- 5 files changed, 76 insertions(+), 45 deletions(-) diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c index 21f362b..db4e066 100644 --- a/src/lexer/lexer.c +++ b/src/lexer/lexer.c @@ -78,36 +78,43 @@ static bool update_lexing_mode(char *stream, ssize_t i, return *lexing_mode != mode_before_update; } -/* @brief: updates the flags only_digits and equal_count +/* @brief: updates the flags only_digits and has_equal. * according to the character at stream[i]. */ -static void update_flags(char *stream, ssize_t i, struct lexer_context *ctx) +static void update_flags(char *stream, ssize_t i, struct token_info *info) { - if (stream[i] == '=') + if (stream[i] == '=' && !info->has_equal) { - ctx->equal_count++; + if (i == 0) + { + perror("Syntax error: word start with a '='"); + return; + } + else + info->has_equal = true; } - else if (!isdigit(stream[i]) && ctx->only_digits) + else if (!isdigit(stream[i]) && info->only_digits) { - ctx->only_digits = false; + info->only_digits = false; } } struct token *peek_token(struct lexer_context *ctx) { + stream_init(ctx); + + // Usefull to know if we are inside a quote or double quote + enum lexing_mode lexing_mode = LEXER_NORMAL; + struct token_info info = {true, 0}; + char *stream = ctx->end_previous_token; + ssize_t i = 0; + // we already created the upcoming token during the previous call to peek() if (ctx->current_token != NULL) { return ctx->current_token; } - stream_init(ctx); - char *stream = ctx->end_previous_token; - ssize_t i = 0; - - // Usefull to know if we are inside a quote or double quote - enum lexing_mode lexing_mode = LEXER_NORMAL; - while (i < ctx->remaining_chars) { // true if we didn't encounter a quote of any type at stream[i] @@ -115,7 +122,7 @@ struct token *peek_token(struct lexer_context *ctx) if (!update_lexing_mode(stream, i, &lexing_mode) && lexing_mode == LEXER_NORMAL) { - update_flags(stream, i, ctx); + update_flags(stream, i, &info); if (is_special_char(stream, i)) { if (i == 0) // where we create spe_char token @@ -137,7 +144,7 @@ struct token *peek_token(struct lexer_context *ctx) i++; } - struct token *tok = new_token(stream, i, ctx->only_digits, ctx->equal_count); + struct token *tok = new_token(stream, i, &info); // if token is comment, we don't want it if (tok->type == TOKEN_COMMENT) @@ -154,6 +161,15 @@ struct token *peek_token(struct lexer_context *ctx) struct token *pop_token(struct lexer_context *ctx) { + stream_init(ctx); + + // Usefull to know if we are inside a quote or double quote + enum lexing_mode lexing_mode = LEXER_NORMAL; + struct token_info info = {true, 0}; + char *stream = ctx->end_previous_token; + ssize_t i = 0; + + if (ctx->current_token != NULL && ctx->current_token->type == TOKEN_EOF) { // we reached end of input, frees all the token still allocated. @@ -161,12 +177,6 @@ struct token *pop_token(struct lexer_context *ctx) free_token(&ctx->current_token); return NULL; } - stream_init(ctx); - char *stream = ctx->end_previous_token; - ssize_t i = 0; - - // Usefull to know if we are inside a quote or double quote - enum lexing_mode lexing_mode = LEXER_NORMAL; while (i < ctx->remaining_chars) { @@ -175,6 +185,7 @@ struct token *pop_token(struct lexer_context *ctx) if (!update_lexing_mode(stream, i, &lexing_mode) && lexing_mode == LEXER_NORMAL) { + update_flags(stream, i, &info); if (is_special_char(stream, i)) { if (i == 0) // where we create spe_char token @@ -200,7 +211,7 @@ struct token *pop_token(struct lexer_context *ctx) // (this should never happen) if (ctx->current_token == NULL) { - ctx->current_token = new_token(stream, i, ctx->only_digits); + ctx->current_token = new_token(stream, i, &info); } save_state(stream, i, ctx); diff --git a/src/lexer/lexer_utils.c b/src/lexer/lexer_utils.c index 54859e8..f860649 100644 --- a/src/lexer/lexer_utils.c +++ b/src/lexer/lexer_utils.c @@ -154,6 +154,21 @@ static void set_token_word(struct token *tok, char *begin, ssize_t size) } } +/* @brief: Sets the token to an assignment_word + * Also allocates the data and fills it. + */ +static void set_token_assignment(struct token *tok, char *begin, ssize_t size) +{ + if (tok->type == TOKEN_NULL && size != 0) + { + tok->type = TOKEN_ASSIGNMENT_WORD; + tok->data = calloc(size + 1, sizeof(char)); + if (tok->data == NULL) + return; + strncpy(tok->data, begin, size); + } +} + /* @brief: Sets the token to an IO number * Also allocates the data and fills it. */ @@ -212,19 +227,23 @@ bool is_special_char(char *stream, ssize_t i) return strchr(special_chars, c) != NULL; } -struct token *new_token(char *begin, ssize_t size, bool only_digits) +struct token *new_token(char *begin, ssize_t size, struct token_info *info) { struct token *tok = calloc(1, sizeof(struct token)); if (tok == NULL) return NULL; - if (only_digits) + if (info->only_digits) set_token_ION(tok, begin, size); - - set_token_operator(tok, begin, size); - set_token_spechar(tok, begin, size); - set_token_keyword(tok, begin, size); - set_token_word(tok, begin, size); + else if (info->has_equal) + set_token_assignment(tok, begin, size); + else + { + set_token_operator(tok, begin, size); + set_token_spechar(tok, begin, size); + set_token_keyword(tok, begin, size); + set_token_word(tok, begin, size); + } return tok; } diff --git a/src/lexer/lexer_utils.h b/src/lexer/lexer_utils.h index 2708662..8fb5219 100644 --- a/src/lexer/lexer_utils.h +++ b/src/lexer/lexer_utils.h @@ -10,13 +10,6 @@ struct lexer_context char *end_previous_token; ssize_t remaining_chars; - // usefull to detect IO numbers. - // tells us if we only lexed digits in current token. - bool only_digits; - - // usefull to detect assignments, and syntax errors with '='. - int equal_count; - struct token *previous_token; struct token *current_token; }; @@ -57,10 +50,6 @@ enum token_type TOKEN_PIPE, TOKEN_NEGATION, - // TODO merge into one and use the data field - // (Too difficult to handle in the parser because of firsts) - // TOKEN_REDIRECTION - // // Redirections TOKEN_REDIR_LEFT, TOKEN_REDIR_RIGHT, @@ -88,18 +77,29 @@ struct token char *data; }; +// used to give info from lexing when creating a new token. +struct token_info +{ + // usefull to detect IO numbers. + // tells us if we only lexed digits in current token. + bool only_digits; + + // usefull to detect assignments, and syntax errors with '='. + bool has_equal; +}; + /* @return: true if a special character from the grammar was found at stream[i], * false otherwise. */ bool is_special_char(char *stream, ssize_t i); /* @brief: return a newly allocated token, with the type corresponding - * to the context given in arguments. + * to the info given in arguments. * The data contains [size] char, starting from [begin]. * * @return: NULL on error, a token otherwise. */ -struct token *new_token(char *begin, ssize_t size, bool only_digits, int equal_count); +struct token *new_token(char *begin, ssize_t size, struct token_info *info); /* @brief: frees the token given in argument */ diff --git a/src/parser/grammar_advanced.c b/src/parser/grammar_advanced.c index eb5869d..713fd99 100644 --- a/src/parser/grammar_advanced.c +++ b/src/parser/grammar_advanced.c @@ -7,6 +7,7 @@ #include #include "grammar_basic.h" +#include "grammar.h" static enum ast_redir_type redir_tok_to_ast_type(enum token_type tok_type) { @@ -66,10 +67,10 @@ struct ast *parse_redirection(struct lexer_context *ctx) struct ast *parse_prefix(struct lexer_context *ctx) { - struct token *token = TOKEN_PEEK(); + struct token *token = PEEK_TOKEN(); if (token->type == TOKEN_ASSIGNMENT_WORD) { - token = TOKEN_POP(); + token = POP_TOKEN(); return ast_create_assignment_word(token->data); } else if (is_first(*token, RULE_REDIRECTION)) diff --git a/src/parser/grammar_basic.c b/src/parser/grammar_basic.c index ad78673..b979b6e 100644 --- a/src/parser/grammar_basic.c +++ b/src/parser/grammar_basic.c @@ -200,7 +200,7 @@ struct ast *parse_simple_command(struct lexer_context *ctx) } if (prefix->type == AST_ASSIGNEMENT) { - assignments = list_append(assignments, prefix) + assignments = list_append(assignments, prefix); } else if (prefix->type == AST_REDIR) {