-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.h
More file actions
86 lines (65 loc) · 1.92 KB
/
parser.h
File metadata and controls
86 lines (65 loc) · 1.92 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
86
//
// Created by alex on 06.10.19.
//
#ifndef COMPILER_PARSER_H
#define COMPILER_PARSER_H
#include <vector>
#include <unordered_map>
#include <exception>
#include "tokens.h"
#include "expressionNodes.h"
typedef std::vector<Tokens*>::iterator TokensIt;
class Parser {
TokensIt currentToken;
TokensIt startToken;
TokensIt endToken;
int getNextToken() {
++currentToken;
return (*currentToken)->getType();
}
bool checkCurrentToken(int id) {
return (*currentToken)->getType() == id;
}
bool checkNextToken(int id) {
++currentToken;
return checkCurrentToken(id);
}
void expectCurrentToken(int id) {
if (!checkCurrentToken(id)) {
std::string error = "expected " + convertTokenToString(id) + ", received " + (*currentToken)->toString();
throwError(error);
}
}
void expectNextToken(int id) {
++currentToken;
expectCurrentToken(id);
}
void throwError(std::string error) {
std::cout << error << std::endl;
throw std::exception();
}
std::vector<TreeNode*> classes;
TreeNode* parseWhileStatement();
TreeNode* parseIfStatement();
TreeNode* parseAssignmentStatement();
TreeNode* parseStatement();
TreeNode* parseIdentifier();
TreeNode* parseExpression();
TreeNode* parseBody();
ParamNode* parseParameter();
std::vector<ParamNode*> parseParameters();
ClassBodyNode* parseClassBody();
TreeNode* parseClass();
public:
Parser(const TokensIt &start, const TokensIt &end) : currentToken(start), startToken(start), endToken(end) { }
std::vector<TreeNode*> parseTokens() {
while(currentToken != endToken)
classes.push_back(parseClass());
return classes;
}
void print() {
for (auto it = classes.begin(); it != classes.end(); ++it)
(*it)->print("");
}
};
#endif //COMPILER_PARSER_H