feat: yet another new parser architecture
This commit is contained in:
parent
6dd19a75ad
commit
96ac2fea77
9 changed files with 182 additions and 128 deletions
287
src/parser/grammar_basic.c
Normal file
287
src/parser/grammar_basic.c
Normal file
|
|
@ -0,0 +1,287 @@
|
|||
#include "grammar_basic.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../lexer/lexer.h"
|
||||
#include "../utils/lists/lists.h"
|
||||
#include "grammar.h"
|
||||
|
||||
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();
|
||||
current_node = parse_and_or(ctx);
|
||||
if (current_node == NULL)
|
||||
{
|
||||
// TODO free list
|
||||
// There must be a function for that
|
||||
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)
|
||||
{
|
||||
return parse_pipeline(ctx);
|
||||
}
|
||||
|
||||
struct ast *parse_pipeline(struct lexer_context *ctx)
|
||||
{
|
||||
return parse_command(ctx);
|
||||
}
|
||||
|
||||
struct ast *parse_command(struct lexer_context *ctx)
|
||||
{
|
||||
struct token *token = PEEK_TOKEN();
|
||||
|
||||
if (token->type == TOKEN_WORD)
|
||||
{
|
||||
return parse_simple_command(ctx);
|
||||
}
|
||||
else if (token->type == TOKEN_IF)
|
||||
{
|
||||
return parse_shell_command(ctx);
|
||||
}
|
||||
else
|
||||
{
|
||||
return ast_create_void(); // TODO not sure what to do
|
||||
}
|
||||
}
|
||||
|
||||
struct ast *parse_simple_command(struct lexer_context *ctx)
|
||||
{
|
||||
struct list *command_elements = NULL;
|
||||
struct token *token = PEEK_TOKEN();
|
||||
if (token->type != TOKEN_WORD)
|
||||
{
|
||||
puts("Expected a command but got a different token type");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
while (token->type == TOKEN_WORD)
|
||||
{
|
||||
token = pop_token(ctx);
|
||||
if (token == NULL)
|
||||
{
|
||||
// TODO free
|
||||
return NULL;
|
||||
}
|
||||
char *word = strdup(token->data);
|
||||
if (word == NULL)
|
||||
{
|
||||
// TODO free
|
||||
puts("Internal error: Couldn't copy token content (is memory full "
|
||||
"?)");
|
||||
return NULL;
|
||||
}
|
||||
command_elements = list_append(command_elements, word);
|
||||
token = peek_token(ctx);
|
||||
if (token == NULL)
|
||||
{
|
||||
// TODO free
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
struct ast *result = ast_create_command(command_elements);
|
||||
if (result == NULL)
|
||||
{
|
||||
// TODO free
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
struct ast *parse_shell_command(struct lexer_context *ctx)
|
||||
{
|
||||
return parse_if_rule(ctx);
|
||||
}
|
||||
|
||||
struct ast *parse_if_rule(struct lexer_context *ctx)
|
||||
{
|
||||
// If keyword
|
||||
struct token *token = POP_TOKEN();
|
||||
if (token->type != TOKEN_IF)
|
||||
{
|
||||
puts("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)
|
||||
{
|
||||
ast_free(&condition_content);
|
||||
puts("Expected the 'then' keyword but token has different type");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Then content
|
||||
struct ast *then_content = parse_compound_list(ctx);
|
||||
if (then_content == NULL)
|
||||
{
|
||||
ast_free(&condition_content);
|
||||
ast_free(&then_content);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Eventual else/elif clause(s)
|
||||
struct ast *else_content = parse_else_clause(ctx);
|
||||
if (else_content == NULL)
|
||||
{
|
||||
ast_free(&condition_content);
|
||||
ast_free(&then_content);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
token = POP_TOKEN();
|
||||
if (token->type != TOKEN_FI)
|
||||
{
|
||||
ast_free(&condition_content);
|
||||
ast_free(&then_content);
|
||||
ast_free(&else_content);
|
||||
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)
|
||||
{
|
||||
ast_free(&condition_content);
|
||||
ast_free(&then_content);
|
||||
ast_free(&else_content);
|
||||
puts("Internal error: could not create a new AST (AST_IF)");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
// and_or
|
||||
current_cmd = parse_and_or(ctx);
|
||||
if (current_cmd == NULL)
|
||||
return NULL;
|
||||
list_append(result_list, current_cmd);
|
||||
|
||||
// Following commands
|
||||
token = PEEK_TOKEN();
|
||||
while (token->type == TOKEN_SEMICOLON || token->type == TOKEN_NEWLINE)
|
||||
{
|
||||
POP_TOKEN();
|
||||
|
||||
// Skip newlines
|
||||
while (token->type == TOKEN_NEWLINE)
|
||||
{
|
||||
token = POP_TOKEN();
|
||||
}
|
||||
|
||||
// and_or
|
||||
current_cmd = parse_and_or(ctx);
|
||||
if (current_cmd == NULL)
|
||||
return NULL;
|
||||
list_append(result_list, current_cmd);
|
||||
|
||||
token = PEEK_TOKEN();
|
||||
}
|
||||
|
||||
// Eventual semicolons
|
||||
if (token->type == TOKEN_SEMICOLON)
|
||||
{
|
||||
token = POP_TOKEN();
|
||||
}
|
||||
|
||||
// Skip newlines
|
||||
while (token->type == TOKEN_NEWLINE)
|
||||
{
|
||||
token = POP_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'
|
||||
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(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)
|
||||
{
|
||||
result = parse_compound_list(ctx);
|
||||
token = POP_TOKEN(); // Forward
|
||||
}
|
||||
|
||||
if (result == NULL)
|
||||
result = ast_create_void();
|
||||
|
||||
return result;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue