merge parser into dev for redirections
This commit is contained in:
commit
e0032dd991
5 changed files with 93 additions and 55 deletions
|
|
@ -78,36 +78,43 @@ static bool update_lexing_mode(char *stream, ssize_t i,
|
||||||
return *lexing_mode != mode_before_update;
|
return *lexing_mode != mode_before_update;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* @brief: updates the flags only_digits and equal_count
|
/* @brief: updates the flags only_digits and has_equal.
|
||||||
* according to the character at stream[i].
|
* according to the character at stream[i].
|
||||||
*/
|
*/
|
||||||
static void update_flags(char *stream, ssize_t i, struct lexer_context *ctx)
|
static void update_flags(char *stream, ssize_t i, struct token_info *info)
|
||||||
{
|
{
|
||||||
if (stream[i] == '=')
|
if (stream[i] == '=' && !info->has_equal)
|
||||||
{
|
{
|
||||||
ctx->equal_count++;
|
if (i == 0)
|
||||||
|
{
|
||||||
|
perror("Syntax error: word start with a '='");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
info->has_equal = true;
|
||||||
}
|
}
|
||||||
else if (!isdigit(stream[i]) && ctx->only_digits)
|
else if (!isdigit(stream[i]) && info->only_digits)
|
||||||
{
|
{
|
||||||
ctx->only_digits = false;
|
info->only_digits = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct token *peek_token(struct lexer_context *ctx)
|
struct token *peek_token(struct lexer_context *ctx)
|
||||||
{
|
{
|
||||||
|
stream_init(ctx);
|
||||||
|
|
||||||
|
// Usefull to know if we are inside a quote or double quote
|
||||||
|
enum lexing_mode lexing_mode = LEXER_NORMAL;
|
||||||
|
struct token_info info = {true, 0};
|
||||||
|
char *stream = ctx->end_previous_token;
|
||||||
|
ssize_t i = 0;
|
||||||
|
|
||||||
// we already created the upcoming token during the previous call to peek()
|
// we already created the upcoming token during the previous call to peek()
|
||||||
if (ctx->current_token != NULL)
|
if (ctx->current_token != NULL)
|
||||||
{
|
{
|
||||||
return ctx->current_token;
|
return ctx->current_token;
|
||||||
}
|
}
|
||||||
|
|
||||||
stream_init(ctx);
|
|
||||||
char *stream = ctx->end_previous_token;
|
|
||||||
ssize_t i = 0;
|
|
||||||
|
|
||||||
// Usefull to know if we are inside a quote or double quote
|
|
||||||
enum lexing_mode lexing_mode = LEXER_NORMAL;
|
|
||||||
|
|
||||||
while (i < ctx->remaining_chars)
|
while (i < ctx->remaining_chars)
|
||||||
{
|
{
|
||||||
// true if we didn't encounter a quote of any type at stream[i]
|
// true if we didn't encounter a quote of any type at stream[i]
|
||||||
|
|
@ -115,7 +122,7 @@ struct token *peek_token(struct lexer_context *ctx)
|
||||||
if (!update_lexing_mode(stream, i, &lexing_mode)
|
if (!update_lexing_mode(stream, i, &lexing_mode)
|
||||||
&& lexing_mode == LEXER_NORMAL)
|
&& lexing_mode == LEXER_NORMAL)
|
||||||
{
|
{
|
||||||
update_flags(stream, i, ctx);
|
update_flags(stream, i, &info);
|
||||||
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
|
||||||
|
|
@ -137,7 +144,7 @@ struct token *peek_token(struct lexer_context *ctx)
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct token *tok = new_token(stream, i, ctx->only_digits, ctx->equal_count);
|
struct token *tok = new_token(stream, i, &info);
|
||||||
// if token is comment, we don't want it
|
// if token is comment, we don't want it
|
||||||
|
|
||||||
if (tok->type == TOKEN_COMMENT)
|
if (tok->type == TOKEN_COMMENT)
|
||||||
|
|
@ -154,6 +161,15 @@ struct token *peek_token(struct lexer_context *ctx)
|
||||||
|
|
||||||
struct token *pop_token(struct lexer_context *ctx)
|
struct token *pop_token(struct lexer_context *ctx)
|
||||||
{
|
{
|
||||||
|
stream_init(ctx);
|
||||||
|
|
||||||
|
// Usefull to know if we are inside a quote or double quote
|
||||||
|
enum lexing_mode lexing_mode = LEXER_NORMAL;
|
||||||
|
struct token_info info = {true, 0};
|
||||||
|
char *stream = ctx->end_previous_token;
|
||||||
|
ssize_t i = 0;
|
||||||
|
|
||||||
|
|
||||||
if (ctx->current_token != NULL && ctx->current_token->type == TOKEN_EOF)
|
if (ctx->current_token != NULL && ctx->current_token->type == TOKEN_EOF)
|
||||||
{
|
{
|
||||||
// we reached end of input, frees all the token still allocated.
|
// we reached end of input, frees all the token still allocated.
|
||||||
|
|
@ -161,12 +177,6 @@ struct token *pop_token(struct lexer_context *ctx)
|
||||||
free_token(&ctx->current_token);
|
free_token(&ctx->current_token);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
stream_init(ctx);
|
|
||||||
char *stream = ctx->end_previous_token;
|
|
||||||
ssize_t i = 0;
|
|
||||||
|
|
||||||
// Usefull to know if we are inside a quote or double quote
|
|
||||||
enum lexing_mode lexing_mode = LEXER_NORMAL;
|
|
||||||
|
|
||||||
while (i < ctx->remaining_chars)
|
while (i < ctx->remaining_chars)
|
||||||
{
|
{
|
||||||
|
|
@ -175,6 +185,7 @@ struct token *pop_token(struct lexer_context *ctx)
|
||||||
if (!update_lexing_mode(stream, i, &lexing_mode)
|
if (!update_lexing_mode(stream, i, &lexing_mode)
|
||||||
&& lexing_mode == LEXER_NORMAL)
|
&& lexing_mode == LEXER_NORMAL)
|
||||||
{
|
{
|
||||||
|
update_flags(stream, i, &info);
|
||||||
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
|
||||||
|
|
@ -200,7 +211,7 @@ struct token *pop_token(struct lexer_context *ctx)
|
||||||
// (this should never happen)
|
// (this should never happen)
|
||||||
if (ctx->current_token == NULL)
|
if (ctx->current_token == NULL)
|
||||||
{
|
{
|
||||||
ctx->current_token = new_token(stream, i, ctx->only_digits);
|
ctx->current_token = new_token(stream, i, &info);
|
||||||
}
|
}
|
||||||
save_state(stream, i, ctx);
|
save_state(stream, i, ctx);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -146,6 +146,21 @@ static void set_token_word(struct token *tok, char *begin, ssize_t size)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* @brief: Sets the token to an assignment_word
|
||||||
|
* Also allocates the data and fills it.
|
||||||
|
*/
|
||||||
|
static void set_token_assignment(struct token *tok, char *begin, ssize_t size)
|
||||||
|
{
|
||||||
|
if (tok->type == TOKEN_NULL && size != 0)
|
||||||
|
{
|
||||||
|
tok->type = TOKEN_ASSIGNMENT_WORD;
|
||||||
|
tok->data = calloc(size + 1, sizeof(char));
|
||||||
|
if (tok->data == NULL)
|
||||||
|
return;
|
||||||
|
strncpy(tok->data, begin, size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* @brief: Sets the token to an IO number
|
/* @brief: Sets the token to an IO number
|
||||||
* Also allocates the data and fills it.
|
* Also allocates the data and fills it.
|
||||||
*/
|
*/
|
||||||
|
|
@ -204,19 +219,23 @@ bool is_special_char(char *stream, ssize_t i)
|
||||||
return strchr(special_chars, c) != NULL;
|
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, struct token_info *info)
|
||||||
{
|
{
|
||||||
struct token *tok = calloc(1, sizeof(struct token));
|
struct token *tok = calloc(1, sizeof(struct token));
|
||||||
if (tok == NULL)
|
if (tok == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (only_digits)
|
if (info->only_digits)
|
||||||
set_token_ION(tok, begin, size);
|
set_token_ION(tok, begin, size);
|
||||||
|
else if (info->has_equal)
|
||||||
set_token_operator(tok, begin, size);
|
set_token_assignment(tok, begin, size);
|
||||||
set_token_spechar(tok, begin, size);
|
else
|
||||||
set_token_keyword(tok, begin, size);
|
{
|
||||||
set_token_word(tok, begin, size);
|
set_token_operator(tok, begin, size);
|
||||||
|
set_token_spechar(tok, begin, size);
|
||||||
|
set_token_keyword(tok, begin, size);
|
||||||
|
set_token_word(tok, begin, size);
|
||||||
|
}
|
||||||
|
|
||||||
return tok;
|
return tok;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,13 +10,6 @@ struct lexer_context
|
||||||
char *end_previous_token;
|
char *end_previous_token;
|
||||||
ssize_t remaining_chars;
|
ssize_t remaining_chars;
|
||||||
|
|
||||||
// usefull to detect IO numbers.
|
|
||||||
// tells us if we only lexed digits in current token.
|
|
||||||
bool only_digits;
|
|
||||||
|
|
||||||
// usefull to detect assignments, and syntax errors with '='.
|
|
||||||
int equal_count;
|
|
||||||
|
|
||||||
struct token *previous_token;
|
struct token *previous_token;
|
||||||
struct token *current_token;
|
struct token *current_token;
|
||||||
};
|
};
|
||||||
|
|
@ -57,10 +50,6 @@ enum token_type
|
||||||
TOKEN_PIPE,
|
TOKEN_PIPE,
|
||||||
TOKEN_NEGATION,
|
TOKEN_NEGATION,
|
||||||
|
|
||||||
// TODO merge into one and use the data field
|
|
||||||
// (Too difficult to handle in the parser because of firsts)
|
|
||||||
// TOKEN_REDIRECTION
|
|
||||||
//
|
|
||||||
// Redirections
|
// Redirections
|
||||||
TOKEN_REDIR_LEFT,
|
TOKEN_REDIR_LEFT,
|
||||||
TOKEN_REDIR_RIGHT,
|
TOKEN_REDIR_RIGHT,
|
||||||
|
|
@ -88,18 +77,29 @@ struct token
|
||||||
char *data;
|
char *data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// used to give info from lexing when creating a new token.
|
||||||
|
struct token_info
|
||||||
|
{
|
||||||
|
// usefull to detect IO numbers.
|
||||||
|
// tells us if we only lexed digits in current token.
|
||||||
|
bool only_digits;
|
||||||
|
|
||||||
|
// usefull to detect assignments, and syntax errors with '='.
|
||||||
|
bool has_equal;
|
||||||
|
};
|
||||||
|
|
||||||
/* @return: true if a special character from the grammar was found at stream[i],
|
/* @return: true if a special character from the grammar was found at stream[i],
|
||||||
* false otherwise.
|
* false otherwise.
|
||||||
*/
|
*/
|
||||||
bool is_special_char(char *stream, ssize_t i);
|
bool is_special_char(char *stream, ssize_t i);
|
||||||
|
|
||||||
/* @brief: return a newly allocated token, with the type corresponding
|
/* @brief: return a newly allocated token, with the type corresponding
|
||||||
* to the context given in arguments.
|
* to the info given in arguments.
|
||||||
* 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.
|
||||||
*/
|
*/
|
||||||
struct token *new_token(char *begin, ssize_t size, bool only_digits, int equal_count);
|
struct token *new_token(char *begin, ssize_t size, struct token_info *info);
|
||||||
|
|
||||||
/* @brief: frees the token given in argument
|
/* @brief: frees the token given in argument
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "grammar_basic.h"
|
#include "grammar_basic.h"
|
||||||
|
#include "grammar.h"
|
||||||
|
|
||||||
static enum ast_redir_type redir_tok_to_ast_type(enum token_type tok_type)
|
static enum ast_redir_type redir_tok_to_ast_type(enum token_type tok_type)
|
||||||
{
|
{
|
||||||
|
|
@ -66,15 +67,15 @@ struct ast *parse_redirection(struct lexer_context *ctx)
|
||||||
|
|
||||||
struct ast *parse_prefix(struct lexer_context *ctx)
|
struct ast *parse_prefix(struct lexer_context *ctx)
|
||||||
{
|
{
|
||||||
struct token *token = TOKEN_PEEK();
|
struct token *token = PEEK_TOKEN();
|
||||||
if (token->type == TOKEN_ASSIGNMENT_WORD)
|
if (token->type == TOKEN_ASSIGNMENT_WORD)
|
||||||
{
|
{
|
||||||
token = TOKEN_POP();
|
token = POP_TOKEN();
|
||||||
return ast_create_assignment_word(token->data);
|
return ast_create_assignment_word(token->data);
|
||||||
}
|
}
|
||||||
else if (is_first(*token, RULE_REDIRECTION))
|
else if (is_first(*token, RULE_REDIRECTION))
|
||||||
return parse_redirection(ctx);
|
return parse_redirection(ctx);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
perror("Syntax error: expected a prefix (redirection or assignment)");
|
perror("Syntax error: expected a prefix (redirection or assignment)");
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
|
||||||
|
|
@ -173,7 +173,7 @@ struct ast *parse_command(struct lexer_context *ctx)
|
||||||
* @return: NULL
|
* @return: NULL
|
||||||
*/
|
*/
|
||||||
static void *err_simple_command(struct list *command_elements,
|
static void *err_simple_command(struct list *command_elements,
|
||||||
struct list *redirections)
|
struct list *redirections)
|
||||||
{
|
{
|
||||||
list_deep_destroy(command_elements);
|
list_deep_destroy(command_elements);
|
||||||
list_deep_destroy(redirections);
|
list_deep_destroy(redirections);
|
||||||
|
|
@ -184,6 +184,7 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
|
||||||
{
|
{
|
||||||
struct list *command_elements = NULL;
|
struct list *command_elements = NULL;
|
||||||
struct list *redirections = NULL; // list of redirection ASTs
|
struct list *redirections = NULL; // list of redirection ASTs
|
||||||
|
struct list *assignments = NULL;
|
||||||
|
|
||||||
bool has_prefix = false;
|
bool has_prefix = false;
|
||||||
struct token *token = PEEK_TOKEN();
|
struct token *token = PEEK_TOKEN();
|
||||||
|
|
@ -192,12 +193,19 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
|
||||||
has_prefix = true;
|
has_prefix = true;
|
||||||
while (is_first(*token, RULE_PREFIX))
|
while (is_first(*token, RULE_PREFIX))
|
||||||
{
|
{
|
||||||
struct ast *redir = parse_prefix(ctx);
|
struct ast *prefix = parse_prefix(ctx);
|
||||||
if (redir == NULL)
|
if (prefix == NULL)
|
||||||
{
|
{
|
||||||
return err_simple_command(command_elements, redirections);
|
return err_simple_command(command_elements, redirections);
|
||||||
}
|
}
|
||||||
redirections = list_append(redirections, redir);
|
if (prefix->type == AST_ASSIGNEMENT)
|
||||||
|
{
|
||||||
|
assignments = list_append(assignments, prefix);
|
||||||
|
}
|
||||||
|
else if (prefix->type == AST_REDIR)
|
||||||
|
{
|
||||||
|
redirections = list_append(redirections, prefix);
|
||||||
|
}
|
||||||
token = PEEK_TOKEN();
|
token = PEEK_TOKEN();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -250,8 +258,7 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
perror("Internal error: unexpected return value from "
|
perror("Internal error: unexpected return value from "
|
||||||
"parse_element "
|
"parse_element in parse_simple_command");
|
||||||
"in parse_simple_command");
|
|
||||||
return err_simple_command(command_elements, redirections);
|
return err_simple_command(command_elements, redirections);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -260,9 +267,8 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Result
|
struct ast *result =
|
||||||
// TODO handle assignments
|
ast_create_command(command_elements, redirections, assignments);
|
||||||
struct ast *result = ast_create_command(command_elements, redirections);
|
|
||||||
if (result == NULL)
|
if (result == NULL)
|
||||||
{
|
{
|
||||||
return err_simple_command(command_elements, redirections);
|
return err_simple_command(command_elements, redirections);
|
||||||
|
|
@ -297,7 +303,8 @@ struct ast *parse_shell_command(struct lexer_context *ctx)
|
||||||
/* @brief: frees all the arguments. (helper func)
|
/* @brief: frees all the arguments. (helper func)
|
||||||
* @return: NULL.
|
* @return: NULL.
|
||||||
*/
|
*/
|
||||||
static void *err_if_rule(struct ast **cond, struct ast **then_clause, struct ast **else_clause)
|
static void *err_if_rule(struct ast **cond, struct ast **then_clause,
|
||||||
|
struct ast **else_clause)
|
||||||
{
|
{
|
||||||
ast_free(cond);
|
ast_free(cond);
|
||||||
ast_free(then_clause);
|
ast_free(then_clause);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue