Merge branch 'lexer' into dev
This commit is contained in:
commit
e80058a765
1 changed files with 82 additions and 71 deletions
|
|
@ -12,7 +12,6 @@
|
|||
|
||||
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;
|
||||
|
||||
|
|
@ -34,17 +33,19 @@ static void update_last_token(struct token *tok)
|
|||
last_token = tok;
|
||||
}
|
||||
|
||||
/* @brief: saves state for the next call to the the lexer.
|
||||
* this function is called by token_pop().
|
||||
*
|
||||
/* @brief: updates the current position in the stream.
|
||||
* [stream] += [i]
|
||||
* 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;
|
||||
at_beginning = false;
|
||||
|
||||
update_last_token(tok);
|
||||
update_last_token(current_token);
|
||||
update_current_token(NULL);
|
||||
}
|
||||
|
||||
/* @return: true if a special character from the grammar was found,
|
||||
|
|
@ -68,62 +69,62 @@ static void set_token_spechar(struct token *tok, char *begin, ssize_t size)
|
|||
return;
|
||||
switch (begin[0])
|
||||
{
|
||||
case EOF:
|
||||
tok->type = TOKEN_EOF;
|
||||
break;
|
||||
case ';':
|
||||
tok->type = TOKEN_SEMICOLON;
|
||||
break;
|
||||
case '\n':
|
||||
tok->type = TOKEN_NEWLINE;
|
||||
break;
|
||||
case '\'':
|
||||
tok->type = TOKEN_QUOTE;
|
||||
break;
|
||||
case '"':
|
||||
tok->type = TOKEN_DOUBLE_QUOTE;
|
||||
break;
|
||||
case '`':
|
||||
tok->type = TOKEN_GRAVE;
|
||||
break;
|
||||
case '#':
|
||||
tok->type = TOKEN_COMMENT;
|
||||
break;
|
||||
case '|':
|
||||
tok->type = TOKEN_PIPE;
|
||||
break;
|
||||
case '&':
|
||||
tok->type = TOKEN_AMPERSAND;
|
||||
break;
|
||||
case '\\':
|
||||
tok->type = TOKEN_BACKSLASH;
|
||||
break;
|
||||
case '$':
|
||||
tok->type = TOKEN_DOLLAR;
|
||||
break;
|
||||
case '(':
|
||||
tok->type = TOKEN_LEFT_PAREN;
|
||||
break;
|
||||
case ')':
|
||||
tok->type = TOKEN_RIGHT_PAREN;
|
||||
break;
|
||||
case '{':
|
||||
tok->type = TOKEN_LEFT_BRACKET;
|
||||
break;
|
||||
case '}':
|
||||
tok->type = TOKEN_RIGHT_BRACKET;
|
||||
break;
|
||||
case '<':
|
||||
tok->type = TOKEN_LESS;
|
||||
break;
|
||||
case '>':
|
||||
tok->type = TOKEN_GREATER;
|
||||
break;
|
||||
case '*':
|
||||
tok->type = TOKEN_STAR;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
case EOF:
|
||||
tok->type = TOKEN_EOF;
|
||||
break;
|
||||
case ';':
|
||||
tok->type = TOKEN_SEMICOLON;
|
||||
break;
|
||||
case '\n':
|
||||
tok->type = TOKEN_NEWLINE;
|
||||
break;
|
||||
case '\'':
|
||||
tok->type = TOKEN_QUOTE;
|
||||
break;
|
||||
case '"':
|
||||
tok->type = TOKEN_DOUBLE_QUOTE;
|
||||
break;
|
||||
case '`':
|
||||
tok->type = TOKEN_GRAVE;
|
||||
break;
|
||||
case '#':
|
||||
tok->type = TOKEN_COMMENT;
|
||||
break;
|
||||
case '|':
|
||||
tok->type = TOKEN_PIPE;
|
||||
break;
|
||||
case '&':
|
||||
tok->type = TOKEN_AMPERSAND;
|
||||
break;
|
||||
case '\\':
|
||||
tok->type = TOKEN_BACKSLASH;
|
||||
break;
|
||||
case '$':
|
||||
tok->type = TOKEN_DOLLAR;
|
||||
break;
|
||||
case '(':
|
||||
tok->type = TOKEN_LEFT_PAREN;
|
||||
break;
|
||||
case ')':
|
||||
tok->type = TOKEN_RIGHT_PAREN;
|
||||
break;
|
||||
case '{':
|
||||
tok->type = TOKEN_LEFT_BRACKET;
|
||||
break;
|
||||
case '}':
|
||||
tok->type = TOKEN_RIGHT_BRACKET;
|
||||
break;
|
||||
case '<':
|
||||
tok->type = TOKEN_LESS;
|
||||
break;
|
||||
case '>':
|
||||
tok->type = TOKEN_GREATER;
|
||||
break;
|
||||
case '*':
|
||||
tok->type = TOKEN_STAR;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -155,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;
|
||||
|
|
@ -203,10 +208,9 @@ char *stream_init(void)
|
|||
{
|
||||
char *stream;
|
||||
|
||||
if (at_beginning)
|
||||
if (last_token == NULL) // at the begining
|
||||
{
|
||||
remaining_chars = stream_read(&stream);
|
||||
// at_beginning = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -222,8 +226,8 @@ char *stream_init(void)
|
|||
|
||||
struct token *peek_token(void)
|
||||
{
|
||||
// EOF looping mode
|
||||
if (current_token != NULL && current_token->type == TOKEN_EOF)
|
||||
// we already created the upcoming token during the previous call to peek()
|
||||
if (current_token != NULL)
|
||||
{
|
||||
return current_token;
|
||||
}
|
||||
|
|
@ -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);
|
||||
// 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 tok;
|
||||
return last_token;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue