fix(lexer): some memory leaks + removed useless [at_begining] variable

This commit is contained in:
matteo 2026-01-17 11:06:50 +01:00 committed by william.valenduc
parent f475f4d4ec
commit 2c1f210d37

View file

@ -12,7 +12,6 @@
static char *end_last_token; static char *end_last_token;
static ssize_t remaining_chars; static ssize_t remaining_chars;
static bool at_beginning = true;
static struct token *last_token; static struct token *last_token;
static struct token *current_token; static struct token *current_token;
@ -35,17 +34,19 @@ static void update_last_token(struct token *tok)
last_token = tok; last_token = tok;
} }
/* @brief: saves state for the next call to the the lexer. /* @brief: updates the current position in the stream.
* this function is called by token_pop(). * [stream] += [i]
* * Also saves the last token sent (so we can free it afterwards).
* Also sets the current token to NULL.
* This function is called by token_pop().
*/ */
static void save_state(char *stream, ssize_t i, struct token *tok) static void save_state(char *stream, ssize_t i, struct token *tok)
{ {
remaining_chars -= i; remaining_chars -= i;
end_last_token = stream + i; end_last_token = stream + i;
at_beginning = false;
update_last_token(tok); update_last_token(tok);
update_current_token(NULL);
} }
/* @return: true if a special character from the grammar was found, /* @return: true if a special character from the grammar was found,
@ -204,10 +205,9 @@ char *stream_init(void)
{ {
char *stream; char *stream;
if (at_beginning) if (last_token == NULL) // at the begining
{ {
remaining_chars = stream_read(&stream); remaining_chars = stream_read(&stream);
// at_beginning = true;
} }
else else
{ {
@ -223,8 +223,8 @@ char *stream_init(void)
struct token *peek_token(void) struct token *peek_token(void)
{ {
// EOF looping mode // we already created the upcoming token during the previous call to peek()
if (current_token != NULL && current_token->type == TOKEN_EOF) if (current_token != NULL)
{ {
return current_token; return current_token;
} }