feat(lexer): rework using a struct token and functions associated - unstable
This commit is contained in:
parent
a9df97740f
commit
204bb515af
2 changed files with 60 additions and 34 deletions
|
|
@ -28,31 +28,43 @@ static void save_state(char *stream, ssize_t i)
|
|||
*/
|
||||
static bool is_special_char(char c)
|
||||
{
|
||||
return c == '\'' || c == '\n' || c == ';';
|
||||
return c == '\'' || c == '\n' || c == ';' || c == 'EOF';
|
||||
}
|
||||
|
||||
/* @return: true if a keyword from the grammar was found, false otherwise.
|
||||
*
|
||||
*/
|
||||
static bool is_keyword(char *stream, ssize_t i)
|
||||
static void new_token_keyword(struct token *tok, char *begin, ssize_t size)
|
||||
{
|
||||
if (i == 2)
|
||||
if (strncmp(begin, "if", size) == 0)
|
||||
{
|
||||
return strcmp(stream, "if") == 0 || strcmp(stream, "fi") == 0;
|
||||
tok->type = TOKEN_IF;
|
||||
}
|
||||
if (i == 4)
|
||||
if (strncmp(begin, "fi", size) == 0)
|
||||
{
|
||||
return strcmp(stream, "then") || strcmp(stream, "else")
|
||||
|| strcmp(stream, "elif");
|
||||
tok->type = TOKEN_FI;
|
||||
}
|
||||
if (strncmp(begin, "then", size) == 0)
|
||||
{
|
||||
tok->type = TOKEN_THEN;
|
||||
}
|
||||
if (strncmp(begin, "else", size) == 0)
|
||||
{
|
||||
tok->type = TOKEN_ELSE;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
char *new_token(char *begin, ssize_t size)
|
||||
struct token *new_token(char *begin, ssize_t size)
|
||||
{
|
||||
char *res = calloc(size + 1, sizeof(char));
|
||||
struct token *res = calloc(1, sizeof(struct token));
|
||||
if (res == NULL)
|
||||
return NULL;
|
||||
|
||||
// checks which type of token
|
||||
|
||||
// is special char
|
||||
|
||||
// is keyword
|
||||
|
||||
// otherwise -> WORD
|
||||
char *token_data = calloc(size + 1, sizeof(char));
|
||||
strncpy(res, begin, size);
|
||||
return res;
|
||||
}
|
||||
|
|
@ -77,7 +89,7 @@ char *stream_init(void)
|
|||
return stream;
|
||||
}
|
||||
|
||||
char *peek_token(void)
|
||||
struct token *peek_token(void)
|
||||
{
|
||||
char *stream = stream_init();
|
||||
|
||||
|
|
@ -95,18 +107,13 @@ char *peek_token(void)
|
|||
{
|
||||
break;
|
||||
}
|
||||
else if (is_keyword(stream, i))
|
||||
{
|
||||
i++;
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
return new_token(stream, i);
|
||||
}
|
||||
|
||||
char *pop_token(void)
|
||||
struct token *pop_token(void)
|
||||
{
|
||||
char *stream = stream_init();
|
||||
|
||||
|
|
@ -124,11 +131,6 @@ char *pop_token(void)
|
|||
{
|
||||
break;
|
||||
}
|
||||
else if (is_keyword(stream, i))
|
||||
{
|
||||
i++;
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,19 +3,38 @@
|
|||
|
||||
#include <sys/types.h>
|
||||
|
||||
enum token_type
|
||||
{
|
||||
TOKEN_EOF,
|
||||
TOKEN_WORD,
|
||||
TOKEN_NEWLINE,
|
||||
TOKEN_QUOTE,
|
||||
TOKEN_SEMICOLON,
|
||||
TOKEN_IF,
|
||||
TOKEN_THEN,
|
||||
TOKEN_ELSE,
|
||||
TOKEN_FI
|
||||
};
|
||||
|
||||
struct token
|
||||
{
|
||||
enum token_type type;
|
||||
char* data;
|
||||
};
|
||||
|
||||
/*
|
||||
* @brief: returns the next (newly allocated) token without consuming it.
|
||||
* if end of input is reached, returns EOF.
|
||||
* if end of input is reached, returns a token of type TOKEN_EOF.
|
||||
*
|
||||
*/
|
||||
char *peek_token(void);
|
||||
struct token *peek_token(void);
|
||||
|
||||
/*
|
||||
* @brief: returns the next (newly allocated) token and consumes it.
|
||||
* if end of input is reached, returns EOF.
|
||||
* if end of input is reached, returns a token of type TOKEN_EOF.
|
||||
*
|
||||
*/
|
||||
char *pop_token(void);
|
||||
struct token *pop_token(void);
|
||||
|
||||
/*
|
||||
* @warning: NOT IMPLEMENTED.
|
||||
|
|
@ -23,16 +42,21 @@ char *pop_token(void);
|
|||
* @note: maybe usefull for subshells.
|
||||
*/
|
||||
|
||||
char *get_token_str(void);
|
||||
struct token *get_token_str(void);
|
||||
|
||||
/*
|
||||
* @brief: return a newly allocated token.
|
||||
* This token contains [size] chars, starting from [begin].
|
||||
* @brief: return a newly allocated token, with the corresponding type.
|
||||
* The data contains [size] char, starting from [begin].
|
||||
*
|
||||
* @return: NULL on error, null-terminated char* otherwise.
|
||||
* @return: NULL on error, a token otherwise.
|
||||
*
|
||||
*/
|
||||
char *new_token(char *begin, ssize_t size);
|
||||
struct token *new_token(char *begin, ssize_t size);
|
||||
|
||||
/* @brief: frees the token given in argument
|
||||
*
|
||||
*/
|
||||
void free_token(struct token* tok);
|
||||
|
||||
/*
|
||||
* @brief: checks if the stream used for the last token creation is empty.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue