style(parser): refactor parse_command
This commit is contained in:
parent
51fc8a3330
commit
efb0a6f148
1 changed files with 50 additions and 44 deletions
|
|
@ -152,14 +152,15 @@ struct ast *parse_pipeline(struct lexer_context *ctx)
|
|||
struct ast *parse_command(struct lexer_context *ctx)
|
||||
{
|
||||
struct token *token = PEEK_TOKEN();
|
||||
struct ast *result = NULL;
|
||||
|
||||
if (is_first(*token, RULE_SIMPLE_COMMAND))
|
||||
{
|
||||
return parse_simple_command(ctx);
|
||||
result = parse_simple_command(ctx);
|
||||
}
|
||||
else if (is_first(*token, RULE_SHELL_COMMAND))
|
||||
{
|
||||
return parse_shell_command(ctx);
|
||||
parse_shell_command(ctx);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -201,13 +202,17 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
|
|||
}
|
||||
}
|
||||
|
||||
if (token->type != TOKEN_WORD && !has_prefix)
|
||||
if (token->type != TOKEN_WORD)
|
||||
{
|
||||
if (!has_prefix)
|
||||
{
|
||||
perror("Expected a command but got a different token type");
|
||||
return err_simple_command(command_elements, redirections);
|
||||
}
|
||||
|
||||
// we can have only prefixes askip
|
||||
// else : only prefixes
|
||||
}
|
||||
else
|
||||
{
|
||||
if (token->type == TOKEN_WORD)
|
||||
{
|
||||
char *command = strdup(token->data);
|
||||
|
|
@ -234,8 +239,7 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
|
|||
// TODO test this fix for the memory leaks
|
||||
char *word = strdup(element_word->word);
|
||||
ast_free(element_word);
|
||||
command_elements =
|
||||
list_append(command_elements, word);
|
||||
command_elements = list_append(command_elements, word);
|
||||
// end of fix
|
||||
}
|
||||
else if (ast_is_redir(element))
|
||||
|
|
@ -245,7 +249,8 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
|
|||
}
|
||||
else
|
||||
{
|
||||
perror("Internal error: unexpected return value from parse_element "
|
||||
perror("Internal error: unexpected return value from "
|
||||
"parse_element "
|
||||
"in parse_simple_command");
|
||||
return err_simple_command(command_elements, redirections);
|
||||
}
|
||||
|
|
@ -253,6 +258,7 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
|
|||
// Forward
|
||||
token = PEEK_TOKEN();
|
||||
}
|
||||
}
|
||||
|
||||
// Result
|
||||
struct ast *result = ast_create_command(command_elements, redirections);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue