c-repl/src/include/parser.h

88 lines
1.8 KiB
C
Raw Normal View History

2023-10-27 17:07:58 +02:00
#ifndef DEF_PARSER_H
#define DEF_PARSER_H
2023-10-27 17:16:41 +02:00
#include <vector>
2023-11-10 13:42:53 +01:00
#include <variant>
2023-11-11 09:11:35 +01:00
#include <stdexcept>
2023-10-27 17:07:58 +02:00
#include "tokenize.h"
using namespace std;
2023-11-10 16:56:50 +01:00
/**
* Utilisé pour revenir en arrière quand quelque chose n'est pas reconnu
*/
2023-11-11 09:05:49 +01:00
class ParseException : public std::exception {
const char* what() const noexcept override {
return "Parse Exception";
}
};
2023-11-15 14:37:20 +01:00
class SyntaxError : public runtime_error {
2023-11-11 09:05:49 +01:00
public:
2023-11-15 14:37:20 +01:00
explicit SyntaxError(const string& message, CodePosition pos)
: runtime_error(message), pos(pos) {}
const CodePosition pos;
2023-11-11 09:05:49 +01:00
};
2023-11-10 16:56:50 +01:00
/**
* Parse a list of tokens and return the associated AST
*/
Node parse(vector<Token> tokens);
2023-11-16 14:09:32 +01:00
/**
* Parse something derivated from Prog
*/
ParseReturn parse_prog(vector<Token> tokens);
2023-11-10 16:56:50 +01:00
/**
* Parse something derivated from Instruction
*/
ParseReturn parse_instruction(vector<Token> tokens);
/**
* Parse something derivated from Statement
*/
ParseReturn parse_statement(vector<Token> tokens);
/**
* Parse something derivated from ExprStatement
*/
ParseReturn parse_expr_statement(vector<Token> tokens);
/**
* Parse something derivated from Expr
*/
ParseReturn parse_expr(vector<Token> tokens);
/**
* Parse something derivated from T
*/
ParseReturn parse_t(vector<Token> tokens);
/**
* Parse something derivated from U
*/
ParseReturn parse_u(vector<Token> tokens);
/**
* Parse something derivated from F
*/
ParseReturn parse_f(vector<Token> tokens);
/**
* Parse something derivated from ParIdentifier
* (An identifier with 0+ parentheses around it)
*/
ParseReturn parse_par_identifier(vector<Token> tokens);
2023-11-10 19:04:24 +01:00
/**
* Prints a tree for debugging it
*/
void _debug_print_tree(const Node& node, int depth = 0, const string& prefix = "");
2023-11-10 19:04:24 +01:00
2023-11-15 14:59:28 +01:00
/**
* Returns the CodePosition of a node
*/
CodePosition get_node_pos(Node node);
2023-10-27 17:07:58 +02:00
#endif