42sh/src/parser/grammar_advanced.c

48 lines
1,017 B
C
Raw Normal View History

2026-01-24 15:34:10 +01:00
#include "grammar_advanced.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2026-01-24 15:34:10 +01:00
#include "grammar_basic.h"
bool
2026-01-24 15:34:10 +01:00
struct ast *parse_redirection(struct lexer_context *ctx)
{
struct token *token = PEEK_TOKEN();
int io_number = -1;
if (token->type == TOKEN_IONUMBER)
{
io_number = atoi(token->data);
2026-01-24 15:34:10 +01:00
POP_TOKEN();
token = PEEK_TOKEN();
}
if (!is_token_redir(token))
2026-01-24 15:34:10 +01:00
{
perror("Syntax error: expected a redirection token but got something "
2026-01-24 15:34:10 +01:00
"else");
return NULL;
}
// char *redir_op = strdup(token->data);
POP_TOKEN();
token = PEEK_TOKEN();
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_op, target);
2026-01-24 15:34:10 +01:00
}
struct ast *parse_prefix(struct lexer_context *ctx)
{
return parse_redirection(ctx);
}