rebased lexer on dev

This commit is contained in:
matteo 2026-01-17 10:43:23 +01:00
parent ccb9438d69
commit d269e82df6
2 changed files with 82 additions and 26 deletions

View file

@ -16,11 +16,10 @@ static bool at_beginning = true;
static struct token *last_token;
static struct token *current_token;
/* @brief: sets the current_token to [tok].
* this function is called by token_peek().
*/
static void update_current_token(struct token* tok)
static void update_current_token(struct token *tok)
{
current_token = tok;
}
@ -30,7 +29,7 @@ static void update_current_token(struct token* tok)
* this function is called by token_pop().
*
*/
static void update_last_token(struct token* tok)
static void update_last_token(struct token *tok)
{
free_token(&last_token);
last_token = tok;
@ -51,43 +50,86 @@ static void save_state(char *stream, ssize_t i, struct token *tok)
/* @return: true if a special character from the grammar was found,
* false otherwise.
*
*/
static bool is_special_char(char c)
{
return c == '\'' || c == '\n' || c == ';' || c == EOF;
if (c == EOF)
return true;
char special_chars[] = "\n'\"`;#|&\\$(){}<>*";
return strchr(special_chars, c) != NULL;
}
/* @brief: if a special character is found at [begin],
* [tok->token_type] is set accordingly
*
*/
static void set_token_spechar(struct token *tok, char *begin, ssize_t size)
{
if (size != 1)
return;
if (begin[0] == EOF)
switch (begin[0])
{
case EOF:
tok->type = TOKEN_EOF;
remaining_chars = 0;
}
else if (begin[0] == ';')
{
tok->type = TOKEN_NEWLINE;
}
else if (begin[0] == '\'')
{
tok->type = TOKEN_QUOTE;
}
else if (begin[0] == ';')
{
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;
}
}
/* @brief: if a keyword is found at [begin],
* [tok->token_type] is set accordingly
*
*/
static void set_token_keyword(struct token *tok, char *begin, ssize_t size)
{