forked from adnanis78612/mana-script
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.hpp
More file actions
85 lines (72 loc) · 2.09 KB
/
parser.hpp
File metadata and controls
85 lines (72 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Adding parser.hpp from Ayush-Debnath
#ifndef MANASCRIPT_PARSER_HPP
#define MANASCRIPT_PARSER_HPP
#include "token.hpp"
#include "ast.hpp"
#include "error.hpp"
#include <vector>
#include <memory>
#include <stdexcept>
#include <functional>
namespace mana {
/**
* @brief Exception thrown by the parser when a syntax error is encountered
*/
class ParseError : public std::runtime_error {
public:
ParseError(const std::string& message) : std::runtime_error(message) {}
};
/**
* @brief Recursive Descent Parser for Manascript
*/
class Parser {
private:
std::vector<Token> tokens;
int current = 0;
int max_params = 255; // Maximum number of parameters in a function
std::string filename;
// Helper methods
bool isAtEnd() const;
Token peek() const;
Token previous() const;
Token advance();
bool check(TokenType type) const;
bool match(TokenType type);
bool match(std::initializer_list<TokenType> types);
// Error handling
ParseError error(const Token& token, const std::string& message);
Token consume(TokenType type, const std::string& message);
void synchronize();
// Recursive descent parsing methods
ExprPtr expression();
ExprPtr assignment();
ExprPtr logicalOr();
ExprPtr logicalAnd();
ExprPtr equality();
ExprPtr comparison();
ExprPtr term();
ExprPtr factor();
ExprPtr unary();
ExprPtr call();
ExprPtr primary();
StmtPtr declaration();
StmtPtr varDeclaration(bool is_const = false);
StmtPtr functionDeclaration();
StmtPtr statement();
StmtPtr expressionStatement();
StmtPtr ifStatement();
StmtPtr whileStatement();
StmtPtr returnStatement();
StmtPtr blockStatement();
// Parsing utilities
ExprPtr finishCall(ExprPtr callee);
public:
Parser(const std::vector<Token>& tokens, const std::string& filename = "");
/**
* @brief Parse the tokens into an AST
* @return Vector of statements
*/
std::vector<StmtPtr> parse();
};
} // namespace mana
#endif // MANASCRIPT_PARSER_HPP// Adding parser.hpp from Ayush-Debnath