From bbb69fddcada9111613c992c1af40d67d1c7b1dc Mon Sep 17 00:00:00 2001 From: Matteo Flebus Date: Tue, 20 Jan 2026 18:59:40 +0100 Subject: [PATCH] refactor(lexer): now use struct instead of static var --- src/lexer/lexer.c | 67 ++++++++++++++++++++++------------------------- src/lexer/lexer.h | 15 ++++++++--- 2 files changed, 43 insertions(+), 39 deletions(-) diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c index 1a630e3..bec5e24 100644 --- a/src/lexer/lexer.c +++ b/src/lexer/lexer.c @@ -10,42 +10,37 @@ #include "io_backend/io_backend.h" #include "utils/string_utils/string_utils.h" -static char *end_last_token; -static ssize_t remaining_chars; -static struct token *last_token; -static struct token *current_token; - -/* @brief: sets the current_token to [tok]. +/* @brief: sets the ctx->current_token to [tok]. * this function is called by token_peek(). */ -static void update_current_token(struct token *tok) +static void update_ctx->current_token(struct token *tok, struct lexer_context *ctx) { - current_token = tok; + ctx->current_token = tok; } /* @brief: frees the last token and sets it to [tok]. - * Also sets current_token to NULL. + * Also sets ctx->current_token to NULL. * this function is called by token_pop(). */ -static void update_last_token(struct token *tok) +static void update_ctx->previous_token(struct token *tok, struct lexer_context *ctx) { - free_token(&last_token); - last_token = tok; + free_token(&ctx->previous_token); + ctx->previous_token = tok; } /* @brief: updates the current position in the stream. * [stream] += [i] - * Also frees the last sent token, and sets it to current_token. + * Also frees the last sent token, and sets it to ctx->current_token. * Current token is then set to NULL. * This function is called by token_pop(). */ -static void save_state(char *stream, ssize_t i) +static void save_state(char *stream, ssize_t i, struct lexer_context *ctx) { - remaining_chars -= i; - end_last_token = stream + i; + ctx->remaining_chars -= i; + ctx->end_ctx->previous_token = stream + i; - update_last_token(current_token); - update_current_token(NULL); + update_ctx->previous_token(ctx->current_token); + update_ctx->current_token(NULL); } /* @return: true if a special character from the grammar was found, @@ -204,21 +199,21 @@ void free_token(struct token **tok) *tok = NULL; } -char *stream_init(void) +char *stream_init(struct lexer_context *ctx) { char *stream; - if (last_token == NULL) // at the begining + if (ctx->previous_token == NULL) // at the begining { - remaining_chars = stream_read(&stream); + ctx->remaining_chars = stream_read(&stream); } else { - stream = end_last_token; + stream = ctx->end_ctx->previous_token; } char *trimed_stream = trim_blank_left(stream); - remaining_chars -= trimed_stream - stream; + ctx->remaining_chars -= trimed_stream - stream; stream = trimed_stream; return stream; @@ -259,12 +254,12 @@ static bool update_lexing_mode(char *stream, ssize_t i, return *lexing_mode != mode_before_update; } -struct token *peek_token(void) +struct token *peek_token(struct lexer_context *ctx) { // we already created the upcoming token during the previous call to peek() - if (current_token != NULL) + if (ctx->current_token != NULL) { - return current_token; + return ctx->current_token; } char *stream = stream_init(); @@ -273,7 +268,7 @@ struct token *peek_token(void) // Usefull to know if we are inside a quote or double quote enum lexing_mode lexing_mode = LEXER_NORMAL; - while (i < remaining_chars) + while (i < ctx->remaining_chars) { // true if we didn't encounter a quote of any type at stream[i] // AND we are not inside quotes @@ -302,17 +297,17 @@ struct token *peek_token(void) } struct token *tok = new_token(stream, i); - update_current_token(tok); + update_ctx->current_token(tok); return tok; } -struct token *pop_token(void) +struct token *pop_token(struct lexer_context *ctx) { - if (current_token != NULL && current_token->type == TOKEN_EOF) + if (ctx->current_token != NULL && ctx->current_token->type == TOKEN_EOF) { // we reached end of input, frees all the token still allocated. - free_token(&last_token); - free_token(¤t_token); + free_token(&ctx->previous_token); + free_token(&ctx->current_token); return NULL; } char *stream = stream_init(); @@ -321,7 +316,7 @@ struct token *pop_token(void) // Usefull to know if we are inside a quote or double quote enum lexing_mode lexing_mode = LEXER_NORMAL; - while (i < remaining_chars) + while (i < ctx->remaining_chars) { // true if we didn't encounter a quote of any type at stream[i] // AND we are not inside quotes @@ -351,11 +346,11 @@ struct token *pop_token(void) // just in case peek() was not called before poping. // (this should never happen) - if (current_token == NULL) + if (ctx->current_token == NULL) { - current_token = new_token(stream, i); + ctx->current_token = new_token(stream, i); } save_state(stream, i); - return last_token; + return ctx->previous_token; } diff --git a/src/lexer/lexer.h b/src/lexer/lexer.h index 0da6f17..95c5504 100644 --- a/src/lexer/lexer.h +++ b/src/lexer/lexer.h @@ -3,6 +3,15 @@ #include +struct lexer_context +{ + char *end_last_token; + ssize_t remaining_chars; + + struct token *last_token; + struct token *current_token; +}; + enum lexing_mode { LEXER_NORMAL, @@ -55,7 +64,7 @@ struct token * @brief: returns the next (newly allocated) token without consuming it. * if end of input is reached, returns a token of type TOKEN_EOF. */ -struct token *peek_token(void); +struct token *peek_token(struct lexer_context *ctx); /* * @brief: returns the next (newly allocated) token and consumes it. @@ -66,7 +75,7 @@ struct token *peek_token(void); * and returns NULL. This means that after peeking a token EOF * in the parser, there must be EXACTLY ONE call to pop_token(). */ -struct token *pop_token(void); +struct token *pop_token(struct lexer_context *ctx); /* @note: maybe usefull for subshells. * @@ -96,6 +105,6 @@ void free_token(struct token **tok); * * @return: char* stream from which we tokenise. */ -char *stream_init(void); +char *stream_init(struct lexer_context *ctx); #endif /* ! LEXER_H */