42sh/src/parser/grammar_basic.c
2026-01-29 09:59:16 +01:00

481 lines
12 KiB
C

#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"
// === Static functions
static enum ast_and_or_type and_or_tok_to_ast(enum token_type tok_type)
{
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;
}
}
// === Functions
struct ast *parse_list(struct lexer_context *ctx)
{
struct list *result_list = NULL;
struct ast *current_node = NULL;
struct token *token = PEEK_TOKEN();
// and_or
current_node = parse_and_or(ctx);
if (current_node == NULL)
return NULL;
result_list = list_append(result_list, current_node);
// Following and_or commands
token = PEEK_TOKEN();
while (token->type == TOKEN_SEMICOLON)
{
token = POP_TOKEN();
token = PEEK_TOKEN();
if (is_first(*token, RULE_AND_OR))
{
current_node = parse_and_or(ctx);
if (current_node == NULL)
{
struct ast *tmp = ast_create_list(result_list);
ast_free(&tmp);
return NULL;
}
result_list = list_append(result_list, current_node);
token = PEEK_TOKEN();
}
}
return ast_create_list(result_list);
}
struct ast *parse_and_or(struct lexer_context *ctx)
{
struct ast *result = parse_pipeline(ctx);
if (result == NULL)
return NULL;
struct token *token = PEEK_TOKEN();
while (token->type == TOKEN_AND || token->type == TOKEN_OR)
{
// Set left part
struct ast *left = result;
// eat and_or token
token = POP_TOKEN();
// Set type
enum ast_and_or_type type = and_or_tok_to_ast(token->type);
token = PEEK_TOKEN();
// Skip newlines
while (token->type == TOKEN_NEWLINE)
{
token = POP_TOKEN();
token = PEEK_TOKEN();
}
// Right part
struct ast *right = parse_pipeline(ctx);
result = ast_create_and_or(left, right, type);
if (result == NULL)
{
ast_free(&left);
ast_free(&right);
return NULL;
}
}
return result;
}
struct ast *parse_pipeline(struct lexer_context *ctx)
{
bool negation = false;
struct token *token = PEEK_TOKEN();
// Eventual '!'
if (token->type == TOKEN_NEGATION)
{
negation = true;
POP_TOKEN();
token = PEEK_TOKEN();
}
struct ast *left = parse_command(ctx);
if (negation)
{
left = ast_create_neg(negation, left);
}
token = PEEK_TOKEN();
while (token->type == TOKEN_PIPE)
{
POP_TOKEN();
// skip newlines
token = PEEK_TOKEN();
while (token->type == TOKEN_NEWLINE)
{
POP_TOKEN();
token = PEEK_TOKEN();
}
struct ast *right = parse_command(ctx);
// Create AST
left = ast_create_pipe(left, right);
token = PEEK_TOKEN();
}
return left;
}
struct ast *parse_command(struct lexer_context *ctx)
{
struct token *token = PEEK_TOKEN();
struct ast *result = NULL;
if (is_first(*token, RULE_SIMPLE_COMMAND))
{
result = parse_simple_command(ctx);
}
else if (is_first(*token, RULE_SHELL_COMMAND))
{
parse_shell_command(ctx);
}
else
{
perror("Syntax error: unexpected token");
return NULL;
}
}
/* @brief: frees command_elements and redirections lists (helper func)
* @return: NULL
*/
static void *err_simple_command(struct list *command_elements,
struct list *redirections)
{
list_deep_destroy(command_elements);
list_deep_destroy(redirections);
return NULL;
}
struct ast *parse_simple_command(struct lexer_context *ctx)
{
struct list *command_elements = NULL;
struct list *redirections = NULL; // list of redirection ASTs
bool has_prefix = false;
struct token *token = PEEK_TOKEN();
if (is_first(*token, RULE_PREFIX))
{
has_prefix = true;
while (is_first(*token, RULE_PREFIX))
{
struct ast *redir = parse_prefix(ctx);
if (redir == NULL)
{
return err_simple_command(command_elements, redirections);
}
redirections = list_append(redirections, redir);
token = PEEK_TOKEN();
}
}
if (token->type != TOKEN_WORD)
{
if (!has_prefix)
{
perror("Expected a command but got a different token type");
return err_simple_command(command_elements, redirections);
}
// else : only prefixes
}
else
{
if (token->type == TOKEN_WORD)
{
char *command = strdup(token->data);
command_elements = list_append(command_elements, command);
POP_TOKEN();
token = PEEK_TOKEN();
}
// Eventual elements
while (is_first(*token, RULE_ELEMENT))
{
// Get element
struct ast *element = parse_element(ctx);
if (element == NULL)
{
return err_simple_command(command_elements, redirections);
}
// Get element type
if (ast_is_word(element))
{
struct ast_word *element_word = ast_get_word(element);
// TODO test this fix for the memory leaks
char *word = strdup(element_word->word);
ast_free(&element);
command_elements = list_append(command_elements, word);
// end of fix
}
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_simple_command(command_elements, redirections);
}
// Forward
token = PEEK_TOKEN();
}
}
// Result
// TODO handle assignements
struct ast *result = ast_create_command(command_elements, redirections);
if (result == NULL)
{
return err_simple_command(command_elements, redirections);
}
return result;
}
struct ast *parse_element(struct lexer_context *ctx)
{
struct token *token = PEEK_TOKEN();
if (token->type == TOKEN_WORD)
{
token = POP_TOKEN();
return ast_create_word(token->data);
}
else if (token->type == TOKEN_IONUMBER || is_token_redir(token))
{
return parse_redirection(ctx);
}
else
{
perror("Syntax error: unexpected token at parse_element");
return NULL;
}
}
struct ast *parse_shell_command(struct lexer_context *ctx)
{
return parse_if_rule(ctx);
}
/* @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;
}
struct ast *parse_if_rule(struct lexer_context *ctx)
{
// If keyword
struct token *token = POP_TOKEN();
if (token->type != TOKEN_IF)
{
perror("Internal error: expected a if rule but token has different "
"type");
return NULL;
}
// Condition content
struct ast *condition_content = parse_compound_list(ctx);
// Then keyword
token = POP_TOKEN();
if (token->type != TOKEN_THEN)
{
perror("Expected the 'then' keyword but token has different type");
return err_if_rule(&condition_content, NULL, NULL);
}
// Then content
struct ast *then_content = parse_compound_list(ctx);
if (then_content == NULL)
{
return err_if_rule(&condition_content, &then_content, NULL);
}
struct ast *else_content = NULL;
// Eventual else/elif clause(s)
if (is_first(*token, RULE_ELSE_CLAUSE))
{
else_content = parse_else_clause(ctx);
if (else_content == NULL)
{
return err_if_rule(&condition_content, &then_content, NULL);
}
}
// Fi keyword
token = POP_TOKEN();
if (token->type != TOKEN_FI)
{
perror("Expected the 'fi' keyword but token has different type");
return err_if_rule(&condition_content, &then_content, &else_content);
}
// Result
struct ast *result =
ast_create_if(condition_content, then_content, else_content);
if (result == NULL)
{
perror("Internal error: could not create a new AST (AST_IF)");
return err_if_rule(&condition_content, &then_content, &else_content);
}
return result;
}
struct ast *parse_compound_list(struct lexer_context *ctx)
{
struct list *result_list = NULL; // ast* list
struct ast *current_cmd = NULL;
struct token *token = PEEK_TOKEN();
// Skip newlines
while (token->type == TOKEN_NEWLINE)
{
token = POP_TOKEN();
token = PEEK_TOKEN();
}
// And/or
current_cmd = parse_and_or(ctx);
if (current_cmd == NULL)
return NULL;
result_list = list_append(result_list, current_cmd);
// Following commands
token = PEEK_TOKEN();
while (token->type == TOKEN_SEMICOLON || token->type == TOKEN_NEWLINE)
{
POP_TOKEN();
token = PEEK_TOKEN();
// Skip newlines
while (token->type == TOKEN_NEWLINE)
{
token = POP_TOKEN();
token = PEEK_TOKEN();
}
// And/or
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);
}
token = PEEK_TOKEN();
}
// Eventual semicolons
if (token->type == TOKEN_SEMICOLON)
{
token = POP_TOKEN();
token = PEEK_TOKEN();
}
// Skip newlines
while (token->type == TOKEN_NEWLINE)
{
token = POP_TOKEN();
token = PEEK_TOKEN();
}
struct ast *result = ast_create_list(result_list);
return result;
}
struct ast *parse_else_clause(struct lexer_context *ctx)
{
struct token *token = PEEK_TOKEN();
// Eventual elif content
while (token->type == TOKEN_ELIF)
{
// Condition
token = POP_TOKEN();
struct ast *condition = parse_compound_list(ctx);
// Then keyword
token = POP_TOKEN();
if (token->type != TOKEN_THEN)
{
perror(
"Expected the 'then' keyword but got a different token type");
return NULL;
}
// Then clause
struct ast *then_content = parse_compound_list(ctx);
// Eventual else clause (recursive)
struct ast *else_content = NULL;
token = PEEK_TOKEN();
if (token->type == TOKEN_ELSE || token->type == TOKEN_ELIF)
{
else_content = parse_else_clause(ctx);
}
struct ast *result =
ast_create_if(condition, then_content, else_content);
return result;
}
// Eventual else content
struct ast *result = NULL;
if (token->type == TOKEN_ELSE)
{
token = POP_TOKEN(); // eat else
result = parse_compound_list(ctx);
}
if (result == NULL)
result = ast_create_void();
return result;
}