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 "io_backend/io_backend.h"
|
||||||
#include "utils/string_utils/string_utils.h"
|
#include "utils/string_utils/string_utils.h"
|
||||||
|
|
||||||
static char *end_last_token;
|
/* @brief: sets the ctx->current_token to [tok].
|
||||||
static ssize_t remaining_chars;
|
|
||||||
static struct token *last_token;
|
|
||||||
static struct token *current_token;
|
|
||||||
|
|
||||||
/* @brief: sets the current_token to [tok].
|
|
||||||
* this function is called by token_peek().
|
* 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].
|
/* @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().
|
* 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);
|
free_token(&ctx->previous_token);
|
||||||
last_token = tok;
|
ctx->previous_token = tok;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* @brief: updates the current position in the stream.
|
/* @brief: updates the current position in the stream.
|
||||||
* [stream] += [i]
|
* [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.
|
* Current token is then set to NULL.
|
||||||
* This function is called by token_pop().
|
* 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;
|
ctx->remaining_chars -= i;
|
||||||
end_last_token = stream + i;
|
ctx->end_ctx->previous_token = stream + i;
|
||||||
|
|
||||||
update_last_token(current_token);
|
update_ctx->previous_token(ctx->current_token);
|
||||||
update_current_token(NULL);
|
update_ctx->current_token(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* @return: true if a special character from the grammar was found,
|
/* @return: true if a special character from the grammar was found,
|
||||||
|
|
@ -204,21 +199,21 @@ void free_token(struct token **tok)
|
||||||
*tok = NULL;
|
*tok = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *stream_init(void)
|
char *stream_init(struct lexer_context *ctx)
|
||||||
{
|
{
|
||||||
char *stream;
|
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
|
else
|
||||||
{
|
{
|
||||||
stream = end_last_token;
|
stream = ctx->end_ctx->previous_token;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *trimed_stream = trim_blank_left(stream);
|
char *trimed_stream = trim_blank_left(stream);
|
||||||
remaining_chars -= trimed_stream - stream;
|
ctx->remaining_chars -= trimed_stream - stream;
|
||||||
stream = trimed_stream;
|
stream = trimed_stream;
|
||||||
|
|
||||||
return stream;
|
return stream;
|
||||||
|
|
@ -259,12 +254,12 @@ static bool update_lexing_mode(char *stream, ssize_t i,
|
||||||
return *lexing_mode != mode_before_update;
|
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()
|
// 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();
|
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
|
// Usefull to know if we are inside a quote or double quote
|
||||||
enum lexing_mode lexing_mode = LEXER_NORMAL;
|
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]
|
// true if we didn't encounter a quote of any type at stream[i]
|
||||||
// AND we are not inside quotes
|
// AND we are not inside quotes
|
||||||
|
|
@ -302,17 +297,17 @@ struct token *peek_token(void)
|
||||||
}
|
}
|
||||||
|
|
||||||
struct token *tok = new_token(stream, i);
|
struct token *tok = new_token(stream, i);
|
||||||
update_current_token(tok);
|
update_ctx->current_token(tok);
|
||||||
return 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.
|
// we reached end of input, frees all the token still allocated.
|
||||||
free_token(&last_token);
|
free_token(&ctx->previous_token);
|
||||||
free_token(¤t_token);
|
free_token(&ctx->current_token);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
char *stream = stream_init();
|
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
|
// Usefull to know if we are inside a quote or double quote
|
||||||
enum lexing_mode lexing_mode = LEXER_NORMAL;
|
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]
|
// true if we didn't encounter a quote of any type at stream[i]
|
||||||
// AND we are not inside quotes
|
// AND we are not inside quotes
|
||||||
|
|
@ -351,11 +346,11 @@ struct token *pop_token(void)
|
||||||
|
|
||||||
// just in case peek() was not called before poping.
|
// just in case peek() was not called before poping.
|
||||||
// (this should never happen)
|
// (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);
|
save_state(stream, i);
|
||||||
|
|
||||||
return last_token;
|
return ctx->previous_token;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,15 @@
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
struct lexer_context
|
||||||
|
{
|
||||||
|
char *end_last_token;
|
||||||
|
ssize_t remaining_chars;
|
||||||
|
|
||||||
|
struct token *last_token;
|
||||||
|
struct token *current_token;
|
||||||
|
};
|
||||||
|
|
||||||
enum lexing_mode
|
enum lexing_mode
|
||||||
{
|
{
|
||||||
LEXER_NORMAL,
|
LEXER_NORMAL,
|
||||||
|
|
@ -55,7 +64,7 @@ struct token
|
||||||
* @brief: returns the next (newly allocated) token without consuming it.
|
* @brief: returns the next (newly allocated) token without consuming it.
|
||||||
* if end of input is reached, returns a token of type TOKEN_EOF.
|
* if end of input is reached, returns a token of type TOKEN_EOF.
|
||||||
*/
|
*/
|
||||||
struct token *peek_token(void);
|
struct token *peek_token(struct lexer_context *ctx);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* @brief: returns the next (newly allocated) token and consumes it.
|
* @brief: returns the next (newly allocated) token and consumes it.
|
||||||
|
|
@ -66,7 +75,7 @@ struct token *peek_token(void);
|
||||||
* and returns NULL. This means that after peeking a token EOF
|
* and returns NULL. This means that after peeking a token EOF
|
||||||
* in the parser, there must be EXACTLY ONE call to pop_token().
|
* in the parser, there must be EXACTLY ONE call to pop_token().
|
||||||
*/
|
*/
|
||||||
struct token *pop_token(void);
|
struct token *pop_token(struct lexer_context *ctx);
|
||||||
|
|
||||||
/* @note: maybe usefull for subshells.
|
/* @note: maybe usefull for subshells.
|
||||||
*
|
*
|
||||||
|
|
@ -96,6 +105,6 @@ void free_token(struct token **tok);
|
||||||
*
|
*
|
||||||
* @return: char* stream from which we tokenise.
|
* @return: char* stream from which we tokenise.
|
||||||
*/
|
*/
|
||||||
char *stream_init(void);
|
char *stream_init(struct lexer_context *ctx);
|
||||||
|
|
||||||
#endif /* ! LEXER_H */
|
#endif /* ! LEXER_H */
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue