feat(parser): redirections

This commit is contained in:
Matteo Flebus 2026-01-27 19:56:33 +01:00
parent 04ff7376eb
commit 8a5c589742
17 changed files with 78 additions and 108 deletions

View file

@ -66,7 +66,7 @@ static bool init_firsts_map(void)
if (firsts_map == NULL)
{
perror("Internal error: couldn't create the firsts_map (is your memory "
"full ?)");
"full ?)");
return false;
}
@ -161,7 +161,7 @@ struct firsts_list *first(enum rule rule)
if (firsts_map == NULL || firsts_map[rule].tokens == NULL)
{
perror("Internal error: attempted to get the firsts of a rule without "
"properly initializing the firsts map");
"properly initializing the firsts map");
return NULL;
}

View file

@ -11,7 +11,7 @@
peek_token(ctx); \
if (token == NULL) \
{ \
perror("Internal error: cannot get the following token"); \
perror("Internal error: cannot get the following token"); \
return NULL; \
}
@ -19,13 +19,14 @@
pop_token(ctx); \
if (token == NULL) \
{ \
perror("Internal error: cannot get the following token"); \
perror("Internal error: cannot get the following token"); \
return NULL; \
}
// === Structures
enum rule {
enum rule
{
RULE_NULL = 0,
RULE_INPUT,
RULE_LIST,
@ -43,8 +44,9 @@ enum rule {
NUMBER_OF_RULES
};
struct firsts_list {
enum token_type* tokens; // Heap allocated array
struct firsts_list
{
enum token_type *tokens; // Heap allocated array
size_t list_length;
};

View file

@ -1,3 +1,5 @@
#define _POSIX_C_SOURCE 200809L
#include "grammar_advanced.h"
#include <stdio.h>
@ -8,23 +10,20 @@
static enum ast_redir_type redir_tok_to_ast_type(enum token_type tok_type)
{
switch(tok_type)
switch (tok_type)
{
case TOKEN_REDIR_LEFT:
return AST_REDIR_TYPE_LESS;
case TOKEN_REDIR_RIGHT:
return AST_REDIR_TYPE_GREAT;
// TODO finish this
default:
return AST_REDIR_TYPE_NULL;
case TOKEN_REDIR_LEFT:
return AST_REDIR_TYPE_LESS;
case TOKEN_REDIR_RIGHT:
return AST_REDIR_TYPE_GREAT;
// TODO finish this
default:
return AST_REDIR_TYPE_NULL;
}
}
struct ast *parse_redirection(struct lexer_context *ctx)
{
(void)ctx;
return NULL;
/*
struct token *token = PEEK_TOKEN();
int io_number = -1;
if (token->type == TOKEN_IONUMBER)
@ -37,10 +36,9 @@ struct ast *parse_redirection(struct lexer_context *ctx)
if (!is_token_redir(token))
{
perror("Syntax error: expected a redirection token but got something "
"else");
"else");
return NULL;
}
// char *redir_op = strdup(token->data);
enum ast_redir_type redir_type = redir_tok_to_ast_type(token->type);
POP_TOKEN();
@ -49,14 +47,12 @@ struct ast *parse_redirection(struct lexer_context *ctx)
if (token->type != TOKEN_WORD)
{
perror("Syntax error: expected a word after redirection");
// free(redir_op);
return NULL;
}
char *target = strdup(token->data);
POP_TOKEN();
return ast_create_redir(io_number, redir_type, target);
*/
return ast_create_redir(target, io_number, redir_type);
}
struct ast *parse_prefix(struct lexer_context *ctx)

View file

@ -8,7 +8,8 @@
/*
* @brief parses a redirection rule
*
* @code redirection = [IONUMBER] ( '>' | '<' | '>>' | '>&' | '<&' | '>|' | '<>' ) WORD ;
* @code redirection = [IONUMBER] ( '>' | '<' | '>>' | '>&' | '<&' | '>|' | '<>'
* ) WORD ;
*
* @first TOKEN_IONUMBER, TOKEN_REDIRECTION
*/
@ -23,5 +24,4 @@ struct ast *parse_redirection(struct lexer_context *ctx);
*/
struct ast *parse_prefix(struct lexer_context *ctx);
#endif /* ! GRAMMAR_ADVANCED_H */

View file

@ -1,13 +1,14 @@
#include <stdbool.h>
#define _POSIX_C_SOURCE 200809L
#include "grammar_basic.h"
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "../utils/lists/lists.h"
#include "grammar.h"
#include "grammar_advanced.h"
#include "grammar_basic.h"
// === Static functions
@ -210,7 +211,7 @@ struct ast *parse_simple_command(struct lexer_context *ctx)
else
{
perror("Internal error: unexpected return value from parse_element "
"in parse_simple_command");
"in parse_simple_command");
list_deep_destroy(command_elements);
return NULL;
}
@ -260,7 +261,7 @@ struct ast *parse_if_rule(struct lexer_context *ctx)
if (token->type != TOKEN_IF)
{
perror("Internal error: expected a if rule but token has different "
"type");
"type");
return NULL;
}
@ -398,7 +399,8 @@ struct ast *parse_else_clause(struct lexer_context *ctx)
token = POP_TOKEN();
if (token->type != TOKEN_THEN)
{
perror("Expected the 'then' keyword but got a different token type");
perror(
"Expected the 'then' keyword but got a different token type");
return NULL;
}

View file

@ -40,19 +40,19 @@ struct ast *get_ast(struct lexer_context *ctx)
if (ctx == NULL)
{
perror("Internal error: called parser with no lexer context (NULL "
"pointer). Aborting.");
"pointer). Aborting.");
return NULL;
}
if (state == PARSER_STATE_NOT_INITIALIZED)
{
perror("Internal error: attempted to call parser without initializing "
"it. Aborting.");
"it. Aborting.");
return NULL;
}
if (state == PARSER_STATE_CLOSED)
{
perror("Internal error: attempted to call parser after closing it. "
"Aborting.");
"Aborting.");
return NULL;
}