new token types

This commit is contained in:
Guillem George 2026-01-16 19:39:17 +01:00
parent bc7f8f3e8c
commit bf59c5717e
2 changed files with 77 additions and 24 deletions

View file

@ -14,7 +14,6 @@ static char *end_last_token;
static ssize_t remaining_chars;
/* @brief: saves state for the next call the the lexer.
*
*/
static void save_state(char *stream, ssize_t i)
{
@ -25,42 +24,84 @@ static void save_state(char *stream, ssize_t i)
/* @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;
}
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;
}
}
/* @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)
{