Merge branch 'parser' into dev
This commit is contained in:
commit
a1a8cffbce
4 changed files with 68 additions and 49 deletions
|
|
@ -17,22 +17,23 @@
|
|||
|
||||
struct ast *get_ast()
|
||||
{
|
||||
struct lexer_context *ctx = calloc(1, sizeof(struct lexer_context));
|
||||
struct token *token = PEEK_TOKEN();
|
||||
struct ast *res;
|
||||
|
||||
if (token->type == TOKEN_EOF)
|
||||
{
|
||||
token = pop_token();
|
||||
token = pop_token(ctx);
|
||||
return ast_create_end();
|
||||
}
|
||||
else if (token->type == TOKEN_NEWLINE)
|
||||
{
|
||||
token = pop_token();
|
||||
token = pop_token(ctx);
|
||||
return ast_create_void();
|
||||
}
|
||||
else // TOKEN WORD
|
||||
{
|
||||
res = parse_list();
|
||||
res = parse_list(ctx);
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -43,6 +44,7 @@ struct ast *get_ast()
|
|||
return NULL;
|
||||
}
|
||||
*/
|
||||
destroy_lexer_context(&ctx);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@
|
|||
*
|
||||
* @warning NOT IMPLEMENTED
|
||||
*/
|
||||
struct ast *get_ast();
|
||||
struct ast *get_ast(void);
|
||||
|
||||
/* @brief Builds the AST representation of the given command string.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -8,13 +8,13 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "lexer/lexer.h"
|
||||
#include "utils/ast/ast.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)
|
||||
|
|
@ -54,12 +54,12 @@ static bool is_end_of_list(struct token *token)
|
|||
|
||||
// === Functions
|
||||
|
||||
struct ast *parse_input(void)
|
||||
struct ast *parse_input(struct lexer_context *ctx)
|
||||
{
|
||||
return parse_list();
|
||||
}
|
||||
|
||||
struct ast *parse_list(void)
|
||||
struct ast *parse_list(struct lexer_context *ctx)
|
||||
{
|
||||
struct list *result_list = NULL;
|
||||
struct ast *current_node = NULL;
|
||||
|
|
@ -70,7 +70,7 @@ struct ast *parse_list(void)
|
|||
current_node = parse_and_or();
|
||||
if (current_node == NULL)
|
||||
return NULL;
|
||||
list_append(result_list, current_node);
|
||||
result_list = list_append(result_list, current_node);
|
||||
|
||||
// Following and_or commands
|
||||
token = PEEK_TOKEN();
|
||||
|
|
@ -82,11 +82,11 @@ struct ast *parse_list(void)
|
|||
current_node = parse_and_or();
|
||||
if (current_node == NULL)
|
||||
{
|
||||
//TODO free list
|
||||
// TODO free list
|
||||
// There must be a function for that
|
||||
return NULL;
|
||||
}
|
||||
list_append(result_list, current_node);
|
||||
result_list = list_append(result_list, current_node);
|
||||
token = PEEK_TOKEN();
|
||||
}
|
||||
}
|
||||
|
|
@ -95,17 +95,17 @@ struct ast *parse_list(void)
|
|||
return ast_create_list(result_list);
|
||||
}
|
||||
|
||||
struct ast *parse_and_or(void)
|
||||
struct ast *parse_and_or(struct lexer_context *ctx)
|
||||
{
|
||||
return parse_pipeline();
|
||||
}
|
||||
|
||||
struct ast *parse_pipeline(void)
|
||||
struct ast *parse_pipeline(struct lexer_context *ctx)
|
||||
{
|
||||
return parse_command();
|
||||
}
|
||||
|
||||
struct ast *parse_command(void)
|
||||
struct ast *parse_command(struct lexer_context *ctx)
|
||||
{
|
||||
struct token *token = PEEK_TOKEN();
|
||||
|
||||
|
|
@ -123,7 +123,7 @@ struct ast *parse_command(void)
|
|||
}
|
||||
}
|
||||
|
||||
struct ast *parse_simple_command(void)
|
||||
struct ast *parse_simple_command(struct lexer_context *ctx)
|
||||
{
|
||||
struct list *command_elements = NULL;
|
||||
struct token *token = PEEK_TOKEN();
|
||||
|
|
@ -135,23 +135,43 @@ struct ast *parse_simple_command(void)
|
|||
|
||||
while (token->type == TOKEN_WORD)
|
||||
{
|
||||
token = POP_TOKEN();
|
||||
char* word = strdup(token->data);
|
||||
token = pop_token();
|
||||
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();
|
||||
token = peek_token();
|
||||
if (token == NULL)
|
||||
{
|
||||
// TODO free
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
struct ast *result = ast_create_command(command_elements);
|
||||
if (result == NULL)
|
||||
{
|
||||
// TODO free
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// TODO check compliance with the grammar
|
||||
struct ast *parse_shell_command(void)
|
||||
struct ast *parse_shell_command(struct lexer_context *ctx)
|
||||
{
|
||||
return parse_if_rule();
|
||||
}
|
||||
|
||||
struct ast *parse_if_rule(void)
|
||||
struct ast *parse_if_rule(struct lexer_context *ctx)
|
||||
{
|
||||
// If keyword
|
||||
struct token *token = POP_TOKEN();
|
||||
|
|
@ -216,7 +236,7 @@ struct ast *parse_if_rule(void)
|
|||
return result;
|
||||
}
|
||||
|
||||
struct ast *parse_compound_list(void)
|
||||
struct ast *parse_compound_list(struct lexer_context *ctx)
|
||||
{
|
||||
struct list *result_list = NULL; // ast* list
|
||||
struct ast *current_cmd = NULL;
|
||||
|
|
@ -267,12 +287,11 @@ struct ast *parse_compound_list(void)
|
|||
token = POP_TOKEN();
|
||||
}
|
||||
|
||||
|
||||
struct ast *result = ast_create_list(result_list);
|
||||
return result;
|
||||
}
|
||||
|
||||
struct ast *parse_else_clause(void)
|
||||
struct ast *parse_else_clause(struct lexer_context *ctx)
|
||||
{
|
||||
struct token *token = PEEK_TOKEN();
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
// === Macros
|
||||
|
||||
#define PEEK_TOKEN() \
|
||||
peek_token(); \
|
||||
peek_token(ctx); \
|
||||
if (token == NULL) \
|
||||
{ \
|
||||
puts("Internal error: cannot get the following token"); \
|
||||
|
|
@ -12,7 +12,7 @@
|
|||
}
|
||||
|
||||
#define POP_TOKEN() \
|
||||
pop_token(); \
|
||||
pop_token(ctx); \
|
||||
if (token == NULL) \
|
||||
{ \
|
||||
puts("Internal error: cannot get the following token"); \
|
||||
|
|
@ -27,26 +27,26 @@
|
|||
* | EOF
|
||||
* ;
|
||||
*/
|
||||
struct ast* parse_input(void);
|
||||
struct ast *parse_input(struct lexer_context *ctx);
|
||||
|
||||
/* @brief: parses a list of [and_or] rules separated by semicolons and that
|
||||
* ends by a newline
|
||||
*
|
||||
* @code list = and_or { ';' and_or } [ ';' ] ;
|
||||
*/
|
||||
struct ast *parse_list(void);
|
||||
struct ast *parse_list(struct lexer_context *ctx);
|
||||
|
||||
/* @brief Only parses a pipeline rule for the moment
|
||||
*
|
||||
* @code and_or = pipeline ;
|
||||
*/
|
||||
struct ast *parse_and_or(void);
|
||||
struct ast *parse_and_or(struct lexer_context *ctx);
|
||||
|
||||
/* @brief Only parses a command rule for the moment
|
||||
*
|
||||
* @code pipeline = command ;
|
||||
*/
|
||||
struct ast* parse_pipeline(void);
|
||||
struct ast *parse_pipeline(struct lexer_context *ctx);
|
||||
|
||||
/* @brief Parses a simple command rule or a shell command rule depending on
|
||||
* the first token.
|
||||
|
|
@ -58,36 +58,34 @@ struct ast* parse_pipeline(void);
|
|||
* | shell_command
|
||||
* ;
|
||||
*/
|
||||
struct ast* parse_command(void);
|
||||
struct ast *parse_command(struct lexer_context *ctx);
|
||||
|
||||
/* @brief Parses a simple list of words (command and arguments)
|
||||
* ending by a separator
|
||||
*
|
||||
* @code simple_command = WORD { element } ;
|
||||
*/
|
||||
struct ast *parse_simple_command(void);
|
||||
|
||||
struct ast *parse_simple_command(struct lexer_context *ctx);
|
||||
|
||||
/* @brief Only parses if rules for the moment
|
||||
*
|
||||
* @code shell_command = if_rule ;
|
||||
*/
|
||||
struct ast *parse_shell_command(void);
|
||||
|
||||
struct ast *parse_shell_command(struct lexer_context *ctx);
|
||||
|
||||
/* @brief Parses a if rule (condition, then-clause, elif-clause, else-clause)
|
||||
*
|
||||
* @code if_rule = 'if' compound_list 'then' compound_list [else_clause] 'fi' ;
|
||||
*/
|
||||
struct ast *parse_if_rule(void);
|
||||
|
||||
struct ast *parse_if_rule(struct lexer_context *ctx);
|
||||
|
||||
/* @brief parses commands inside if/else clauses and returns the corresponding
|
||||
* AST list
|
||||
*
|
||||
* @code compound_list = {'\n'} and_or { ( ';' | '\n' ) {'\n'} and_or } [';'] {'\n'} ;
|
||||
* @code compound_list = {'\n'} and_or { ( ';' | '\n' ) {'\n'} and_or } [';']
|
||||
* {'\n'} ;
|
||||
*/
|
||||
struct ast* parse_compound_list(void);
|
||||
struct ast *parse_compound_list(struct lexer_context *ctx);
|
||||
|
||||
/* @brief
|
||||
*
|
||||
|
|
@ -95,6 +93,6 @@ struct ast* parse_compound_list(void);
|
|||
* | 'elif' compound_list 'then' compound_list [else_clause]
|
||||
* ;
|
||||
*/
|
||||
struct ast *parse_else_clause(void);
|
||||
struct ast *parse_else_clause(struct lexer_context *ctx);
|
||||
|
||||
#endif /* ! PARSING_UTILS_H */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue