-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.h
197 lines (161 loc) · 5.86 KB
/
Parser.h
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#pragma once
#include "CommonTypes.h"
#include "Token.h"
#include "ASTNode.h"
#include <cassert>
#include <algorithm>
#define TRY_PARSE(AST_NODE) if (auto node = Parse##AST_NODE()) return node
class Parser {
public:
explicit Parser(TokenList tokens)
: m_Tokens(std::move(tokens))
{}
bool Consume(TokenType type) {
if (m_Tokens[m_Current].type == type) {
++m_Current;
return true;
}
return false;
}
[[nodiscard]] const Token& GetCurrentToken() const {
return m_Tokens[m_Current];
}
[[nodiscard]] const Token& GetPrevToken() const {
assert(m_Current > 0);
return m_Tokens[m_Current - 1];
}
ASTNodeRef ParseExpression() {
if (Consume(TokenType::INT_LITERAL)) {
int initialValue = std::get<int>(GetPrevToken().data);
return MakeShared<IntegerLiteralExpression>(initialValue);
} else if (Consume(TokenType::IDENTIFIER)) {
String initialValue = std::get<String>(GetPrevToken().data);
return MakeShared<IdentifierExpression>(initialValue);
}
return nullptr;
}
bool ConsumeOneOf(const Vector<TokenType>& tokens) {
return std::any_of(tokens.begin(), tokens.end(), [this](TokenType token) {
return Consume(token);
});
}
ASTNodeRef ParseAdditiveExpression() {
auto left = ParseMultiplicativeExpression();
while (ConsumeOneOf({ TokenType::PLUS, TokenType::MINUS })) {
TokenType op = GetPrevToken().type;
if (auto right = ParseMultiplicativeExpression()) {
left = MakeShared<BinaryExpression>(op, left, right);
} else {
return nullptr;
}
}
return left;
}
ASTNodeRef ParseMultiplicativeExpression() {
ASTNodeRef left = ParseExpression();
while (ConsumeOneOf({ TokenType::STAR, TokenType::SLASH })) {
TokenType op = GetPrevToken().type;
if (auto right = ParseExpression()) {
left = MakeShared<BinaryExpression>(op, left, right);
} else {
return nullptr;
}
}
return left;
}
ASTNodeRef ParseAssignmentExpression() {
TRY_PARSE(AdditiveExpression);
return nullptr;
}
ASTNodeRef ParseVariableDeclaration() {
if (Consume(TokenType::LET) && Consume(TokenType::IDENTIFIER)) {
auto identifierName = std::get<String>(GetPrevToken().data);
if (Consume(TokenType::EQUALS)) {
if (auto initialValueExpr = ParseAdditiveExpression()) {
if (Consume(TokenType::SEMI_COLON)) {
return MakeShared<VariableDeclaration>(identifierName, initialValueExpr);
}
}
}
}
return nullptr;
}
bool ParseParameter(Vector<FuncParam>& params) {
while (Consume(TokenType::IDENTIFIER)) {
String paramName = std::get<String>(GetPrevToken().data);
if (Consume(TokenType::COLON) && Consume(TokenType::IDENTIFIER)) {
String paramType = std::get<String>(GetPrevToken().data);
params.push_back(FuncParam{ std::move(paramName), std::move(paramType) });
return true;
}
}
return false;
}
ASTNodeRef ParseFunctionDeclaration() {
auto ParseParameterList = [&](Vector<FuncParam>& params) -> bool {
if (Consume(TokenType::LPAREN)) {
while (ParseParameter(params)) {
if (!Consume(TokenType::COMMA)) {
break;
}
}
if (Consume(TokenType::RPAREN)) {
return true;
}
}
return false;
};
auto ParseBody = [&]() -> ASTNodeRef {
if (Consume(TokenType::LCURLY)) {
if (auto statements = ParseTopStatements()) {
if (Consume(TokenType::RCURLY)) {
return statements;
}
}
}
return nullptr;
};
if (Consume(TokenType::FUNCTION) && Consume(TokenType::IDENTIFIER)) {
String functionIdent = std::get<String>(GetPrevToken().data);
Vector<FuncParam> params;
if (ParseParameterList(params)) {
if (Consume(TokenType::ARROW) && Consume(TokenType::IDENTIFIER)) {
String returnType = std::get<String>(GetPrevToken().data);
if (auto body = ParseBody()) {
return MakeShared<FunctionDeclaration>(functionIdent, params, returnType, body);
}
}
}
}
return nullptr;
}
ASTNodeRef ParseTopStatement() {
TRY_PARSE(FunctionDeclaration);
TRY_PARSE(VariableDeclaration);
return nullptr;
}
ASTNodeRef ParseTopStatements() {
Vector<ASTNodeRef> statements;
while (auto statement = ParseTopStatement()) {
statements.push_back(std::move(statement));
}
return MakeShared<TopStatements>(statements);
}
static ASTNodeRef Parse(const TokenList& tokens) {
Parser parser(tokens);
auto statements = parser.ParseTopStatements();
const Token& currentToken = parser.GetCurrentToken();
if (currentToken.type != TokenType::END_OF_FILE) {
std::cout << "Unexpected token: " << GetTokenName(currentToken.type);
std::cout << " (" << currentToken.location.line << ":" << currentToken.location.column << ")";
std::cout << std::endl;
}
return statements;
}
private:
TokenList m_Tokens;
int m_Current = 0;
};
static ASTNodeRef Parse(const TokenList& tokens) {
return Parser::Parse(tokens);
}