Merge branch 'lexer' into dev

This commit is contained in:
Matteo Flebus 2026-01-20 19:59:06 +01:00
commit bf929d413f
2 changed files with 29 additions and 9 deletions

View file

@ -10,10 +10,12 @@
#include "io_backend/io_backend.h"
#include "utils/string_utils/string_utils.h"
// ######## STATIC FUNCTIONS ##############
/* @brief: sets the ctx->current_token to [tok].
* this function is called by token_peek().
*/
static void update_ctx->current_token(struct token *tok, struct lexer_context *ctx)
static void update_current_token(struct token *tok, struct lexer_context *ctx)
{
ctx->current_token = tok;
}
@ -22,7 +24,7 @@ static void update_ctx->current_token(struct token *tok, struct lexer_context *c
* Also sets ctx->current_token to NULL.
* this function is called by token_pop().
*/
static void update_ctx->previous_token(struct token *tok, struct lexer_context *ctx)
static void update_previous_token(struct token *tok, struct lexer_context *ctx)
{
free_token(&ctx->previous_token);
ctx->previous_token = tok;
@ -37,10 +39,10 @@ static void update_ctx->previous_token(struct token *tok, struct lexer_context *
static void save_state(char *stream, ssize_t i, struct lexer_context *ctx)
{
ctx->remaining_chars -= i;
ctx->end_ctx->previous_token = stream + i;
ctx->end_previous_token = stream + i;
update_ctx->previous_token(ctx->current_token);
update_ctx->current_token(NULL);
update_previous_token(ctx->current_token);
update_current_token(NULL);
}
/* @return: true if a special character from the grammar was found,
@ -176,6 +178,20 @@ static void set_token_word(struct token *tok, char *begin, ssize_t size)
}
}
void destroy_lexer_context(struct lexer_context **ctx)
{
if (ctx == NULL || *ctx == NULL)
return;
if (ctx->end_previous_token != NULL)
free(ctx->end_previous_token);
if (ctx->previous_token != NULL)
free(ctx->previous_token);
if (ctx->current_token != NULL)
free(ctx->current_token);
free(*ctx);
*ctx = NULL;
}
struct token *new_token(char *begin, ssize_t size)
{
struct token *tok = calloc(1, sizeof(struct token));
@ -209,7 +225,7 @@ char *stream_init(struct lexer_context *ctx)
}
else
{
stream = ctx->end_ctx->previous_token;
stream = ctx->end_previous_token;
}
char *trimed_stream = trim_blank_left(stream);
@ -297,7 +313,7 @@ struct token *peek_token(struct lexer_context *ctx)
}
struct token *tok = new_token(stream, i);
update_ctx->current_token(tok);
update_current_token(tok);
return tok;
}

View file

@ -5,13 +5,17 @@
struct lexer_context
{
char *end_last_token;
char *end_previous_token;
ssize_t remaining_chars;
struct token *last_token;
struct token *previous_token;
struct token *current_token;
};
/* @brief: frees all fields of ctx and sets ctx to NULL.
*/
void destroy_lexer_context(struct lexer_context **ctx);
enum lexing_mode
{
LEXER_NORMAL,