2026-01-26 19:00:20 +01:00
|
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
|
|
2026-01-27 19:56:33 +01:00
|
|
|
#include "grammar_basic.h"
|
|
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
2026-01-14 20:53:47 +01:00
|
|
|
#include <stdio.h>
|
2026-01-17 19:11:55 +01:00
|
|
|
#include <string.h>
|
2026-01-14 20:53:47 +01:00
|
|
|
|
2026-01-23 17:01:26 +01:00
|
|
|
#include "../utils/lists/lists.h"
|
|
|
|
|
#include "grammar.h"
|
2026-01-24 15:34:10 +01:00
|
|
|
#include "grammar_advanced.h"
|
2026-01-14 20:53:47 +01:00
|
|
|
|
|
|
|
|
// === Static functions
|
|
|
|
|
|
2026-01-23 17:33:15 +01:00
|
|
|
static enum ast_and_or_type and_or_tok_to_ast(enum token_type tok_type)
|
2026-01-22 19:57:49 +01:00
|
|
|
{
|
|
|
|
|
switch (tok_type)
|
|
|
|
|
{
|
|
|
|
|
case TOKEN_AND:
|
|
|
|
|
return AST_AND_OR_TYPE_AND;
|
|
|
|
|
case TOKEN_OR:
|
|
|
|
|
return AST_AND_OR_TYPE_OR;
|
|
|
|
|
default:
|
|
|
|
|
fprintf(stderr, "and_or impossible to init, wrong token type");
|
|
|
|
|
return AST_AND_OR_NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 20:00:09 +01:00
|
|
|
/* @brief: frees all the arguments. (helper func)
|
|
|
|
|
* @return: NULL.
|
|
|
|
|
*/
|
|
|
|
|
static void *err_if_rule(struct ast **cond, struct ast **then_clause,
|
|
|
|
|
struct ast **else_clause)
|
|
|
|
|
{
|
|
|
|
|
ast_free(cond);
|
|
|
|
|
ast_free(then_clause);
|
|
|
|
|
ast_free(else_clause);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 19:48:31 +01:00
|
|
|
/* @brief: frees command_elements and redirections lists (helper func)
|
|
|
|
|
* @return: NULL
|
|
|
|
|
*/
|
2026-01-30 20:00:09 +01:00
|
|
|
static void *err_s_com(struct list *command_elements, struct list *redirections,
|
|
|
|
|
struct list *assignments)
|
2026-01-30 19:48:31 +01:00
|
|
|
{
|
|
|
|
|
list_deep_destroy(command_elements);
|
|
|
|
|
list_deep_destroy(redirections);
|
2026-01-30 20:00:09 +01:00
|
|
|
list_deep_destroy(assignments);
|
2026-01-30 19:48:31 +01:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 20:00:09 +01:00
|
|
|
/* @brief: used when export keyword is found, and expects an assignment after.
|
|
|
|
|
* @return: an ast_assignment with the field [global] set to true.
|
2026-01-30 19:48:31 +01:00
|
|
|
*/
|
2026-01-30 20:00:09 +01:00
|
|
|
static struct ast *parse_export(struct lexer_context *ctx)
|
2026-01-30 19:48:31 +01:00
|
|
|
{
|
2026-01-30 20:00:09 +01:00
|
|
|
struct token *token = PEEK_TOKEN();
|
|
|
|
|
if (token->type != TOKEN_EXPORT)
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, "expected the export keyword in parse_export");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
// export
|
|
|
|
|
POP_TOKEN();
|
|
|
|
|
|
|
|
|
|
token = PEEK_TOKEN();
|
|
|
|
|
|
|
|
|
|
if (token->type != TOKEN_ASSIGNMENT_WORD)
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, "in parser: export must be followed by 'x=y'");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// assignment
|
|
|
|
|
POP_TOKEN();
|
|
|
|
|
|
|
|
|
|
return ast_create_assignment(token->data, true);
|
2026-01-30 19:48:31 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-14 20:53:47 +01:00
|
|
|
// === Functions
|
|
|
|
|
|
2026-01-20 19:54:29 +01:00
|
|
|
struct ast *parse_list(struct lexer_context *ctx)
|
2026-01-16 20:12:29 +01:00
|
|
|
{
|
|
|
|
|
struct list *result_list = NULL;
|
|
|
|
|
struct ast *current_node = NULL;
|
|
|
|
|
|
|
|
|
|
struct token *token = PEEK_TOKEN();
|
|
|
|
|
|
2026-01-17 20:15:27 +01:00
|
|
|
// and_or
|
2026-01-20 20:32:59 +01:00
|
|
|
current_node = parse_and_or(ctx);
|
2026-01-17 20:15:27 +01:00
|
|
|
if (current_node == NULL)
|
|
|
|
|
return NULL;
|
2026-01-20 19:25:55 +01:00
|
|
|
result_list = list_append(result_list, current_node);
|
2026-01-17 20:15:27 +01:00
|
|
|
|
|
|
|
|
// Following and_or commands
|
|
|
|
|
token = PEEK_TOKEN();
|
|
|
|
|
while (token->type == TOKEN_SEMICOLON)
|
2026-01-16 20:12:29 +01:00
|
|
|
{
|
2026-01-30 20:58:10 +01:00
|
|
|
// Forward
|
|
|
|
|
POP_TOKEN();
|
2026-01-24 16:13:16 +01:00
|
|
|
token = PEEK_TOKEN();
|
2026-01-30 20:58:10 +01:00
|
|
|
|
|
|
|
|
// 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
|
2026-01-24 16:13:16 +01:00
|
|
|
if (is_first(*token, RULE_AND_OR))
|
2026-01-23 17:01:26 +01:00
|
|
|
{
|
2026-01-24 16:13:16 +01:00
|
|
|
current_node = parse_and_or(ctx);
|
|
|
|
|
if (current_node == NULL)
|
2026-01-24 16:48:21 +01:00
|
|
|
{
|
|
|
|
|
struct ast *tmp = ast_create_list(result_list);
|
|
|
|
|
ast_free(&tmp);
|
2026-01-24 16:13:16 +01:00
|
|
|
return NULL;
|
2026-01-24 16:48:21 +01:00
|
|
|
}
|
2026-01-24 16:13:16 +01:00
|
|
|
result_list = list_append(result_list, current_node);
|
|
|
|
|
token = PEEK_TOKEN();
|
2026-01-23 17:01:26 +01:00
|
|
|
}
|
2026-01-16 20:12:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ast_create_list(result_list);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 19:54:29 +01:00
|
|
|
struct ast *parse_and_or(struct lexer_context *ctx)
|
2026-01-17 17:20:13 +01:00
|
|
|
{
|
2026-01-22 19:57:49 +01:00
|
|
|
struct ast *result = parse_pipeline(ctx);
|
2026-01-24 16:48:21 +01:00
|
|
|
if (result == NULL)
|
|
|
|
|
return NULL;
|
2026-01-22 19:57:49 +01:00
|
|
|
struct token *token = PEEK_TOKEN();
|
|
|
|
|
|
2026-01-24 16:48:21 +01:00
|
|
|
while (token->type == TOKEN_AND || token->type == TOKEN_OR)
|
2026-01-22 19:57:49 +01:00
|
|
|
{
|
2026-01-30 19:48:31 +01:00
|
|
|
// Build AST (left part)
|
2026-01-22 19:57:49 +01:00
|
|
|
enum ast_and_or_type type = and_or_tok_to_ast(token->type);
|
2026-01-30 19:48:31 +01:00
|
|
|
struct ast *left = result;
|
2026-01-22 19:57:49 +01:00
|
|
|
|
2026-01-30 19:48:31 +01:00
|
|
|
POP_TOKEN();
|
2026-01-22 19:57:49 +01:00
|
|
|
token = PEEK_TOKEN();
|
|
|
|
|
|
2026-01-23 17:33:15 +01:00
|
|
|
// Skip newlines
|
2026-01-22 19:57:49 +01:00
|
|
|
while (token->type == TOKEN_NEWLINE)
|
|
|
|
|
{
|
|
|
|
|
token = POP_TOKEN();
|
|
|
|
|
token = PEEK_TOKEN();
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-23 17:33:15 +01:00
|
|
|
// Right part
|
2026-01-22 19:57:49 +01:00
|
|
|
struct ast *right = parse_pipeline(ctx);
|
2026-01-30 19:48:31 +01:00
|
|
|
if (right == NULL)
|
|
|
|
|
{
|
|
|
|
|
ast_free(&left);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
token = PEEK_TOKEN();
|
2026-01-22 19:57:49 +01:00
|
|
|
|
|
|
|
|
result = ast_create_and_or(left, right, type);
|
2026-01-23 17:33:15 +01:00
|
|
|
if (result == NULL)
|
|
|
|
|
{
|
|
|
|
|
ast_free(&left);
|
|
|
|
|
ast_free(&right);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2026-01-22 19:57:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
2026-01-17 17:20:13 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-20 19:54:29 +01:00
|
|
|
struct ast *parse_pipeline(struct lexer_context *ctx)
|
2026-01-17 17:20:13 +01:00
|
|
|
{
|
2026-01-27 00:30:19 +01:00
|
|
|
bool negation = false;
|
|
|
|
|
struct token *token = PEEK_TOKEN();
|
|
|
|
|
|
|
|
|
|
// Eventual '!'
|
|
|
|
|
if (token->type == TOKEN_NEGATION)
|
|
|
|
|
{
|
|
|
|
|
negation = true;
|
|
|
|
|
POP_TOKEN();
|
|
|
|
|
token = PEEK_TOKEN();
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 19:48:31 +01:00
|
|
|
// command rule
|
2026-01-27 00:30:19 +01:00
|
|
|
struct ast *left = parse_command(ctx);
|
2026-01-30 19:48:31 +01:00
|
|
|
token = PEEK_TOKEN();
|
2026-01-27 16:35:30 +01:00
|
|
|
if (negation)
|
|
|
|
|
{
|
|
|
|
|
left = ast_create_neg(negation, left);
|
|
|
|
|
}
|
2026-01-27 00:30:19 +01:00
|
|
|
|
2026-01-30 19:48:31 +01:00
|
|
|
// Pipes
|
2026-01-27 00:30:19 +01:00
|
|
|
while (token->type == TOKEN_PIPE)
|
|
|
|
|
{
|
|
|
|
|
POP_TOKEN();
|
2026-01-30 19:48:31 +01:00
|
|
|
token = PEEK_TOKEN();
|
2026-01-27 00:30:19 +01:00
|
|
|
|
|
|
|
|
// skip newlines
|
|
|
|
|
while (token->type == TOKEN_NEWLINE)
|
|
|
|
|
{
|
|
|
|
|
POP_TOKEN();
|
|
|
|
|
token = PEEK_TOKEN();
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 19:48:31 +01:00
|
|
|
// command rule
|
2026-01-27 00:30:19 +01:00
|
|
|
struct ast *right = parse_command(ctx);
|
2026-01-30 19:48:31 +01:00
|
|
|
token = PEEK_TOKEN();
|
2026-01-27 00:30:19 +01:00
|
|
|
|
|
|
|
|
// Create AST
|
|
|
|
|
left = ast_create_pipe(left, right);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return left;
|
2026-01-17 17:20:13 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-20 19:54:29 +01:00
|
|
|
struct ast *parse_command(struct lexer_context *ctx)
|
2026-01-17 17:20:13 +01:00
|
|
|
{
|
|
|
|
|
struct token *token = PEEK_TOKEN();
|
2026-01-28 12:21:57 +01:00
|
|
|
struct ast *result = NULL;
|
2026-01-17 17:20:13 +01:00
|
|
|
|
2026-01-28 12:11:40 +01:00
|
|
|
if (is_first(*token, RULE_SIMPLE_COMMAND))
|
2026-01-17 17:20:13 +01:00
|
|
|
{
|
2026-01-28 12:21:57 +01:00
|
|
|
result = parse_simple_command(ctx);
|
2026-01-17 17:20:13 +01:00
|
|
|
}
|
2026-01-28 12:11:40 +01:00
|
|
|
else if (is_first(*token, RULE_SHELL_COMMAND))
|
2026-01-17 17:20:13 +01:00
|
|
|
{
|
2026-01-29 19:47:59 +01:00
|
|
|
result = parse_shell_command(ctx);
|
2026-01-17 17:20:13 +01:00
|
|
|
}
|
2026-01-30 19:48:31 +01:00
|
|
|
// WARNING funcdec seems to require a LL(2) parser
|
|
|
|
|
else if (is_first(*token, RULE_FUNCDEC))
|
|
|
|
|
{
|
|
|
|
|
result = parse_funcdec(ctx);
|
|
|
|
|
}
|
2026-01-17 17:20:13 +01:00
|
|
|
else
|
|
|
|
|
{
|
2026-01-27 16:05:11 +01:00
|
|
|
perror("Syntax error: unexpected token");
|
2026-01-24 16:48:21 +01:00
|
|
|
return NULL;
|
2026-01-17 17:20:13 +01:00
|
|
|
}
|
2026-01-29 19:47:59 +01:00
|
|
|
|
|
|
|
|
return result;
|
2026-01-17 17:20:13 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-20 19:54:29 +01:00
|
|
|
struct ast *parse_simple_command(struct lexer_context *ctx)
|
2026-01-13 19:41:37 +01:00
|
|
|
{
|
2026-01-15 17:46:24 +01:00
|
|
|
struct list *command_elements = NULL;
|
2026-01-27 21:06:36 +01:00
|
|
|
struct list *redirections = NULL; // list of redirection ASTs
|
2026-01-29 12:38:03 +01:00
|
|
|
struct list *assignments = NULL;
|
2026-01-24 15:34:10 +01:00
|
|
|
|
2026-01-28 12:11:40 +01:00
|
|
|
bool has_prefix = false;
|
|
|
|
|
struct token *token = PEEK_TOKEN();
|
|
|
|
|
if (is_first(*token, RULE_PREFIX))
|
2026-01-17 20:15:27 +01:00
|
|
|
{
|
2026-01-28 12:11:40 +01:00
|
|
|
has_prefix = true;
|
|
|
|
|
while (is_first(*token, RULE_PREFIX))
|
|
|
|
|
{
|
2026-01-29 12:38:03 +01:00
|
|
|
struct ast *prefix = parse_prefix(ctx);
|
|
|
|
|
if (prefix == NULL)
|
2026-01-28 12:11:40 +01:00
|
|
|
{
|
2026-01-30 19:37:05 +01:00
|
|
|
return err_s_com(command_elements, redirections, assignments);
|
2026-01-28 12:11:40 +01:00
|
|
|
}
|
2026-01-29 18:46:11 +01:00
|
|
|
if (prefix->type == AST_ASSIGNMENT)
|
2026-01-29 12:38:03 +01:00
|
|
|
{
|
2026-01-29 18:21:44 +01:00
|
|
|
assignments = list_append(assignments, prefix);
|
2026-01-29 12:38:03 +01:00
|
|
|
}
|
|
|
|
|
else if (prefix->type == AST_REDIR)
|
|
|
|
|
{
|
|
|
|
|
redirections = list_append(redirections, prefix);
|
|
|
|
|
}
|
2026-01-28 12:11:40 +01:00
|
|
|
token = PEEK_TOKEN();
|
|
|
|
|
}
|
2026-01-17 20:15:27 +01:00
|
|
|
}
|
2026-01-14 20:53:47 +01:00
|
|
|
|
2026-01-28 12:21:57 +01:00
|
|
|
if (token->type != TOKEN_WORD)
|
2026-01-28 12:11:40 +01:00
|
|
|
{
|
2026-01-30 19:13:27 +01:00
|
|
|
if (!has_prefix && token->type != TOKEN_EXPORT)
|
2026-01-20 19:25:55 +01:00
|
|
|
{
|
2026-01-28 12:21:57 +01:00
|
|
|
perror("Expected a command but got a different token type");
|
2026-01-30 19:13:27 +01:00
|
|
|
return err_s_com(command_elements, redirections, assignments);
|
2026-01-20 19:25:55 +01:00
|
|
|
}
|
2026-01-30 19:13:27 +01:00
|
|
|
if (token->type == TOKEN_EXPORT)
|
2026-01-24 15:34:10 +01:00
|
|
|
{
|
2026-01-30 19:13:27 +01:00
|
|
|
struct ast *assignment_export = parse_export(ctx);
|
2026-01-30 19:37:05 +01:00
|
|
|
if (assignment_export == NULL)
|
2026-01-30 19:13:27 +01:00
|
|
|
return err_s_com(command_elements, redirections, assignments);
|
2026-01-28 12:21:57 +01:00
|
|
|
|
2026-01-30 19:13:27 +01:00
|
|
|
assignments = list_append(assignments, assignment_export);
|
2026-01-20 19:25:55 +01:00
|
|
|
}
|
2026-01-30 19:13:27 +01:00
|
|
|
}
|
|
|
|
|
else // TOKEN WORD
|
|
|
|
|
{
|
|
|
|
|
char *command = strdup(token->data);
|
|
|
|
|
command_elements = list_append(command_elements, command);
|
2026-01-24 15:34:10 +01:00
|
|
|
|
2026-01-30 19:13:27 +01:00
|
|
|
POP_TOKEN();
|
|
|
|
|
}
|
2026-01-30 19:37:05 +01:00
|
|
|
token = PEEK_TOKEN();
|
2026-01-30 19:13:27 +01:00
|
|
|
// Eventual elements
|
|
|
|
|
while (is_first(*token, RULE_ELEMENT))
|
|
|
|
|
{
|
|
|
|
|
// Get element
|
|
|
|
|
struct ast *element = parse_element(ctx);
|
|
|
|
|
if (element == NULL)
|
|
|
|
|
{
|
|
|
|
|
return err_s_com(command_elements, redirections, assignments);
|
|
|
|
|
}
|
2026-01-28 12:21:57 +01:00
|
|
|
|
2026-01-30 19:13:27 +01:00
|
|
|
// Get element type
|
|
|
|
|
if (ast_is_word(element))
|
|
|
|
|
{
|
2026-01-30 21:27:23 +01:00
|
|
|
// Extract word
|
2026-01-30 19:13:27 +01:00
|
|
|
struct ast_word *element_word = ast_get_word(element);
|
2026-01-30 21:27:23 +01:00
|
|
|
char *word = element_word->word;
|
|
|
|
|
element_word->word = NULL; // Prevents word to be freed
|
2026-01-30 19:13:27 +01:00
|
|
|
ast_free(&element);
|
|
|
|
|
command_elements = list_append(command_elements, word);
|
2026-01-28 12:21:57 +01:00
|
|
|
}
|
2026-01-30 19:13:27 +01:00
|
|
|
else if (ast_is_redir(element))
|
|
|
|
|
{
|
|
|
|
|
// append redirections to the list of redirections
|
|
|
|
|
redirections = list_append(redirections, element);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
perror("Internal error: unexpected return value from "
|
|
|
|
|
"parse_element in parse_simple_command");
|
|
|
|
|
return err_s_com(command_elements, redirections, assignments);
|
2026-01-28 12:21:57 +01:00
|
|
|
}
|
2026-01-30 19:13:27 +01:00
|
|
|
|
|
|
|
|
// Forward
|
|
|
|
|
token = PEEK_TOKEN();
|
2026-01-14 20:53:47 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-29 12:38:03 +01:00
|
|
|
struct ast *result =
|
|
|
|
|
ast_create_command(command_elements, redirections, assignments);
|
2026-01-20 19:25:55 +01:00
|
|
|
if (result == NULL)
|
|
|
|
|
{
|
2026-01-30 19:13:27 +01:00
|
|
|
return err_s_com(command_elements, redirections, assignments);
|
2026-01-20 19:25:55 +01:00
|
|
|
}
|
2026-01-14 20:53:47 +01:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-24 15:34:10 +01:00
|
|
|
struct ast *parse_element(struct lexer_context *ctx)
|
|
|
|
|
{
|
|
|
|
|
struct token *token = PEEK_TOKEN();
|
2026-01-30 16:51:10 +01:00
|
|
|
if (token->type == TOKEN_WORD || token->type == TOKEN_ASSIGNMENT_WORD)
|
2026-01-24 15:34:10 +01:00
|
|
|
{
|
2026-01-30 20:58:10 +01:00
|
|
|
POP_TOKEN();
|
|
|
|
|
|
2026-01-30 21:27:23 +01:00
|
|
|
struct ast *result = ast_create_word(token->data);
|
2026-01-30 20:58:10 +01:00
|
|
|
if (result == NULL)
|
|
|
|
|
{
|
|
|
|
|
perror("Internal error: could not create ast node (is your memory "
|
|
|
|
|
"full ?)");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
2026-01-24 15:34:10 +01:00
|
|
|
}
|
2026-01-26 19:00:20 +01:00
|
|
|
else if (token->type == TOKEN_IONUMBER || is_token_redir(token))
|
2026-01-24 15:34:10 +01:00
|
|
|
{
|
|
|
|
|
return parse_redirection(ctx);
|
|
|
|
|
}
|
2026-01-30 19:13:27 +01:00
|
|
|
else if (token->type == TOKEN_EXPORT)
|
|
|
|
|
{
|
|
|
|
|
return parse_export(ctx);
|
|
|
|
|
}
|
2026-01-24 15:34:10 +01:00
|
|
|
else
|
|
|
|
|
{
|
2026-01-27 16:05:11 +01:00
|
|
|
perror("Syntax error: unexpected token at parse_element");
|
2026-01-24 15:34:10 +01:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 19:54:29 +01:00
|
|
|
struct ast *parse_shell_command(struct lexer_context *ctx)
|
2026-01-16 20:12:29 +01:00
|
|
|
{
|
2026-01-30 19:48:31 +01:00
|
|
|
struct token *token = PEEK_TOKEN();
|
|
|
|
|
struct ast *result = NULL;
|
2026-01-16 20:12:29 +01:00
|
|
|
|
2026-01-30 19:48:31 +01:00
|
|
|
// Grouping
|
|
|
|
|
// '(' or '{'
|
|
|
|
|
if (token->type == TOKEN_LEFT_BRACKET || token->type == TOKEN_LEFT_PAREN)
|
|
|
|
|
{
|
|
|
|
|
POP_TOKEN();
|
|
|
|
|
result = parse_compound_list(ctx);
|
|
|
|
|
if (result == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
// ')' or '}'
|
|
|
|
|
token = PEEK_TOKEN();
|
|
|
|
|
if (token->type == TOKEN_LEFT_BRACKET
|
|
|
|
|
|| token->type == TOKEN_LEFT_PAREN)
|
|
|
|
|
{
|
|
|
|
|
ast_free(&result);
|
|
|
|
|
perror("Syntax error: bracket/parenthesis mismatch");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
POP_TOKEN();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
else if (is_first(*token, RULE_IF))
|
|
|
|
|
{
|
|
|
|
|
return parse_if_rule(ctx);
|
|
|
|
|
}
|
2026-01-30 20:00:44 +01:00
|
|
|
else if (is_first(*token, RULE_WHILE))
|
|
|
|
|
{
|
|
|
|
|
return parse_while(ctx);
|
|
|
|
|
}
|
|
|
|
|
else if (is_first(*token, RULE_UNTIL))
|
|
|
|
|
{
|
|
|
|
|
return parse_until(ctx);
|
|
|
|
|
}
|
|
|
|
|
// TODO for and case
|
2026-01-30 19:48:31 +01:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
perror("Syntax error: unexpected token in parse_shell_command");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2026-01-28 19:16:48 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-20 19:54:29 +01:00
|
|
|
struct ast *parse_if_rule(struct lexer_context *ctx)
|
2026-01-14 20:53:47 +01:00
|
|
|
{
|
2026-01-17 20:15:27 +01:00
|
|
|
// If keyword
|
2026-01-14 20:53:47 +01:00
|
|
|
struct token *token = POP_TOKEN();
|
|
|
|
|
if (token->type != TOKEN_IF)
|
2026-01-13 19:41:37 +01:00
|
|
|
{
|
2026-01-27 16:05:11 +01:00
|
|
|
perror("Internal error: expected a if rule but token has different "
|
2026-01-27 19:56:33 +01:00
|
|
|
"type");
|
2026-01-13 19:41:37 +01:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-17 20:15:27 +01:00
|
|
|
// Condition content
|
2026-01-20 20:06:25 +01:00
|
|
|
struct ast *condition_content = parse_compound_list(ctx);
|
2026-01-30 20:58:10 +01:00
|
|
|
if (condition_content == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
token = PEEK_TOKEN();
|
2026-01-14 20:53:47 +01:00
|
|
|
|
2026-01-17 20:15:27 +01:00
|
|
|
// Then keyword
|
2026-01-14 20:53:47 +01:00
|
|
|
if (token->type != TOKEN_THEN)
|
2026-01-13 19:41:37 +01:00
|
|
|
{
|
2026-01-30 20:58:10 +01:00
|
|
|
perror("Syntax error: Expected the 'then' keyword but token has "
|
|
|
|
|
"different type");
|
2026-01-28 19:16:48 +01:00
|
|
|
return err_if_rule(&condition_content, NULL, NULL);
|
2026-01-13 19:41:37 +01:00
|
|
|
}
|
2026-01-30 12:21:29 +01:00
|
|
|
POP_TOKEN();
|
2026-01-13 19:41:37 +01:00
|
|
|
|
2026-01-17 20:15:27 +01:00
|
|
|
// Then content
|
2026-01-20 20:06:25 +01:00
|
|
|
struct ast *then_content = parse_compound_list(ctx);
|
2026-01-17 20:15:27 +01:00
|
|
|
if (then_content == NULL)
|
|
|
|
|
{
|
2026-01-28 19:16:48 +01:00
|
|
|
return err_if_rule(&condition_content, &then_content, NULL);
|
2026-01-17 20:15:27 +01:00
|
|
|
}
|
2026-01-30 20:58:10 +01:00
|
|
|
token = PEEK_TOKEN();
|
2026-01-14 20:53:47 +01:00
|
|
|
|
2026-01-28 19:16:48 +01:00
|
|
|
struct ast *else_content = NULL;
|
2026-01-14 20:53:47 +01:00
|
|
|
// Eventual else/elif clause(s)
|
2026-01-28 19:16:48 +01:00
|
|
|
if (is_first(*token, RULE_ELSE_CLAUSE))
|
2026-01-17 20:15:27 +01:00
|
|
|
{
|
2026-01-28 19:16:48 +01:00
|
|
|
else_content = parse_else_clause(ctx);
|
|
|
|
|
if (else_content == NULL)
|
|
|
|
|
{
|
|
|
|
|
return err_if_rule(&condition_content, &then_content, NULL);
|
|
|
|
|
}
|
2026-01-30 20:58:10 +01:00
|
|
|
token = PEEK_TOKEN();
|
2026-01-17 20:15:27 +01:00
|
|
|
}
|
2026-01-14 20:53:47 +01:00
|
|
|
|
2026-01-23 17:33:15 +01:00
|
|
|
// Fi keyword
|
2026-01-14 20:53:47 +01:00
|
|
|
if (token->type != TOKEN_FI)
|
2026-01-13 19:41:37 +01:00
|
|
|
{
|
2026-01-27 16:05:11 +01:00
|
|
|
perror("Expected the 'fi' keyword but token has different type");
|
2026-01-28 19:16:48 +01:00
|
|
|
return err_if_rule(&condition_content, &then_content, &else_content);
|
2026-01-14 20:53:47 +01:00
|
|
|
}
|
2026-01-30 12:21:29 +01:00
|
|
|
POP_TOKEN();
|
2026-01-14 20:53:47 +01:00
|
|
|
|
2026-01-23 17:33:15 +01:00
|
|
|
// Result
|
2026-01-14 20:53:47 +01:00
|
|
|
struct ast *result =
|
|
|
|
|
ast_create_if(condition_content, then_content, else_content);
|
|
|
|
|
if (result == NULL)
|
|
|
|
|
{
|
2026-01-27 16:05:11 +01:00
|
|
|
perror("Internal error: could not create a new AST (AST_IF)");
|
2026-01-28 19:16:48 +01:00
|
|
|
return err_if_rule(&condition_content, &then_content, &else_content);
|
2026-01-13 19:41:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 19:54:29 +01:00
|
|
|
struct ast *parse_compound_list(struct lexer_context *ctx)
|
2026-01-13 19:41:37 +01:00
|
|
|
{
|
2026-01-14 20:53:47 +01:00
|
|
|
struct list *result_list = NULL; // ast* list
|
2026-01-17 20:15:27 +01:00
|
|
|
struct ast *current_cmd = NULL;
|
2026-01-14 20:53:47 +01:00
|
|
|
struct token *token = PEEK_TOKEN();
|
|
|
|
|
|
2026-01-17 20:15:27 +01:00
|
|
|
// Skip newlines
|
2026-01-19 19:15:28 +01:00
|
|
|
while (token->type == TOKEN_NEWLINE)
|
|
|
|
|
{
|
2026-01-30 20:58:10 +01:00
|
|
|
POP_TOKEN();
|
2026-01-22 19:06:58 +01:00
|
|
|
token = PEEK_TOKEN();
|
2026-01-19 19:15:28 +01:00
|
|
|
}
|
2026-01-17 20:15:27 +01:00
|
|
|
|
2026-01-23 17:33:15 +01:00
|
|
|
// And/or
|
2026-01-20 20:06:25 +01:00
|
|
|
current_cmd = parse_and_or(ctx);
|
2026-01-17 20:15:27 +01:00
|
|
|
if (current_cmd == NULL)
|
|
|
|
|
return NULL;
|
2026-01-22 19:06:58 +01:00
|
|
|
result_list = list_append(result_list, current_cmd);
|
2026-01-30 20:58:10 +01:00
|
|
|
token = PEEK_TOKEN();
|
2026-01-19 19:15:28 +01:00
|
|
|
|
2026-01-17 20:15:27 +01:00
|
|
|
// Following commands
|
2026-01-19 19:15:28 +01:00
|
|
|
while (token->type == TOKEN_SEMICOLON || token->type == TOKEN_NEWLINE)
|
2026-01-14 20:53:47 +01:00
|
|
|
{
|
2026-01-17 20:15:27 +01:00
|
|
|
POP_TOKEN();
|
2026-01-22 19:06:58 +01:00
|
|
|
token = PEEK_TOKEN();
|
2026-01-14 20:53:47 +01:00
|
|
|
|
2026-01-17 20:15:27 +01:00
|
|
|
// Skip newlines
|
2026-01-19 19:15:28 +01:00
|
|
|
while (token->type == TOKEN_NEWLINE)
|
2026-01-14 20:53:47 +01:00
|
|
|
{
|
2026-01-30 20:58:10 +01:00
|
|
|
POP_TOKEN();
|
2026-01-22 19:06:58 +01:00
|
|
|
token = PEEK_TOKEN();
|
2026-01-14 20:53:47 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-23 17:33:15 +01:00
|
|
|
// And/or
|
2026-01-24 16:13:16 +01:00
|
|
|
if (is_first(*token, RULE_AND_OR))
|
|
|
|
|
{
|
|
|
|
|
current_cmd = parse_and_or(ctx);
|
|
|
|
|
if (current_cmd == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
result_list = list_append(result_list, current_cmd);
|
2026-01-30 20:58:10 +01:00
|
|
|
token = PEEK_TOKEN();
|
2026-01-24 16:13:16 +01:00
|
|
|
}
|
2026-01-14 20:53:47 +01:00
|
|
|
}
|
2026-01-17 19:11:55 +01:00
|
|
|
|
2026-01-30 20:58:10 +01:00
|
|
|
// Eventual semicolon
|
2026-01-17 20:15:27 +01:00
|
|
|
if (token->type == TOKEN_SEMICOLON)
|
2026-01-19 19:15:28 +01:00
|
|
|
{
|
2026-01-30 20:58:10 +01:00
|
|
|
POP_TOKEN();
|
2026-01-22 19:06:58 +01:00
|
|
|
token = PEEK_TOKEN();
|
2026-01-14 20:53:47 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-17 20:15:27 +01:00
|
|
|
// Skip newlines
|
2026-01-19 19:15:28 +01:00
|
|
|
while (token->type == TOKEN_NEWLINE)
|
|
|
|
|
{
|
2026-01-30 20:58:10 +01:00
|
|
|
POP_TOKEN();
|
2026-01-22 19:06:58 +01:00
|
|
|
token = PEEK_TOKEN();
|
2026-01-19 19:15:28 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-14 20:53:47 +01:00
|
|
|
struct ast *result = ast_create_list(result_list);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 19:54:29 +01:00
|
|
|
struct ast *parse_else_clause(struct lexer_context *ctx)
|
2026-01-14 20:53:47 +01:00
|
|
|
{
|
2026-01-15 17:29:34 +01:00
|
|
|
struct token *token = PEEK_TOKEN();
|
2026-01-14 20:53:47 +01:00
|
|
|
|
2026-01-17 17:20:13 +01:00
|
|
|
// Eventual elif content
|
|
|
|
|
while (token->type == TOKEN_ELIF)
|
|
|
|
|
{
|
|
|
|
|
// Condition
|
|
|
|
|
token = POP_TOKEN();
|
2026-01-20 20:06:25 +01:00
|
|
|
struct ast *condition = parse_compound_list(ctx);
|
2026-01-17 17:20:13 +01:00
|
|
|
|
2026-01-23 17:33:15 +01:00
|
|
|
// Then keyword
|
2026-01-17 17:20:13 +01:00
|
|
|
token = POP_TOKEN();
|
|
|
|
|
if (token->type != TOKEN_THEN)
|
|
|
|
|
{
|
2026-01-30 19:13:27 +01:00
|
|
|
perror("Expected the 'then' keyword but got a different token "
|
|
|
|
|
"type");
|
2026-01-17 17:20:13 +01:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 20:06:25 +01:00
|
|
|
struct ast *then_content = parse_compound_list(ctx);
|
2026-01-30 20:58:10 +01:00
|
|
|
if (then_content == NULL)
|
|
|
|
|
{
|
|
|
|
|
ast_free(&condition);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
token = PEEK_TOKEN();
|
2026-01-17 17:20:13 +01:00
|
|
|
|
|
|
|
|
// Eventual else clause (recursive)
|
|
|
|
|
struct ast *else_content = NULL;
|
2026-01-19 19:15:28 +01:00
|
|
|
if (token->type == TOKEN_ELSE || token->type == TOKEN_ELIF)
|
2026-01-17 17:20:13 +01:00
|
|
|
{
|
2026-01-20 20:06:25 +01:00
|
|
|
else_content = parse_else_clause(ctx);
|
2026-01-30 20:58:10 +01:00
|
|
|
if (else_content == NULL)
|
|
|
|
|
{
|
|
|
|
|
ast_free(&then_content);
|
|
|
|
|
ast_free(&condition);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
else_content = ast_create_void();
|
2026-01-17 17:20:13 +01:00
|
|
|
}
|
2026-01-14 20:53:47 +01:00
|
|
|
|
2026-01-30 20:58:10 +01:00
|
|
|
return ast_create_if(condition, then_content, else_content);
|
2026-01-17 17:20:13 +01:00
|
|
|
}
|
2026-01-14 20:53:47 +01:00
|
|
|
|
|
|
|
|
// Eventual else content
|
2026-01-17 17:20:13 +01:00
|
|
|
|
|
|
|
|
struct ast *result = NULL;
|
|
|
|
|
|
2026-01-14 20:53:47 +01:00
|
|
|
if (token->type == TOKEN_ELSE)
|
|
|
|
|
{
|
2026-01-30 20:58:10 +01:00
|
|
|
token = POP_TOKEN();
|
2026-01-20 20:06:25 +01:00
|
|
|
result = parse_compound_list(ctx);
|
2026-01-30 20:58:10 +01:00
|
|
|
if (result == NULL)
|
|
|
|
|
return NULL;
|
2026-01-14 20:53:47 +01:00
|
|
|
}
|
2026-01-30 20:58:10 +01:00
|
|
|
else
|
|
|
|
|
{
|
2026-01-20 20:32:59 +01:00
|
|
|
result = ast_create_void();
|
2026-01-30 20:58:10 +01:00
|
|
|
}
|
2026-01-14 20:53:47 +01:00
|
|
|
|
|
|
|
|
return result;
|
2026-01-13 19:41:37 +01:00
|
|
|
}
|