feat(parser): implementing all redirection types -- WIP

This commit is contained in:
Matteo Flebus 2026-01-26 19:00:20 +01:00
parent d707c6180b
commit 666517e3c1
3 changed files with 29 additions and 1 deletions

View file

@ -5,6 +5,7 @@
#include <stdlib.h>
#include "grammar_basic.h"
#include "../lexer/lexer.h"
// === Static variables
@ -84,11 +85,30 @@ static void add_first_redir(enum rule rule)
add_first(rule, TOKEN_REDIR_LEFT_RIGHT);
add_first(rule, TOKEN_REDIR_DOUBLE_RIGHT);
add_first(rule, TOKEN_REDIR_LEFT_AMP);
add_first(rule, TOKEN_REDIR_RIGHT_AMP);
add_first(rule, TOKEN_REDIR_RIGHT_PIPE);
}
// === Functions
bool is_token_redir(struct token *token)
{
switch (token->type)
{
case TOKEN_REDIR_LEFT:
case TOKEN_REDIR_RIGHT:
case TOKEN_REDIR_LEFT_RIGHT:
case TOKEN_REDIR_DOUBLE_RIGHT:
case TOKEN_REDIR_LEFT_AMP:
case TOKEN_REDIR_RIGHT_AMP:
case TOKEN_REDIR_RIGHT_PIPE:
return true;
default:
return false;
}
}
bool grammar_init(void)
{
// Initialize the firsts map
@ -196,7 +216,9 @@ struct ast *parse_input(struct lexer_context *ctx)
if (token->type == TOKEN_NEWLINE || token->type == TOKEN_EOF)
{
if (token->type == TOKEN_NEWLINE)
{
POP_TOKEN();
}
return ast;
}

View file

@ -50,6 +50,10 @@ struct firsts_list {
// === Functions
/* @brief: returns true if token has a type redirection (not pipe or IONUMBER).
*/
bool is_token_redir(struct token *token);
/*
* @brief Initializes the grammar submodule
* @return PARSER_INIT_SUCCESS on success PARSER_INIT_ERROR on error

View file

@ -1,3 +1,5 @@
#define _POSIX_C_SOURCE 200809L
#include "grammar_basic.h"
#include <stdio.h>
@ -198,7 +200,7 @@ struct ast *parse_element(struct lexer_context *ctx)
token = POP_TOKEN();
return ast_create_word(token->data);
}
else if (token->type == TOKEN_IONUMBER || token->type == TOKEN_REDIRECTION)
else if (token->type == TOKEN_IONUMBER || is_token_redir(token))
{
return parse_redirection(ctx);
}