refactor(lexer): now use struct instead of static var
This commit is contained in:
parent
603def1597
commit
f1955f0532
2 changed files with 43 additions and 39 deletions
|
|
@ -10,42 +10,37 @@
|
|||
#include "io_backend/io_backend.h"
|
||||
#include "utils/string_utils/string_utils.h"
|
||||
|
||||
static char *end_last_token;
|
||||
static ssize_t remaining_chars;
|
||||
static struct token *last_token;
|
||||
static struct token *current_token;
|
||||
|
||||
/* @brief: sets the current_token to [tok].
|
||||
/* @brief: sets the ctx->current_token to [tok].
|
||||
* this function is called by token_peek().
|
||||
*/
|
||||
static void update_current_token(struct token *tok)
|
||||
static void update_ctx->current_token(struct token *tok, struct lexer_context *ctx)
|
||||
{
|
||||
current_token = tok;
|
||||
ctx->current_token = tok;
|
||||
}
|
||||
|
||||
/* @brief: frees the last token and sets it to [tok].
|
||||
* Also sets current_token to NULL.
|
||||
* Also sets ctx->current_token to NULL.
|
||||
* this function is called by token_pop().
|
||||
*/
|
||||
static void update_last_token(struct token *tok)
|
||||
static void update_ctx->previous_token(struct token *tok, struct lexer_context *ctx)
|
||||
{
|
||||
free_token(&last_token);
|
||||
last_token = tok;
|
||||
free_token(&ctx->previous_token);
|
||||
ctx->previous_token = tok;
|
||||
}
|
||||
|
||||
/* @brief: updates the current position in the stream.
|
||||
* [stream] += [i]
|
||||
* Also frees the last sent token, and sets it to current_token.
|
||||
* Also frees the last sent token, and sets it to ctx->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)
|
||||
static void save_state(char *stream, ssize_t i, struct lexer_context *ctx)
|
||||
{
|
||||
remaining_chars -= i;
|
||||
end_last_token = stream + i;
|
||||
ctx->remaining_chars -= i;
|
||||
ctx->end_ctx->previous_token = stream + i;
|
||||
|
||||
update_last_token(current_token);
|
||||
update_current_token(NULL);
|
||||
update_ctx->previous_token(ctx->current_token);
|
||||
update_ctx->current_token(NULL);
|
||||
}
|
||||
|
||||
/* @return: true if a special character from the grammar was found,
|
||||
|
|
@ -204,21 +199,21 @@ void free_token(struct token **tok)
|
|||
*tok = NULL;
|
||||
}
|
||||
|
||||
char *stream_init(void)
|
||||
char *stream_init(struct lexer_context *ctx)
|
||||
{
|
||||
char *stream;
|
||||
|
||||
if (last_token == NULL) // at the begining
|
||||
if (ctx->previous_token == NULL) // at the begining
|
||||
{
|
||||
remaining_chars = stream_read(&stream);
|
||||
ctx->remaining_chars = stream_read(&stream);
|
||||
}
|
||||
else
|
||||
{
|
||||
stream = end_last_token;
|
||||
stream = ctx->end_ctx->previous_token;
|
||||
}
|
||||
|
||||
char *trimed_stream = trim_blank_left(stream);
|
||||
remaining_chars -= trimed_stream - stream;
|
||||
ctx->remaining_chars -= trimed_stream - stream;
|
||||
stream = trimed_stream;
|
||||
|
||||
return stream;
|
||||
|
|
@ -259,12 +254,12 @@ static bool update_lexing_mode(char *stream, ssize_t i,
|
|||
return *lexing_mode != mode_before_update;
|
||||
}
|
||||
|
||||
struct token *peek_token(void)
|
||||
struct token *peek_token(struct lexer_context *ctx)
|
||||
{
|
||||
// we already created the upcoming token during the previous call to peek()
|
||||
if (current_token != NULL)
|
||||
if (ctx->current_token != NULL)
|
||||
{
|
||||
return current_token;
|
||||
return ctx->current_token;
|
||||
}
|
||||
|
||||
char *stream = stream_init();
|
||||
|
|
@ -273,7 +268,7 @@ struct token *peek_token(void)
|
|||
// Usefull to know if we are inside a quote or double quote
|
||||
enum lexing_mode lexing_mode = LEXER_NORMAL;
|
||||
|
||||
while (i < remaining_chars)
|
||||
while (i < ctx->remaining_chars)
|
||||
{
|
||||
// true if we didn't encounter a quote of any type at stream[i]
|
||||
// AND we are not inside quotes
|
||||
|
|
@ -302,17 +297,17 @@ struct token *peek_token(void)
|
|||
}
|
||||
|
||||
struct token *tok = new_token(stream, i);
|
||||
update_current_token(tok);
|
||||
update_ctx->current_token(tok);
|
||||
return tok;
|
||||
}
|
||||
|
||||
struct token *pop_token(void)
|
||||
struct token *pop_token(struct lexer_context *ctx)
|
||||
{
|
||||
if (current_token != NULL && current_token->type == TOKEN_EOF)
|
||||
if (ctx->current_token != NULL && ctx->current_token->type == TOKEN_EOF)
|
||||
{
|
||||
// we reached end of input, frees all the token still allocated.
|
||||
free_token(&last_token);
|
||||
free_token(¤t_token);
|
||||
free_token(&ctx->previous_token);
|
||||
free_token(&ctx->current_token);
|
||||
return NULL;
|
||||
}
|
||||
char *stream = stream_init();
|
||||
|
|
@ -321,7 +316,7 @@ struct token *pop_token(void)
|
|||
// Usefull to know if we are inside a quote or double quote
|
||||
enum lexing_mode lexing_mode = LEXER_NORMAL;
|
||||
|
||||
while (i < remaining_chars)
|
||||
while (i < ctx->remaining_chars)
|
||||
{
|
||||
// true if we didn't encounter a quote of any type at stream[i]
|
||||
// AND we are not inside quotes
|
||||
|
|
@ -351,11 +346,11 @@ struct token *pop_token(void)
|
|||
|
||||
// just in case peek() was not called before poping.
|
||||
// (this should never happen)
|
||||
if (current_token == NULL)
|
||||
if (ctx->current_token == NULL)
|
||||
{
|
||||
current_token = new_token(stream, i);
|
||||
ctx->current_token = new_token(stream, i);
|
||||
}
|
||||
save_state(stream, i);
|
||||
|
||||
return last_token;
|
||||
return ctx->previous_token;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue