diff options
| author | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:07:58 +0200 |
|---|---|---|
| committer | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:07:58 +0200 |
| commit | 967be9e750221ab2ab783f95df79bb26d290a45e (patch) | |
| tree | 6802900a5e975f9f68b169f0f503f040056d6952 /42sh/src/parser/parser_functions.h | |
Diffstat (limited to '42sh/src/parser/parser_functions.h')
| -rw-r--r-- | 42sh/src/parser/parser_functions.h | 198 |
1 files changed, 198 insertions, 0 deletions
diff --git a/42sh/src/parser/parser_functions.h b/42sh/src/parser/parser_functions.h new file mode 100644 index 0000000..bc87cf6 --- /dev/null +++ b/42sh/src/parser/parser_functions.h @@ -0,0 +1,198 @@ +#ifndef PARSER_FUNCTIONS_H +#define PARSER_FUNCTIONS_H + +#include "ast/ast.h" +#include "lexer/lexer.h" +#include "parser/parser_utils.h" + +#define QUICK_CLEANUP \ + cleanup(NULL, state); \ + return NULL; + +#define TST_REDIR next.type == TOKEN_IONUMBER || next.type == TOKEN_REDIR + +/** + * @brief This function parses the list grammar + * @param lexer The lexer used to parse the current expression. + * @param state Current state of the parser. Set to `ERROR` when needed. + * @note Here is the Extended Backus-Naur form: + * + * `list = and_or { ';' and_or } [ ';' ] ;` + */ +struct ast *parse_list(struct lexer *lexer, enum parser_state *state); + +/** + * @brief This function parses the and_or grammar + * @param lexer The lexer used to parse the current expression. + * @param state Current state of the parser. Set to `ERROR` when needed. + * @note Here is the Extended Backus-Naur form: + * + * `and_or = pipeline { ( '&&' | '||' ) {'\\n'} pipeline } ;` + */ +struct ast *parse_and_or(struct lexer *lexer, enum parser_state *state); + +/** + * @brief This function parses the pipeline grammar + * @param lexer The lexer used to parse the current expression. + * @param state Current state of the parser. Set to `ERROR` when needed. + * @note Here is the Extended Backus-Naur form: + * + * `pipeline = ['!'] command { '|' {'\\n'} command } ;` + */ +struct ast *parse_pipeline(struct lexer *lexer, enum parser_state *state); + +/** + * @brief This function parses the command grammar + * @param lexer The lexer used to parse the current expression. + * @param state Current state of the parser. Set to `ERROR` when needed. + * @note Here is the Extended Backus-Naur form: + * + * `command = simple_command + * | shell command { redirection } + * | fundec { redirection } + * ;` + */ +struct ast *parse_command(struct lexer *lexer, enum parser_state *state); + +/** + * @brief This function parses the simple_command grammar + * @param lexer The lexer used to parse the current expression. + * @param state Current state of the parser. Set to `ERROR` when needed. + * @note Here is the Extended Backus-Naur form: + * + * `simple_command = prefix { prefix } + * | { prefix } WORD { element } + * ;` + */ +struct ast *parse_simple_command(struct lexer *lexer, enum parser_state *state); + +/** + * @brief This function parses the element grammar + * @param lexer The lexer used to parse the current expression. + * @param state Current state of the parser. Set to `ERROR` when needed. + * @note Here is the Extended Backus-Naur form (different from subject): + * + * `element = { (WORD | redirection) };` + */ +struct ast *parse_element(struct lexer *lexer, enum parser_state *state, + struct ast *lst, struct ast *up); + +/** + * @brief This function parses the shell_command grammar + * @param lexer The lexer used to parse the current expression. + * @param state Current state of the parser. Set to `ERROR` when needed. + * @note Here is the Extended Backus-Naur form: + * + * `shell_command = rule_if + * | rule_while + * | rule_until + * | rule_for + * | '{' compound_list '}' + * | '(' compound_list ')' + * ;` + */ +struct ast *parse_shell_command(struct lexer *lexer, enum parser_state *state); + +/** + * @brief This function parses the rule_if grammar + * @param lexer The lexer used to parse the current expression. + * @param state Current state of the parser. Set to `ERROR` when needed. + * @note Here is the Extended Backus-Naur form: + * + * `rule_if = 'if' compound_list 'then' compound_list [else_clause] 'fi' ;` + */ +struct ast *parse_rule_if(struct lexer *lexer, enum parser_state *state); + +/** + * @brief This function parses the else_clause grammar + * @param lexer The lexer used to parse the current expression. + * @param state Current state of the parser. Set to `ERROR` when needed. + * @note Here is the Extended Backus-Naur form: + * + * `else_clause = 'else' compound_list + * | 'elif' compound_list 'then' compound_list [else_clause]` + */ +struct ast *parse_else_clause(struct lexer *lexer, enum parser_state *state); + +/** + * @brief This function parses the rule_while grammar + * @param lexer The lexer used to parse the current expression. + * @param state Current state of the parser. Set to `ERROR` when needed. + * @note Here is the Extended Backus-Naur form: + * + * `rule_while = 'while' compound_list 'do' compound_list 'done' ;` + */ +struct ast *parse_rule_while(struct lexer *lexer, enum parser_state *state); + +/** + * @brief This function parses the rule_until grammar + * @param lexer The lexer used to parse the current expression. + * @param state Current state of the parser. Set to `ERROR` when needed. + * @note Here is the Extended Backus-Naur form: + * + * `rule_until = 'until' compound_list 'do' compound_list 'done' ;` + */ +struct ast *parse_rule_until(struct lexer *lexer, enum parser_state *state); + +/** + * @brief This function parses the rule_for grammar + * @param lexer The lexer used to parse the current expression. + * @param state Current state of the parser. Set to `ERROR` when needed. + * @note Here is the Extended Backus-Naur form: + * + * `rule_for = 'for' WORD ( [';'] | [ {'\\n'} 'in' { WORD } ( ';' | '\\n' ) ] ) + * {'\\n'} 'do' compound_list 'done' + * ;` + */ +struct ast *parse_rule_for(struct lexer *lexer, enum parser_state *state); + +/** + * @brief This function parses the prefix grammar + * @param lexer The lexer used to parse the current expression. + * @param state Current state of the parser. Set to `ERROR` when needed. + * @note Here is the Extended Backus-Naur form: + * + * `prefix = ASSIGNMENT_WORD + * | redirection + * ;` + */ +struct ast *parse_prefix(struct lexer *lexer, enum parser_state *state); + +/** + * @brief This function parses the redirection grammar + * @param lexer The lexer used to parse the current expression. + * @param state Current state of the parser. Set to `ERROR` when needed. + * @note Here is the Extended Backus-Naur form: + * + * `redirection = [IONUMBER] ( '>' | '<' | '>>' | '>&' | '<&' | '>|' | '<>' ) + * WORD + * ;` + */ +struct ast *parse_redirection(struct lexer *lexer, enum parser_state *state); + +/** + * @brief This function parses the compound_list grammar + * @param lexer The lexer used to parse the current expression. + * @param state Current state of the parser. Set to `ERROR` when needed. + * @note Here is the Extended Backus-Naur form: + * + * `compound_list = { '\\n' } and_or { ( ';' | '\\n' ) { '\\n' } and_or } [ ';' + * ] { '\\n' } ;` + */ +struct ast *parse_compound_list(struct lexer *lexer, enum parser_state *state); + +/** + * @brief This function parses the funcdec grammar + * @param lexer The lexer used to parse the current expression. + * @param state Current state of the parser. Set to `ERROR` when needed. + * @param txt The string to use as the name of the function. NULL means it must + * be fetched from the lexer. + * @note This change in declaration is to accomodate for the way we manage the + * conflict between the simple_command and the fundec rules (both start w/ WORD) + * @note Here is the Extended Backus-Naur form: + * + * funcdec = WORD '(' ')' {'\n'} shell_command ; + */ +struct ast *parse_funcdec(struct lexer *lexer, enum parser_state *state, + struct string *txt); +#endif /* ! PARSER_FUNCTIONS_H */ |
