Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(106)

Side by Side Diff: src/sksl/SkSLParser.cpp

Issue 1984363002: initial checkin of SkSL compiler (Closed) Base URL: https://skia.googlesource.com/skia@master
Patch Set: more cleanups Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "stdio.h"
9 #include "SkSLParser.h"
10 #include "SkSLToken.h"
11
12 #define register
13 #ifdef __clang__
14 #pragma clang diagnostic push
15 #pragma clang diagnostic ignored "-Wunneeded-internal-declaration"
16 #endif
17 #include "lex.sksl.c"
18 #ifdef __clang__
19 #pragma clang diagnostic pop
20 #endif
21 #undef register
22
23 #include "ast/SkSLASTBinaryExpression.h"
24 #include "ast/SkSLASTBlock.h"
25 #include "ast/SkSLASTBoolLiteral.h"
26 #include "ast/SkSLASTBreakStatement.h"
27 #include "ast/SkSLASTCallSuffix.h"
28 #include "ast/SkSLASTContinueStatement.h"
29 #include "ast/SkSLASTDiscardStatement.h"
30 #include "ast/SkSLASTDoStatement.h"
31 #include "ast/SkSLASTExpression.h"
32 #include "ast/SkSLASTExpressionStatement.h"
33 #include "ast/SkSLASTExtension.h"
34 #include "ast/SkSLASTFieldSuffix.h"
35 #include "ast/SkSLASTFloatLiteral.h"
36 #include "ast/SkSLASTForStatement.h"
37 #include "ast/SkSLASTFunction.h"
38 #include "ast/SkSLASTIdentifier.h"
39 #include "ast/SkSLASTIfStatement.h"
40 #include "ast/SkSLASTIndexSuffix.h"
41 #include "ast/SkSLASTInterfaceBlock.h"
42 #include "ast/SkSLASTIntLiteral.h"
43 #include "ast/SkSLASTParameter.h"
44 #include "ast/SkSLASTPrefixExpression.h"
45 #include "ast/SkSLASTReturnStatement.h"
46 #include "ast/SkSLASTStatement.h"
47 #include "ast/SkSLASTSuffixExpression.h"
48 #include "ast/SkSLASTTernaryExpression.h"
49 #include "ast/SkSLASTType.h"
50 #include "ast/SkSLASTVarDeclaration.h"
51 #include "ast/SkSLASTVarDeclarationStatement.h"
52 #include "ast/SkSLASTWhileStatement.h"
53 #include "ir/SkSLSymbolTable.h"
54
55 namespace SkSL {
56
57 Parser::Parser(std::string text, SymbolTable& types, ErrorReporter& errors)
58 : fPushback(Position(-1, -1), Token::INVALID_TOKEN, "")
59 , fTypes(types)
60 , fErrors(errors) {
61 sksllex_init(&fScanner);
62 fBuffer = sksl_scan_string(text.c_str(), fScanner);
63 skslset_lineno(1, fScanner);
64
65 if (false) {
66 // avoid unused warning
67 yyunput(0, nullptr, fScanner);
68 }
69 }
70
71 Parser::~Parser() {
72 sksl_delete_buffer(fBuffer, fScanner);
73 }
74
75 /* (precision | directive | declaration)* END_OF_FILE */
76 std::vector<std::unique_ptr<ASTDeclaration>> Parser::file() {
77 std::vector<std::unique_ptr<ASTDeclaration>> result;
78 for (;;) {
79 switch (this->peek().fKind) {
80 case Token::END_OF_FILE:
81 return result;
82 case Token::PRECISION:
83 this->precision();
84 break;
85 case Token::DIRECTIVE: {
86 std::unique_ptr<ASTDeclaration> decl = this->directive();
87 if (decl != nullptr) {
88 result.push_back(std::move(decl));
89 }
90 break;
91 }
92 default: {
93 std::unique_ptr<ASTDeclaration> decl = this->declaration();
94 if (decl == nullptr) {
95 continue;
96 }
97 result.push_back(std::move(decl));
98 }
99 }
100 }
101 }
102
103 Token Parser::nextToken() {
104 if (fPushback.fKind != Token::INVALID_TOKEN) {
105 Token result = fPushback;
106 fPushback.fKind = Token::INVALID_TOKEN;
107 fPushback.fText = "";
108 return result;
109 }
110 int token = sksllex(fScanner);
111 return Token(Position(skslget_lineno(fScanner), -1), (Token::Kind) token,
112 token == Token::END_OF_FILE ? "<end of file>" :
113 std::string(skslget_text(fScanner )));
114 }
115
116 void Parser::pushback(Token t) {
117 ASSERT(fPushback == Token::INVALID_TOKEN);
118 fPushback = t;
119 }
120
121 Token Parser::peek() {
122 fPushback = this->nextToken();
123 return fPushback;
124 }
125
126 bool Parser::expect(Token::Kind kind, std::string expected, Token* result) {
127 Token next = this->nextToken();
128 if (next.fKind == kind) {
129 if (result != nullptr) {
130 *result = next;
131 }
132 return true;
133 } else {
134 this->error(next.fPosition, "expected " + expected + ", but found '" + n ext.fText + "'");
135 return false;
136 }
137 }
138
139 void Parser::error(Position p, std::string msg) {
140 fErrors.error(p, msg);
141 }
142
143 bool Parser::isType(std::string name) {
144 return fTypes[name] != nullptr;
145 }
146
147 /* PRECISION (LOWP | MEDIUMP | HIGHP) type SEMICOLON */
148 void Parser::precision() {
149 if (!this->expect(Token::PRECISION, "'precision'")) {
150 return;
151 }
152 Token p = this->nextToken();
153 switch (p.fKind) {
154 case Token::LOWP: // fall through
155 case Token::MEDIUMP: // fall through
156 case Token::HIGHP:
157 // ignored for now
158 break;
159 default:
160 this->error(p.fPosition, "expected 'lowp', 'mediump', or 'highp', bu t found '" +
161 p.fText + "'");
162 return;
163 }
164 if (this->type() == nullptr) {
165 return;
166 }
167 this->expect(Token::SEMICOLON, "';'");
168 }
169
170 /* DIRECTIVE(#version) INT_LITERAL | DIRECTIVE(#extension) IDENTIFIER COLON IDEN TIFIER */
171 std::unique_ptr<ASTDeclaration> Parser::directive() {
172 Token start;
173 if (!this->expect(Token::DIRECTIVE, "a directive", &start)) {
174 return nullptr;
175 }
176 if (start.fText == "#version") {
177 this->expect(Token::INT_LITERAL, "a version number");
178 // ignored for now
179 return nullptr;
180 } else if (start.fText == "#extension") {
181 Token name;
182 if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) {
183 return nullptr;
184 }
185 if (!this->expect(Token::COLON, "':'")) {
186 return nullptr;
187 }
188 // FIXME: need to start paying attention to this token
189 if (!this->expect(Token::IDENTIFIER, "an identifier")) {
190 return nullptr;
191 }
192 return std::unique_ptr<ASTDeclaration>(new ASTExtension(start.fPosition,
193 std::move(name.f Text)));
194 } else {
195 this->error(start.fPosition, "unsupported directive '" + start.fText + " '");
196 return nullptr;
197 }
198 }
199
200 /* modifiers (structVarDeclaration | type IDENTIFIER ((LPAREN parameter
201 (COMMA parameter)* RPAREN (block | SEMICOLON)) | SEMICOLON) | interfaceBlock) */
202 std::unique_ptr<ASTDeclaration> Parser::declaration() {
203 ASTModifiers modifiers = this->modifiers();
204 Token lookahead = this->peek();
205 if (lookahead.fKind == Token::IDENTIFIER && !this->isType(lookahead.fText)) {
206 // we have an identifier that's not a type, could be the start of an int erface block
207 return this->interfaceBlock(modifiers);
208 }
209 if (lookahead.fKind == Token::STRUCT) {
210 return this->structVarDeclaration(modifiers);
211 }
212 std::unique_ptr<ASTType> type(this->type());
213 if (!type) {
214 return nullptr;
215 }
216 if (type->fKind == ASTType::kStruct_Kind && peek().fKind == Token::SEMICOLON ) {
217 this->nextToken();
218 return nullptr;
219 }
220 Token name;
221 if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) {
222 return nullptr;
223 }
224 if (!modifiers.fFlags && this->peek().fKind == Token::LPAREN) {
225 this->nextToken();
226 std::vector<std::unique_ptr<ASTParameter>> parameters;
227 while (this->peek().fKind != Token::RPAREN) {
228 if (parameters.size() > 0) {
229 if (!this->expect(Token::COMMA, "','")) {
230 return nullptr;
231 }
232 }
233 std::unique_ptr<ASTParameter> parameter = this->parameter();
234 if (!parameter) {
235 return nullptr;
236 }
237 parameters.push_back(std::move(parameter));
238 }
239 this->nextToken();
240 std::unique_ptr<ASTBlock> body;
241 if (this->peek().fKind == Token::SEMICOLON) {
242 this->nextToken();
243 } else {
244 body = this->block();
245 if (!body) {
246 return nullptr;
247 }
248 }
249 return std::unique_ptr<ASTDeclaration>(new ASTFunction(name.fPosition, s td::move(type),
250 std::move(name.fT ext),
251 std::move(paramet ers),
252 std::move(body))) ;
253 } else {
254 return this->varDeclarationEnd(modifiers, std::move(type), name.fText);
255 }
256 }
257
258 /* modifiers type IDENTIFIER (EQ expression)? (COMMA IDENTIFER (EQ expression)?) * SEMICOLON */
259 std::unique_ptr<ASTVarDeclaration> Parser::varDeclaration() {
260 ASTModifiers modifiers = this->modifiers();
261 std::unique_ptr<ASTType> type(this->type());
262 if (!type) {
263 return nullptr;
264 }
265 Token name;
266 if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) {
267 return nullptr;
268 }
269 return this->varDeclarationEnd(modifiers, std::move(type), std::move(name.fT ext));
270 }
271
272 /* STRUCT IDENTIFIER LBRACE varDeclaration* RBRACE */
273 std::unique_ptr<ASTType> Parser::structDeclaration() {
274 if (!this->expect(Token::STRUCT, "'struct'")) {
275 return nullptr;
276 }
277 Token name;
278 if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) {
279 return nullptr;
280 }
281 if (!this->expect(Token::LBRACE, "'{'")) {
282 return nullptr;
283 }
284 std::vector<Type::Field> fields;
285 while (this->peek().fKind != Token::RBRACE) {
286 std::unique_ptr<ASTVarDeclaration> decl = this->varDeclaration();
287 if (decl == nullptr) {
288 return nullptr;
289 }
290 for (size_t i = 0; i < decl->fNames.size(); i++) {
291 std::shared_ptr<Type> type = std::static_pointer_cast<Type>(fTypes[d ecl->fType->fName]);
292 for (int j = (int) decl->fSizes[i].size() - 1; j >= 0; j--) {
dogben 2016/06/23 15:25:12 nit: It seems odd that for fields we reverse the a
ethannicholas 2016/06/24 21:23:09 I agree that it's not ideal, but did not feel it w
293 if (decl->fSizes[i][j]->fKind == ASTExpression::kInt_Kind) {
294 this->error(decl->fPosition, "array size in struct field mus t be a constant");
295 }
296 uint64_t columns = ((ASTIntLiteral&) *decl->fSizes[i][j]).fValue ;
297 std::string name = type->name() + "[" + to_string(columns) + "]" ;
298 type = std::shared_ptr<Type>(new Type(name, Type::kArray_Kind, t ype,
299 (int) columns));
300 }
301 fields.push_back(Type::Field(decl->fModifiers, decl->fNames[i], type ));
302 if (decl->fValues[i] != nullptr) {
303 this->error(decl->fPosition, "initializers are not permitted on struct fields");
304 }
305 }
306 }
307 if (!this->expect(Token::RBRACE, "'}'")) {
308 return nullptr;
309 }
310 std::shared_ptr<Type> type(new Type(name.fText, fields));
311 fTypes.add(type->fName, type);
312 return std::unique_ptr<ASTType>(new ASTType(name.fPosition, type->fName,
313 ASTType::kStruct_Kind));
314 }
315
316 /* structDeclaration varDeclarationEnd */
317 std::unique_ptr<ASTVarDeclaration> Parser::structVarDeclaration(ASTModifiers mod ifiers) {
318 std::unique_ptr<ASTType> type = this->structDeclaration();
319 if (type == nullptr) {
320 return nullptr;
321 }
322 if (peek().fKind == Token::IDENTIFIER) {
323 Token name = this->nextToken();
324 std::unique_ptr<ASTVarDeclaration> result = this->varDeclarationEnd(modi fiers,
325 std: :move(type),
326 std: :move(name.fText));
327 if (result != nullptr) {
328 for (size_t i = 0; i < result->fValues.size(); i++) {
329 if (result->fValues[i] != nullptr) {
330 this->error(result->fValues[i]->fPosition,
331 "struct variables cannot be initialized");
332 }
333 }
334 }
335 return result;
336 }
337 this->expect(Token::SEMICOLON, "';'");
338 return nullptr;
339 }
340
341 /* (LBRACKET expression? RBRACKET)* (EQ expression)? (COMMA IDENTIFER
342 (LBRACKET expression? RBRACKET)* (EQ expression)?)* SEMICOLON */
343 std::unique_ptr<ASTVarDeclaration> Parser::varDeclarationEnd(ASTModifiers mods,
344 std::unique_ptr<AST Type> type,
345 std::string name) {
346 std::vector<std::string> names;
347 std::vector<std::vector<std::unique_ptr<ASTExpression>>> sizes;
348 names.push_back(name);
349 std::vector<std::unique_ptr<ASTExpression>> currentVarSizes;
350 while (this->peek().fKind == Token::LBRACKET) {
351 this->nextToken();
352 if (this->peek().fKind == Token::RBRACKET) {
353 this->nextToken();
354 currentVarSizes.push_back(nullptr);
355 } else {
356 std::unique_ptr<ASTExpression> size(this->expression());
357 if (!size) {
358 return nullptr;
359 }
360 currentVarSizes.push_back(std::move(size));
361 if (!this->expect(Token::RBRACKET, "']'")) {
362 return nullptr;
363 }
364 }
365 }
366 sizes.push_back(std::move(currentVarSizes));
367 std::vector<std::unique_ptr<ASTExpression>> values;
368 if (this->peek().fKind == Token::EQ) {
369 this->nextToken();
370 std::unique_ptr<ASTExpression> value(this->expression());
371 if (!value) {
372 return nullptr;
373 }
374 values.push_back(std::move(value));
375 } else {
376 values.push_back(nullptr);
377 }
378 while (this->peek().fKind == Token::COMMA) {
379 this->nextToken();
380 Token name;
381 if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) {
382 return nullptr;
383 }
384 names.push_back(name.fText);
385 currentVarSizes.clear();
386 while (this->peek().fKind == Token::LBRACKET) {
387 this->nextToken();
388 if (this->peek().fKind == Token::RBRACKET) {
389 this->nextToken();
390 currentVarSizes.push_back(nullptr);
391 } else {
392 std::unique_ptr<ASTExpression> size(this->expression());
393 if (!size) {
394 return nullptr;
395 }
396 currentVarSizes.push_back(std::move(size));
397 if (!this->expect(Token::RBRACKET, "']'")) {
398 return nullptr;
399 }
400 }
401 }
402 sizes.push_back(std::move(currentVarSizes));
403 if (this->peek().fKind == Token::EQ) {
404 this->nextToken();
405 std::unique_ptr<ASTExpression> value(this->expression());
406 if (!value) {
407 return nullptr;
408 }
409 values.push_back(std::move(value));
410 } else {
411 values.push_back(nullptr);
412 }
413 }
414 if (!this->expect(Token::SEMICOLON, "';'")) {
415 return nullptr;
416 }
417 return std::unique_ptr<ASTVarDeclaration>(new ASTVarDeclaration(std::move(mo ds),
418 std::move(ty pe),
419 std::move(na mes),
420 std::move(si zes),
421 std::move(va lues)));
422 }
423
424 /* modifiers type IDENTIFIER (LBRACKET INT_LITERAL RBRACKET)? */
425 std::unique_ptr<ASTParameter> Parser::parameter() {
426 ASTModifiers modifiers = this->modifiers();
427 std::unique_ptr<ASTType> type = this->type();
428 if (!type) {
429 return nullptr;
430 }
431 Token name;
432 if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) {
433 return nullptr;
434 }
435 std::vector<int> sizes;
436 while (this->peek().fKind == Token::LBRACKET) {
437 this->nextToken();
438 Token sizeToken;
439 if (!this->expect(Token::INT_LITERAL, "a positive integer", &sizeToken)) {
440 return nullptr;
441 }
442 sizes.push_back(SkSL::stoi(sizeToken.fText));
443 if (!this->expect(Token::RBRACKET, "']'")) {
444 return nullptr;
445 }
446 }
447 return std::unique_ptr<ASTParameter>(new ASTParameter(name.fPosition, modifi ers,
448 std::move(type), name. fText,
449 std::move(sizes)));
450 }
451
452 /** (EQ INT_LITERAL)? */
453 int Parser::layoutInt() {
454 if (!this->expect(Token::EQ, "'='")) {
455 return -1;
456 }
457 Token resultToken;
458 if (this->expect(Token::INT_LITERAL, "a non-negative integer", &resultToken) ) {
459 return SkSL::stoi(resultToken.fText);
460 }
461 return -1;
462 }
463
464 /* LAYOUT LPAREN IDENTIFIER EQ INT_LITERAL (COMMA IDENTIFIER EQ INT_LITERAL)*
465 RPAREN */
466 ASTLayout Parser::layout() {
467 int location = -1;
468 int binding = -1;
469 int index = -1;
470 int set = -1;
471 int builtin = -1;
472 if (this->peek().fKind == Token::LAYOUT) {
473 this->nextToken();
474 if (!this->expect(Token::LPAREN, "'('")) {
475 return ASTLayout(location, binding, index, set, builtin);
476 }
477 for (;;) {
478 Token t = this->nextToken();
479 if (t.fText == "location") {
480 location = this->layoutInt();
481 } else if (t.fText == "binding") {
482 binding = this->layoutInt();
483 } else if (t.fText == "index") {
484 index = this->layoutInt();
485 } else if (t.fText == "set") {
486 set = this->layoutInt();
487 } else if (t.fText == "builtin") {
488 builtin = this->layoutInt();
489 } else {
490 this->error(t.fPosition, ("'" + t.fText +
491 "' is not a valid layout qualifier").c _str());
492 }
493 if (this->peek().fKind == Token::RPAREN) {
494 this->nextToken();
495 break;
496 }
497 if (!this->expect(Token::COMMA, "','")) {
498 break;
499 }
500 }
501 }
502 return ASTLayout(location, binding, index, set, builtin);
503 }
504
505 /* layout? (UNIFORM | CONST | IN | OUT | INOUT | LOWP |
506 MEDIUMP | HIGHP)* */
507 ASTModifiers Parser::modifiers() {
508 ASTLayout layout = this->layout();
509 int flags = 0;
510 for (;;) {
511 // TODO: handle duplicate / incompatible flags
512 switch (peek().fKind) {
513 case Token::UNIFORM:
514 this->nextToken();
515 flags |= ASTModifiers::kUniform_Flag;
516 break;
517 case Token::CONST:
518 this->nextToken();
519 flags |= ASTModifiers::kConst_Flag;
520 break;
521 case Token::IN:
522 this->nextToken();
523 flags |= ASTModifiers::kIn_Flag;
524 break;
525 case Token::OUT:
526 this->nextToken();
527 flags |= ASTModifiers::kOut_Flag;
528 break;
529 case Token::INOUT:
530 this->nextToken();
531 flags |= ASTModifiers::kIn_Flag;
532 flags |= ASTModifiers::kOut_Flag;
533 break;
534 case Token::LOWP:
535 this->nextToken();
536 flags |= ASTModifiers::kLowp_Flag;
537 break;
538 case Token::MEDIUMP:
539 this->nextToken();
540 flags |= ASTModifiers::kMediump_Flag;
541 break;
542 case Token::HIGHP:
543 this->nextToken();
544 flags |= ASTModifiers::kHighp_Flag;
545 break;
546 default:
547 return ASTModifiers(layout, flags);
548 }
549 }
550 }
551
552 /* ifStatement | forStatement | doStatement | whileStatement | block | expressio n */
553 std::unique_ptr<ASTStatement> Parser::statement() {
554 Token start = this->peek();
555 switch (start.fKind) {
556 case Token::IF:
557 return this->ifStatement();
558 case Token::FOR:
559 return this->forStatement();
560 case Token::DO:
561 return this->doStatement();
562 case Token::WHILE:
563 return this->whileStatement();
564 case Token::RETURN:
565 return this->returnStatement();
566 case Token::BREAK:
567 return this->breakStatement();
568 case Token::CONTINUE:
569 return this->continueStatement();
570 case Token::DISCARD:
571 return this->discardStatement();
572 case Token::LBRACE:
573 return this->block();
574 case Token::SEMICOLON:
575 this->nextToken();
576 return std::unique_ptr<ASTStatement>(new ASTBlock(start.fPosition, { }));
577 case Token::CONST: // fall through
578 case Token::HIGHP: // fall through
579 case Token::MEDIUMP: // fall through
580 case Token::LOWP: {
581 auto decl = this->varDeclaration();
582 if (decl == nullptr) {
583 return nullptr;
584 }
585 return std::unique_ptr<ASTStatement>(new ASTVarDeclarationStatement( std::move(decl)));
586 }
587 case Token::IDENTIFIER:
588 if (this->isType(start.fText)) {
589 auto decl = this->varDeclaration();
590 if (decl == nullptr) {
591 return nullptr;
592 }
593 return std::unique_ptr<ASTStatement>(new ASTVarDeclarationStatem ent(
594 std::move(decl)));
595 }
596 // fall through
597 default:
598 return this->expressionStatement();
599 }
600 }
601
602 /* IDENTIFIER(type) */
603 std::unique_ptr<ASTType> Parser::type() {
604 Token type;
605 if (!this->expect(Token::IDENTIFIER, "a type", &type)) {
606 return nullptr;
607 }
608 if (!this->isType(type.fText)) {
609 this->error(type.fPosition, ("no type named '" + type.fText + "'").c_str ());
610 return nullptr;
611 }
612 return std::unique_ptr<ASTType>(new ASTType(type.fPosition, std::move(type.f Text),
613 ASTType::kIdentifier_Kind));
614 }
615
616 /* IDENTIFIER LBRACE varDeclaration* RBRACE */
617 std::unique_ptr<ASTDeclaration> Parser::interfaceBlock(ASTModifiers mods) {
618 Token name;
619 if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) {
620 return nullptr;
621 }
622 if (peek().fKind != Token::LBRACE) {
623 // we only get into interfaceBlock if we found a top-level identifier wh ich was not a type.
624 // 99% of the time, the user was not actually intending to create an int erface block, so
625 // it's better to report it as an unknown type
626 this->error(name.fPosition, "no type named '" + name.fText + "'");
627 return nullptr;
628 }
629 this->nextToken();
630 std::vector<std::unique_ptr<ASTVarDeclaration>> decls;
631 while (this->peek().fKind != Token::RBRACE) {
632 std::unique_ptr<ASTVarDeclaration> decl = this->varDeclaration();
633 if (decl == nullptr) {
634 return nullptr;
635 }
636 decls.push_back(std::move(decl));
637 }
638 this->nextToken();
639 std::string valueName;
640 if (this->peek().fKind == Token::IDENTIFIER) {
641 valueName = this->nextToken().fText;
642 }
643 this->expect(Token::SEMICOLON, "';'");
644 return std::unique_ptr<ASTDeclaration>(new ASTInterfaceBlock(name.fPosition, mods,
645 name.fText, std ::move(valueName),
646 std::move(decls )));
647 }
648
649 /* IF LPAREN expression RPAREN statement (ELSE statement)? */
650 std::unique_ptr<ASTIfStatement> Parser::ifStatement() {
651 Token start;
652 if (!this->expect(Token::IF, "'if'", &start)) {
653 return nullptr;
654 }
655 if (!this->expect(Token::LPAREN, "'('")) {
656 return nullptr;
657 }
658 std::unique_ptr<ASTExpression> test(this->expression());
659 if (test == nullptr) {
660 return nullptr;
661 }
662 if (!this->expect(Token::RPAREN, "')'")) {
663 return nullptr;
664 }
665 std::unique_ptr<ASTStatement> ifTrue(this->statement());
666 if (ifTrue == nullptr) {
667 return nullptr;
668 }
669 std::unique_ptr<ASTStatement> ifFalse;
670 if (this->peek().fKind == Token::ELSE) {
671 this->nextToken();
672 ifFalse = this->statement();
673 if (!ifFalse) {
674 return nullptr;
675 }
676 }
677 return std::unique_ptr<ASTIfStatement>(new ASTIfStatement(start.fPosition, s td::move(test),
678 std::move(ifTrue),
679 std::move(ifFalse) ));
680 }
681
682 /* DO statement WHILE LPAREN expression RPAREN SEMICOLON */
683 std::unique_ptr<ASTDoStatement> Parser::doStatement() {
684 Token start;
685 if (!this->expect(Token::DO, "'do'", &start)) {
686 return nullptr;
687 }
688 std::unique_ptr<ASTStatement> statement(this->statement());
689 if (statement == nullptr) {
690 return nullptr;
691 }
692 if (!this->expect(Token::WHILE, "'while'")) {
693 return nullptr;
694 }
695 if (!this->expect(Token::LPAREN, "'('")) {
696 return nullptr;
697 }
698 std::unique_ptr<ASTExpression> test(this->expression());
699 if (test == nullptr) {
700 return nullptr;
701 }
702 if (!this->expect(Token::RPAREN, "')'")) {
703 return nullptr;
704 }
705 if (!this->expect(Token::SEMICOLON, "';'")) {
706 return nullptr;
707 }
708 return std::unique_ptr<ASTDoStatement>(new ASTDoStatement(start.fPosition,
709 std::move(statemen t),
710 std::move(test)));
711 }
712
713 /* WHILE LPAREN expression RPAREN STATEMENT */
714 std::unique_ptr<ASTWhileStatement> Parser::whileStatement() {
715 Token start;
716 if (!this->expect(Token::WHILE, "'while'", &start)) {
717 return nullptr;
718 }
719 if (!this->expect(Token::LPAREN, "'('")) {
720 return nullptr;
721 }
722 std::unique_ptr<ASTExpression> test(this->expression());
723 if (test == nullptr) {
724 return nullptr;
725 }
726 if (!this->expect(Token::RPAREN, "')'")) {
727 return nullptr;
728 }
729 std::unique_ptr<ASTStatement> statement(this->statement());
730 if (statement == nullptr) {
731 return nullptr;
732 }
733 return std::unique_ptr<ASTWhileStatement>(new ASTWhileStatement(start.fPosit ion,
734 std::move(te st),
735 std::move(st atement)));
736 }
737
738 /* FOR LPAREN (declaration | expression)? SEMICOLON expression? SEMICOLON expres sion? RPAREN
739 STATEMENT */
740 std::unique_ptr<ASTForStatement> Parser::forStatement() {
741 Token start;
742 if (!this->expect(Token::FOR, "'for'", &start)) {
743 return nullptr;
744 }
745 if (!this->expect(Token::LPAREN, "'('")) {
746 return nullptr;
747 }
748 std::unique_ptr<ASTStatement> initializer;
749 Token nextToken = this->peek();
750 switch (nextToken.fKind) {
751 case Token::SEMICOLON:
752 break;
753 case Token::CONST:
754 initializer = std::unique_ptr<ASTStatement>(new ASTVarDeclarationSta tement(
755 this- >varDeclaration()));
756 break;
757 case Token::IDENTIFIER:
758 if (this->isType(nextToken.fText)) {
759 initializer = std::unique_ptr<ASTStatement>(new ASTVarDeclaratio nStatement(
760 this- >varDeclaration()));
761 break;
762 }
763 // fall through
764 default:
765 initializer = this->expressionStatement();
766 }
767 std::unique_ptr<ASTExpression> test;
768 if (this->peek().fKind != Token::SEMICOLON) {
769 test = this->expression();
770 if (!test) {
771 return nullptr;
772 }
773 }
774 if (!this->expect(Token::SEMICOLON, "';'")) {
775 return nullptr;
776 }
777 std::unique_ptr<ASTExpression> next;
778 if (this->peek().fKind != Token::SEMICOLON) {
779 next = this->expression();
780 if (!next) {
781 return nullptr;
782 }
783 }
784 if (!this->expect(Token::RPAREN, "')'")) {
785 return nullptr;
786 }
787 std::unique_ptr<ASTStatement> statement(this->statement());
788 if (statement == nullptr) {
789 return nullptr;
790 }
791 return std::unique_ptr<ASTForStatement>(new ASTForStatement(start.fPosition,
792 std::move(initia lizer),
793 std::move(test), std::move(next),
794 std::move(statem ent)));
795 }
796
797 /* RETURN expression? SEMICOLON */
798 std::unique_ptr<ASTReturnStatement> Parser::returnStatement() {
799 Token start;
800 if (!this->expect(Token::RETURN, "'return'", &start)) {
801 return nullptr;
802 }
803 std::unique_ptr<ASTExpression> expression;
804 if (this->peek().fKind != Token::SEMICOLON) {
805 expression = this->expression();
806 if (!expression) {
807 return nullptr;
808 }
809 }
810 if (!this->expect(Token::SEMICOLON, "';'")) {
811 return nullptr;
812 }
813 return std::unique_ptr<ASTReturnStatement>(new ASTReturnStatement(start.fPos ition,
814 std::move( expression)));
815 }
816
817 /* BREAK SEMICOLON */
818 std::unique_ptr<ASTBreakStatement> Parser::breakStatement() {
819 Token start;
820 if (!this->expect(Token::BREAK, "'break'", &start)) {
821 return nullptr;
822 }
823 if (!this->expect(Token::SEMICOLON, "';'")) {
824 return nullptr;
825 }
826 return std::unique_ptr<ASTBreakStatement>(new ASTBreakStatement(start.fPosit ion));
827 }
828
829 /* CONTINUE SEMICOLON */
830 std::unique_ptr<ASTContinueStatement> Parser::continueStatement() {
831 Token start;
832 if (!this->expect(Token::CONTINUE, "'continue'", &start)) {
833 return nullptr;
834 }
835 if (!this->expect(Token::SEMICOLON, "';'")) {
836 return nullptr;
837 }
838 return std::unique_ptr<ASTContinueStatement>(new ASTContinueStatement(start. fPosition));
839 }
840
841 /* DISCARD SEMICOLON */
842 std::unique_ptr<ASTDiscardStatement> Parser::discardStatement() {
843 Token start;
844 if (!this->expect(Token::DISCARD, "'continue'", &start)) {
845 return nullptr;
846 }
847 if (!this->expect(Token::SEMICOLON, "';'")) {
848 return nullptr;
849 }
850 return std::unique_ptr<ASTDiscardStatement>(new ASTDiscardStatement(start.fP osition));
851 }
852
853 /* LBRACE statement* RBRACE */
854 std::unique_ptr<ASTBlock> Parser::block() {
855 Token start;
856 if (!this->expect(Token::LBRACE, "'{'", &start)) {
857 return nullptr;
858 }
859 std::vector<std::unique_ptr<ASTStatement>> statements;
860 for (;;) {
861 switch (this->peek().fKind) {
862 case Token::RBRACE:
863 this->nextToken();
864 return std::unique_ptr<ASTBlock>(new ASTBlock(start.fPosition,
865 std::move(statemen ts)));
866 case Token::END_OF_FILE:
867 this->error(this->peek().fPosition, "expected '}', but found end of file");
868 return nullptr;
869 default: {
870 std::unique_ptr<ASTStatement> statement = this->statement();
871 if (statement == nullptr) {
872 return nullptr;
873 }
874 statements.push_back(std::move(statement));
875 }
876 }
877 }
878 }
879
880 /* expression SEMICOLON */
881 std::unique_ptr<ASTExpressionStatement> Parser::expressionStatement() {
882 std::unique_ptr<ASTExpression> expr = this->expression();
883 if (expr) {
884 if (this->expect(Token::SEMICOLON, "';'")) {
885 ASTExpressionStatement* result = new ASTExpressionStatement(std::mov e(expr));
886 return std::unique_ptr<ASTExpressionStatement>(result);
887 }
888 }
889 return nullptr;
890 }
891
892 /* assignmentExpression */
893 std::unique_ptr<ASTExpression> Parser::expression() {
894 return this->assignmentExpression();
895 }
896
897 /* ternaryExpression ((EQEQ | STAREQ | SLASHEQ | PERCENTEQ | PLUSEQ | MINUSEQ | SHLEQ | SHREQ |
898 BITWISEANDEQ | BITWISEXOREQ | BITWISEOREQ | LOGICALANDEQ | LOGICALXOREQ | LOG ICALOREQ)
899 assignmentExpression)*
900 */
901 std::unique_ptr<ASTExpression> Parser::assignmentExpression() {
902 std::unique_ptr<ASTExpression> result = this->ternaryExpression();
903 if (!result) {
904 return nullptr;
905 }
906 for (;;) {
907 switch (this->peek().fKind) {
908 case Token::EQ: // fall through
909 case Token::STAREQ: // fall through
910 case Token::SLASHEQ: // fall through
911 case Token::PERCENTEQ: // fall through
912 case Token::PLUSEQ: // fall through
913 case Token::MINUSEQ: // fall through
914 case Token::SHLEQ: // fall through
915 case Token::SHREQ: // fall through
916 case Token::BITWISEANDEQ: // fall through
917 case Token::BITWISEXOREQ: // fall through
918 case Token::BITWISEOREQ: // fall through
919 case Token::LOGICALANDEQ: // fall through
920 case Token::LOGICALXOREQ: // fall through
921 case Token::LOGICALOREQ: {
922 Token t = this->nextToken();
923 std::unique_ptr<ASTExpression> right = this->assignmentExpressio n();
924 if (right == nullptr) {
925 return nullptr;
926 }
927 result = std::unique_ptr<ASTExpression>(new ASTBinaryExpression( std::move(result),
928 t,
929 std::move(right)));
930 }
931 default:
932 return result;
933 }
934 }
935 }
936
937 /* logicalOrExpression ('?' expression ':' assignmentExpression)? */
938 std::unique_ptr<ASTExpression> Parser::ternaryExpression() {
939 std::unique_ptr<ASTExpression> result = this->logicalOrExpression();
940 if (result == nullptr) {
941 return nullptr;
942 }
943 if (this->peek().fKind == Token::QUESTION) {
944 Token question = this->nextToken();
945 std::unique_ptr<ASTExpression> trueExpr = this->expression();
946 if (trueExpr == nullptr) {
947 return nullptr;
948 }
949 if (this->expect(Token::COLON, "':'")) {
950 std::unique_ptr<ASTExpression> falseExpr = this->assignmentExpressio n();
951 return std::unique_ptr<ASTExpression>(new ASTTernaryExpression(std:: move(result),
952 std:: move(trueExpr),
953 std:: move(falseExpr)));
954 }
955 return nullptr;
956 }
957 return result;
958 }
959
960 /* logicalXorExpression (LOGICALOR logicalXorExpression)* */
961 std::unique_ptr<ASTExpression> Parser::logicalOrExpression() {
962 std::unique_ptr<ASTExpression> result = this->logicalXorExpression();
963 if (result == nullptr) {
964 return nullptr;
965 }
966 while (this->peek().fKind == Token::LOGICALOR) {
967 Token t = this->nextToken();
968 std::unique_ptr<ASTExpression> right = this->logicalXorExpression();
969 if (right == nullptr) {
970 return nullptr;
971 }
972 result.reset(new ASTBinaryExpression(std::move(result), t, std::move(rig ht)));
973 }
974 return result;
975 }
976
977 /* logicalAndExpression (LOGICALXOR logicalAndExpression)* */
978 std::unique_ptr<ASTExpression> Parser::logicalXorExpression() {
979 std::unique_ptr<ASTExpression> result = this->logicalAndExpression();
980 if (result == nullptr) {
981 return nullptr;
982 }
983 while (this->peek().fKind == Token::LOGICALXOR) {
984 Token t = this->nextToken();
985 std::unique_ptr<ASTExpression> right = this->logicalAndExpression();
986 if (right == nullptr) {
987 return nullptr;
988 }
989 result.reset(new ASTBinaryExpression(std::move(result), t, std::move(rig ht)));
990 }
991 return result;
992 }
993
994 /* bitwiseOrExpression (LOGICALAND bitwiseOrExpression)* */
995 std::unique_ptr<ASTExpression> Parser::logicalAndExpression() {
996 std::unique_ptr<ASTExpression> result = this->bitwiseOrExpression();
997 if (result == nullptr) {
998 return nullptr;
999 }
1000 while (this->peek().fKind == Token::LOGICALAND) {
1001 Token t = this->nextToken();
1002 std::unique_ptr<ASTExpression> right = this->bitwiseOrExpression();
1003 if (right == nullptr) {
1004 return nullptr;
1005 }
1006 result.reset(new ASTBinaryExpression(std::move(result), t, std::move(rig ht)));
1007 }
1008 return result;
1009 }
1010
1011 /* bitwiseXorExpression (BITWISEOR bitwiseXorExpression)* */
1012 std::unique_ptr<ASTExpression> Parser::bitwiseOrExpression() {
1013 std::unique_ptr<ASTExpression> result = this->bitwiseXorExpression();
1014 if (result == nullptr) {
1015 return nullptr;
1016 }
1017 while (this->peek().fKind == Token::BITWISEOR) {
1018 Token t = this->nextToken();
1019 std::unique_ptr<ASTExpression> right = this->bitwiseXorExpression();
1020 if (right == nullptr) {
1021 return nullptr;
1022 }
1023 result.reset(new ASTBinaryExpression(std::move(result), t, std::move(rig ht)));
1024 }
1025 return result;
1026 }
1027
1028 /* bitwiseAndExpression (BITWISEXOR bitwiseAndExpression)* */
1029 std::unique_ptr<ASTExpression> Parser::bitwiseXorExpression() {
1030 std::unique_ptr<ASTExpression> result = this->bitwiseAndExpression();
1031 if (result == nullptr) {
1032 return nullptr;
1033 }
1034 while (this->peek().fKind == Token::BITWISEXOR) {
1035 Token t = this->nextToken();
1036 std::unique_ptr<ASTExpression> right = this->bitwiseAndExpression();
1037 if (right == nullptr) {
1038 return nullptr;
1039 }
1040 result.reset(new ASTBinaryExpression(std::move(result), t, std::move(rig ht)));
1041 }
1042 return result;
1043 }
1044
1045 /* equalityExpression (BITWISEAND equalityExpression)* */
1046 std::unique_ptr<ASTExpression> Parser::bitwiseAndExpression() {
1047 std::unique_ptr<ASTExpression> result = this->equalityExpression();
1048 if (result == nullptr) {
1049 return nullptr;
1050 }
1051 while (this->peek().fKind == Token::BITWISEAND) {
1052 Token t = this->nextToken();
1053 std::unique_ptr<ASTExpression> right = this->equalityExpression();
1054 if (right == nullptr) {
1055 return nullptr;
1056 }
1057 result.reset(new ASTBinaryExpression(std::move(result), t, std::move(rig ht)));
1058 }
1059 return result;
1060 }
1061
1062 /* relationalExpression ((EQEQ | NEQ) relationalExpression)* */
1063 std::unique_ptr<ASTExpression> Parser::equalityExpression() {
1064 std::unique_ptr<ASTExpression> result = this->relationalExpression();
1065 if (result == nullptr) {
1066 return nullptr;
1067 }
1068 for (;;) {
1069 switch (this->peek().fKind) {
1070 case Token::EQEQ: // fall through
1071 case Token::NEQ: {
1072 Token t = this->nextToken();
1073 std::unique_ptr<ASTExpression> right = this->relationalExpressio n();
1074 if (right == nullptr) {
1075 return nullptr;
1076 }
1077 result.reset(new ASTBinaryExpression(std::move(result), t, std:: move(right)));
1078 break;
1079 }
1080 default:
1081 return result;
1082 }
1083 }
1084 }
1085
1086 /* shiftExpression ((LT | GT | LTEQ | GTEQ) shiftExpression)* */
1087 std::unique_ptr<ASTExpression> Parser::relationalExpression() {
1088 std::unique_ptr<ASTExpression> result = this->shiftExpression();
1089 if (result == nullptr) {
1090 return nullptr;
1091 }
1092 for (;;) {
1093 switch (this->peek().fKind) {
1094 case Token::LT: // fall through
1095 case Token::GT: // fall through
1096 case Token::LTEQ: // fall through
1097 case Token::GTEQ: {
1098 Token t = this->nextToken();
1099 std::unique_ptr<ASTExpression> right = this->shiftExpression();
1100 if (right == nullptr) {
1101 return nullptr;
1102 }
1103 result.reset(new ASTBinaryExpression(std::move(result), t, std:: move(right)));
1104 break;
1105 }
1106 default:
1107 return result;
1108 }
1109 }
1110 }
1111
1112 /* additiveExpression ((SHL | SHR) additiveExpression)* */
1113 std::unique_ptr<ASTExpression> Parser::shiftExpression() {
1114 std::unique_ptr<ASTExpression> result = this->additiveExpression();
1115 if (result == nullptr) {
1116 return nullptr;
1117 }
1118 for (;;) {
1119 switch (this->peek().fKind) {
1120 case Token::SHL: // fall through
1121 case Token::SHR: {
1122 Token t = this->nextToken();
1123 std::unique_ptr<ASTExpression> right = this->additiveExpression( );
1124 if (right == nullptr) {
1125 return nullptr;
1126 }
1127 result.reset(new ASTBinaryExpression(std::move(result), t, std:: move(right)));
1128 break;
1129 }
1130 default:
1131 return result;
1132 }
1133 }
1134 }
1135
1136 /* multiplicativeExpression ((PLUS | MINUS) multiplicativeExpression)* */
1137 std::unique_ptr<ASTExpression> Parser::additiveExpression() {
1138 std::unique_ptr<ASTExpression> result = this->multiplicativeExpression();
1139 if (result == nullptr) {
1140 return nullptr;
1141 }
1142 for (;;) {
1143 switch (this->peek().fKind) {
1144 case Token::PLUS: // fall through
1145 case Token::MINUS: {
1146 Token t = this->nextToken();
1147 std::unique_ptr<ASTExpression> right = this->multiplicativeExpre ssion();
1148 if (right == nullptr) {
1149 return nullptr;
1150 }
1151 result.reset(new ASTBinaryExpression(std::move(result), t, std:: move(right)));
1152 break;
1153 }
1154 default:
1155 return result;
1156 }
1157 }
1158 }
1159
1160 /* unaryExpression ((STAR | SLASH | PERCENT) unaryExpression)* */
1161 std::unique_ptr<ASTExpression> Parser::multiplicativeExpression() {
1162 std::unique_ptr<ASTExpression> result = this->unaryExpression();
1163 if (result == nullptr) {
1164 return nullptr;
1165 }
1166 for (;;) {
1167 switch (this->peek().fKind) {
1168 case Token::STAR: // fall through
1169 case Token::SLASH: // fall through
1170 case Token::PERCENT: {
1171 Token t = this->nextToken();
1172 std::unique_ptr<ASTExpression> right = this->unaryExpression();
1173 if (right == nullptr) {
1174 return nullptr;
1175 }
1176 result.reset(new ASTBinaryExpression(std::move(result), t, std:: move(right)));
1177 break;
1178 }
1179 default:
1180 return result;
1181 }
1182 }
1183 }
1184
1185 /* postfixExpression | (PLUS | MINUS | NOT | PLUSPLUS | MINUSMINUS) unaryExpress ion */
1186 std::unique_ptr<ASTExpression> Parser::unaryExpression() {
1187 switch (this->peek().fKind) {
1188 case Token::PLUS: // fall through
1189 case Token::MINUS: // fall through
1190 case Token::NOT: // fall through
1191 case Token::PLUSPLUS: // fall through
1192 case Token::MINUSMINUS: {
1193 Token t = this->nextToken();
1194 std::unique_ptr<ASTExpression> expr = this->unaryExpression();
1195 if (expr == nullptr) {
1196 return nullptr;
1197 }
1198 return std::unique_ptr<ASTExpression>(new ASTPrefixExpression(t, std ::move(expr)));
1199 }
1200 default:
1201 return this->postfixExpression();
1202 }
1203 }
1204
1205 /* term suffix* */
1206 std::unique_ptr<ASTExpression> Parser::postfixExpression() {
1207 std::unique_ptr<ASTExpression> result = this->term();
1208 if (result == nullptr) {
1209 return nullptr;
1210 }
1211 for (;;) {
1212 switch (this->peek().fKind) {
1213 case Token::LBRACKET: // fall through
1214 case Token::DOT: // fall through
1215 case Token::LPAREN: // fall through
1216 case Token::PLUSPLUS: // fall through
1217 case Token::MINUSMINUS: {
1218 std::unique_ptr<ASTSuffix> s = this->suffix();
1219 if (s == nullptr) {
1220 return nullptr;
1221 }
1222 result.reset(new ASTSuffixExpression(std::move(result), std::mov e(s)));
1223 break;
1224 }
1225 default:
1226 return result;
1227 }
1228 }
1229 }
1230
1231 /* LBRACKET expression RBRACKET | DOT IDENTIFIER | LPAREN parameters RPAREN |
1232 PLUSPLUS | MINUSMINUS */
1233 std::unique_ptr<ASTSuffix> Parser::suffix() {
1234 Token next = this->nextToken();
1235 switch (next.fKind) {
1236 case Token::LBRACKET: {
1237 std::unique_ptr<ASTExpression> e = this->expression();
1238 if (e == nullptr) {
1239 return nullptr;
1240 }
1241 this->expect(Token::RBRACKET, "']' to complete array access expressi on");
1242 return std::unique_ptr<ASTSuffix>(new ASTIndexSuffix(std::move(e)));
1243 }
1244 case Token::DOT: {
1245 Position pos = this->peek().fPosition;
1246 std::string text;
1247 if (this->identifier(&text)) {
1248 return std::unique_ptr<ASTSuffix>(new ASTFieldSuffix(pos, std::m ove(text)));
1249 }
1250 return nullptr;
1251 }
1252 case Token::LPAREN: {
1253 std::vector<std::unique_ptr<ASTExpression>> parameters;
1254 if (this->peek().fKind != Token::RPAREN) {
1255 for (;;) {
1256 std::unique_ptr<ASTExpression> expr = this->expression();
1257 if (expr == nullptr) {
1258 return nullptr;
1259 }
1260 parameters.push_back(std::move(expr));
1261 if (this->peek().fKind != Token::COMMA) {
1262 break;
1263 }
1264 this->nextToken();
1265 }
1266 }
1267 this->expect(Token::RPAREN, "')' to complete function parameters");
1268 return std::unique_ptr<ASTSuffix>(new ASTCallSuffix(next.fPosition,
1269 std::move(parame ters)));
1270 }
1271 case Token::PLUSPLUS:
1272 return std::unique_ptr<ASTSuffix>(new ASTSuffix(next.fPosition,
1273 ASTSuffix::kPostIncr ement_Kind));
1274 case Token::MINUSMINUS:
1275 return std::unique_ptr<ASTSuffix>(new ASTSuffix(next.fPosition,
1276 ASTSuffix::kPostDecr ement_Kind));
1277 default: {
1278 this->error(next.fPosition, "expected expression suffix, but found '" + next.fText +
1279 "'\n");
1280 return nullptr;
1281 }
1282 }
1283 }
1284
1285 /* IDENTIFIER | intLiteral | floatLiteral | boolLiteral | '(' expression ')' */
1286 std::unique_ptr<ASTExpression> Parser::term() {
1287 std::unique_ptr<ASTExpression> result;
1288 Token t = this->peek();
1289 switch (t.fKind) {
1290 case Token::IDENTIFIER: {
1291 std::string text;
1292 if (this->identifier(&text)) {
1293 result.reset(new ASTIdentifier(t.fPosition, std::move(text)));
1294 }
1295 break;
1296 }
1297 case Token::INT_LITERAL: {
1298 int64_t i;
1299 if (this->intLiteral(&i)) {
1300 result.reset(new ASTIntLiteral(t.fPosition, i));
1301 }
1302 break;
1303 }
1304 case Token::FLOAT_LITERAL: {
1305 double f;
1306 if (this->floatLiteral(&f)) {
1307 result.reset(new ASTFloatLiteral(t.fPosition, f));
1308 }
1309 break;
1310 }
1311 case Token::TRUE_LITERAL: // fall through
1312 case Token::FALSE_LITERAL: {
1313 bool b;
1314 if (this->boolLiteral(&b)) {
1315 result.reset(new ASTBoolLiteral(t.fPosition, b));
1316 }
1317 break;
1318 }
1319 case Token::LPAREN: {
1320 this->nextToken();
1321 result = this->expression();
1322 if (result) {
1323 this->expect(Token::RPAREN, "')' to complete expression");
1324 }
1325 break;
1326 }
1327 default:
1328 this->nextToken();
1329 this->error(t.fPosition, "expected expression, but found '" + t.fTe xt + "'\n");
1330 result = nullptr;
1331 }
1332 return result;
1333 }
1334
1335 /* INT_LITERAL */
1336 bool Parser::intLiteral(int64_t* dest) {
1337 Token t;
1338 if (this->expect(Token::INT_LITERAL, "integer literal", &t)) {
1339 *dest = SkSL::stol(t.fText);
1340 return true;
1341 }
1342 return false;
1343 }
1344
1345 /* FLOAT_LITERAL */
1346 bool Parser::floatLiteral(double* dest) {
1347 Token t;
1348 if (this->expect(Token::FLOAT_LITERAL, "float literal", &t)) {
1349 *dest = SkSL::stod(t.fText);
1350 return true;
1351 }
1352 return false;
1353 }
1354
1355 /* TRUE_LITERAL | FALSE_LITERAL */
1356 bool Parser::boolLiteral(bool* dest) {
1357 Token t = this->nextToken();
1358 switch (t.fKind) {
1359 case Token::TRUE_LITERAL:
1360 *dest = true;
1361 return true;
1362 case Token::FALSE_LITERAL:
1363 *dest = false;
1364 return true;
1365 default:
1366 this->error(t.fPosition, "expected 'true' or 'false', but found '" + t.fText + "'\n");
1367 return false;
1368 }
1369 }
1370
1371 /* IDENTIFIER */
1372 bool Parser::identifier(std::string* dest) {
1373 Token t;
1374 if (this->expect(Token::IDENTIFIER, "identifier", &t)) {
1375 *dest = t.fText;
1376 return true;
1377 }
1378 return false;
1379 }
1380
1381 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698