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 ssize_t remaining_chars;
static bool at_beginning = true;
static struct token *last_token;
static struct token *current_token;
@ -35,17 +34,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,
@ -204,10 +205,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
{
@ -223,8 +223,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;
}