42sh/src/parser/parsing_utils.c

272 lines
5.9 KiB
C
Raw Normal View History

// === Includes
#include "parsing_utils.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include "lexer/lexer.h"
#include "utils/ast/ast.h"
// === Static functions
/* Returns true if c is a command terminator, false otherwise
*/
static bool isterminator(struct token *token)
{
if (token == NULL)
return false;
switch (token->type)
{
case TOKEN_NEWLINE:
case TOKEN_SEMICOLON:
case TOKEN_EOF:
return true;
default:
return false;
}
}
2026-01-16 20:12:29 +01:00
/* @brief: returns true if token is an end of list indicator.
*/
static bool is_end_of_list(struct token *token)
{
if (token == NULL)
return false;
switch (token->type)
{
case TOKEN_NEWLINE:
case TOKEN_EOF:
return true;
default:
return false;
}
}
// === Functions
struct ast *parse_input(void)
{
return parse_list();
}
struct ast *parse_list(void)
{
struct list *result_list = NULL;
struct ast *current_node = NULL;
struct token *token = PEEK_TOKEN();
while (!is_end_of_list(token))
{
if (token->type == TOKEN_SEMICOLON)
{
result_list = list_append(result_list, current_node);
}
else
{
current_node = parse_and_or();
}
token = PEEK_TOKEN();
}
return ast_create_list(result_list);
}
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
}
}
struct ast *parse_simple_command(void)
{
2026-01-15 17:46:24 +01:00
struct list *command_elements = NULL;
struct token *token = PEEK_TOKEN();
while (token->type == TOKEN_WORD)
{
token = POP_TOKEN();
command_elements = list_append(command_elements, token->data);
token = PEEK_TOKEN();
}
2026-01-15 17:46:24 +01:00
struct ast *result = ast_create_command(command_elements);
return result;
}
// TODO check compliance with the grammar
struct ast *parse_shell_command(void)
2026-01-16 20:12:29 +01:00
{
struct token *token = PEEK_TOKEN();
switch (token->type)
2026-01-16 20:12:29 +01:00
{
case TOKEN_IF:
return parse_if_rule();
2026-01-16 20:12:29 +01:00
default:
puts("I think it's not implemented yet");
return NULL;
}
2026-01-16 20:12:29 +01:00
}
// TODO check compliance with the grammar
struct ast *parse_if_rule(void)
{
// If condition
struct token *token = POP_TOKEN();
if (token->type != TOKEN_IF)
{
puts("Internal error: expected a if rule but token has different "
"type");
return NULL;
}
struct ast *condition_content = parse_compound_list();
// Then content
token = POP_TOKEN();
if (token->type != TOKEN_THEN)
{
puts("Expected the 'then' keyword but token has different type");
return NULL;
}
struct ast *then_content = parse_compound_list();
// Eventual else/elif clause(s)
struct ast *else_content = parse_else_clause();
token = POP_TOKEN();
if (token->type != TOKEN_FI)
{
puts("Expected the 'fi' keyword but token has different type");
// TODO free previous asts
return NULL;
}
struct ast *result =
ast_create_if(condition_content, then_content, else_content);
if (result == NULL)
{
puts("Internal error: could not create a new AST (AST_IF)");
// TODO free previous asts
return NULL;
}
return result;
}
// TODO comply with header's grammar
struct ast *parse_compound_list(void)
{
struct list *result_list = NULL; // ast* list
2026-01-15 17:46:24 +01:00
struct list *command_elements = NULL; // token* list
struct token *token = PEEK_TOKEN();
while (token->type != TOKEN_THEN || token->type != TOKEN_ELIF
|| token->type != TOKEN_ELSE)
{
// Parse simple command
if (token->type == TOKEN_SEMICOLON || token->type == TOKEN_NEWLINE)
{
// Stage (-> next command)
2026-01-15 17:46:24 +01:00
struct ast *command = ast_create_command(command_elements);
result_list = list_append(result_list, command);
2026-01-15 17:46:24 +01:00
command_elements = NULL;
}
if (token->type == TOKEN_EOF)
{
puts("Syntax error: Unexpected end of stream"); // TODO pas très
// bien dit
return NULL;
}
2026-01-15 17:46:24 +01:00
command_elements = list_append(command_elements, token->data);
token = POP_TOKEN();
}
struct ast *result = ast_create_list(result_list);
return result;
}
struct ast *parse_else_clause(void)
{
struct token *token = PEEK_TOKEN();
// TODO handle ELIF
// 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();
if (token->type == TOKEN_ELSE || token_type == TOKEN_ELIF)
{
else_content = parse_else_clause();
}
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)
{
result = parse_compound_list();
token = POP_TOKEN(); // Forward
}
if (result == NULL)
result = ast_create_void();
return result;
}