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

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: IR 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 auto type = std::static_pointer_cast<Type>(fTypes[decl->fType->fName ]);
292 for (int j = (int) decl->fSizes[i].size() - 1; j >= 0; j--) {
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],
302 std::shared_ptr<Type>(type)));
303 if (decl->fValues[i] != nullptr) {
304 this->error(decl->fPosition, "initializers are not permitted on struct fields");
305 }
306 }
307 }
308 if (!this->expect(Token::RBRACE, "'}'")) {
309 return nullptr;
310 }
311 std::shared_ptr<Type> type(new Type(name.fText, fields));
312 fTypes.add(type->fName, type);
313 return std::unique_ptr<ASTType>(new ASTType(name.fPosition, type->fName,
314 ASTType::kStruct_Kind));
315 }
316
317 /* structDeclaration varDeclarationEnd */
318 std::unique_ptr<ASTVarDeclaration> Parser::structVarDeclaration(ASTModifiers mod ifiers) {
319 std::unique_ptr<ASTType> type = this->structDeclaration();
320 if (type == nullptr) {
321 return nullptr;
322 }
323 if (peek().fKind == Token::IDENTIFIER) {
324 Token name = this->nextToken();
325 std::unique_ptr<ASTVarDeclaration> result = this->varDeclarationEnd(modi fiers,
326 std: :move(type),
327 std: :move(name.fText));
328 if (result != nullptr) {
329 for (size_t i = 0; i < result->fValues.size(); i++) {
330 if (result->fValues[i] != nullptr) {
331 this->error(result->fValues[i]->fPosition,
332 "struct variables cannot be initialized");
333 }
334 }
335 }
336 return result;
337 }
338 this->expect(Token::SEMICOLON, "';'");
339 return nullptr;
340 }
341
342 /* (LBRACKET expression? RBRACKET)* (EQ expression)? (COMMA IDENTIFER
343 (LBRACKET expression? RBRACKET)* (EQ expression)?)* SEMICOLON */
344 std::unique_ptr<ASTVarDeclaration> Parser::varDeclarationEnd(ASTModifiers mods,
345 std::unique_ptr<AST Type> type,
346 std::string name) {
347 std::vector<std::string> names;
348 std::vector<std::vector<std::unique_ptr<ASTExpression>>> sizes;
349 names.push_back(name);
350 std::vector<std::unique_ptr<ASTExpression>> currentVarSizes;
351 while (this->peek().fKind == Token::LBRACKET) {
352 this->nextToken();
353 if (this->peek().fKind == Token::RBRACKET) {
354 this->nextToken();
355 currentVarSizes.push_back(nullptr);
356 } else {
357 std::unique_ptr<ASTExpression> size(this->expression());
358 if (!size) {
359 return nullptr;
360 }
361 currentVarSizes.push_back(std::move(size));
362 if (!this->expect(Token::RBRACKET, "']'")) {
363 return nullptr;
364 }
365 }
366 }
367 sizes.push_back(std::move(currentVarSizes));
368 std::vector<std::unique_ptr<ASTExpression>> values;
369 if (this->peek().fKind == Token::EQ) {
370 this->nextToken();
371 std::unique_ptr<ASTExpression> value(this->expression());
372 if (!value) {
373 return nullptr;
374 }
375 values.push_back(std::move(value));
376 } else {
377 values.push_back(nullptr);
378 }
379 while (this->peek().fKind == Token::COMMA) {
380 this->nextToken();
381 Token name;
382 if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) {
383 return nullptr;
384 }
385 names.push_back(name.fText);
386 currentVarSizes.clear();
387 while (this->peek().fKind == Token::LBRACKET) {
388 this->nextToken();
389 if (this->peek().fKind == Token::RBRACKET) {
390 this->nextToken();
391 currentVarSizes.push_back(nullptr);
392 } else {
393 std::unique_ptr<ASTExpression> size(this->expression());
394 if (!size) {
395 return nullptr;
396 }
397 currentVarSizes.push_back(std::move(size));
398 if (!this->expect(Token::RBRACKET, "']'")) {
399 return nullptr;
400 }
401 }
402 }
403 sizes.push_back(std::move(currentVarSizes));
404 if (this->peek().fKind == Token::EQ) {
405 this->nextToken();
406 std::unique_ptr<ASTExpression> value(this->expression());
407 if (!value) {
408 return nullptr;
409 }
410 values.push_back(std::move(value));
411 } else {
412 values.push_back(nullptr);
413 }
414 }
415 if (!this->expect(Token::SEMICOLON, "';'")) {
416 return nullptr;
417 }
418 return std::unique_ptr<ASTVarDeclaration>(new ASTVarDeclaration(std::move(mo ds),
419 std::move(ty pe),
420 std::move(na mes),
421 std::move(si zes),
422 std::move(va lues)));
423 }
424
425 /* modifiers type IDENTIFIER (LBRACKET INT_LITERAL RBRACKET)? */
426 std::unique_ptr<ASTParameter> Parser::parameter() {
427 ASTModifiers modifiers = this->modifiers();
428 std::unique_ptr<ASTType> type = this->type();
429 if (!type) {
430 return nullptr;
431 }
432 Token name;
433 if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) {
434 return nullptr;
435 }
436 std::vector<int> sizes;
437 while (this->peek().fKind == Token::LBRACKET) {
438 this->nextToken();
439 Token sizeToken;
440 if (!this->expect(Token::INT_LITERAL, "a positive integer", &sizeToken)) {
441 return nullptr;
442 }
443 sizes.push_back(SkSL::stoi(sizeToken.fText));
444 if (!this->expect(Token::RBRACKET, "']'")) {
445 return nullptr;
446 }
447 }
448 return std::unique_ptr<ASTParameter>(new ASTParameter(name.fPosition, modifi ers,
449 std::move(type), name. fText,
450 std::move(sizes)));
451 }
452
453 /** (EQ INT_LITERAL)? */
454 int Parser::layoutInt() {
455 if (!this->expect(Token::EQ, "'='")) {
456 return -1;
457 }
458 Token resultToken;
459 if (this->expect(Token::INT_LITERAL, "a non-negative integer", &resultToken) ) {
460 return SkSL::stoi(resultToken.fText);
461 }
462 return -1;
463 }
464
465 /* LAYOUT LPAREN IDENTIFIER EQ INT_LITERAL (COMMA IDENTIFIER EQ INT_LITERAL)*
466 RPAREN */
467 ASTLayout Parser::layout() {
468 int location = -1;
469 int binding = -1;
470 int index = -1;
471 int set = -1;
472 int builtin = -1;
473 if (this->peek().fKind == Token::LAYOUT) {
474 this->nextToken();
475 if (!this->expect(Token::LPAREN, "'('")) {
476 return ASTLayout(location, binding, index, set, builtin);
477 }
478 for (;;) {
479 Token t = this->nextToken();
480 if (t.fText == "location") {
481 location = this->layoutInt();
482 } else if (t.fText == "binding") {
483 binding = this->layoutInt();
484 } else if (t.fText == "index") {
485 index = this->layoutInt();
486 } else if (t.fText == "set") {
487 set = this->layoutInt();
488 } else if (t.fText == "builtin") {
489 builtin = this->layoutInt();
490 } else {
491 this->error(t.fPosition, ("'" + t.fText +
492 "' is not a valid layout qualifier").c _str());
493 }
494 if (this->peek().fKind == Token::RPAREN) {
495 this->nextToken();
496 break;
497 }
498 if (!this->expect(Token::COMMA, "','")) {
499 break;
500 }
501 }
502 }
503 return ASTLayout(location, binding, index, set, builtin);
504 }
505
506 /* layout? (UNIFORM | CONST | IN | OUT | INOUT | LOWP |
507 MEDIUMP | HIGHP)* */
508 ASTModifiers Parser::modifiers() {
509 ASTLayout layout = this->layout();
510 int flags = 0;
511 for (;;) {
512 // TODO: handle duplicate / incompatible flags
513 switch (peek().fKind) {
514 case Token::UNIFORM:
515 this->nextToken();
516 flags |= ASTModifiers::kUniform_Flag;
517 break;
518 case Token::CONST:
519 this->nextToken();
520 flags |= ASTModifiers::kConst_Flag;
521 break;
522 case Token::IN:
523 this->nextToken();
524 flags |= ASTModifiers::kIn_Flag;
525 break;
526 case Token::OUT:
527 this->nextToken();
528 flags |= ASTModifiers::kOut_Flag;
529 break;
530 case Token::INOUT:
531 this->nextToken();
532 flags |= ASTModifiers::kIn_Flag;
533 flags |= ASTModifiers::kOut_Flag;
534 break;
535 case Token::LOWP:
536 this->nextToken();
537 flags |= ASTModifiers::kLowp_Flag;
538 break;
539 case Token::MEDIUMP:
540 this->nextToken();
541 flags |= ASTModifiers::kMediump_Flag;
542 break;
543 case Token::HIGHP:
544 this->nextToken();
545 flags |= ASTModifiers::kHighp_Flag;
546 break;
547 default:
548 return ASTModifiers(layout, flags);
549 }
550 }
551 }
552
553 /* ifStatement | forStatement | doStatement | whileStatement | block | expressio n */
554 std::unique_ptr<ASTStatement> Parser::statement() {
555 Token start = this->peek();
556 switch (start.fKind) {
557 case Token::IF:
558 return this->ifStatement();
559 case Token::FOR:
560 return this->forStatement();
561 case Token::DO:
562 return this->doStatement();
563 case Token::WHILE:
564 return this->whileStatement();
565 case Token::RETURN:
566 return this->returnStatement();
567 case Token::BREAK:
568 return this->breakStatement();
569 case Token::CONTINUE:
570 return this->continueStatement();
571 case Token::DISCARD:
572 return this->discardStatement();
573 case Token::LBRACE:
574 return this->block();
575 case Token::SEMICOLON:
576 this->nextToken();
577 return std::unique_ptr<ASTStatement>(new ASTBlock(start.fPosition, { }));
578 case Token::CONST: // fall through
579 case Token::HIGHP: // fall through
580 case Token::MEDIUMP: // fall through
581 case Token::LOWP: {
582 auto decl = this->varDeclaration();
583 if (decl == nullptr) {
584 return nullptr;
585 }
586 return std::unique_ptr<ASTStatement>(new ASTVarDeclarationStatement( std::move(decl)));
587 }
588 case Token::IDENTIFIER:
589 if (this->isType(start.fText)) {
590 auto decl = this->varDeclaration();
591 if (decl == nullptr) {
592 return nullptr;
593 }
594 return std::unique_ptr<ASTStatement>(new ASTVarDeclarationStatem ent(
595 std::move(decl)));
596 }
597 // fall through
598 default:
599 return this->expressionStatement();
600 }
601 }
602
603 /* IDENTIFIER(type) */
604 std::unique_ptr<ASTType> Parser::type() {
605 Token type;
606 if (!this->expect(Token::IDENTIFIER, "a type", &type)) {
607 return nullptr;
608 }
609 if (!this->isType(type.fText)) {
610 this->error(type.fPosition, ("no type named '" + type.fText + "'").c_str ());
611 return nullptr;
612 }
613 return std::unique_ptr<ASTType>(new ASTType(type.fPosition, std::move(type.f Text),
614 ASTType::kIdentifier_Kind));
615 }
616
617 /* IDENTIFIER LBRACE varDeclaration* RBRACE */
618 std::unique_ptr<ASTDeclaration> Parser::interfaceBlock(ASTModifiers mods) {
619 Token name;
620 if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) {
621 return nullptr;
622 }
623 if (peek().fKind != Token::LBRACE) {
624 // we only get into interfaceBlock if we found a top-level identifier wh ich was not a type.
625 // 99% of the time, the user was not actually intending to create an int erface block, so
626 // it's better to report it as an unknown type
627 this->error(name.fPosition, "no type named '" + name.fText + "'");
628 return nullptr;
629 }
630 this->nextToken();
631 std::vector<std::unique_ptr<ASTVarDeclaration>> decls;
632 while (this->peek().fKind != Token::RBRACE) {
633 std::unique_ptr<ASTVarDeclaration> decl = this->varDeclaration();
634 if (decl == nullptr) {
635 return nullptr;
636 }
637 decls.push_back(std::move(decl));
638 }
639 this->nextToken();
640 std::string valueName;
641 if (this->peek().fKind == Token::IDENTIFIER) {
642 valueName = this->nextToken().fText;
643 }
644 this->expect(Token::SEMICOLON, "';'");
645 return std::unique_ptr<ASTDeclaration>(new ASTInterfaceBlock(name.fPosition, mods,
646 name.fText, std ::move(valueName),
647 std::move(decls )));
648 }
649
650 /* IF LPAREN expression RPAREN statement (ELSE statement)? */
651 std::unique_ptr<ASTIfStatement> Parser::ifStatement() {
652 Token start;
653 if (!this->expect(Token::IF, "'if'", &start)) {
654 return nullptr;
655 }
656 if (!this->expect(Token::LPAREN, "'('")) {
657 return nullptr;
658 }
659 std::unique_ptr<ASTExpression> test(this->expression());
660 if (test == nullptr) {
661 return nullptr;
662 }
663 if (!this->expect(Token::RPAREN, "')'")) {
664 return nullptr;
665 }
666 std::unique_ptr<ASTStatement> ifTrue(this->statement());
667 if (ifTrue == nullptr) {
668 return nullptr;
669 }
670 std::unique_ptr<ASTStatement> ifFalse;
671 if (this->peek().fKind == Token::ELSE) {
672 this->nextToken();
673 ifFalse = this->statement();
674 if (!ifFalse) {
675 return nullptr;
676 }
677 }
678 return std::unique_ptr<ASTIfStatement>(new ASTIfStatement(start.fPosition, s td::move(test),
679 std::move(ifTrue),
680 std::move(ifFalse) ));
681 }
682
683 /* DO statement WHILE LPAREN expression RPAREN SEMICOLON */
684 std::unique_ptr<ASTDoStatement> Parser::doStatement() {
685 Token start;
686 if (!this->expect(Token::DO, "'do'", &start)) {
687 return nullptr;
688 }
689 std::unique_ptr<ASTStatement> statement(this->statement());
690 if (statement == nullptr) {
691 return nullptr;
692 }
693 if (!this->expect(Token::WHILE, "'while'")) {
694 return nullptr;
695 }
696 if (!this->expect(Token::LPAREN, "'('")) {
697 return nullptr;
698 }
699 std::unique_ptr<ASTExpression> test(this->expression());
700 if (test == nullptr) {
701 return nullptr;
702 }
703 if (!this->expect(Token::RPAREN, "')'")) {
704 return nullptr;
705 }
706 if (!this->expect(Token::SEMICOLON, "';'")) {
707 return nullptr;
708 }
709 return std::unique_ptr<ASTDoStatement>(new ASTDoStatement(start.fPosition,
710 std::move(statemen t),
711 std::move(test)));
712 }
713
714 /* WHILE LPAREN expression RPAREN STATEMENT */
715 std::unique_ptr<ASTWhileStatement> Parser::whileStatement() {
716 Token start;
717 if (!this->expect(Token::WHILE, "'while'", &start)) {
718 return nullptr;
719 }
720 if (!this->expect(Token::LPAREN, "'('")) {
721 return nullptr;
722 }
723 std::unique_ptr<ASTExpression> test(this->expression());
724 if (test == nullptr) {
725 return nullptr;
726 }
727 if (!this->expect(Token::RPAREN, "')'")) {
728 return nullptr;
729 }
730 std::unique_ptr<ASTStatement> statement(this->statement());
731 if (statement == nullptr) {
732 return nullptr;
733 }
734 return std::unique_ptr<ASTWhileStatement>(new ASTWhileStatement(start.fPosit ion,
735 std::move(te st),
736 std::move(st atement)));
737 }
738
739 /* FOR LPAREN (declaration | expression)? SEMICOLON expression? SEMICOLON expres sion? RPAREN
740 STATEMENT */
741 std::unique_ptr<ASTForStatement> Parser::forStatement() {
742 Token start;
743 if (!this->expect(Token::FOR, "'for'", &start)) {
744 return nullptr;
745 }
746 if (!this->expect(Token::LPAREN, "'('")) {
747 return nullptr;
748 }
749 std::unique_ptr<ASTStatement> initializer;
750 Token nextToken = this->peek();
751 switch (nextToken.fKind) {
752 case Token::SEMICOLON:
753 break;
754 case Token::CONST:
755 initializer = std::unique_ptr<ASTStatement>(new ASTVarDeclarationSta tement(
756 this- >varDeclaration()));
757 break;
758 case Token::IDENTIFIER:
759 if (this->isType(nextToken.fText)) {
760 initializer = std::unique_ptr<ASTStatement>(new ASTVarDeclaratio nStatement(
761 this- >varDeclaration()));
762 break;
763 }
764 // fall through
765 default:
766 initializer = this->expressionStatement();
767 }
768 std::unique_ptr<ASTExpression> test;
769 if (this->peek().fKind != Token::SEMICOLON) {
770 test = this->expression();
771 if (!test) {
772 return nullptr;
773 }
774 }
775 if (!this->expect(Token::SEMICOLON, "';'")) {
776 return nullptr;
777 }
778 std::unique_ptr<ASTExpression> next;
779 if (this->peek().fKind != Token::SEMICOLON) {
780 next = this->expression();
781 if (!next) {
782 return nullptr;
783 }
784 }
785 if (!this->expect(Token::RPAREN, "')'")) {
786 return nullptr;
787 }
788 std::unique_ptr<ASTStatement> statement(this->statement());
789 if (statement == nullptr) {
790 return nullptr;
791 }
792 return std::unique_ptr<ASTForStatement>(new ASTForStatement(start.fPosition,
793 std::move(initia lizer),
794 std::move(test), std::move(next),
795 std::move(statem ent)));
796 }
797
798 /* RETURN expression? SEMICOLON */
799 std::unique_ptr<ASTReturnStatement> Parser::returnStatement() {
800 Token start;
801 if (!this->expect(Token::RETURN, "'return'", &start)) {
802 return nullptr;
803 }
804 std::unique_ptr<ASTExpression> expression;
805 if (this->peek().fKind != Token::SEMICOLON) {
806 expression = this->expression();
807 if (!expression) {
808 return nullptr;
809 }
810 }
811 if (!this->expect(Token::SEMICOLON, "';'")) {
812 return nullptr;
813 }
814 return std::unique_ptr<ASTReturnStatement>(new ASTReturnStatement(start.fPos ition,
815 std::move( expression)));
816 }
817
818 /* BREAK SEMICOLON */
819 std::unique_ptr<ASTBreakStatement> Parser::breakStatement() {
820 Token start;
821 if (!this->expect(Token::BREAK, "'break'", &start)) {
822 return nullptr;
823 }
824 if (!this->expect(Token::SEMICOLON, "';'")) {
825 return nullptr;
826 }
827 return std::unique_ptr<ASTBreakStatement>(new ASTBreakStatement(start.fPosit ion));
828 }
829
830 /* CONTINUE SEMICOLON */
831 std::unique_ptr<ASTContinueStatement> Parser::continueStatement() {
832 Token start;
833 if (!this->expect(Token::CONTINUE, "'continue'", &start)) {
834 return nullptr;
835 }
836 if (!this->expect(Token::SEMICOLON, "';'")) {
837 return nullptr;
838 }
839 return std::unique_ptr<ASTContinueStatement>(new ASTContinueStatement(start. fPosition));
840 }
841
842 /* DISCARD SEMICOLON */
843 std::unique_ptr<ASTDiscardStatement> Parser::discardStatement() {
844 Token start;
845 if (!this->expect(Token::DISCARD, "'continue'", &start)) {
846 return nullptr;
847 }
848 if (!this->expect(Token::SEMICOLON, "';'")) {
849 return nullptr;
850 }
851 return std::unique_ptr<ASTDiscardStatement>(new ASTDiscardStatement(start.fP osition));
852 }
853
854 /* LBRACE statement* RBRACE */
855 std::unique_ptr<ASTBlock> Parser::block() {
856 Token start;
857 if (!this->expect(Token::LBRACE, "'{'", &start)) {
858 return nullptr;
859 }
860 std::vector<std::unique_ptr<ASTStatement>> statements;
861 for (;;) {
862 switch (this->peek().fKind) {
863 case Token::RBRACE:
864 this->nextToken();
865 return std::unique_ptr<ASTBlock>(new ASTBlock(start.fPosition,
866 std::move(statemen ts)));
867 case Token::END_OF_FILE:
868 this->error(this->peek().fPosition, "expected '}', but found end of file");
869 return nullptr;
870 default: {
871 std::unique_ptr<ASTStatement> statement = this->statement();
872 if (statement == nullptr) {
873 return nullptr;
874 }
875 statements.push_back(std::move(statement));
876 }
877 }
878 }
879 }
880
881 /* expression SEMICOLON */
882 std::unique_ptr<ASTExpressionStatement> Parser::expressionStatement() {
883 std::unique_ptr<ASTExpression> expr = this->expression();
884 if (expr) {
885 if (this->expect(Token::SEMICOLON, "';'")) {
886 ASTExpressionStatement* result = new ASTExpressionStatement(std::mov e(expr));
887 return std::unique_ptr<ASTExpressionStatement>(result);
888 }
889 }
890 return nullptr;
891 }
892
893 /* assignmentExpression */
894 std::unique_ptr<ASTExpression> Parser::expression() {
895 return this->assignmentExpression();
896 }
897
898 /* ternaryExpression ((EQEQ | STAREQ | SLASHEQ | PERCENTEQ | PLUSEQ | MINUSEQ | SHLEQ | SHREQ |
899 BITWISEANDEQ | BITWISEXOREQ | BITWISEOREQ | LOGICALANDEQ | LOGICALXOREQ | LOG ICALOREQ)
900 assignmentExpression)*
901 */
902 std::unique_ptr<ASTExpression> Parser::assignmentExpression() {
903 std::unique_ptr<ASTExpression> result = this->ternaryExpression();
904 if (!result) {
905 return nullptr;
906 }
907 for (;;) {
908 switch (this->peek().fKind) {
909 case Token::EQ: // fall through
910 case Token::STAREQ: // fall through
911 case Token::SLASHEQ: // fall through
912 case Token::PERCENTEQ: // fall through
913 case Token::PLUSEQ: // fall through
914 case Token::MINUSEQ: // fall through
915 case Token::SHLEQ: // fall through
916 case Token::SHREQ: // fall through
917 case Token::BITWISEANDEQ: // fall through
918 case Token::BITWISEXOREQ: // fall through
919 case Token::BITWISEOREQ: // fall through
920 case Token::LOGICALANDEQ: // fall through
921 case Token::LOGICALXOREQ: // fall through
922 case Token::LOGICALOREQ: {
923 Token t = this->nextToken();
924 std::unique_ptr<ASTExpression> right = this->assignmentExpressio n();
925 if (right == nullptr) {
926 return nullptr;
927 }
928 result = std::unique_ptr<ASTExpression>(new ASTBinaryExpression( std::move(result),
929 t,
930 std::move(right)));
931 }
932 default:
933 return result;
934 }
935 }
936 }
937
938 /* logicalOrExpression ('?' expression ':' assignmentExpression)? */
939 std::unique_ptr<ASTExpression> Parser::ternaryExpression() {
940 std::unique_ptr<ASTExpression> result = this->logicalOrExpression();
941 if (result == nullptr) {
942 return nullptr;
943 }
944 if (this->peek().fKind == Token::QUESTION) {
945 Token question = this->nextToken();
946 std::unique_ptr<ASTExpression> trueExpr = this->expression();
947 if (trueExpr == nullptr) {
948 return nullptr;
949 }
950 if (this->expect(Token::COLON, "':'")) {
951 std::unique_ptr<ASTExpression> falseExpr = this->assignmentExpressio n();
952 return std::unique_ptr<ASTExpression>(new ASTTernaryExpression(std:: move(result),
953 std:: move(trueExpr),
954 std:: move(falseExpr)));
955 }
956 return nullptr;
957 }
958 return result;
959 }
960
961 /* logicalXorExpression (LOGICALOR logicalXorExpression)* */
962 std::unique_ptr<ASTExpression> Parser::logicalOrExpression() {
963 std::unique_ptr<ASTExpression> result = this->logicalXorExpression();
964 if (result == nullptr) {
965 return nullptr;
966 }
967 while (this->peek().fKind == Token::LOGICALOR) {
968 Token t = this->nextToken();
969 std::unique_ptr<ASTExpression> right = this->logicalXorExpression();
970 if (right == nullptr) {
971 return nullptr;
972 }
973 result.reset(new ASTBinaryExpression(std::move(result), t, std::move(rig ht)));
974 }
975 return result;
976 }
977
978 /* logicalAndExpression (LOGICALXOR logicalAndExpression)* */
979 std::unique_ptr<ASTExpression> Parser::logicalXorExpression() {
980 std::unique_ptr<ASTExpression> result = this->logicalAndExpression();
981 if (result == nullptr) {
982 return nullptr;
983 }
984 while (this->peek().fKind == Token::LOGICALXOR) {
985 Token t = this->nextToken();
986 std::unique_ptr<ASTExpression> right = this->logicalAndExpression();
987 if (right == nullptr) {
988 return nullptr;
989 }
990 result.reset(new ASTBinaryExpression(std::move(result), t, std::move(rig ht)));
991 }
992 return result;
993 }
994
995 /* bitwiseOrExpression (LOGICALAND bitwiseOrExpression)* */
996 std::unique_ptr<ASTExpression> Parser::logicalAndExpression() {
997 std::unique_ptr<ASTExpression> result = this->bitwiseOrExpression();
998 if (result == nullptr) {
999 return nullptr;
1000 }
1001 while (this->peek().fKind == Token::LOGICALAND) {
1002 Token t = this->nextToken();
1003 std::unique_ptr<ASTExpression> right = this->bitwiseOrExpression();
1004 if (right == nullptr) {
1005 return nullptr;
1006 }
1007 result.reset(new ASTBinaryExpression(std::move(result), t, std::move(rig ht)));
1008 }
1009 return result;
1010 }
1011
1012 /* bitwiseXorExpression (BITWISEOR bitwiseXorExpression)* */
1013 std::unique_ptr<ASTExpression> Parser::bitwiseOrExpression() {
1014 std::unique_ptr<ASTExpression> result = this->bitwiseXorExpression();
1015 if (result == nullptr) {
1016 return nullptr;
1017 }
1018 while (this->peek().fKind == Token::BITWISEOR) {
1019 Token t = this->nextToken();
1020 std::unique_ptr<ASTExpression> right = this->bitwiseXorExpression();
1021 if (right == nullptr) {
1022 return nullptr;
1023 }
1024 result.reset(new ASTBinaryExpression(std::move(result), t, std::move(rig ht)));
1025 }
1026 return result;
1027 }
1028
1029 /* bitwiseAndExpression (BITWISEXOR bitwiseAndExpression)* */
1030 std::unique_ptr<ASTExpression> Parser::bitwiseXorExpression() {
1031 std::unique_ptr<ASTExpression> result = this->bitwiseAndExpression();
1032 if (result == nullptr) {
1033 return nullptr;
1034 }
1035 while (this->peek().fKind == Token::BITWISEXOR) {
1036 Token t = this->nextToken();
1037 std::unique_ptr<ASTExpression> right = this->bitwiseAndExpression();
1038 if (right == nullptr) {
1039 return nullptr;
1040 }
1041 result.reset(new ASTBinaryExpression(std::move(result), t, std::move(rig ht)));
1042 }
1043 return result;
1044 }
1045
1046 /* equalityExpression (BITWISEAND equalityExpression)* */
1047 std::unique_ptr<ASTExpression> Parser::bitwiseAndExpression() {
1048 std::unique_ptr<ASTExpression> result = this->equalityExpression();
1049 if (result == nullptr) {
1050 return nullptr;
1051 }
1052 while (this->peek().fKind == Token::BITWISEAND) {
1053 Token t = this->nextToken();
1054 std::unique_ptr<ASTExpression> right = this->equalityExpression();
1055 if (right == nullptr) {
1056 return nullptr;
1057 }
1058 result.reset(new ASTBinaryExpression(std::move(result), t, std::move(rig ht)));
1059 }
1060 return result;
1061 }
1062
1063 /* relationalExpression ((EQEQ | NEQ) relationalExpression)* */
1064 std::unique_ptr<ASTExpression> Parser::equalityExpression() {
1065 std::unique_ptr<ASTExpression> result = this->relationalExpression();
1066 if (result == nullptr) {
1067 return nullptr;
1068 }
1069 for (;;) {
1070 switch (this->peek().fKind) {
1071 case Token::EQEQ: // fall through
1072 case Token::NEQ: {
1073 Token t = this->nextToken();
1074 std::unique_ptr<ASTExpression> right = this->relationalExpressio n();
1075 if (right == nullptr) {
1076 return nullptr;
1077 }
1078 result.reset(new ASTBinaryExpression(std::move(result), t, std:: move(right)));
1079 break;
1080 }
1081 default:
1082 return result;
1083 }
1084 }
1085 }
1086
1087 /* shiftExpression ((LT | GT | LTEQ | GTEQ) shiftExpression)* */
1088 std::unique_ptr<ASTExpression> Parser::relationalExpression() {
1089 std::unique_ptr<ASTExpression> result = this->shiftExpression();
1090 if (result == nullptr) {
1091 return nullptr;
1092 }
1093 for (;;) {
1094 switch (this->peek().fKind) {
1095 case Token::LT: // fall through
1096 case Token::GT: // fall through
1097 case Token::LTEQ: // fall through
1098 case Token::GTEQ: {
1099 Token t = this->nextToken();
1100 std::unique_ptr<ASTExpression> right = this->shiftExpression();
1101 if (right == nullptr) {
1102 return nullptr;
1103 }
1104 result.reset(new ASTBinaryExpression(std::move(result), t, std:: move(right)));
1105 break;
1106 }
1107 default:
1108 return result;
1109 }
1110 }
1111 }
1112
1113 /* additiveExpression ((SHL | SHR) additiveExpression)* */
1114 std::unique_ptr<ASTExpression> Parser::shiftExpression() {
1115 std::unique_ptr<ASTExpression> result = this->additiveExpression();
1116 if (result == nullptr) {
1117 return nullptr;
1118 }
1119 for (;;) {
1120 switch (this->peek().fKind) {
1121 case Token::SHL: // fall through
1122 case Token::SHR: {
1123 Token t = this->nextToken();
1124 std::unique_ptr<ASTExpression> right = this->additiveExpression( );
1125 if (right == nullptr) {
1126 return nullptr;
1127 }
1128 result.reset(new ASTBinaryExpression(std::move(result), t, std:: move(right)));
1129 break;
1130 }
1131 default:
1132 return result;
1133 }
1134 }
1135 }
1136
1137 /* multiplicativeExpression ((PLUS | MINUS) multiplicativeExpression)* */
1138 std::unique_ptr<ASTExpression> Parser::additiveExpression() {
1139 std::unique_ptr<ASTExpression> result = this->multiplicativeExpression();
1140 if (result == nullptr) {
1141 return nullptr;
1142 }
1143 for (;;) {
1144 switch (this->peek().fKind) {
1145 case Token::PLUS: // fall through
1146 case Token::MINUS: {
1147 Token t = this->nextToken();
1148 std::unique_ptr<ASTExpression> right = this->multiplicativeExpre ssion();
1149 if (right == nullptr) {
1150 return nullptr;
1151 }
1152 result.reset(new ASTBinaryExpression(std::move(result), t, std:: move(right)));
1153 break;
1154 }
1155 default:
1156 return result;
1157 }
1158 }
1159 }
1160
1161 /* unaryExpression ((STAR | SLASH | PERCENT) unaryExpression)* */
1162 std::unique_ptr<ASTExpression> Parser::multiplicativeExpression() {
1163 std::unique_ptr<ASTExpression> result = this->unaryExpression();
1164 if (result == nullptr) {
1165 return nullptr;
1166 }
1167 for (;;) {
1168 switch (this->peek().fKind) {
1169 case Token::STAR: // fall through
1170 case Token::SLASH: // fall through
1171 case Token::PERCENT: {
1172 Token t = this->nextToken();
1173 std::unique_ptr<ASTExpression> right = this->unaryExpression();
1174 if (right == nullptr) {
1175 return nullptr;
1176 }
1177 result.reset(new ASTBinaryExpression(std::move(result), t, std:: move(right)));
1178 break;
1179 }
1180 default:
1181 return result;
1182 }
1183 }
1184 }
1185
1186 /* postfixExpression | (PLUS | MINUS | NOT | PLUSPLUS | MINUSMINUS) unaryExpress ion */
1187 std::unique_ptr<ASTExpression> Parser::unaryExpression() {
1188 switch (this->peek().fKind) {
1189 case Token::PLUS: // fall through
1190 case Token::MINUS: // fall through
1191 case Token::NOT: // fall through
1192 case Token::PLUSPLUS: // fall through
1193 case Token::MINUSMINUS: {
1194 Token t = this->nextToken();
1195 std::unique_ptr<ASTExpression> expr = this->unaryExpression();
1196 if (expr == nullptr) {
1197 return nullptr;
1198 }
1199 return std::unique_ptr<ASTExpression>(new ASTPrefixExpression(t, std ::move(expr)));
1200 }
1201 default:
1202 return this->postfixExpression();
1203 }
1204 }
1205
1206 /* term suffix* */
1207 std::unique_ptr<ASTExpression> Parser::postfixExpression() {
1208 std::unique_ptr<ASTExpression> result = this->term();
1209 if (result == nullptr) {
1210 return nullptr;
1211 }
1212 for (;;) {
1213 switch (this->peek().fKind) {
1214 case Token::LBRACKET: // fall through
1215 case Token::DOT: // fall through
1216 case Token::LPAREN: // fall through
1217 case Token::PLUSPLUS: // fall through
1218 case Token::MINUSMINUS: {
1219 std::unique_ptr<ASTSuffix> s = this->suffix();
1220 if (s == nullptr) {
1221 return nullptr;
1222 }
1223 result.reset(new ASTSuffixExpression(std::move(result), std::mov e(s)));
1224 break;
1225 }
1226 default:
1227 return result;
1228 }
1229 }
1230 }
1231
1232 /* LBRACKET expression RBRACKET | DOT IDENTIFIER | LPAREN parameters RPAREN |
1233 PLUSPLUS | MINUSMINUS */
1234 std::unique_ptr<ASTSuffix> Parser::suffix() {
1235 Token next = this->nextToken();
1236 switch (next.fKind) {
1237 case Token::LBRACKET: {
1238 std::unique_ptr<ASTExpression> e = this->expression();
1239 if (e == nullptr) {
1240 return nullptr;
1241 }
1242 this->expect(Token::RBRACKET, "']' to complete array access expressi on");
1243 return std::unique_ptr<ASTSuffix>(new ASTIndexSuffix(std::move(e)));
1244 }
1245 case Token::DOT: {
1246 Position pos = this->peek().fPosition;
1247 std::string text;
1248 if (this->identifier(&text)) {
1249 return std::unique_ptr<ASTSuffix>(new ASTFieldSuffix(pos, std::m ove(text)));
1250 }
1251 return nullptr;
1252 }
1253 case Token::LPAREN: {
1254 std::vector<std::unique_ptr<ASTExpression>> parameters;
1255 if (this->peek().fKind != Token::RPAREN) {
1256 for (;;) {
1257 std::unique_ptr<ASTExpression> expr = this->expression();
1258 if (expr == nullptr) {
1259 return nullptr;
1260 }
1261 parameters.push_back(std::move(expr));
1262 if (this->peek().fKind != Token::COMMA) {
1263 break;
1264 }
1265 this->nextToken();
1266 }
1267 }
1268 this->expect(Token::RPAREN, "')' to complete function parameters");
1269 return std::unique_ptr<ASTSuffix>(new ASTCallSuffix(next.fPosition,
1270 std::move(parame ters)));
1271 }
1272 case Token::PLUSPLUS:
1273 return std::unique_ptr<ASTSuffix>(new ASTSuffix(next.fPosition,
1274 ASTSuffix::kPostIncr ement_Kind));
1275 case Token::MINUSMINUS:
1276 return std::unique_ptr<ASTSuffix>(new ASTSuffix(next.fPosition,
1277 ASTSuffix::kPostDecr ement_Kind));
1278 default: {
1279 this->error(next.fPosition, "expected expression suffix, but found '" + next.fText +
1280 "'\n");
1281 return nullptr;
1282 }
1283 }
1284 }
1285
1286 /* IDENTIFIER | intLiteral | floatLiteral | boolLiteral | '(' expression ')' */
1287 std::unique_ptr<ASTExpression> Parser::term() {
1288 std::unique_ptr<ASTExpression> result;
1289 Token t = this->peek();
1290 switch (t.fKind) {
1291 case Token::IDENTIFIER: {
1292 std::string text;
1293 if (this->identifier(&text)) {
1294 result.reset(new ASTIdentifier(t.fPosition, std::move(text)));
1295 }
1296 break;
1297 }
1298 case Token::INT_LITERAL: {
1299 int64_t i;
1300 if (this->intLiteral(&i)) {
1301 result.reset(new ASTIntLiteral(t.fPosition, i));
1302 }
1303 break;
1304 }
1305 case Token::FLOAT_LITERAL: {
1306 double f;
1307 if (this->floatLiteral(&f)) {
1308 result.reset(new ASTFloatLiteral(t.fPosition, f));
1309 }
1310 break;
1311 }
1312 case Token::TRUE_LITERAL: // fall through
1313 case Token::FALSE_LITERAL: {
1314 bool b;
1315 if (this->boolLiteral(&b)) {
1316 result.reset(new ASTBoolLiteral(t.fPosition, b));
1317 }
1318 break;
1319 }
1320 case Token::LPAREN: {
1321 this->nextToken();
1322 result = this->expression();
1323 if (result) {
1324 this->expect(Token::RPAREN, "')' to complete expression");
1325 }
1326 break;
1327 }
1328 default:
1329 this->nextToken();
1330 this->error(t.fPosition, "expected expression, but found '" + t.fTe xt + "'\n");
1331 result = nullptr;
1332 }
1333 return result;
1334 }
1335
1336 /* INT_LITERAL */
1337 bool Parser::intLiteral(int64_t* dest) {
1338 Token t;
1339 if (this->expect(Token::INT_LITERAL, "integer literal", &t)) {
1340 *dest = SkSL::stol(t.fText);
1341 return true;
1342 }
1343 return false;
1344 }
1345
1346 /* FLOAT_LITERAL */
1347 bool Parser::floatLiteral(double* dest) {
1348 Token t;
1349 if (this->expect(Token::FLOAT_LITERAL, "float literal", &t)) {
1350 *dest = SkSL::stod(t.fText);
1351 return true;
1352 }
1353 return false;
1354 }
1355
1356 /* TRUE_LITERAL | FALSE_LITERAL */
1357 bool Parser::boolLiteral(bool* dest) {
1358 Token t = this->nextToken();
1359 switch (t.fKind) {
1360 case Token::TRUE_LITERAL:
1361 *dest = true;
1362 return true;
1363 case Token::FALSE_LITERAL:
1364 *dest = false;
1365 return true;
1366 default:
1367 this->error(t.fPosition, "expected 'true' or 'false', but found '" + t.fText + "'\n");
1368 return false;
1369 }
1370 }
1371
1372 /* IDENTIFIER */
1373 bool Parser::identifier(std::string* dest) {
1374 Token t;
1375 if (this->expect(Token::IDENTIFIER, "identifier", &t)) {
1376 *dest = t.fText;
1377 return true;
1378 }
1379 return false;
1380 }
1381
1382 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698