fix(lexer): memory leaks
This commit is contained in:
parent
6715d72cd7
commit
f4d7f58ef5
1 changed files with 75 additions and 64 deletions
|
|
@ -35,16 +35,16 @@ static void update_last_token(struct token *tok)
|
|||
|
||||
/* @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.
|
||||
* Also frees the last sent token, and sets it to 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, struct token *tok)
|
||||
static void save_state(char *stream, ssize_t i)
|
||||
{
|
||||
remaining_chars -= i;
|
||||
end_last_token = stream + i;
|
||||
|
||||
update_last_token(tok);
|
||||
update_last_token(current_token);
|
||||
update_current_token(NULL);
|
||||
}
|
||||
|
||||
|
|
@ -156,6 +156,10 @@ static void set_token_keyword(struct token *tok, char *begin, ssize_t size)
|
|||
tok->type = TOKEN_ELIF;
|
||||
}
|
||||
|
||||
// no keywords found.
|
||||
if (tok->type == TOKEN_NULL)
|
||||
return;
|
||||
|
||||
tok->data = calloc(size + 1, sizeof(char));
|
||||
if (tok->data == NULL)
|
||||
return;
|
||||
|
|
@ -254,9 +258,11 @@ struct token *peek_token(void)
|
|||
|
||||
struct token *pop_token(void)
|
||||
{
|
||||
if (last_token != NULL && last_token->type == TOKEN_EOF)
|
||||
if (current_token != NULL && current_token->type == TOKEN_EOF)
|
||||
{
|
||||
// we reached end of input, frees all the token still allocated.
|
||||
free_token(&last_token);
|
||||
free_token(¤t_token);
|
||||
return NULL;
|
||||
}
|
||||
char *stream = stream_init();
|
||||
|
|
@ -278,8 +284,13 @@ struct token *pop_token(void)
|
|||
i++;
|
||||
}
|
||||
|
||||
struct token *tok = new_token(stream, i);
|
||||
save_state(stream, i, tok);
|
||||
|
||||
return tok;
|
||||
// just in case peek() was not called before poping.
|
||||
// (this should never happen)
|
||||
if (current_token == NULL)
|
||||
{
|
||||
current_token = new_token(stream, i);
|
||||
}
|
||||
save_state(stream, i);
|
||||
|
||||
return last_token;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue