feat: Merge branch 'parser' into dev

This commit is contained in:
Gu://em_ 2026-01-30 21:32:57 +01:00
commit d6e6bfacf2
3 changed files with 61 additions and 29 deletions

View file

@ -97,8 +97,13 @@ struct ast *parse_list(struct lexer_context *ctx)
token = PEEK_TOKEN();
while (token->type == TOKEN_SEMICOLON)
{
token = POP_TOKEN();
// Forward
POP_TOKEN();
token = PEEK_TOKEN();
// TODO seems a little akward (not fully compliant with the grammar)
// but it's time consuming to rewrite to only cover edge cases.
// So it'll probably stay like that for now
if (is_first(*token, RULE_AND_OR))
{
current_node = parse_and_or(ctx);
@ -299,13 +304,12 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
// Get element type
if (ast_is_word(element))
{
// Extract word
struct ast_word *element_word = ast_get_word(element);
// TODO test this fix for the memory leaks
char *word = strdup(element_word->word);
char *word = element_word->word;
element_word->word = NULL; // Prevents word to be freed
ast_free(&element);
command_elements = list_append(command_elements, word);
// end of fix
}
else if (ast_is_redir(element))
{
@ -337,8 +341,17 @@ struct ast *parse_element(struct lexer_context *ctx)
struct token *token = PEEK_TOKEN();
if (token->type == TOKEN_WORD || token->type == TOKEN_ASSIGNMENT_WORD)
{
token = POP_TOKEN();
return ast_create_word(token->data);
POP_TOKEN();
struct ast *result = ast_create_word(token->data);
if (result == NULL)
{
perror("Internal error: could not create ast node (is your memory "
"full ?)");
return NULL;
}
return result;
}
else if (token->type == TOKEN_IONUMBER || is_token_redir(token))
{
@ -415,12 +428,15 @@ struct ast *parse_if_rule(struct lexer_context *ctx)
// Condition content
struct ast *condition_content = parse_compound_list(ctx);
if (condition_content == NULL)
return NULL;
token = PEEK_TOKEN();
// Then keyword
token = PEEK_TOKEN();
if (token->type != TOKEN_THEN)
{
perror("Expected the 'then' keyword but token has different type");
perror("Syntax error: Expected the 'then' keyword but token has "
"different type");
return err_if_rule(&condition_content, NULL, NULL);
}
POP_TOKEN();
@ -431,9 +447,9 @@ struct ast *parse_if_rule(struct lexer_context *ctx)
{
return err_if_rule(&condition_content, &then_content, NULL);
}
token = PEEK_TOKEN();
struct ast *else_content = NULL;
token = PEEK_TOKEN();
// Eventual else/elif clause(s)
if (is_first(*token, RULE_ELSE_CLAUSE))
{
@ -442,10 +458,10 @@ struct ast *parse_if_rule(struct lexer_context *ctx)
{
return err_if_rule(&condition_content, &then_content, NULL);
}
token = PEEK_TOKEN();
}
// Fi keyword
token = PEEK_TOKEN();
if (token->type != TOKEN_FI)
{
perror("Expected the 'fi' keyword but token has different type");
@ -474,7 +490,7 @@ struct ast *parse_compound_list(struct lexer_context *ctx)
// Skip newlines
while (token->type == TOKEN_NEWLINE)
{
token = POP_TOKEN();
POP_TOKEN();
token = PEEK_TOKEN();
}
@ -483,9 +499,9 @@ struct ast *parse_compound_list(struct lexer_context *ctx)
if (current_cmd == NULL)
return NULL;
result_list = list_append(result_list, current_cmd);
token = PEEK_TOKEN();
// Following commands
token = PEEK_TOKEN();
while (token->type == TOKEN_SEMICOLON || token->type == TOKEN_NEWLINE)
{
POP_TOKEN();
@ -494,7 +510,7 @@ struct ast *parse_compound_list(struct lexer_context *ctx)
// Skip newlines
while (token->type == TOKEN_NEWLINE)
{
token = POP_TOKEN();
POP_TOKEN();
token = PEEK_TOKEN();
}
@ -505,22 +521,21 @@ struct ast *parse_compound_list(struct lexer_context *ctx)
if (current_cmd == NULL)
return NULL;
result_list = list_append(result_list, current_cmd);
token = PEEK_TOKEN();
}
token = PEEK_TOKEN();
}
// Eventual semicolons
// Eventual semicolon
if (token->type == TOKEN_SEMICOLON)
{
token = POP_TOKEN();
POP_TOKEN();
token = PEEK_TOKEN();
}
// Skip newlines
while (token->type == TOKEN_NEWLINE)
{
token = POP_TOKEN();
POP_TOKEN();
token = PEEK_TOKEN();
}
@ -549,18 +564,31 @@ struct ast *parse_else_clause(struct lexer_context *ctx)
}
struct ast *then_content = parse_compound_list(ctx);
if (then_content == NULL)
{
ast_free(&condition);
return NULL;
}
token = PEEK_TOKEN();
// Eventual else clause (recursive)
struct ast *else_content = NULL;
token = PEEK_TOKEN();
if (token->type == TOKEN_ELSE || token->type == TOKEN_ELIF)
{
else_content = parse_else_clause(ctx);
if (else_content == NULL)
{
ast_free(&then_content);
ast_free(&condition);
return NULL;
}
}
else
{
else_content = ast_create_void();
}
struct ast *result =
ast_create_if(condition, then_content, else_content);
return result;
return ast_create_if(condition, then_content, else_content);
}
// Eventual else content
@ -569,12 +597,15 @@ struct ast *parse_else_clause(struct lexer_context *ctx)
if (token->type == TOKEN_ELSE)
{
token = POP_TOKEN(); // eat else
token = POP_TOKEN();
result = parse_compound_list(ctx);
if (result == NULL)
return NULL;
}
if (result == NULL)
else
{
result = ast_create_void();
}
return result;
}

View file

@ -2,7 +2,6 @@
#include "ast_word.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -43,6 +42,8 @@ void ast_free_word(struct ast_word *ast_node)
if (ast_node == NULL)
return;
free(ast_node->word);
if (ast_node->word != NULL)
free(ast_node->word);
free(ast_node);
}

View file

@ -251,7 +251,7 @@ summarize() {
# Print
echo -e $BWhite "\n\n""===========" $UWhite"Summary"$Color_Off "\n"
echo -e " Passed $coverage_color$passed_tests/$total_tests$Color_Off tests ($coverage_color$tests_percentage$Color_Off%)"
echo -e " Passed $coverage_color$passed_tests/$total_tests$Color_Off tests ($coverage_color$tests_percentage%$Color_Off)"
echo -e " Got $timeouts_color$timeouts_count timeout(s)$Color_Off"
if [ "$OUTPUT_FILE" != "" ]; then
echo $tests_percentage > "$OUTPUT_FILE";