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