summaryrefslogtreecommitdiff
path: root/42sh/src/parser/parser_utils.h
diff options
context:
space:
mode:
Diffstat (limited to '42sh/src/parser/parser_utils.h')
-rw-r--r--42sh/src/parser/parser_utils.h75
1 files changed, 75 insertions, 0 deletions
diff --git a/42sh/src/parser/parser_utils.h b/42sh/src/parser/parser_utils.h
new file mode 100644
index 0000000..142913c
--- /dev/null
+++ b/42sh/src/parser/parser_utils.h
@@ -0,0 +1,75 @@
+#ifndef PARSER_UTILS_H
+#define PARSER_UTILS_H
+
+#include "ast/ast.h"
+#include "ast/ast_accessors.h"
+#include "lexer/lexer.h"
+
+#define CMDSIZE 10
+
+enum parser_state
+{
+ OK,
+ ERROR,
+};
+
+/**
+ * @brief Method used by error_check() but made available to the developper
+ * to ease the error management if an error where to occur. It sets state
+ * to the `ERROR` value and frees the ast if it is not NULL.
+ * @param root The root of the ast that will be freed.
+ * @param state The current state of the lexer that will be changed to `ERROR`.
+ * @note Because giving NULL for the root argument will not free anything,
+ * it can be used to set the state variable to the `ERROR` status.
+ */
+void cleanup(struct ast *root, enum parser_state *state);
+
+/**
+ * @brief This method will return an integer a nonzero value if an error was
+ * detected. If so, cleanup() will be called. An error is detected if the state
+ * argument is set to `ERROR` or if the token next is of type `TOKEN_ERROR`.
+ * @param root The root of the ast to be freed if there is an error.
+ * @param state The current state of the parser. Set to `ERROR` if needed.
+ * @param next The next token given by the lexer.
+ * @note This function allows for quick and efficient error checks:
+ * if (error_check(root, state, next))
+ * {
+ * return NULL;
+ * }
+ */
+int error_check(struct ast *root, enum parser_state *state, struct token next);
+
+/**
+ * @brief This creates a node as an ast_assign and fills it with the variable
+ * next and its value found within said token. Data in the token needs to be
+ * parsed before doing anything
+ * @param next The token containing the value to fill in the ast
+ */
+struct ast *assign_setup(struct token next);
+
+/**
+ * @brief This function returns the number of elements in an ast_list
+ * @param lst The ast_list we want to know how much childrens it has
+ * @note Tre result is undefined if the ast type is not AST_LIST
+ */
+size_t list_length(struct ast *lst);
+
+/**
+ * @brief This function will fill the token with the appropriate string
+ * when encountering a reserved word that we want to use as a word
+ * @param next the token to fill
+ * @note NULL is set in the token if string cannot be created
+ */
+void litteral_reserved_word(struct token *next);
+
+/**
+ * @brief This function will pop all the token with the same type as the next
+ * parameter. This is useful because a lot of grammars can have consecutive
+ * tokens of the same type you need to get rid of. Stops when encountering
+ * another type of token on the lexer
+ * @param type The type we want to get rid of
+ * @param lexer The lexer that will give us the tokens
+ * @note the function won't do anything if it cannot find the matching type
+ */
+void clean_cons_tokens(enum token_type type, struct lexer *lexer);
+#endif /* ! PARSER_UTILS_H */