feat(lexer): operators done
This commit is contained in:
parent
3cdba0ad9c
commit
1e5593fc8e
3 changed files with 111 additions and 43 deletions
|
|
@ -11,18 +11,6 @@
|
||||||
#include "../utils/string_utils/string_utils.h"
|
#include "../utils/string_utils/string_utils.h"
|
||||||
#include "lexer_utils.h"
|
#include "lexer_utils.h"
|
||||||
|
|
||||||
/* @return: true if a special character from the grammar was found,
|
|
||||||
* false otherwise.
|
|
||||||
*/
|
|
||||||
static bool is_special_char(char c)
|
|
||||||
{
|
|
||||||
if (c == EOF)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
char special_chars[] = "\n'\"`;#|&\\(){}<>*";
|
|
||||||
return strchr(special_chars, c) != NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* @brief: sets the ctx->current_token to [tok].
|
/* @brief: sets the ctx->current_token to [tok].
|
||||||
* this function is called by token_peek().
|
* this function is called by token_peek().
|
||||||
*/
|
*/
|
||||||
|
|
@ -177,7 +165,7 @@ struct token *pop_token(struct lexer_context *ctx)
|
||||||
if (is_special_char(stream[i]))
|
if (is_special_char(stream[i]))
|
||||||
{
|
{
|
||||||
if (i == 0) // where we create spe_char token
|
if (i == 0) // where we create spe_char token
|
||||||
i++;
|
i += len_op_sepchar(stream, i);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (isblank(stream[i]))
|
if (isblank(stream[i]))
|
||||||
|
|
|
||||||
|
|
@ -24,24 +24,12 @@ static void set_token_spechar(struct token *tok, char *begin, ssize_t size)
|
||||||
case '\n':
|
case '\n':
|
||||||
tok->type = TOKEN_NEWLINE;
|
tok->type = TOKEN_NEWLINE;
|
||||||
break;
|
break;
|
||||||
case '\'':
|
|
||||||
tok->type = TOKEN_QUOTE;
|
|
||||||
break;
|
|
||||||
case '"':
|
|
||||||
tok->type = TOKEN_DOUBLE_QUOTE;
|
|
||||||
break;
|
|
||||||
case '`':
|
case '`':
|
||||||
tok->type = TOKEN_GRAVE;
|
tok->type = TOKEN_GRAVE;
|
||||||
break;
|
break;
|
||||||
case '#':
|
case '#':
|
||||||
tok->type = TOKEN_COMMENT;
|
tok->type = TOKEN_COMMENT;
|
||||||
break;
|
break;
|
||||||
case '|':
|
|
||||||
tok->type = TOKEN_PIPE;
|
|
||||||
break;
|
|
||||||
case '&':
|
|
||||||
tok->type = TOKEN_AMPERSAND;
|
|
||||||
break;
|
|
||||||
case '\\':
|
case '\\':
|
||||||
tok->type = TOKEN_BACKSLASH;
|
tok->type = TOKEN_BACKSLASH;
|
||||||
break;
|
break;
|
||||||
|
|
@ -57,12 +45,6 @@ static void set_token_spechar(struct token *tok, char *begin, ssize_t size)
|
||||||
case '}':
|
case '}':
|
||||||
tok->type = TOKEN_RIGHT_BRACKET;
|
tok->type = TOKEN_RIGHT_BRACKET;
|
||||||
break;
|
break;
|
||||||
case '<':
|
|
||||||
tok->type = TOKEN_LESS;
|
|
||||||
break;
|
|
||||||
case '>':
|
|
||||||
tok->type = TOKEN_GREATER;
|
|
||||||
break;
|
|
||||||
case '*':
|
case '*':
|
||||||
tok->type = TOKEN_STAR;
|
tok->type = TOKEN_STAR;
|
||||||
break;
|
break;
|
||||||
|
|
@ -117,6 +99,47 @@ static void set_token_keyword(struct token *tok, char *begin, ssize_t size)
|
||||||
strncpy(tok->data, begin, size);
|
strncpy(tok->data, begin, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* @brief: if an operator is found at [begin],
|
||||||
|
* [tok->token_type] is set accordingly
|
||||||
|
*/
|
||||||
|
static void set_token_operator(struct token *tok, char *begin, ssize_t size)
|
||||||
|
{
|
||||||
|
if (tok->type != TOKEN_NULL)
|
||||||
|
return;
|
||||||
|
if (strncmp(begin, ">", size) == 0)
|
||||||
|
{
|
||||||
|
tok->type = TOKEN_REDIR_RIGHT;
|
||||||
|
}
|
||||||
|
else if (strncmp(begin, "<", size) == 0)
|
||||||
|
{
|
||||||
|
tok->type = TOKEN_REDIR_LEFT;
|
||||||
|
}
|
||||||
|
else if (strncmp(begin, ">>", size) == 0)
|
||||||
|
{
|
||||||
|
tok->type = TOKEN_REDIR_DOUBLE_RIGHT;
|
||||||
|
}
|
||||||
|
else if (strncmp(begin, ">&", size) == 0)
|
||||||
|
{
|
||||||
|
tok->type = TOKEN_REDIR_RIGHT_AMP;
|
||||||
|
}
|
||||||
|
else if (strncmp(begin, ">|", size) == 0)
|
||||||
|
{
|
||||||
|
tok->type = TOKEN_REDIR_RIGHT_PIPE;
|
||||||
|
}
|
||||||
|
else if (strncmp(begin, "<&", size) == 0)
|
||||||
|
{
|
||||||
|
tok->type = TOKEN_REDIR_LEFT_AMP;
|
||||||
|
}
|
||||||
|
else if (strncmp(begin, "<>", size) == 0)
|
||||||
|
{
|
||||||
|
tok->type = TOKEN_REDIR_LEFT_RIGHT;
|
||||||
|
}
|
||||||
|
else if (strncmp(begin, "|", size) == 0)
|
||||||
|
{
|
||||||
|
tok->type = TOKEN_PIPE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* @brief: if token_type has not yet been set, then it is a TOKEN_WORD
|
/* @brief: if token_type has not yet been set, then it is a TOKEN_WORD
|
||||||
* Also allocates the data and fills it.
|
* Also allocates the data and fills it.
|
||||||
*/
|
*/
|
||||||
|
|
@ -147,6 +170,23 @@ static void set_token_ION(struct token *tok, char *begin, ssize_t size)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* @brief: check if [c] is a delimiter for end of line.
|
||||||
|
* @return: true if [c] == '\n' or EOF. false otherwise.
|
||||||
|
*/
|
||||||
|
static bool is_end_of_line(char c)
|
||||||
|
{
|
||||||
|
return c == EOF || c == '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
bool is_special_char(char c)
|
||||||
|
{
|
||||||
|
if (c == EOF)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
char special_chars[] = "\n'\"`;#|&\\(){}<>*";
|
||||||
|
return strchr(special_chars, c) != NULL;
|
||||||
|
}
|
||||||
|
|
||||||
struct token *new_token(char *begin, ssize_t size, bool only_digits)
|
struct token *new_token(char *begin, ssize_t size, bool only_digits)
|
||||||
{
|
{
|
||||||
struct token *tok = calloc(1, sizeof(struct token));
|
struct token *tok = calloc(1, sizeof(struct token));
|
||||||
|
|
@ -156,6 +196,7 @@ struct token *new_token(char *begin, ssize_t size, bool only_digits)
|
||||||
if (only_digits)
|
if (only_digits)
|
||||||
set_token_ION(tok, begin, size);
|
set_token_ION(tok, begin, size);
|
||||||
|
|
||||||
|
set_token_operator(tok, begin, size);
|
||||||
set_token_spechar(tok, begin, size);
|
set_token_spechar(tok, begin, size);
|
||||||
set_token_keyword(tok, begin, size);
|
set_token_keyword(tok, begin, size);
|
||||||
set_token_word(tok, begin, size);
|
set_token_word(tok, begin, size);
|
||||||
|
|
@ -204,14 +245,35 @@ void stream_init(struct lexer_context *ctx)
|
||||||
ctx->end_previous_token = trimed_stream;
|
ctx->end_previous_token = trimed_stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ssize_t len_op_sepchar(char *stream, ssize_t i)
|
||||||
|
{
|
||||||
|
if (!is_special_char(stream[i]))
|
||||||
|
return -1; // should never happen
|
||||||
|
|
||||||
|
if (stream[i] != '>' && stream[i] != '<')
|
||||||
|
return 1; // special character (cannot be operator)
|
||||||
|
|
||||||
|
// operator
|
||||||
|
|
||||||
|
if (stream[i] == '<')
|
||||||
|
{
|
||||||
|
if (stream[i + 1] == '&' || stream[i + 1] == '>')
|
||||||
|
return 2; // <&, <>
|
||||||
|
}
|
||||||
|
else if (stream[i + 1] == '>' || stream[i + 1] == '|'
|
||||||
|
|| stream[i + 1] == '&')
|
||||||
|
return 2; // >>, >&, >|
|
||||||
|
|
||||||
|
return 1; // >, <
|
||||||
|
}
|
||||||
|
|
||||||
void go_end_of_line(struct lexer_context *ctx)
|
void go_end_of_line(struct lexer_context *ctx)
|
||||||
{
|
{
|
||||||
if (ctx == NULL || ctx->end_previous_token == NULL)
|
if (ctx == NULL || ctx->end_previous_token == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
ssize_t i = 0;
|
ssize_t i = 0;
|
||||||
while (ctx->end_previous_token[i] != '\n'
|
while (!is_end_of_line(ctx->end_previous_token[i]))
|
||||||
&& ctx->end_previous_token[i] != EOF)
|
|
||||||
{
|
{
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -53,11 +53,16 @@ enum token_type
|
||||||
TOKEN_RIGHT_BRACKET,
|
TOKEN_RIGHT_BRACKET,
|
||||||
|
|
||||||
// redirections
|
// redirections
|
||||||
TOKEN_LESS,
|
TOKEN_REDIR_LEFT,
|
||||||
TOKEN_GREATER,
|
TOKEN_REDIR_RIGHT,
|
||||||
TOKEN_PIPE,
|
TOKEN_REDIR_LEFT_RIGHT,
|
||||||
TOKEN_AMPERSAND,
|
TOKEN_REDIR_DOUBLE_RIGHT,
|
||||||
|
TOKEN_REDIR_LEFT_AMP,
|
||||||
|
TOKEN_REDIR_RIGHT_AMP,
|
||||||
|
TOKEN_REDIR_RIGHT_PIPE,
|
||||||
|
|
||||||
TOKEN_IONUMBER,
|
TOKEN_IONUMBER,
|
||||||
|
TOKEN_PIPE,
|
||||||
|
|
||||||
// Keywords
|
// Keywords
|
||||||
TOKEN_IF,
|
TOKEN_IF,
|
||||||
|
|
@ -75,8 +80,12 @@ struct token
|
||||||
char *data;
|
char *data;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/* @return: true if a special character from the grammar was found,
|
||||||
* @brief: return a newly allocated token, with the corresponding type.
|
* false otherwise.
|
||||||
|
*/
|
||||||
|
bool is_special_char(char c);
|
||||||
|
|
||||||
|
/* @brief: return a newly allocated token, with the corresponding type.
|
||||||
* The data contains [size] char, starting from [begin].
|
* The data contains [size] char, starting from [begin].
|
||||||
*
|
*
|
||||||
* @return: NULL on error, a token otherwise.
|
* @return: NULL on error, a token otherwise.
|
||||||
|
|
@ -99,13 +108,22 @@ void free_token(struct token **tok);
|
||||||
void stream_init(struct lexer_context *ctx);
|
void stream_init(struct lexer_context *ctx);
|
||||||
|
|
||||||
/* @brief: finds the next '\n' or EOF character,
|
/* @brief: finds the next '\n' or EOF character,
|
||||||
* starting at [ctx->end_previous_token],
|
* starting at [ctx->end_previous_token],
|
||||||
* and updates the stream and remaining_chars accordingly.
|
* and updates the stream and remaining_chars accordingly.
|
||||||
|
*
|
||||||
|
* @note: Daft Punk. bang.
|
||||||
*/
|
*/
|
||||||
void go_end_of_line(struct lexer_context *ctx);
|
void go_end_of_line(struct lexer_context *ctx);
|
||||||
|
|
||||||
/*
|
/* @brief: this function is called when we found a special character
|
||||||
* @brief: drops the current stream and asks IOB for a new one
|
* in the stream. This can either be an operator (ig '>>' or '<&' etc),
|
||||||
|
* or a special char (ig '\' or '#' etc).
|
||||||
|
* @return: the length of the operator/special char found (can be 1, 2 or 3).
|
||||||
|
* -1 on error.
|
||||||
|
*/
|
||||||
|
ssize_t len_op_sepchar(char *stream, ssize_t i);
|
||||||
|
|
||||||
|
/* @brief: drops the current stream and asks IOB for a new one
|
||||||
*/
|
*/
|
||||||
void get_next_stream(struct lexer_context *ctx);
|
void get_next_stream(struct lexer_context *ctx);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue