From bf59c5717eb8a860600423d364fed7363c303ee0 Mon Sep 17 00:00:00 2001 From: Guillem George Date: Fri, 16 Jan 2026 19:39:17 +0100 Subject: [PATCH] new token types --- src/lexer/lexer.c | 75 ++++++++++++++++++++++++++++++++++++----------- src/lexer/lexer.h | 26 +++++++++++----- 2 files changed, 77 insertions(+), 24 deletions(-) diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c index 748ad63..5249633 100644 --- a/src/lexer/lexer.c +++ b/src/lexer/lexer.c @@ -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) { diff --git a/src/lexer/lexer.h b/src/lexer/lexer.h index 332355f..3c06d35 100644 --- a/src/lexer/lexer.h +++ b/src/lexer/lexer.h @@ -5,12 +5,29 @@ enum token_type { + // Special characters TOKEN_NULL = 0, TOKEN_EOF, TOKEN_WORD, TOKEN_NEWLINE, TOKEN_QUOTE, + TOKEN_DOUBLE_QUOTE, + TOKEN_GRAVE, TOKEN_SEMICOLON, + TOKEN_COMMENT, + TOKEN_PIPE, + TOKEN_AMPERSAND, + TOKEN_BACKSLASH, + TOKEN_DOLLAR, + TOKEN_LEFT_PAREN, + TOKEN_RIGHT_PAREN, + TOKEN_LEFT_BRACKET, + TOKEN_RIGHT_BRACKET, + TOKEN_LESS, + TOKEN_GREATER, + TOKEN_STAR, + + // Keywords TOKEN_IF, TOKEN_THEN, TOKEN_ELSE, @@ -27,21 +44,18 @@ struct token /* * @brief: returns the next (newly allocated) token without consuming it. * if end of input is reached, returns a token of type TOKEN_EOF. - * */ struct token *peek_token(void); /* * @brief: returns the next (newly allocated) token and consumes it. * if end of input is reached, returns a token of type TOKEN_EOF. - * */ struct token *pop_token(void); -/* - * @warning: NOT IMPLEMENTED. +/* @note: maybe usefull for subshells. * - * @note: maybe usefull for subshells. + * @warning: NOT IMPLEMENTED. */ struct token *get_token_str(void); @@ -51,12 +65,10 @@ struct token *get_token_str(void); * The data contains [size] char, starting from [begin]. * * @return: NULL on error, a token otherwise. - * */ struct token *new_token(char *begin, ssize_t size); /* @brief: frees the token given in argument - * */ void free_token(struct token *tok);