style(parser): refactor parse_command

This commit is contained in:
matteo 2026-01-28 12:21:57 +01:00
parent 51fc8a3330
commit efb0a6f148

View file

@ -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,57 +202,62 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
} }
} }
if (token->type != TOKEN_WORD && !has_prefix) if (token->type != TOKEN_WORD)
{ {
perror("Expected a command but got a different token type"); if (!has_prefix)
return err_simple_command(command_elements, redirections);
}
// we can have only prefixes askip
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)
{ {
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
// Get element type }
if (ast_is_word(element)) else
{
if (token->type == TOKEN_WORD)
{ {
struct ast_word *element_word = ast_get_word(element); char *command = strdup(token->data);
command_elements = list_append(command_elements, command);
// TODO test this fix for the memory leaks POP_TOKEN();
char *word = strdup(element_word->word); token = PEEK_TOKEN();
ast_free(element_word);
command_elements =
list_append(command_elements, word);
// end of fix
} }
else if (ast_is_redir(element)) // Eventual elements
while (is_first(*token, RULE_ELEMENT))
{ {
// append redirections to the list of redirections // Get element
redirections = list_append(redirections, element); struct ast *element = parse_element(ctx);
} if (element == NULL)
else {
{ return err_simple_command(command_elements, redirections);
perror("Internal error: unexpected return value from parse_element " }
"in parse_simple_command");
return err_simple_command(command_elements, redirections);
}
// Forward // Get element type
token = PEEK_TOKEN(); if (ast_is_word(element))
{
struct ast_word *element_word = ast_get_word(element);
// 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);
// 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();
}
} }
// Result // Result