Repository navigation
Expand file tree
/
Copy pathParser.cpp
More file actions
427 lines (341 loc) · 12.2 KB
/
Copy pathParser.cpp
File metadata and controls
427 lines (341 loc) · 12.2 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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#include "Parser.h"
#include "Utils.h"
ParseError error(const Token &token, const std::string &message) {
Utils::error(token, message);
return ParseError(message);
}
Token Parser::peek() const {
return tokens[current];
}
Token Parser::previous() const {
return tokens[current - 1];
}
bool Parser::isAtEnd() const {
return peek().type == TokenType::END_OF_FILE;
}
Token Parser::advance() {
if (!isAtEnd()) current++;
return previous();
}
bool Parser::check(TokenType type) const {
if (isAtEnd()) return false;
return peek().type == type;
}
template<typename... Types> requires (std::is_same_v<Types, TokenType> && ...)
bool Parser::match(Types... types) {
return ((check(types) ? (advance(), true) : false) or ...);
}
Token Parser::consume(const TokenType type, const std::string &message) {
if (check(type)) return advance();
hadError = true;
throw error(peek(), message);
}
Parser::Parser(const std::vector<Token> &tokens) : tokens(tokens) {}
ExprPtr Parser::expression() {
return assignment();
}
ExprPtr Parser::comma() {
ExprPtr expr = assignment();
while (match(TokenType::COMMA)) {
Token op = previous();
ExprPtr rhs = assignment();
expr = std::make_unique<Expr::Binary>(std::move(expr), std::move(op), std::move(rhs));
}
return expr;
}
ExprPtr Parser::assignment() {
ExprPtr expr = logic_or();
if (match(TokenType::EQUAL)) {
const Token equals = previous();
ExprPtr value = assignment();
if (auto d = dynamic_cast<Expr::Variable*>(expr.get()); d != nullptr) {
Token name = d->name;
return std::make_unique<Expr::Assign>(name, std::move(value));
}
if (auto d = dynamic_cast<Expr::Get*>(expr.get()); d != nullptr) {
return std::make_unique<Expr::Set>(std::move(d->object), d->name, std::move(value));
}
error(equals, "Invalid assignment target.");
}
return expr;
}
ExprPtr Parser::logic_or() {
ExprPtr expr = logic_and();
while (match(TokenType::OR)) {
Token op = previous();
ExprPtr rhs = logic_and();
expr = std::make_unique<Expr::Logical>(std::move(expr), std::move(op), std::move(rhs));
}
return expr;
}
ExprPtr Parser::logic_and() {
ExprPtr expr = equality();
while (match(TokenType::AND)) {
Token op = previous();
ExprPtr rhs = equality();
expr = std::make_unique<Expr::Logical>(std::move(expr), std::move(op), std::move(rhs));
}
return expr;
}
ExprPtr Parser::equality() {
ExprPtr expr = comparison();
while (match(TokenType::BANG_EQUAL, TokenType::EQUAL_EQUAL)) {
Token op = previous();
ExprPtr rhs = comparison();
expr = std::make_unique<Expr::Binary>(std::move(expr), op, std::move(rhs));
}
return expr;
}
ExprPtr Parser::comparison() {
ExprPtr expr = term();
while (match(TokenType::GREATER, TokenType::GREATER_EQUAL, TokenType::LESS, TokenType::LESS_EQUAL)) {
Token op = previous();
ExprPtr rhs = term();
expr = std::make_unique<Expr::Binary>(std::move(expr), op, std::move(rhs));
}
return expr;
}
ExprPtr Parser::term() {
ExprPtr expr = factor();
while (match(TokenType::MINUS, TokenType::PLUS)) {
Token op = previous();
ExprPtr rhs = factor();
expr = std::make_unique<Expr::Binary>(std::move(expr), op, std::move(rhs));
}
return expr;
}
ExprPtr Parser::factor() {
ExprPtr expr = unary();
while (match(TokenType::SLASH, TokenType::STAR)) {
Token op = previous();
ExprPtr rhs = unary();
expr = std::make_unique<Expr::Binary>(std::move(expr), op, std::move(rhs));
}
return expr;
}
ExprPtr Parser::unary() {
if (match(TokenType::BANG, TokenType::MINUS)) {
Token op = previous();
ExprPtr rhs = unary();
return std::make_unique<Expr::Unary>(op, std::move(rhs));
}
return call();
}
ExprPtr Parser::call() {
ExprPtr expr = primary();
while (true) {
if (match(TokenType::LEFT_PAREN)) {
auto arguments = std::make_unique<std::vector<ExprPtr>>();
if (!check(TokenType::RIGHT_PAREN)) {
do {
if (arguments->size() >= 255) error(peek(), "Can't have more than 255 arguments.");
arguments->push_back(std::move(expression()));
} while (match(TokenType::COMMA));
}
Token paren = consume(TokenType::RIGHT_PAREN, "Expect ')' after arguments.");
expr = std::make_unique<Expr::Call>(std::move(expr), paren, std::move(arguments));
} else if (match(TokenType::DOT)) {
Token name = consume(TokenType::IDENTIFIER, "Expect property name after '.'.");
expr = std::make_unique<Expr::Get>(std::move(expr), name);
} else {
break;
}
}
return expr;
}
ExprPtr Parser::primary() {
if (match(TokenType::FALSE)) return std::make_unique<Expr::Literal>(std::make_shared<Object>(false));
if (match(TokenType::TRUE)) return std::make_unique<Expr::Literal>(std::make_shared<Object>(true));
if (match(TokenType::NIL)) return std::make_unique<Expr::Literal>(std::make_shared<Object>(std::monostate{}));
if (match(TokenType::NUMBER, TokenType::STRING)) {
return std::make_unique<Expr::Literal>(previous().literal);
}
if (match(TokenType::SUPER)) {
Token keyword = previous();
consume(TokenType::DOT, "Expect '.' after 'super'.");
Token method = consume(TokenType::IDENTIFIER, "Expect superclass method name.");
return std::make_unique<Expr::Super>(keyword, method);
}
if (match(TokenType::THIS)) return std::make_unique<Expr::This>(previous());
if (match(TokenType::IDENTIFIER)) return std::make_unique<Expr::Variable>(previous());
if (match(TokenType::LEFT_PAREN)) {
ExprPtr expr = expression();
consume(TokenType::RIGHT_PAREN, "Expect ')' after expression.");
return std::make_unique<Expr::Grouping>(std::move(expr));
}
throw error(peek(), "Expect expression.");
}
std::unique_ptr<std::vector<std::unique_ptr<Stmt::Stmt>>> Parser::block() {
auto statements = std::make_unique<std::vector<std::unique_ptr<Stmt::Stmt>>>();
while (!check(TokenType::RIGHT_BRACE) && !isAtEnd()) {
statements->push_back(std::move(declaration()));
}
consume(TokenType::RIGHT_BRACE, "Expect '}' after block.");
return statements;
}
StmtPtr Parser::ifStatement() {
consume(TokenType::LEFT_PAREN, "Expect '(' after if statement.");
ExprPtr condition = expression();
consume(TokenType::RIGHT_PAREN, "Expect ')' after if condition.");
StmtPtr thenBranch = statement();
StmtPtr elseBranch = nullptr;
if (match(TokenType::ELSE)) {
elseBranch = statement();
}
return std::make_unique<Stmt::If>(std::move(condition), std::move(thenBranch), std::move(elseBranch));
}
StmtPtr Parser::printStatement() {
ExprPtr expr = expression();
consume(TokenType::SEMICOLON, "Expect ';' after value.");
return std::make_unique<Stmt::Print>(std::move(expr));
}
StmtPtr Parser::returnStatement() {
Token keyword = previous();
ExprPtr value = nullptr;
if (!check(TokenType::SEMICOLON)) {
value = expression();
}
consume(TokenType::SEMICOLON, "Expect ';' after return value.");
return std::make_unique<Stmt::Return>(keyword, std::move(value));
}
StmtPtr Parser::whileStatement() {
consume(TokenType::LEFT_PAREN, "Expect '(' after 'while' statement.");
ExprPtr condition = expression();
consume(TokenType::RIGHT_PAREN, "Expect ')' after condition.");
StmtPtr body = statement();
return std::make_unique<Stmt::While>(std::move(condition), std::move(body));
}
StmtPtr Parser::forStatement() {
consume(TokenType::LEFT_PAREN, "Expect '(' after 'for'.");
StmtPtr initializer;
if (match(TokenType::SEMICOLON)) {
initializer = nullptr;
} else if (match(TokenType::VAR)) {
initializer = varDeclaration();
} else {
initializer = expressionStatement();
}
ExprPtr condition;
if (!check(TokenType::SEMICOLON)) {
condition = expression();
}
consume(TokenType::SEMICOLON, "Expect ';' after loop condition.");
ExprPtr increment;
if (!check(TokenType::RIGHT_PAREN)) {
increment = expression();
}
consume(TokenType::RIGHT_PAREN, "Expect ')' after for clauses.");
StmtPtr body = statement();
if (increment != nullptr) {
auto bodyStmts = std::make_unique<std::vector<std::unique_ptr<Stmt::Stmt>>>();
bodyStmts->push_back(std::move(body));
bodyStmts->push_back(std::make_unique<Stmt::Expression>(std::move(increment)));
body = std::make_unique<Stmt::Block>(std::move(bodyStmts));
}
if (condition == nullptr) condition = std::make_unique<Expr::Literal>(std::make_shared<Object>(true));
body = std::make_unique<Stmt::While>(std::move(condition), std::move(body));
if (initializer != nullptr) {
auto bodyStmts = std::make_unique<std::vector<std::unique_ptr<Stmt::Stmt>>>();
bodyStmts->push_back(std::move(initializer));
bodyStmts->push_back(std::move(body));
body = std::make_unique<Stmt::Block>(std::move(bodyStmts));
}
return body;
}
StmtPtr Parser::expressionStatement() {
ExprPtr expr = expression();
consume(TokenType::SEMICOLON, "Expect ';' after expression.");
return std::make_unique<Stmt::Expression>(std::move(expr));
}
std::unique_ptr<Stmt::Function> Parser::function(std::string kind, bool is_static = false) {
Token name = consume(TokenType::IDENTIFIER, "Expect " + kind + " name.");
consume(TokenType::LEFT_PAREN, "Expect '(' after " + kind + " name.");
std::vector<Token> params;
if (!check(TokenType::RIGHT_PAREN)) {
do {
if (params.size() >= 255) {
error(peek(), "Can't have more than 255 parameters.");
}
params.push_back(consume(TokenType::IDENTIFIER, "Expect parameter name."));
} while (match(TokenType::COMMA));
}
consume(TokenType::RIGHT_PAREN, "Expect ')' after parameters.");
consume(TokenType::LEFT_BRACE, "Expect '{' before " + kind + " body.");
auto body = block();
return std::make_unique<Stmt::Function>(name, params, std::move(body), is_static);
}
StmtPtr Parser::varDeclaration() {
Token name = consume(TokenType::IDENTIFIER, "Expect variable name.");
ExprPtr init = nullptr;
if (match(TokenType::EQUAL)) {
init = expression();
}
consume(TokenType::SEMICOLON, "Expect ';' after variable declaration.");
return std::make_unique<Stmt::Var>(name, std::move(init));
}
StmtPtr Parser::classDeclaration() {
Token name = consume(TokenType::IDENTIFIER, "Expect class name.");
std::unique_ptr<Expr::Variable> superclass = nullptr;
if (match(TokenType::LESS)) {
consume(TokenType::IDENTIFIER, "Expect superclass name.");
superclass = std::make_unique<Expr::Variable>(previous());
}
consume(TokenType::LEFT_BRACE, "Expect '{' before class declaration.");
auto methods = std::make_unique<std::vector<std::unique_ptr<Stmt::Function>>>();
while (!check(TokenType::RIGHT_BRACE) && !isAtEnd()) {
bool is_static = false;
if (match(TokenType::CLASS)) is_static = true;
methods->push_back(std::move(function("method", is_static)));
}
consume(TokenType::RIGHT_BRACE, "Expect '}' after class body.");
return std::make_unique<Stmt::Class>(name, std::move(superclass), std::move(methods));
}
StmtPtr Parser::statement() {
if (match(TokenType::FOR)) return forStatement();
if (match(TokenType::IF)) return ifStatement();
if (match(TokenType::PRINT)) return printStatement();
if (match(TokenType::RETURN)) return returnStatement();
if (match(TokenType::WHILE)) return whileStatement();
if (match(TokenType::LEFT_BRACE)) return std::make_unique<Stmt::Block>(block());
return expressionStatement();
}
StmtPtr Parser::declaration() {
try {
if (match(TokenType::CLASS)) return classDeclaration();
if (match(TokenType::FUN)) return function("function");
if (match(TokenType::VAR)) return varDeclaration();
return statement();
} catch (ParseError &e) {
synchronize();
return nullptr;
}
}
std::shared_ptr<std::vector<StmtPtr>> Parser::parse() {
try {
auto stmts = std::make_shared<std::vector<StmtPtr>>();
while (!isAtEnd()) {
stmts->push_back(declaration());
}
return stmts;
} catch (ParseError &e) {
return {};
}
}
void Parser::synchronize() {
advance();
while (!isAtEnd()) {
if (previous().type == TokenType::SEMICOLON) return;
switch (peek().type) {
case TokenType::CLASS:
case TokenType::FUN:
case TokenType::VAR:
case TokenType::FOR:
case TokenType::IF:
case TokenType::WHILE:
case TokenType::PRINT:
case TokenType::RETURN:
return;
default: advance();
}
}
}