88 lines
1.8 KiB
C++
88 lines
1.8 KiB
C++
#ifndef DEF_PARSER_H
|
|
#define DEF_PARSER_H
|
|
|
|
#include <vector>
|
|
#include <variant>
|
|
#include <stdexcept>
|
|
#include "tokenize.h"
|
|
using namespace std;
|
|
|
|
/**
|
|
* Utilisé pour revenir en arrière quand quelque chose n'est pas reconnu
|
|
*/
|
|
class ParseException : public std::exception {
|
|
const char* what() const noexcept override {
|
|
return "Parse Exception";
|
|
}
|
|
};
|
|
|
|
class SyntaxError : public runtime_error {
|
|
public:
|
|
explicit SyntaxError(const string& message, CodePosition pos)
|
|
: runtime_error(message), pos(pos) {}
|
|
|
|
const CodePosition pos;
|
|
};
|
|
|
|
/**
|
|
* Parse a list of tokens and return the associated AST
|
|
*/
|
|
Node parse(vector<Token> tokens);
|
|
|
|
/**
|
|
* Parse something derivated from Prog
|
|
*/
|
|
ParseReturn parse_prog(vector<Token> tokens);
|
|
|
|
/**
|
|
* 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);
|
|
|
|
/**
|
|
* Prints a tree for debugging it
|
|
*/
|
|
void _debug_print_tree(const Node& node, int depth = 0, const string& prefix = "");
|
|
|
|
/**
|
|
* Returns the CodePosition of a node
|
|
*/
|
|
CodePosition get_node_pos(Node node);
|
|
|
|
#endif |