2026-01-23 17:01:26 +01:00
|
|
|
#ifndef GRAMMAR_BASIC_H
|
|
|
|
|
#define GRAMMAR_BASIC_H
|
2026-01-16 19:45:15 +01:00
|
|
|
|
2026-01-23 17:01:26 +01:00
|
|
|
#include "../utils/ast/ast.h"
|
2026-01-20 20:32:59 +01:00
|
|
|
#include "../lexer/lexer.h"
|
|
|
|
|
|
2026-01-23 17:01:26 +01:00
|
|
|
// === Functions
|
2026-01-17 17:20:13 +01:00
|
|
|
|
|
|
|
|
/* @brief: parses a list of [and_or] rules separated by semicolons and that
|
|
|
|
|
* ends by a newline
|
|
|
|
|
*
|
|
|
|
|
* @code list = and_or { ';' and_or } [ ';' ] ;
|
2026-01-16 20:27:57 +01:00
|
|
|
*/
|
2026-01-20 19:54:29 +01:00
|
|
|
struct ast *parse_list(struct lexer_context *ctx);
|
2026-01-16 20:27:57 +01:00
|
|
|
|
2026-01-17 16:40:53 +01:00
|
|
|
/* @brief Only parses a pipeline rule for the moment
|
|
|
|
|
*
|
2026-01-23 17:33:15 +01:00
|
|
|
* @code and_or = pipeline { ( '&&' | '||' ) {'\n'} pipeline } ;
|
2026-01-17 16:40:53 +01:00
|
|
|
*/
|
2026-01-20 19:54:29 +01:00
|
|
|
struct ast *parse_and_or(struct lexer_context *ctx);
|
2026-01-17 16:40:53 +01:00
|
|
|
|
|
|
|
|
/* @brief Only parses a command rule for the moment
|
|
|
|
|
*
|
|
|
|
|
* @code pipeline = command ;
|
|
|
|
|
*/
|
2026-01-20 19:54:29 +01:00
|
|
|
struct ast *parse_pipeline(struct lexer_context *ctx);
|
2026-01-17 16:40:53 +01:00
|
|
|
|
|
|
|
|
/* @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
|
|
|
|
|
* ;
|
|
|
|
|
*/
|
2026-01-20 19:54:29 +01:00
|
|
|
struct ast *parse_command(struct lexer_context *ctx);
|
2026-01-17 16:40:53 +01:00
|
|
|
|
2026-01-13 19:41:37 +01:00
|
|
|
/* @brief Parses a simple list of words (command and arguments)
|
2026-01-17 16:40:53 +01:00
|
|
|
* ending by a separator
|
|
|
|
|
*
|
|
|
|
|
* @code simple_command = WORD { element } ;
|
2026-01-13 19:41:37 +01:00
|
|
|
*/
|
2026-01-20 19:54:29 +01:00
|
|
|
struct ast *parse_simple_command(struct lexer_context *ctx);
|
2026-01-13 19:41:37 +01:00
|
|
|
|
2026-01-17 16:40:53 +01:00
|
|
|
/* @brief Only parses if rules for the moment
|
|
|
|
|
*
|
|
|
|
|
* @code shell_command = if_rule ;
|
2026-01-13 19:41:37 +01:00
|
|
|
*/
|
2026-01-20 19:54:29 +01:00
|
|
|
struct ast *parse_shell_command(struct lexer_context *ctx);
|
2026-01-17 16:40:53 +01:00
|
|
|
|
|
|
|
|
/* @brief Parses a if rule (condition, then-clause, elif-clause, else-clause)
|
|
|
|
|
*
|
|
|
|
|
* @code if_rule = 'if' compound_list 'then' compound_list [else_clause] 'fi' ;
|
2026-01-13 19:41:37 +01:00
|
|
|
*/
|
2026-01-20 19:54:29 +01:00
|
|
|
struct ast *parse_if_rule(struct lexer_context *ctx);
|
2026-01-17 16:40:53 +01:00
|
|
|
|
2026-01-14 20:53:47 +01:00
|
|
|
/* @brief parses commands inside if/else clauses and returns the corresponding
|
|
|
|
|
* AST list
|
2026-01-17 16:40:53 +01:00
|
|
|
*
|
2026-01-20 19:54:29 +01:00
|
|
|
* @code compound_list = {'\n'} and_or { ( ';' | '\n' ) {'\n'} and_or } [';']
|
|
|
|
|
* {'\n'} ;
|
2026-01-13 19:41:37 +01:00
|
|
|
*/
|
2026-01-20 19:54:29 +01:00
|
|
|
struct ast *parse_compound_list(struct lexer_context *ctx);
|
2026-01-13 19:41:37 +01:00
|
|
|
|
2026-01-17 16:40:53 +01:00
|
|
|
/* @brief
|
|
|
|
|
*
|
|
|
|
|
* @code else_clause = 'else' compound_list
|
|
|
|
|
* | 'elif' compound_list 'then' compound_list [else_clause]
|
|
|
|
|
* ;
|
2026-01-13 19:41:37 +01:00
|
|
|
*/
|
2026-01-20 19:54:29 +01:00
|
|
|
struct ast *parse_else_clause(struct lexer_context *ctx);
|
2026-01-16 19:45:15 +01:00
|
|
|
|
2026-01-23 17:01:26 +01:00
|
|
|
#endif /* ! GRAMMAR_BASIC_H */
|