42sh/src/parser/grammar_basic.h

112 lines
2.6 KiB
C

#ifndef GRAMMAR_BASIC_H
#define GRAMMAR_BASIC_H
#include "../lexer/lexer.h"
#include "../utils/ast/ast.h"
// === Functions
/*
* @brief parses a list of [and_or] rules separated by semicolons and that
* ends by a newline
*
* @code list = and_or { ';' and_or } [ ';' ] ;
*
* @first first(and_or)
*/
struct ast *parse_list(struct lexer_context *ctx);
/*
* @brief Only parses a pipeline rule for the moment
*
* @code and_or = pipeline { ( '&&' | '||' ) {'\n'} pipeline } ;
*
* @first first(pipeline)
*/
struct ast *parse_and_or(struct lexer_context *ctx);
/*
* @brief Only parses a command rule for the moment
*
* @code pipeline = ['!'] command { '|' {'\n'} command } ;
*
* @first '!', first(command)
*/
struct ast *parse_pipeline(struct lexer_context *ctx);
/*
* @brief Parses a simple command rule or a shell command rule depending on
* the first token.
* @note
* TOKEN_WORD => simple_command
* TOKEN_IF => shell_command
*
* @code command = simple_command
* | shell_command
* ;
* @first first(simple_command), first(shell_command)
*/
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 } ;
*
* @first WORD
*/
struct ast *parse_simple_command(struct lexer_context *ctx);
/*
* @brief Parses an element rule
*
* @code element = WORD
* | redirection
* ;
*
* @first WORD, first(redirection)
*/
struct ast *parse_element(struct lexer_context *ctx);
/*
* @brief Only parses if rules for the moment
*
* @code shell_command = if_rule ;
*
* @first first(if_rule)
*/
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' ;
*
* @first TOKEN_IF
*/
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'} ;
*
* @first TOKEN_NEWLINE, first(and_or)
*/
struct ast *parse_compound_list(struct lexer_context *ctx);
/*
* @brief
*
* @code else_clause = 'else' compound_list
* | 'elif' compound_list 'then' compound_list [else_clause]
* ;
*
* @first TOKEN_ELSE, TOKEN_ELIF
*/
struct ast *parse_else_clause(struct lexer_context *ctx);
#endif /* ! GRAMMAR_BASIC_H */