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 ast *parse_command(struct lexer_context *ctx)
|
||||||
{
|
{
|
||||||
struct token *token = PEEK_TOKEN();
|
struct token *token = PEEK_TOKEN();
|
||||||
|
struct ast *result = NULL;
|
||||||
|
|
||||||
if (is_first(*token, RULE_SIMPLE_COMMAND))
|
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))
|
else if (is_first(*token, RULE_SHELL_COMMAND))
|
||||||
{
|
{
|
||||||
return parse_shell_command(ctx);
|
parse_shell_command(ctx);
|
||||||
}
|
}
|
||||||
else
|
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");
|
perror("Expected a command but got a different token type");
|
||||||
return err_simple_command(command_elements, redirections);
|
return err_simple_command(command_elements, redirections);
|
||||||
}
|
}
|
||||||
|
// else : only prefixes
|
||||||
// we can have only prefixes askip
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
if (token->type == TOKEN_WORD)
|
if (token->type == TOKEN_WORD)
|
||||||
{
|
{
|
||||||
char *command = strdup(token->data);
|
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
|
// TODO test this fix for the memory leaks
|
||||||
char *word = strdup(element_word->word);
|
char *word = strdup(element_word->word);
|
||||||
ast_free(element_word);
|
ast_free(element_word);
|
||||||
command_elements =
|
command_elements = list_append(command_elements, word);
|
||||||
list_append(command_elements, word);
|
|
||||||
// end of fix
|
// end of fix
|
||||||
}
|
}
|
||||||
else if (ast_is_redir(element))
|
else if (ast_is_redir(element))
|
||||||
|
|
@ -245,7 +249,8 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
perror("Internal error: unexpected return value from parse_element "
|
perror("Internal error: unexpected return value from "
|
||||||
|
"parse_element "
|
||||||
"in parse_simple_command");
|
"in parse_simple_command");
|
||||||
return err_simple_command(command_elements, redirections);
|
return err_simple_command(command_elements, redirections);
|
||||||
}
|
}
|
||||||
|
|
@ -253,6 +258,7 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
|
||||||
// Forward
|
// Forward
|
||||||
token = PEEK_TOKEN();
|
token = PEEK_TOKEN();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Result
|
// Result
|
||||||
struct ast *result = ast_create_command(command_elements, redirections);
|
struct ast *result = ast_create_command(command_elements, redirections);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue