fix(parser + lexer): interaction -- WIP
This commit is contained in:
parent
04529f858c
commit
10ce140e37
5 changed files with 99 additions and 48 deletions
|
|
@ -13,16 +13,40 @@
|
|||
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;
|
||||
|
||||
/* @brief: saves state for the next call the the lexer.
|
||||
|
||||
/* @brief: sets the current_token to [tok].
|
||||
* this function is called by token_peek().
|
||||
*/
|
||||
static void update_current_token(struct token* tok)
|
||||
{
|
||||
current_token = tok;
|
||||
}
|
||||
|
||||
/* @brief: frees the last token and sets it to [tok].
|
||||
* Also sets current_token to NULL.
|
||||
* this function is called by token_pop().
|
||||
*
|
||||
*/
|
||||
static void save_state(char *stream, ssize_t i)
|
||||
static void update_last_token(struct token* tok)
|
||||
{
|
||||
free_token(&last_token);
|
||||
last_token = tok;
|
||||
}
|
||||
|
||||
/* @brief: saves state for the next call to the the lexer.
|
||||
* 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;
|
||||
return;
|
||||
|
||||
update_last_token(tok);
|
||||
}
|
||||
|
||||
/* @return: true if a special character from the grammar was found,
|
||||
|
|
@ -67,7 +91,7 @@ static void set_token_spechar(struct token *tok, char *begin, ssize_t size)
|
|||
*/
|
||||
static void set_token_keyword(struct token *tok, char *begin, ssize_t size)
|
||||
{
|
||||
if (tok->type != TOKEN_NULL)
|
||||
if (tok->type != TOKEN_NULL || size == 0)
|
||||
return;
|
||||
if (strncmp(begin, "if", size) == 0)
|
||||
{
|
||||
|
|
@ -101,7 +125,7 @@ static void set_token_keyword(struct token *tok, char *begin, ssize_t size)
|
|||
*/
|
||||
static void set_token_word(struct token *tok, char *begin, ssize_t size)
|
||||
{
|
||||
if (tok->type == TOKEN_NULL)
|
||||
if (tok->type == TOKEN_NULL && size != 0)
|
||||
{
|
||||
tok->type = TOKEN_WORD;
|
||||
tok->data = calloc(size + 1, sizeof(char));
|
||||
|
|
@ -124,13 +148,14 @@ struct token *new_token(char *begin, ssize_t size)
|
|||
return tok;
|
||||
}
|
||||
|
||||
void free_token(struct token *tok)
|
||||
void free_token(struct token **tok)
|
||||
{
|
||||
if (tok == NULL)
|
||||
if (tok == NULL || *tok == NULL)
|
||||
return;
|
||||
if (tok->data != NULL)
|
||||
free(tok->data);
|
||||
free(tok);
|
||||
if ((*tok)->data != NULL)
|
||||
free((*tok)->data);
|
||||
free(*tok);
|
||||
*tok = NULL;
|
||||
}
|
||||
|
||||
char *stream_init(void)
|
||||
|
|
@ -156,6 +181,12 @@ char *stream_init(void)
|
|||
|
||||
struct token *peek_token(void)
|
||||
{
|
||||
// EOF looping mode
|
||||
if (current_token != NULL && current_token->type == TOKEN_EOF)
|
||||
{
|
||||
return current_token;
|
||||
}
|
||||
|
||||
char *stream = stream_init();
|
||||
|
||||
ssize_t i = 0;
|
||||
|
|
@ -175,11 +206,18 @@ struct token *peek_token(void)
|
|||
i++;
|
||||
}
|
||||
|
||||
return new_token(stream, i);
|
||||
struct token *tok = new_token(stream, i);
|
||||
update_current_token(tok);
|
||||
return tok;
|
||||
}
|
||||
|
||||
struct token *pop_token(void)
|
||||
{
|
||||
if (last_token != NULL && last_token->type == TOKEN_EOF)
|
||||
{
|
||||
free_token(&last_token);
|
||||
return NULL;
|
||||
}
|
||||
char *stream = stream_init();
|
||||
|
||||
ssize_t i = 0;
|
||||
|
|
@ -199,7 +237,8 @@ struct token *pop_token(void)
|
|||
i++;
|
||||
}
|
||||
|
||||
save_state(stream, i);
|
||||
struct token *tok = new_token(stream, i);
|
||||
save_state(stream, i, tok);
|
||||
|
||||
return new_token(stream, i);
|
||||
return tok;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue