2026-01-17 19:11:55 +01:00
|
|
|
#define _POSIX_C_SOURCE 200809L
|
2026-01-19 19:15:28 +01:00
|
|
|
|
2026-01-14 20:53:47 +01:00
|
|
|
// === Includes
|
|
|
|
|
#include "parsing_utils.h"
|
|
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <stdio.h>
|
2026-01-17 19:11:55 +01:00
|
|
|
#include <string.h>
|
2026-01-14 20:53:47 +01:00
|
|
|
|
|
|
|
|
#include "lexer/lexer.h"
|
|
|
|
|
#include "utils/ast/ast.h"
|
|
|
|
|
|
|
|
|
|
// === Static functions
|
|
|
|
|
|
|
|
|
|
/* Returns true if c is a command terminator, false otherwise
|
2026-01-19 19:15:28 +01:00
|
|
|
*/
|
2026-01-14 20:53:47 +01:00
|
|
|
static bool isterminator(struct token *token)
|
|
|
|
|
{
|
|
|
|
|
if (token == NULL)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
switch (token->type)
|
|
|
|
|
{
|
2026-01-19 19:15:28 +01:00
|
|
|
case TOKEN_NEWLINE:
|
|
|
|
|
case TOKEN_SEMICOLON:
|
|
|
|
|
case TOKEN_EOF:
|
|
|
|
|
return true;
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
2026-01-14 20:53:47 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-16 20:12:29 +01:00
|
|
|
/* @brief: returns true if token is an end of list indicator.
|
2026-01-19 19:15:28 +01:00
|
|
|
* @warning: not used
|
2026-01-16 20:12:29 +01:00
|
|
|
*/
|
2026-01-19 19:15:28 +01:00
|
|
|
|
|
|
|
|
/*
|
2026-01-16 20:12:29 +01:00
|
|
|
static bool is_end_of_list(struct token *token)
|
|
|
|
|
{
|
|
|
|
|
if (token == NULL)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
switch (token->type)
|
|
|
|
|
{
|
2026-01-19 19:15:28 +01:00
|
|
|
case TOKEN_NEWLINE:
|
|
|
|
|
case TOKEN_EOF:
|
|
|
|
|
return true;
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
2026-01-16 20:12:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
2026-01-19 19:15:28 +01:00
|
|
|
*/
|
2026-01-16 20:12:29 +01:00
|
|
|
|
2026-01-14 20:53:47 +01:00
|
|
|
// === Functions
|
|
|
|
|
|
2026-01-17 16:40:53 +01:00
|
|
|
struct ast *parse_input(void)
|
2026-01-13 19:41:37 +01:00
|
|
|
{
|
2026-01-17 16:40:53 +01:00
|
|
|
return parse_list();
|
2026-01-14 20:53:47 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-16 20:12:29 +01:00
|
|
|
struct ast *parse_list(void)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
current_node = parse_and_or();
|
|
|
|
|
if (current_node == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
list_append(result_list, current_node);
|
|
|
|
|
|
|
|
|
|
// Following and_or commands
|
|
|
|
|
token = PEEK_TOKEN();
|
|
|
|
|
while (token->type == TOKEN_SEMICOLON)
|
2026-01-16 20:12:29 +01:00
|
|
|
{
|
2026-01-17 20:15:27 +01:00
|
|
|
token = POP_TOKEN();
|
|
|
|
|
if (!isterminator(token)) // Follow(list)
|
2026-01-16 20:12:29 +01:00
|
|
|
{
|
2026-01-17 17:20:13 +01:00
|
|
|
current_node = parse_and_or();
|
2026-01-17 20:15:27 +01:00
|
|
|
if (current_node == NULL)
|
|
|
|
|
{
|
|
|
|
|
//TODO free list
|
|
|
|
|
// There must be a function for that
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
list_append(result_list, current_node);
|
|
|
|
|
token = PEEK_TOKEN();
|
2026-01-16 20:12:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
2026-01-17 19:11:55 +01:00
|
|
|
result_list = list_append(result_list, current_node);
|
2026-01-16 20:12:29 +01:00
|
|
|
|
|
|
|
|
return ast_create_list(result_list);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-17 17:20:13 +01:00
|
|
|
struct ast *parse_and_or(void)
|
|
|
|
|
{
|
|
|
|
|
return parse_pipeline();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ast *parse_pipeline(void)
|
|
|
|
|
{
|
|
|
|
|
return parse_command();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ast *parse_command(void)
|
|
|
|
|
{
|
|
|
|
|
struct token *token = PEEK_TOKEN();
|
|
|
|
|
|
|
|
|
|
if (token->type == TOKEN_WORD)
|
|
|
|
|
{
|
|
|
|
|
return parse_simple_command();
|
|
|
|
|
}
|
|
|
|
|
else if (token->type == TOKEN_IF)
|
|
|
|
|
{
|
|
|
|
|
return parse_shell_command();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return ast_create_void(); // TODO not sure what to do
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-13 19:41:37 +01:00
|
|
|
struct ast *parse_simple_command(void)
|
|
|
|
|
{
|
2026-01-15 17:46:24 +01:00
|
|
|
struct list *command_elements = NULL;
|
2026-01-15 20:42:28 +01:00
|
|
|
struct token *token = PEEK_TOKEN();
|
2026-01-17 20:15:27 +01:00
|
|
|
if (token->type != TOKEN_WORD)
|
|
|
|
|
{
|
|
|
|
|
puts("Expected a command but got a different token type");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2026-01-14 20:53:47 +01:00
|
|
|
|
2026-01-17 16:40:53 +01:00
|
|
|
while (token->type == TOKEN_WORD)
|
2026-01-14 20:53:47 +01:00
|
|
|
{
|
|
|
|
|
token = POP_TOKEN();
|
2026-01-17 20:15:27 +01:00
|
|
|
char* word = strdup(token->data);
|
|
|
|
|
command_elements = list_append(command_elements, word);
|
2026-01-16 19:31:58 +01:00
|
|
|
token = PEEK_TOKEN();
|
2026-01-14 20:53:47 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-15 17:46:24 +01:00
|
|
|
struct ast *result = ast_create_command(command_elements);
|
2026-01-14 20:53:47 +01:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-17 17:20:13 +01:00
|
|
|
// TODO check compliance with the grammar
|
|
|
|
|
struct ast *parse_shell_command(void)
|
2026-01-16 20:12:29 +01:00
|
|
|
{
|
2026-01-17 20:15:27 +01:00
|
|
|
return parse_if_rule();
|
2026-01-16 20:12:29 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-14 20:53:47 +01:00
|
|
|
struct ast *parse_if_rule(void)
|
|
|
|
|
{
|
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-14 20:53:47 +01:00
|
|
|
puts("Internal error: expected a if rule but token has different "
|
2026-01-19 19:15:28 +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-14 20:53:47 +01:00
|
|
|
struct ast *condition_content = parse_compound_list();
|
|
|
|
|
|
2026-01-17 20:15:27 +01:00
|
|
|
// Then keyword
|
2026-01-14 20:53:47 +01:00
|
|
|
token = POP_TOKEN();
|
|
|
|
|
if (token->type != TOKEN_THEN)
|
2026-01-13 19:41:37 +01:00
|
|
|
{
|
2026-01-17 20:15:27 +01:00
|
|
|
ast_free(&condition_content);
|
2026-01-14 20:53:47 +01:00
|
|
|
puts("Expected the 'then' keyword but token has different type");
|
|
|
|
|
return NULL;
|
2026-01-13 19:41:37 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-17 20:15:27 +01:00
|
|
|
// Then content
|
2026-01-14 20:53:47 +01:00
|
|
|
struct ast *then_content = parse_compound_list();
|
2026-01-17 20:15:27 +01:00
|
|
|
if (then_content == NULL)
|
|
|
|
|
{
|
|
|
|
|
ast_free(&condition_content);
|
|
|
|
|
ast_free(&then_content);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2026-01-14 20:53:47 +01:00
|
|
|
|
|
|
|
|
// Eventual else/elif clause(s)
|
|
|
|
|
struct ast *else_content = parse_else_clause();
|
2026-01-17 20:15:27 +01:00
|
|
|
if (else_content == NULL)
|
|
|
|
|
{
|
|
|
|
|
ast_free(&condition_content);
|
|
|
|
|
ast_free(&then_content);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2026-01-14 20:53:47 +01:00
|
|
|
|
|
|
|
|
token = POP_TOKEN();
|
|
|
|
|
if (token->type != TOKEN_FI)
|
2026-01-13 19:41:37 +01:00
|
|
|
{
|
2026-01-17 20:15:27 +01:00
|
|
|
ast_free(&condition_content);
|
|
|
|
|
ast_free(&then_content);
|
|
|
|
|
ast_free(&else_content);
|
2026-01-14 20:53:47 +01:00
|
|
|
puts("Expected the 'fi' keyword but token has different type");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ast *result =
|
|
|
|
|
ast_create_if(condition_content, then_content, else_content);
|
|
|
|
|
if (result == NULL)
|
|
|
|
|
{
|
2026-01-17 20:15:27 +01:00
|
|
|
ast_free(&condition_content);
|
|
|
|
|
ast_free(&then_content);
|
|
|
|
|
ast_free(&else_content);
|
2026-01-14 20:53:47 +01:00
|
|
|
puts("Internal error: could not create a new AST (AST_IF)");
|
2026-01-13 19:41:37 +01:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-14 20:53:47 +01:00
|
|
|
struct ast *parse_compound_list(void)
|
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-17 20:15:27 +01:00
|
|
|
token = POP_TOKEN();
|
2026-01-19 19:15:28 +01:00
|
|
|
}
|
2026-01-17 20:15:27 +01:00
|
|
|
|
|
|
|
|
// and_or
|
|
|
|
|
current_cmd = parse_and_or();
|
|
|
|
|
if (current_cmd == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
list_append(result_list, current_cmd);
|
2026-01-19 19:15:28 +01:00
|
|
|
|
2026-01-17 20:15:27 +01:00
|
|
|
// Following commands
|
|
|
|
|
token = PEEK_TOKEN();
|
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-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-17 20:15:27 +01:00
|
|
|
token = POP_TOKEN();
|
2026-01-14 20:53:47 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-17 20:15:27 +01:00
|
|
|
// and_or
|
|
|
|
|
current_cmd = parse_and_or();
|
|
|
|
|
if (current_cmd == NULL)
|
2026-01-14 20:53:47 +01:00
|
|
|
return NULL;
|
2026-01-17 20:15:27 +01:00
|
|
|
list_append(result_list, current_cmd);
|
2026-01-14 20:53:47 +01:00
|
|
|
|
2026-01-17 20:15:27 +01:00
|
|
|
token = PEEK_TOKEN();
|
2026-01-14 20:53:47 +01:00
|
|
|
}
|
2026-01-17 19:11:55 +01:00
|
|
|
|
2026-01-17 20:15:27 +01:00
|
|
|
// Eventual semicolons
|
|
|
|
|
if (token->type == TOKEN_SEMICOLON)
|
2026-01-19 19:15:28 +01:00
|
|
|
{
|
2026-01-14 20:53:47 +01:00
|
|
|
token = POP_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-17 20:15:27 +01:00
|
|
|
token = POP_TOKEN();
|
2026-01-19 19:15:28 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-17 20:15:27 +01:00
|
|
|
|
2026-01-14 20:53:47 +01:00
|
|
|
struct ast *result = ast_create_list(result_list);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ast *parse_else_clause(void)
|
|
|
|
|
{
|
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();
|
|
|
|
|
struct ast *condition = parse_compound_list();
|
|
|
|
|
|
|
|
|
|
// 'then'
|
|
|
|
|
token = POP_TOKEN();
|
|
|
|
|
if (token->type != TOKEN_THEN)
|
|
|
|
|
{
|
|
|
|
|
puts("Expected the 'then' keyword but got a different token type");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Then clause
|
|
|
|
|
struct ast *then_content = parse_compound_list();
|
|
|
|
|
|
|
|
|
|
// Eventual else clause (recursive)
|
|
|
|
|
struct ast *else_content = NULL;
|
|
|
|
|
token = PEEK_TOKEN();
|
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
|
|
|
{
|
|
|
|
|
else_content = parse_else_clause();
|
|
|
|
|
}
|
2026-01-14 20:53:47 +01:00
|
|
|
|
2026-01-17 17:20:13 +01:00
|
|
|
struct ast *result =
|
|
|
|
|
ast_create_if(condition, then_content, else_content);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
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)
|
|
|
|
|
{
|
|
|
|
|
result = parse_compound_list();
|
|
|
|
|
token = POP_TOKEN(); // Forward
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result == NULL)
|
2026-01-15 17:29:34 +01:00
|
|
|
result = ast_create_void();
|
2026-01-14 20:53:47 +01:00
|
|
|
|
|
|
|
|
return result;
|
2026-01-13 19:41:37 +01:00
|
|
|
}
|