feat(lexer + parser): export handling
This commit is contained in:
parent
bada9c6a29
commit
115377edfe
3 changed files with 80 additions and 50 deletions
|
|
@ -72,6 +72,8 @@ static void set_token_keyword(struct token *tok, char *begin, ssize_t size)
|
||||||
tok->type = TOKEN_ELSE;
|
tok->type = TOKEN_ELSE;
|
||||||
else if (strncmp(begin, "elif", size) == 0 && size == 4)
|
else if (strncmp(begin, "elif", size) == 0 && size == 4)
|
||||||
tok->type = TOKEN_ELIF;
|
tok->type = TOKEN_ELIF;
|
||||||
|
else if (strncmp(begin, "export", size) == 0 && size == 6)
|
||||||
|
tok->type = TOKEN_ELIF;
|
||||||
|
|
||||||
// no keywords found.
|
// no keywords found.
|
||||||
if (tok->type == TOKEN_NULL)
|
if (tok->type == TOKEN_NULL)
|
||||||
|
|
|
||||||
|
|
@ -72,7 +72,8 @@ enum token_type
|
||||||
TOKEN_FOR,
|
TOKEN_FOR,
|
||||||
TOKEN_WHILE,
|
TOKEN_WHILE,
|
||||||
TOKEN_UNTIL,
|
TOKEN_UNTIL,
|
||||||
TOKEN_CASE
|
TOKEN_CASE,
|
||||||
|
TOKEN_EXPORT
|
||||||
};
|
};
|
||||||
|
|
||||||
struct token
|
struct token
|
||||||
|
|
|
||||||
|
|
@ -174,14 +174,33 @@ struct ast *parse_command(struct lexer_context *ctx)
|
||||||
/* @brief: frees command_elements and redirections lists (helper func)
|
/* @brief: frees command_elements and redirections lists (helper func)
|
||||||
* @return: NULL
|
* @return: NULL
|
||||||
*/
|
*/
|
||||||
static void *err_simple_command(struct list *command_elements,
|
static void *err_s_com(struct list *command_elements, struct list *redirections,
|
||||||
struct list *redirections)
|
struct list *assignments);
|
||||||
{
|
{
|
||||||
list_deep_destroy(command_elements);
|
list_deep_destroy(command_elements);
|
||||||
list_deep_destroy(redirections);
|
list_deep_destroy(redirections);
|
||||||
|
list_deep_destroy(assignments);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static ast *parse_export(struct lexer_context *ctx)
|
||||||
|
{
|
||||||
|
struct token *token = PEEK_TOKEN();
|
||||||
|
if (token->type != TOKEN_EXPORT)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "expected the export keyword in parse_export");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
POP_TOKEN();
|
||||||
|
token = PEEK_TOKEN();
|
||||||
|
if (token->type != TOKEN_ASSIGNMENT_WORD)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "in parser: export must be followed by 'x=y'");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return ast_create_assignment(token->data, true);
|
||||||
|
}
|
||||||
|
|
||||||
struct ast *parse_simple_command(struct lexer_context *ctx)
|
struct ast *parse_simple_command(struct lexer_context *ctx)
|
||||||
{
|
{
|
||||||
struct list *command_elements = NULL;
|
struct list *command_elements = NULL;
|
||||||
|
|
@ -198,7 +217,7 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
|
||||||
struct ast *prefix = parse_prefix(ctx);
|
struct ast *prefix = parse_prefix(ctx);
|
||||||
if (prefix == NULL)
|
if (prefix == NULL)
|
||||||
{
|
{
|
||||||
return err_simple_command(command_elements, redirections);
|
return err_s_com(command_elements, redirections);
|
||||||
}
|
}
|
||||||
if (prefix->type == AST_ASSIGNMENT)
|
if (prefix->type == AST_ASSIGNMENT)
|
||||||
{
|
{
|
||||||
|
|
@ -214,66 +233,70 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
|
||||||
|
|
||||||
if (token->type != TOKEN_WORD)
|
if (token->type != TOKEN_WORD)
|
||||||
{
|
{
|
||||||
if (!has_prefix)
|
if (!has_prefix && token->type != TOKEN_EXPORT)
|
||||||
{
|
{
|
||||||
perror("Expected a command but got a different token type");
|
perror("Expected a command but got a different token type");
|
||||||
return err_simple_command(command_elements, redirections);
|
return err_s_com(command_elements, redirections, assignments);
|
||||||
|
}
|
||||||
|
if (token->type == TOKEN_EXPORT)
|
||||||
|
{
|
||||||
|
struct ast *assignment_export = parse_export(ctx);
|
||||||
|
if (assignment == NULL)
|
||||||
|
return err_s_com(command_elements, redirections, assignments);
|
||||||
|
|
||||||
|
assignments = list_append(assignments, assignment_export);
|
||||||
}
|
}
|
||||||
// else : only prefixes
|
|
||||||
}
|
}
|
||||||
else
|
else // TOKEN WORD
|
||||||
{
|
{
|
||||||
if (token->type == TOKEN_WORD)
|
char *command = strdup(token->data);
|
||||||
|
command_elements = list_append(command_elements, command);
|
||||||
|
|
||||||
|
POP_TOKEN();
|
||||||
|
token = PEEK_TOKEN();
|
||||||
|
}
|
||||||
|
// Eventual elements
|
||||||
|
while (is_first(*token, RULE_ELEMENT))
|
||||||
|
{
|
||||||
|
// Get element
|
||||||
|
struct ast *element = parse_element(ctx);
|
||||||
|
if (element == NULL)
|
||||||
{
|
{
|
||||||
char *command = strdup(token->data);
|
return err_s_com(command_elements, redirections, assignments);
|
||||||
command_elements = list_append(command_elements, command);
|
|
||||||
|
|
||||||
POP_TOKEN();
|
|
||||||
token = PEEK_TOKEN();
|
|
||||||
}
|
}
|
||||||
// Eventual elements
|
|
||||||
while (is_first(*token, RULE_ELEMENT))
|
// Get element type
|
||||||
|
if (ast_is_word(element))
|
||||||
{
|
{
|
||||||
// Get element
|
struct ast_word *element_word = ast_get_word(element);
|
||||||
struct ast *element = parse_element(ctx);
|
|
||||||
if (element == NULL)
|
|
||||||
{
|
|
||||||
return err_simple_command(command_elements, redirections);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get element type
|
// TODO test this fix for the memory leaks
|
||||||
if (ast_is_word(element))
|
char *word = strdup(element_word->word);
|
||||||
{
|
ast_free(&element);
|
||||||
struct ast_word *element_word = ast_get_word(element);
|
command_elements = list_append(command_elements, word);
|
||||||
|
// end of fix
|
||||||
// TODO test this fix for the memory leaks
|
|
||||||
char *word = strdup(element_word->word);
|
|
||||||
ast_free(&element);
|
|
||||||
command_elements = list_append(command_elements, word);
|
|
||||||
// end of fix
|
|
||||||
}
|
|
||||||
else if (ast_is_redir(element))
|
|
||||||
{
|
|
||||||
// append redirections to the list of redirections
|
|
||||||
redirections = list_append(redirections, element);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
perror("Internal error: unexpected return value from "
|
|
||||||
"parse_element in parse_simple_command");
|
|
||||||
return err_simple_command(command_elements, redirections);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Forward
|
|
||||||
token = PEEK_TOKEN();
|
|
||||||
}
|
}
|
||||||
|
else if (ast_is_redir(element))
|
||||||
|
{
|
||||||
|
// append redirections to the list of redirections
|
||||||
|
redirections = list_append(redirections, element);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
perror("Internal error: unexpected return value from "
|
||||||
|
"parse_element in parse_simple_command");
|
||||||
|
return err_s_com(command_elements, redirections, assignments);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Forward
|
||||||
|
token = PEEK_TOKEN();
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ast *result =
|
struct ast *result =
|
||||||
ast_create_command(command_elements, redirections, assignments);
|
ast_create_command(command_elements, redirections, assignments);
|
||||||
if (result == NULL)
|
if (result == NULL)
|
||||||
{
|
{
|
||||||
return err_simple_command(command_elements, redirections);
|
return err_s_com(command_elements, redirections, assignments);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
@ -290,6 +313,10 @@ struct ast *parse_element(struct lexer_context *ctx)
|
||||||
{
|
{
|
||||||
return parse_redirection(ctx);
|
return parse_redirection(ctx);
|
||||||
}
|
}
|
||||||
|
else if (token->type == TOKEN_EXPORT)
|
||||||
|
{
|
||||||
|
return parse_export(ctx);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
perror("Syntax error: unexpected token at parse_element");
|
perror("Syntax error: unexpected token at parse_element");
|
||||||
|
|
@ -455,8 +482,8 @@ struct ast *parse_else_clause(struct lexer_context *ctx)
|
||||||
token = POP_TOKEN();
|
token = POP_TOKEN();
|
||||||
if (token->type != TOKEN_THEN)
|
if (token->type != TOKEN_THEN)
|
||||||
{
|
{
|
||||||
perror(
|
perror("Expected the 'then' keyword but got a different token "
|
||||||
"Expected the 'then' keyword but got a different token type");
|
"type");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue