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

This commit is contained in:
matteo 2026-01-17 11:06:50 +01:00
parent f226013955
commit 6715d72cd7

View file

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