diff --git a/src/parser/grammar_basic.c b/src/parser/grammar_basic.c index 932d7f5..ab4194c 100644 --- a/src/parser/grammar_basic.c +++ b/src/parser/grammar_basic.c @@ -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,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"); - 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) + if (!has_prefix) { + perror("Expected a command but got a different token type"); return err_simple_command(command_elements, redirections); } - - // Get element type - if (ast_is_word(element)) + // else : only prefixes + } + 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 - char *word = strdup(element_word->word); - ast_free(element_word); - command_elements = - list_append(command_elements, word); - // end of fix + POP_TOKEN(); + token = PEEK_TOKEN(); } - else if (ast_is_redir(element)) + // Eventual elements + while (is_first(*token, RULE_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); - } + // Get element + struct ast *element = parse_element(ctx); + if (element == NULL) + { + return err_simple_command(command_elements, redirections); + } - // Forward - token = PEEK_TOKEN(); + // Get element type + 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