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

@ -25,7 +25,8 @@
// === Structures
enum rule {
enum rule
{
RULE_NULL = 0,
RULE_INPUT,
RULE_LIST,
@ -43,7 +44,8 @@ enum rule {
NUMBER_OF_RULES
};
struct firsts_list {
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>
@ -22,9 +24,6 @@ static enum ast_redir_type redir_tok_to_ast_type(enum token_type tok_type)
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)
@ -40,7 +39,6 @@ struct ast *parse_redirection(struct lexer_context *ctx)
"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
@ -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

@ -7,11 +7,11 @@
#include "ast_end.h"
#include "ast_if.h"
#include "ast_list.h"
#include "ast_neg.h"
#include "ast_pipe.h"
#include "ast_redir.h"
#include "ast_void.h"
#include "ast_word.h"
#include "ast_pipe.h"
#include "ast_neg.h"
/**
* Prints the Graphviz DOT representation of the given AST to stdout.

View file

@ -1,8 +1,8 @@
#ifndef AST_BASE_H
#define AST_BASE_H
#include <stdlib.h>
#include <stdbool.h>
#include <stdlib.h>
enum ast_type
{

View file

@ -14,13 +14,12 @@ struct ast_redir *ast_get_redir(struct ast *node)
return NULL;
}
struct ast *ast_create_redir(struct ast *child, char *filename, int io_number,
struct ast *ast_create_redir(char *filename, int io_number,
enum ast_redir_type type)
{
struct ast_redir *redir = malloc(sizeof(struct ast_redir));
if (!redir)
return NULL;
redir->child = child;
redir->filename =
filename; // Takes ownership? Usually yes in simple ASTs, or dup. Let's
// assume pointer copy for now, but user must manage memory.
@ -34,7 +33,6 @@ void ast_free_redir(struct ast_redir *redir)
{
if (!redir)
return;
ast_free(&redir->child);
free(redir->filename);
free(redir);
}

View file

@ -17,7 +17,6 @@ enum ast_redir_type
struct ast_redir
{
struct ast *child;
char *filename;
int io_number; // The FD being redirected (default -1 if not specified,
// implies 0 or 1 based on type)
@ -26,7 +25,7 @@ struct ast_redir
bool ast_is_redir(struct ast *node);
struct ast_redir *ast_get_redir(struct ast *node);
struct ast *ast_create_redir(struct ast *child, char *filename, int io_number,
struct ast *ast_create_redir(char *filename, int io_number,
enum ast_redir_type type);
void ast_free_redir(struct ast_redir *redir);

View file

@ -1,8 +1,8 @@
#define _POSIX_C_SOURCE 200809L
#include "ast_word.h"
#include <stdio.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -17,6 +17,7 @@ struct ast *ast_create_word(char *word)
struct ast *res = ast_create(AST_WORD, ast_node);
if (res == NULL)
{
free(ast_node->word);
free(ast_node);
return NULL;
}

View file

@ -9,11 +9,7 @@ TestSuite(IO_Backend);
Test(IO_Backend, init_null)
{
struct iob_context ctx =
{
.mode = IOB_MODE_NULL,
.args = NULL
};
struct iob_context ctx = { .mode = IOB_MODE_NULL, .args = NULL };
int actual = iob_init(&ctx);
int expected = IOB_ERROR_BAD_ARG;
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
@ -21,11 +17,7 @@ cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
Test(IO_Backend, init_stdin)
{
struct iob_context ctx =
{
.mode = IOB_MODE_STDIN,
.args = NULL
};
struct iob_context ctx = { .mode = IOB_MODE_STDIN, .args = NULL };
int actual = iob_init(&ctx);
int expected = 0;
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
@ -37,10 +29,7 @@ iob_close();
Test(IO_Backend, init_script)
{
char *script_name = "script.tmp";
struct iob_context ctx = {
.mode = IOB_MODE_SCRIPT,
.args = script_name
};
struct iob_context ctx = { .mode = IOB_MODE_SCRIPT, .args = script_name };
// Create file
FILE *f = fopen(script_name, "w");
fclose(f);
@ -55,10 +44,7 @@ remove(script_name);
Test(IO_Backend, init_script_not_a_file)
{
char *script_name = "not_a_file.tmp";
struct iob_context ctx = {
.mode = IOB_MODE_SCRIPT,
.args = script_name
};
struct iob_context ctx = { .mode = IOB_MODE_SCRIPT, .args = script_name };
int actual = iob_init(&ctx);
int expected = IOB_ERROR_CANNOT_OPEN_FILE;
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
@ -66,11 +52,7 @@ cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
Test(IO_Backend, init_script_null)
{
struct iob_context ctx =
{
.mode = IOB_MODE_SCRIPT,
.args = NULL
};
struct iob_context ctx = { .mode = IOB_MODE_SCRIPT, .args = NULL };
int actual = iob_init(&ctx);
int expected = IOB_ERROR_CANNOT_OPEN_FILE;
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
@ -79,10 +61,7 @@ cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
Test(IO_Backend, init_cmd)
{
char *cmd = "iamacommand --yesido";
struct iob_context ctx = {
.mode = IOB_MODE_CMD,
.args = cmd
};
struct iob_context ctx = { .mode = IOB_MODE_CMD, .args = cmd };
int actual = iob_init(&ctx);
int expected = 0;
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
@ -91,11 +70,7 @@ iob_close();
Test(IO_Backend, init_cmd_null)
{
struct iob_context ctx =
{
.mode = IOB_MODE_CMD,
.args = NULL
};
struct iob_context ctx = { .mode = IOB_MODE_CMD, .args = NULL };
int actual = iob_init(&ctx);
int expected = IOB_ERROR_BAD_ARG;
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
@ -104,10 +79,7 @@ cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
Test(IO_Backend, init_already_init)
{
char *cmd = "iamacommand --yesido";
struct iob_context ctx = {
.mode = IOB_MODE_CMD,
.args = cmd
};
struct iob_context ctx = { .mode = IOB_MODE_CMD, .args = cmd };
iob_init(&ctx);
int actual = iob_init(&ctx);
int expected = IOB_ERROR_MODULE_ALREADY_INITIALIZED;