• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ast/SkSLASTBinaryExpression.h"
11 #include "ast/SkSLASTBlock.h"
12 #include "ast/SkSLASTBoolLiteral.h"
13 #include "ast/SkSLASTBreakStatement.h"
14 #include "ast/SkSLASTCallSuffix.h"
15 #include "ast/SkSLASTContinueStatement.h"
16 #include "ast/SkSLASTDiscardStatement.h"
17 #include "ast/SkSLASTDoStatement.h"
18 #include "ast/SkSLASTEnum.h"
19 #include "ast/SkSLASTExpression.h"
20 #include "ast/SkSLASTExpressionStatement.h"
21 #include "ast/SkSLASTExtension.h"
22 #include "ast/SkSLASTFieldSuffix.h"
23 #include "ast/SkSLASTFloatLiteral.h"
24 #include "ast/SkSLASTForStatement.h"
25 #include "ast/SkSLASTFunction.h"
26 #include "ast/SkSLASTIdentifier.h"
27 #include "ast/SkSLASTIfStatement.h"
28 #include "ast/SkSLASTIndexSuffix.h"
29 #include "ast/SkSLASTInterfaceBlock.h"
30 #include "ast/SkSLASTIntLiteral.h"
31 #include "ast/SkSLASTModifiersDeclaration.h"
32 #include "ast/SkSLASTNullLiteral.h"
33 #include "ast/SkSLASTParameter.h"
34 #include "ast/SkSLASTPrefixExpression.h"
35 #include "ast/SkSLASTReturnStatement.h"
36 #include "ast/SkSLASTSection.h"
37 #include "ast/SkSLASTStatement.h"
38 #include "ast/SkSLASTSuffixExpression.h"
39 #include "ast/SkSLASTSwitchCase.h"
40 #include "ast/SkSLASTSwitchStatement.h"
41 #include "ast/SkSLASTTernaryExpression.h"
42 #include "ast/SkSLASTType.h"
43 #include "ast/SkSLASTVarDeclaration.h"
44 #include "ast/SkSLASTVarDeclarationStatement.h"
45 #include "ast/SkSLASTWhileStatement.h"
46 #include "ir/SkSLSymbolTable.h"
47 #include "ir/SkSLModifiers.h"
48 #include "ir/SkSLType.h"
49 
50 #ifndef SKSL_STANDALONE
51 #include "SkOnce.h"
52 #endif
53 
54 namespace SkSL {
55 
56 #define MAX_PARSE_DEPTH 50
57 
58 class AutoDepth {
59 public:
AutoDepth(Parser * p)60     AutoDepth(Parser* p)
61     : fParser(p) {
62         fParser->fDepth++;
63     }
64 
~AutoDepth()65     ~AutoDepth() {
66         fParser->fDepth--;
67     }
68 
checkValid()69     bool checkValid() {
70         if (fParser->fDepth > MAX_PARSE_DEPTH) {
71             fParser->error(fParser->peek(), String("exceeded max parse depth"));
72             return false;
73         }
74         return true;
75     }
76 
77 private:
78     Parser* fParser;
79 };
80 
81 std::unordered_map<String, Parser::LayoutToken>* Parser::layoutTokens;
82 
InitLayoutMap()83 void Parser::InitLayoutMap() {
84     layoutTokens = new std::unordered_map<String, LayoutToken>;
85     #define TOKEN(name, text) (*layoutTokens)[text] = LayoutToken::name
86     TOKEN(LOCATION,                     "location");
87     TOKEN(OFFSET,                       "offset");
88     TOKEN(BINDING,                      "binding");
89     TOKEN(INDEX,                        "index");
90     TOKEN(SET,                          "set");
91     TOKEN(BUILTIN,                      "builtin");
92     TOKEN(INPUT_ATTACHMENT_INDEX,       "input_attachment_index");
93     TOKEN(ORIGIN_UPPER_LEFT,            "origin_upper_left");
94     TOKEN(OVERRIDE_COVERAGE,            "override_coverage");
95     TOKEN(BLEND_SUPPORT_ALL_EQUATIONS,  "blend_support_all_equations");
96     TOKEN(BLEND_SUPPORT_MULTIPLY,       "blend_support_multiply");
97     TOKEN(BLEND_SUPPORT_SCREEN,         "blend_support_screen");
98     TOKEN(BLEND_SUPPORT_OVERLAY,        "blend_support_overlay");
99     TOKEN(BLEND_SUPPORT_DARKEN,         "blend_support_darken");
100     TOKEN(BLEND_SUPPORT_LIGHTEN,        "blend_support_lighten");
101     TOKEN(BLEND_SUPPORT_COLORDODGE,     "blend_support_colordodge");
102     TOKEN(BLEND_SUPPORT_COLORBURN,      "blend_support_colorburn");
103     TOKEN(BLEND_SUPPORT_HARDLIGHT,      "blend_support_hardlight");
104     TOKEN(BLEND_SUPPORT_SOFTLIGHT,      "blend_support_softlight");
105     TOKEN(BLEND_SUPPORT_DIFFERENCE,     "blend_support_difference");
106     TOKEN(BLEND_SUPPORT_EXCLUSION,      "blend_support_exclusion");
107     TOKEN(BLEND_SUPPORT_HSL_HUE,        "blend_support_hsl_hue");
108     TOKEN(BLEND_SUPPORT_HSL_SATURATION, "blend_support_hsl_saturation");
109     TOKEN(BLEND_SUPPORT_HSL_COLOR,      "blend_support_hsl_color");
110     TOKEN(BLEND_SUPPORT_HSL_LUMINOSITY, "blend_support_hsl_luminosity");
111     TOKEN(PUSH_CONSTANT,                "push_constant");
112     TOKEN(POINTS,                       "points");
113     TOKEN(LINES,                        "lines");
114     TOKEN(LINE_STRIP,                   "line_strip");
115     TOKEN(LINES_ADJACENCY,              "lines_adjacency");
116     TOKEN(TRIANGLES,                    "triangles");
117     TOKEN(TRIANGLE_STRIP,               "triangle_strip");
118     TOKEN(TRIANGLES_ADJACENCY,          "triangles_adjacency");
119     TOKEN(MAX_VERTICES,                 "max_vertices");
120     TOKEN(INVOCATIONS,                  "invocations");
121     TOKEN(WHEN,                         "when");
122     TOKEN(KEY,                          "key");
123     TOKEN(TRACKED,                      "tracked");
124     TOKEN(CTYPE,                        "ctype");
125     TOKEN(SKPMCOLOR4F,                  "SkPMColor4f");
126     TOKEN(SKRECT,                       "SkRect");
127     TOKEN(SKIRECT,                      "SkIRect");
128     TOKEN(SKPMCOLOR,                    "SkPMColor");
129     #undef TOKEN
130 }
131 
Parser(const char * text,size_t length,SymbolTable & types,ErrorReporter & errors)132 Parser::Parser(const char* text, size_t length, SymbolTable& types, ErrorReporter& errors)
133 : fText(text)
134 , fPushback(Token::INVALID, -1, -1)
135 , fTypes(types)
136 , fErrors(errors) {
137     fLexer.start(text, length);
138     static const bool layoutMapInitialized = []{ return (void)InitLayoutMap(), true; }();
139     (void) layoutMapInitialized;
140 }
141 
142 /* (directive | section | declaration)* END_OF_FILE */
file()143 std::vector<std::unique_ptr<ASTDeclaration>> Parser::file() {
144     std::vector<std::unique_ptr<ASTDeclaration>> result;
145     for (;;) {
146         switch (this->peek().fKind) {
147             case Token::END_OF_FILE:
148                 return result;
149             case Token::DIRECTIVE: {
150                 std::unique_ptr<ASTDeclaration> decl = this->directive();
151                 if (decl) {
152                     result.push_back(std::move(decl));
153                 }
154                 break;
155             }
156             case Token::SECTION: {
157                 std::unique_ptr<ASTDeclaration> section = this->section();
158                 if (section) {
159                     result.push_back(std::move(section));
160                 }
161                 break;
162             }
163             default: {
164                 std::unique_ptr<ASTDeclaration> decl = this->declaration();
165                 if (!decl) {
166                     continue;
167                 }
168                 result.push_back(std::move(decl));
169             }
170         }
171     }
172 }
173 
nextRawToken()174 Token Parser::nextRawToken() {
175     if (fPushback.fKind != Token::INVALID) {
176         Token result = fPushback;
177         fPushback.fKind = Token::INVALID;
178         return result;
179     }
180     Token result = fLexer.next();
181     return result;
182 }
183 
nextToken()184 Token Parser::nextToken() {
185     Token token = this->nextRawToken();
186     while (token.fKind == Token::WHITESPACE || token.fKind == Token::LINE_COMMENT ||
187            token.fKind == Token::BLOCK_COMMENT) {
188         token = this->nextRawToken();
189     }
190     return token;
191 }
192 
pushback(Token t)193 void Parser::pushback(Token t) {
194     SkASSERT(fPushback.fKind == Token::INVALID);
195     fPushback = std::move(t);
196 }
197 
peek()198 Token Parser::peek() {
199     if (fPushback.fKind == Token::INVALID) {
200         fPushback = this->nextToken();
201     }
202     return fPushback;
203 }
204 
checkNext(Token::Kind kind,Token * result)205 bool Parser::checkNext(Token::Kind kind, Token* result) {
206     if (fPushback.fKind != Token::INVALID && fPushback.fKind != kind) {
207         return false;
208     }
209     Token next = this->nextToken();
210     if (next.fKind == kind) {
211         if (result) {
212             *result = next;
213         }
214         return true;
215     }
216     this->pushback(std::move(next));
217     return false;
218 }
219 
expect(Token::Kind kind,const char * expected,Token * result)220 bool Parser::expect(Token::Kind kind, const char* expected, Token* result) {
221     Token next = this->nextToken();
222     if (next.fKind == kind) {
223         if (result) {
224             *result = std::move(next);
225         }
226         return true;
227     } else {
228         this->error(next, "expected " + String(expected) + ", but found '" +
229                     this->text(next) + "'");
230         return false;
231     }
232 }
233 
text(Token token)234 StringFragment Parser::text(Token token) {
235     return StringFragment(fText + token.fOffset, token.fLength);
236 }
237 
error(Token token,String msg)238 void Parser::error(Token token, String msg) {
239     this->error(token.fOffset, msg);
240 }
241 
error(int offset,String msg)242 void Parser::error(int offset, String msg) {
243     fErrors.error(offset, msg);
244 }
245 
isType(StringFragment name)246 bool Parser::isType(StringFragment name) {
247     return nullptr != fTypes[name];
248 }
249 
250 /* DIRECTIVE(#version) INT_LITERAL ("es" | "compatibility")? |
251    DIRECTIVE(#extension) IDENTIFIER COLON IDENTIFIER */
directive()252 std::unique_ptr<ASTDeclaration> Parser::directive() {
253     Token start;
254     if (!this->expect(Token::DIRECTIVE, "a directive", &start)) {
255         return nullptr;
256     }
257     StringFragment text = this->text(start);
258     if (text == "#version") {
259         this->expect(Token::INT_LITERAL, "a version number");
260         Token next = this->peek();
261         StringFragment nextText = this->text(next);
262         if (nextText == "es" || nextText == "compatibility") {
263             this->nextToken();
264         }
265         // version is ignored for now; it will eventually become an error when we stop pretending
266         // to be GLSL
267         return nullptr;
268     } else if (text == "#extension") {
269         Token name;
270         if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) {
271             return nullptr;
272         }
273         if (!this->expect(Token::COLON, "':'")) {
274             return nullptr;
275         }
276         // FIXME: need to start paying attention to this token
277         if (!this->expect(Token::IDENTIFIER, "an identifier")) {
278             return nullptr;
279         }
280         return std::unique_ptr<ASTDeclaration>(new ASTExtension(start.fOffset,
281                                                                 String(this->text(name))));
282     } else {
283         this->error(start, "unsupported directive '" + this->text(start) + "'");
284         return nullptr;
285     }
286 }
287 
288 /* SECTION LBRACE (LPAREN IDENTIFIER RPAREN)? <any sequence of tokens with balanced braces>
289    RBRACE */
section()290 std::unique_ptr<ASTDeclaration> Parser::section() {
291     Token start;
292     if (!this->expect(Token::SECTION, "a section token", &start)) {
293         return nullptr;
294     }
295     String argument;
296     if (this->peek().fKind == Token::LPAREN) {
297         this->nextToken();
298         Token argToken;
299         if (!this->expect(Token::IDENTIFIER, "an identifier", &argToken)) {
300             return nullptr;
301         }
302         argument = this->text(argToken);
303         if (!this->expect(Token::RPAREN, "')'")) {
304             return nullptr;
305         }
306     }
307     if (!this->expect(Token::LBRACE, "'{'")) {
308         return nullptr;
309     }
310     String text;
311     int level = 1;
312     for (;;) {
313         Token next = this->nextRawToken();
314         switch (next.fKind) {
315             case Token::LBRACE:
316                 ++level;
317                 break;
318             case Token::RBRACE:
319                 --level;
320                 break;
321             case Token::END_OF_FILE:
322                 this->error(start, "reached end of file while parsing section");
323                 return nullptr;
324             default:
325                 break;
326         }
327         if (!level) {
328             break;
329         }
330         text += this->text(next);
331     }
332     StringFragment name = this->text(start);
333     ++name.fChars;
334     --name.fLength;
335     return std::unique_ptr<ASTDeclaration>(new ASTSection(start.fOffset,
336                                                           String(name),
337                                                           argument,
338                                                           text));
339 }
340 
341 /* ENUM CLASS IDENTIFIER LBRACE (IDENTIFIER (EQ expression)? (COMMA IDENTIFIER (EQ expression))*)?
342    RBRACE */
enumDeclaration()343 std::unique_ptr<ASTDeclaration> Parser::enumDeclaration() {
344     Token start;
345     if (!this->expect(Token::ENUM, "'enum'", &start)) {
346         return nullptr;
347     }
348     if (!this->expect(Token::CLASS, "'class'")) {
349         return nullptr;
350     }
351     Token name;
352     if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) {
353         return nullptr;
354     }
355     if (!this->expect(Token::LBRACE, "'{'")) {
356         return nullptr;
357     }
358     fTypes.add(this->text(name), std::unique_ptr<Symbol>(new Type(this->text(name),
359                                                                   Type::kEnum_Kind)));
360     std::vector<StringFragment> names;
361     std::vector<std::unique_ptr<ASTExpression>> values;
362     if (!this->checkNext(Token::RBRACE)) {
363         Token id;
364         if (!this->expect(Token::IDENTIFIER, "an identifier", &id)) {
365             return nullptr;
366         }
367         names.push_back(this->text(id));
368         if (this->checkNext(Token::EQ)) {
369             std::unique_ptr<ASTExpression> value = this->assignmentExpression();
370             if (!value) {
371                 return nullptr;
372             }
373             values.push_back(std::move(value));
374         } else {
375             values.push_back(nullptr);
376         }
377         while (!this->checkNext(Token::RBRACE)) {
378             if (!this->expect(Token::COMMA, "','")) {
379                 return nullptr;
380             }
381             if (!this->expect(Token::IDENTIFIER, "an identifier", &id)) {
382                 return nullptr;
383             }
384             names.push_back(this->text(id));
385             if (this->checkNext(Token::EQ)) {
386                 std::unique_ptr<ASTExpression> value = this->assignmentExpression();
387                 if (!value) {
388                     return nullptr;
389                 }
390                 values.push_back(std::move(value));
391             } else {
392                 values.push_back(nullptr);
393             }
394         }
395     }
396     this->expect(Token::SEMICOLON, "';'");
397     return std::unique_ptr<ASTDeclaration>(new ASTEnum(name.fOffset, this->text(name), names,
398                                                        std::move(values)));
399 }
400 
401 /* enumDeclaration | modifiers (structVarDeclaration | type IDENTIFIER ((LPAREN parameter
402    (COMMA parameter)* RPAREN (block | SEMICOLON)) | SEMICOLON) | interfaceBlock) */
declaration()403 std::unique_ptr<ASTDeclaration> Parser::declaration() {
404     Token lookahead = this->peek();
405     if (lookahead.fKind == Token::ENUM) {
406         return this->enumDeclaration();
407     }
408     Modifiers modifiers = this->modifiers();
409     lookahead = this->peek();
410     if (lookahead.fKind == Token::IDENTIFIER && !this->isType(this->text(lookahead))) {
411         // we have an identifier that's not a type, could be the start of an interface block
412         return this->interfaceBlock(modifiers);
413     }
414     if (lookahead.fKind == Token::STRUCT) {
415         return this->structVarDeclaration(modifiers);
416     }
417     if (lookahead.fKind == Token::SEMICOLON) {
418         this->nextToken();
419         return std::unique_ptr<ASTDeclaration>(new ASTModifiersDeclaration(modifiers));
420     }
421     std::unique_ptr<ASTType> type(this->type());
422     if (!type) {
423         return nullptr;
424     }
425     if (type->fKind == ASTType::kStruct_Kind && this->checkNext(Token::SEMICOLON)) {
426         return nullptr;
427     }
428     Token name;
429     if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) {
430         return nullptr;
431     }
432     if (this->checkNext(Token::LPAREN)) {
433         std::vector<std::unique_ptr<ASTParameter>> parameters;
434         while (this->peek().fKind != Token::RPAREN) {
435             if (parameters.size() > 0) {
436                 if (!this->expect(Token::COMMA, "','")) {
437                     return nullptr;
438                 }
439             }
440             std::unique_ptr<ASTParameter> parameter = this->parameter();
441             if (!parameter) {
442                 return nullptr;
443             }
444             parameters.push_back(std::move(parameter));
445         }
446         this->nextToken();
447         std::unique_ptr<ASTBlock> body;
448         if (!this->checkNext(Token::SEMICOLON)) {
449             body = this->block();
450             if (!body) {
451                 return nullptr;
452             }
453         }
454         return std::unique_ptr<ASTDeclaration>(new ASTFunction(name.fOffset,
455                                                                modifiers,
456                                                                std::move(type),
457                                                                this->text(name),
458                                                                std::move(parameters),
459                                                                std::move(body)));
460     } else {
461         return this->varDeclarationEnd(modifiers, std::move(type), this->text(name));
462     }
463 }
464 
465 /* modifiers type IDENTIFIER varDeclarationEnd */
varDeclarations()466 std::unique_ptr<ASTVarDeclarations> Parser::varDeclarations() {
467     Modifiers modifiers = this->modifiers();
468     std::unique_ptr<ASTType> type(this->type());
469     if (!type) {
470         return nullptr;
471     }
472     Token name;
473     if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) {
474         return nullptr;
475     }
476     return this->varDeclarationEnd(modifiers, std::move(type), this->text(name));
477 }
478 
479 /* STRUCT IDENTIFIER LBRACE varDeclaration* RBRACE */
structDeclaration()480 std::unique_ptr<ASTType> Parser::structDeclaration() {
481     if (!this->expect(Token::STRUCT, "'struct'")) {
482         return nullptr;
483     }
484     Token name;
485     if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) {
486         return nullptr;
487     }
488     if (!this->expect(Token::LBRACE, "'{'")) {
489         return nullptr;
490     }
491     std::vector<Type::Field> fields;
492     while (this->peek().fKind != Token::RBRACE) {
493         std::unique_ptr<ASTVarDeclarations> decl = this->varDeclarations();
494         if (!decl) {
495             return nullptr;
496         }
497         for (const auto& var : decl->fVars) {
498             auto type = (const Type*) fTypes[decl->fType->fName];
499             for (int i = (int) var.fSizes.size() - 1; i >= 0; i--) {
500                 if (!var.fSizes[i] || var.fSizes[i]->fKind != ASTExpression::kInt_Kind) {
501                     this->error(decl->fOffset, "array size in struct field must be a constant");
502                     return nullptr;
503                 }
504                 uint64_t columns = ((ASTIntLiteral&) *var.fSizes[i]).fValue;
505                 String name = type->name() + "[" + to_string(columns) + "]";
506                 type = new Type(name, Type::kArray_Kind, *type, (int) columns);
507                 fTypes.takeOwnership((Type*) type);
508             }
509             fields.push_back(Type::Field(decl->fModifiers, var.fName, type));
510             if (var.fValue) {
511                 this->error(decl->fOffset, "initializers are not permitted on struct fields");
512             }
513         }
514     }
515     if (!this->expect(Token::RBRACE, "'}'")) {
516         return nullptr;
517     }
518     fTypes.add(this->text(name), std::unique_ptr<Type>(new Type(name.fOffset, this->text(name),
519                                                                 fields)));
520     return std::unique_ptr<ASTType>(new ASTType(name.fOffset, this->text(name),
521                                                 ASTType::kStruct_Kind, std::vector<int>(), false));
522 }
523 
524 /* structDeclaration ((IDENTIFIER varDeclarationEnd) | SEMICOLON) */
structVarDeclaration(Modifiers modifiers)525 std::unique_ptr<ASTVarDeclarations> Parser::structVarDeclaration(Modifiers modifiers) {
526     std::unique_ptr<ASTType> type = this->structDeclaration();
527     if (!type) {
528         return nullptr;
529     }
530     Token name;
531     if (this->checkNext(Token::IDENTIFIER, &name)) {
532         std::unique_ptr<ASTVarDeclarations> result = this->varDeclarationEnd(modifiers,
533                                                                              std::move(type),
534                                                                              this->text(name));
535         if (result) {
536             for (const auto& var : result->fVars) {
537                 if (var.fValue) {
538                     this->error(var.fValue->fOffset,
539                                 "struct variables cannot be initialized");
540                 }
541             }
542         }
543         return result;
544     }
545     this->expect(Token::SEMICOLON, "';'");
546     return nullptr;
547 }
548 
549 /* (LBRACKET expression? RBRACKET)* (EQ assignmentExpression)? (COMMA IDENTIFER
550    (LBRACKET expression? RBRACKET)* (EQ assignmentExpression)?)* SEMICOLON */
varDeclarationEnd(Modifiers mods,std::unique_ptr<ASTType> type,StringFragment name)551 std::unique_ptr<ASTVarDeclarations> Parser::varDeclarationEnd(Modifiers mods,
552                                                               std::unique_ptr<ASTType> type,
553                                                               StringFragment name) {
554     std::vector<ASTVarDeclaration> vars;
555     std::vector<std::unique_ptr<ASTExpression>> currentVarSizes;
556     while (this->checkNext(Token::LBRACKET)) {
557         if (this->checkNext(Token::RBRACKET)) {
558             currentVarSizes.push_back(nullptr);
559         } else {
560             std::unique_ptr<ASTExpression> size(this->expression());
561             if (!size) {
562                 return nullptr;
563             }
564             currentVarSizes.push_back(std::move(size));
565             if (!this->expect(Token::RBRACKET, "']'")) {
566                 return nullptr;
567             }
568         }
569     }
570     std::unique_ptr<ASTExpression> value;
571     if (this->checkNext(Token::EQ)) {
572         value = this->assignmentExpression();
573         if (!value) {
574             return nullptr;
575         }
576     }
577     vars.emplace_back(name, std::move(currentVarSizes), std::move(value));
578     while (this->checkNext(Token::COMMA)) {
579         Token name;
580         if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) {
581             return nullptr;
582         }
583         currentVarSizes.clear();
584         value.reset();
585         while (this->checkNext(Token::LBRACKET)) {
586             if (this->checkNext(Token::RBRACKET)) {
587                 currentVarSizes.push_back(nullptr);
588             } else {
589                 std::unique_ptr<ASTExpression> size(this->expression());
590                 if (!size) {
591                     return nullptr;
592                 }
593                 currentVarSizes.push_back(std::move(size));
594                 if (!this->expect(Token::RBRACKET, "']'")) {
595                     return nullptr;
596                 }
597             }
598         }
599         if (this->checkNext(Token::EQ)) {
600             value = this->assignmentExpression();
601             if (!value) {
602                 return nullptr;
603             }
604         }
605         vars.emplace_back(this->text(name), std::move(currentVarSizes), std::move(value));
606     }
607     if (!this->expect(Token::SEMICOLON, "';'")) {
608         return nullptr;
609     }
610     return std::unique_ptr<ASTVarDeclarations>(new ASTVarDeclarations(std::move(mods),
611                                                                       std::move(type),
612                                                                       std::move(vars)));
613 }
614 
615 /* modifiers type IDENTIFIER (LBRACKET INT_LITERAL RBRACKET)? */
parameter()616 std::unique_ptr<ASTParameter> Parser::parameter() {
617     Modifiers modifiers = this->modifiersWithDefaults(0);
618     std::unique_ptr<ASTType> type = this->type();
619     if (!type) {
620         return nullptr;
621     }
622     Token name;
623     if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) {
624         return nullptr;
625     }
626     std::vector<int> sizes;
627     while (this->checkNext(Token::LBRACKET)) {
628         Token sizeToken;
629         if (!this->expect(Token::INT_LITERAL, "a positive integer", &sizeToken)) {
630             return nullptr;
631         }
632         sizes.push_back(SkSL::stoi(this->text(sizeToken)));
633         if (!this->expect(Token::RBRACKET, "']'")) {
634             return nullptr;
635         }
636     }
637     return std::unique_ptr<ASTParameter>(new ASTParameter(name.fOffset, modifiers, std::move(type),
638                                                           this->text(name), std::move(sizes)));
639 }
640 
641 /** EQ INT_LITERAL */
layoutInt()642 int Parser::layoutInt() {
643     if (!this->expect(Token::EQ, "'='")) {
644         return -1;
645     }
646     Token resultToken;
647     if (this->expect(Token::INT_LITERAL, "a non-negative integer", &resultToken)) {
648         return SkSL::stoi(this->text(resultToken));
649     }
650     return -1;
651 }
652 
653 /** EQ IDENTIFIER */
layoutIdentifier()654 StringFragment Parser::layoutIdentifier() {
655     if (!this->expect(Token::EQ, "'='")) {
656         return StringFragment();
657     }
658     Token resultToken;
659     if (!this->expect(Token::IDENTIFIER, "an identifier", &resultToken)) {
660         return StringFragment();
661     }
662     return this->text(resultToken);
663 }
664 
665 
666 /** EQ <any sequence of tokens with balanced parentheses and no top-level comma> */
layoutCode()667 String Parser::layoutCode() {
668     if (!this->expect(Token::EQ, "'='")) {
669         return "";
670     }
671     Token start = this->nextRawToken();
672     this->pushback(start);
673     String code;
674     int level = 1;
675     bool done = false;
676     while (!done) {
677         Token next = this->nextRawToken();
678         switch (next.fKind) {
679             case Token::LPAREN:
680                 ++level;
681                 break;
682             case Token::RPAREN:
683                 --level;
684                 break;
685             case Token::COMMA:
686                 if (level == 1) {
687                     done = true;
688                 }
689                 break;
690             case Token::END_OF_FILE:
691                 this->error(start, "reached end of file while parsing layout");
692                 return nullptr;
693             default:
694                 break;
695         }
696         if (!level) {
697             done = true;
698         }
699         if (done) {
700             this->pushback(std::move(next));
701         }
702         else {
703             code += this->text(next);
704         }
705     }
706     return code;
707 }
708 
709 /** (EQ IDENTIFIER('identity'))? */
layoutKey()710 Layout::Key Parser::layoutKey() {
711     if (this->peek().fKind == Token::EQ) {
712         this->expect(Token::EQ, "'='");
713         Token key;
714         if (this->expect(Token::IDENTIFIER, "an identifer", &key)) {
715             if (this->text(key) == "identity") {
716                 return Layout::kIdentity_Key;
717             } else {
718                 this->error(key, "unsupported layout key");
719             }
720         }
721     }
722     return Layout::kKey_Key;
723 }
724 
layoutCType()725 Layout::CType Parser::layoutCType() {
726     if (this->expect(Token::EQ, "'='")) {
727         Token t = this->nextToken();
728         String text = this->text(t);
729         auto found = layoutTokens->find(text);
730         if (found != layoutTokens->end()) {
731             switch (found->second) {
732                 case LayoutToken::SKPMCOLOR4F:
733                     return Layout::CType::kSkPMColor4f;
734                 case LayoutToken::SKRECT:
735                     return Layout::CType::kSkRect;
736                 case LayoutToken::SKIRECT:
737                     return Layout::CType::kSkIRect;
738                 case LayoutToken::SKPMCOLOR:
739                     return Layout::CType::kSkPMColor;
740                 default:
741                     break;
742             }
743         }
744         this->error(t, "unsupported ctype");
745     }
746     return Layout::CType::kDefault;
747 }
748 
749 /* LAYOUT LPAREN IDENTIFIER (EQ INT_LITERAL)? (COMMA IDENTIFIER (EQ INT_LITERAL)?)* RPAREN */
layout()750 Layout Parser::layout() {
751     int flags = 0;
752     int location = -1;
753     int offset = -1;
754     int binding = -1;
755     int index = -1;
756     int set = -1;
757     int builtin = -1;
758     int inputAttachmentIndex = -1;
759     Layout::Format format = Layout::Format::kUnspecified;
760     Layout::Primitive primitive = Layout::kUnspecified_Primitive;
761     int maxVertices = -1;
762     int invocations = -1;
763     String when;
764     Layout::Key key = Layout::kNo_Key;
765     Layout::CType ctype = Layout::CType::kDefault;
766     if (this->checkNext(Token::LAYOUT)) {
767         if (!this->expect(Token::LPAREN, "'('")) {
768             return Layout(flags, location, offset, binding, index, set, builtin,
769                           inputAttachmentIndex, format, primitive, maxVertices, invocations, when,
770                           key, ctype);
771         }
772         for (;;) {
773             Token t = this->nextToken();
774             String text = this->text(t);
775             auto found = layoutTokens->find(text);
776             if (found != layoutTokens->end()) {
777                 switch (found->second) {
778                     case LayoutToken::LOCATION:
779                         location = this->layoutInt();
780                         break;
781                     case LayoutToken::OFFSET:
782                         offset = this->layoutInt();
783                         break;
784                     case LayoutToken::BINDING:
785                         binding = this->layoutInt();
786                         break;
787                     case LayoutToken::INDEX:
788                         index = this->layoutInt();
789                         break;
790                     case LayoutToken::SET:
791                         set = this->layoutInt();
792                         break;
793                     case LayoutToken::BUILTIN:
794                         builtin = this->layoutInt();
795                         break;
796                     case LayoutToken::INPUT_ATTACHMENT_INDEX:
797                         inputAttachmentIndex = this->layoutInt();
798                         break;
799                     case LayoutToken::ORIGIN_UPPER_LEFT:
800                         flags |= Layout::kOriginUpperLeft_Flag;
801                         break;
802                     case LayoutToken::OVERRIDE_COVERAGE:
803                         flags |= Layout::kOverrideCoverage_Flag;
804                         break;
805                     case LayoutToken::BLEND_SUPPORT_ALL_EQUATIONS:
806                         flags |= Layout::kBlendSupportAllEquations_Flag;
807                         break;
808                     case LayoutToken::BLEND_SUPPORT_MULTIPLY:
809                         flags |= Layout::kBlendSupportMultiply_Flag;
810                         break;
811                     case LayoutToken::BLEND_SUPPORT_SCREEN:
812                         flags |= Layout::kBlendSupportScreen_Flag;
813                         break;
814                     case LayoutToken::BLEND_SUPPORT_OVERLAY:
815                         flags |= Layout::kBlendSupportOverlay_Flag;
816                         break;
817                     case LayoutToken::BLEND_SUPPORT_DARKEN:
818                         flags |= Layout::kBlendSupportDarken_Flag;
819                         break;
820                     case LayoutToken::BLEND_SUPPORT_LIGHTEN:
821                         flags |= Layout::kBlendSupportLighten_Flag;
822                         break;
823                     case LayoutToken::BLEND_SUPPORT_COLORDODGE:
824                         flags |= Layout::kBlendSupportColorDodge_Flag;
825                         break;
826                     case LayoutToken::BLEND_SUPPORT_COLORBURN:
827                         flags |= Layout::kBlendSupportColorBurn_Flag;
828                         break;
829                     case LayoutToken::BLEND_SUPPORT_HARDLIGHT:
830                         flags |= Layout::kBlendSupportHardLight_Flag;
831                         break;
832                     case LayoutToken::BLEND_SUPPORT_SOFTLIGHT:
833                         flags |= Layout::kBlendSupportSoftLight_Flag;
834                         break;
835                     case LayoutToken::BLEND_SUPPORT_DIFFERENCE:
836                         flags |= Layout::kBlendSupportDifference_Flag;
837                         break;
838                     case LayoutToken::BLEND_SUPPORT_EXCLUSION:
839                         flags |= Layout::kBlendSupportExclusion_Flag;
840                         break;
841                     case LayoutToken::BLEND_SUPPORT_HSL_HUE:
842                         flags |= Layout::kBlendSupportHSLHue_Flag;
843                         break;
844                     case LayoutToken::BLEND_SUPPORT_HSL_SATURATION:
845                         flags |= Layout::kBlendSupportHSLSaturation_Flag;
846                         break;
847                     case LayoutToken::BLEND_SUPPORT_HSL_COLOR:
848                         flags |= Layout::kBlendSupportHSLColor_Flag;
849                         break;
850                     case LayoutToken::BLEND_SUPPORT_HSL_LUMINOSITY:
851                         flags |= Layout::kBlendSupportHSLLuminosity_Flag;
852                         break;
853                     case LayoutToken::PUSH_CONSTANT:
854                         flags |= Layout::kPushConstant_Flag;
855                         break;
856                     case LayoutToken::TRACKED:
857                         flags |= Layout::kTracked_Flag;
858                         break;
859                     case LayoutToken::POINTS:
860                         primitive = Layout::kPoints_Primitive;
861                         break;
862                     case LayoutToken::LINES:
863                         primitive = Layout::kLines_Primitive;
864                         break;
865                     case LayoutToken::LINE_STRIP:
866                         primitive = Layout::kLineStrip_Primitive;
867                         break;
868                     case LayoutToken::LINES_ADJACENCY:
869                         primitive = Layout::kLinesAdjacency_Primitive;
870                         break;
871                     case LayoutToken::TRIANGLES:
872                         primitive = Layout::kTriangles_Primitive;
873                         break;
874                     case LayoutToken::TRIANGLE_STRIP:
875                         primitive = Layout::kTriangleStrip_Primitive;
876                         break;
877                     case LayoutToken::TRIANGLES_ADJACENCY:
878                         primitive = Layout::kTrianglesAdjacency_Primitive;
879                         break;
880                     case LayoutToken::MAX_VERTICES:
881                         maxVertices = this->layoutInt();
882                         break;
883                     case LayoutToken::INVOCATIONS:
884                         invocations = this->layoutInt();
885                         break;
886                     case LayoutToken::WHEN:
887                         when = this->layoutCode();
888                         break;
889                     case LayoutToken::KEY:
890                         key = this->layoutKey();
891                         break;
892                     case LayoutToken::CTYPE:
893                         ctype = this->layoutCType();
894                         break;
895                     default:
896                         this->error(t, ("'" + text + "' is not a valid layout qualifier").c_str());
897                         break;
898                 }
899             } else if (Layout::ReadFormat(text, &format)) {
900                // AST::ReadFormat stored the result in 'format'.
901             } else {
902                 this->error(t, ("'" + text + "' is not a valid layout qualifier").c_str());
903             }
904             if (this->checkNext(Token::RPAREN)) {
905                 break;
906             }
907             if (!this->expect(Token::COMMA, "','")) {
908                 break;
909             }
910         }
911     }
912     return Layout(flags, location, offset, binding, index, set, builtin, inputAttachmentIndex,
913                   format, primitive, maxVertices, invocations, when, key, ctype);
914 }
915 
916 /* layout? (UNIFORM | CONST | IN | OUT | INOUT | LOWP | MEDIUMP | HIGHP | FLAT | NOPERSPECTIVE |
917             READONLY | WRITEONLY | COHERENT | VOLATILE | RESTRICT | BUFFER | PLS | PLSIN |
918             PLSOUT)* */
modifiers()919 Modifiers Parser::modifiers() {
920     Layout layout = this->layout();
921     int flags = 0;
922     for (;;) {
923         // TODO: handle duplicate / incompatible flags
924         switch (peek().fKind) {
925             case Token::UNIFORM:
926                 this->nextToken();
927                 flags |= Modifiers::kUniform_Flag;
928                 break;
929             case Token::CONST:
930                 this->nextToken();
931                 flags |= Modifiers::kConst_Flag;
932                 break;
933             case Token::IN:
934                 this->nextToken();
935                 flags |= Modifiers::kIn_Flag;
936                 break;
937             case Token::OUT:
938                 this->nextToken();
939                 flags |= Modifiers::kOut_Flag;
940                 break;
941             case Token::INOUT:
942                 this->nextToken();
943                 flags |= Modifiers::kIn_Flag;
944                 flags |= Modifiers::kOut_Flag;
945                 break;
946             case Token::FLAT:
947                 this->nextToken();
948                 flags |= Modifiers::kFlat_Flag;
949                 break;
950             case Token::NOPERSPECTIVE:
951                 this->nextToken();
952                 flags |= Modifiers::kNoPerspective_Flag;
953                 break;
954             case Token::READONLY:
955                 this->nextToken();
956                 flags |= Modifiers::kReadOnly_Flag;
957                 break;
958             case Token::WRITEONLY:
959                 this->nextToken();
960                 flags |= Modifiers::kWriteOnly_Flag;
961                 break;
962             case Token::COHERENT:
963                 this->nextToken();
964                 flags |= Modifiers::kCoherent_Flag;
965                 break;
966             case Token::VOLATILE:
967                 this->nextToken();
968                 flags |= Modifiers::kVolatile_Flag;
969                 break;
970             case Token::RESTRICT:
971                 this->nextToken();
972                 flags |= Modifiers::kRestrict_Flag;
973                 break;
974             case Token::BUFFER:
975                 this->nextToken();
976                 flags |= Modifiers::kBuffer_Flag;
977                 break;
978             case Token::HASSIDEEFFECTS:
979                 this->nextToken();
980                 flags |= Modifiers::kHasSideEffects_Flag;
981                 break;
982             case Token::PLS:
983                 this->nextToken();
984                 flags |= Modifiers::kPLS_Flag;
985                 break;
986             case Token::PLSIN:
987                 this->nextToken();
988                 flags |= Modifiers::kPLSIn_Flag;
989                 break;
990             case Token::PLSOUT:
991                 this->nextToken();
992                 flags |= Modifiers::kPLSOut_Flag;
993                 break;
994             default:
995                 return Modifiers(layout, flags);
996         }
997     }
998 }
999 
modifiersWithDefaults(int defaultFlags)1000 Modifiers Parser::modifiersWithDefaults(int defaultFlags) {
1001     Modifiers result = this->modifiers();
1002     if (!result.fFlags) {
1003         return Modifiers(result.fLayout, defaultFlags);
1004     }
1005     return result;
1006 }
1007 
1008 /* ifStatement | forStatement | doStatement | whileStatement | block | expression */
statement()1009 std::unique_ptr<ASTStatement> Parser::statement() {
1010     Token start = this->peek();
1011     switch (start.fKind) {
1012         case Token::IF: // fall through
1013         case Token::STATIC_IF:
1014             return this->ifStatement();
1015         case Token::FOR:
1016             return this->forStatement();
1017         case Token::DO:
1018             return this->doStatement();
1019         case Token::WHILE:
1020             return this->whileStatement();
1021         case Token::SWITCH: // fall through
1022         case Token::STATIC_SWITCH:
1023             return this->switchStatement();
1024         case Token::RETURN:
1025             return this->returnStatement();
1026         case Token::BREAK:
1027             return this->breakStatement();
1028         case Token::CONTINUE:
1029             return this->continueStatement();
1030         case Token::DISCARD:
1031             return this->discardStatement();
1032         case Token::LBRACE:
1033             return this->block();
1034         case Token::SEMICOLON:
1035             this->nextToken();
1036             return std::unique_ptr<ASTStatement>(new ASTBlock(start.fOffset,
1037                                                      std::vector<std::unique_ptr<ASTStatement>>()));
1038         case Token::CONST: {
1039             auto decl = this->varDeclarations();
1040             if (!decl) {
1041                 return nullptr;
1042             }
1043             return std::unique_ptr<ASTStatement>(new ASTVarDeclarationStatement(std::move(decl)));
1044         }
1045         case Token::IDENTIFIER:
1046             if (this->isType(this->text(start))) {
1047                 auto decl = this->varDeclarations();
1048                 if (!decl) {
1049                     return nullptr;
1050                 }
1051                 return std::unique_ptr<ASTStatement>(new ASTVarDeclarationStatement(
1052                                                                                   std::move(decl)));
1053             }
1054             // fall through
1055         default:
1056             return this->expressionStatement();
1057     }
1058 }
1059 
1060 /* IDENTIFIER(type) (LBRACKET intLiteral? RBRACKET)* QUESTION? */
type()1061 std::unique_ptr<ASTType> Parser::type() {
1062     Token type;
1063     if (!this->expect(Token::IDENTIFIER, "a type", &type)) {
1064         return nullptr;
1065     }
1066     if (!this->isType(this->text(type))) {
1067         this->error(type, ("no type named '" + this->text(type) + "'").c_str());
1068         return nullptr;
1069     }
1070     std::vector<int> sizes;
1071     while (this->checkNext(Token::LBRACKET)) {
1072         if (this->peek().fKind != Token::RBRACKET) {
1073             int64_t i;
1074             if (this->intLiteral(&i)) {
1075                 sizes.push_back(i);
1076             } else {
1077                 return nullptr;
1078             }
1079         } else {
1080             sizes.push_back(-1);
1081         }
1082         this->expect(Token::RBRACKET, "']'");
1083     }
1084     bool nullable = this->checkNext(Token::QUESTION);
1085     return std::unique_ptr<ASTType>(new ASTType(type.fOffset, this->text(type),
1086                                                 ASTType::kIdentifier_Kind, sizes, nullable));
1087 }
1088 
1089 /* IDENTIFIER LBRACE varDeclaration* RBRACE (IDENTIFIER (LBRACKET expression? RBRACKET)*)? */
interfaceBlock(Modifiers mods)1090 std::unique_ptr<ASTDeclaration> Parser::interfaceBlock(Modifiers mods) {
1091     Token name;
1092     if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) {
1093         return nullptr;
1094     }
1095     if (peek().fKind != Token::LBRACE) {
1096         // we only get into interfaceBlock if we found a top-level identifier which was not a type.
1097         // 99% of the time, the user was not actually intending to create an interface block, so
1098         // it's better to report it as an unknown type
1099         this->error(name, "no type named '" + this->text(name) + "'");
1100         return nullptr;
1101     }
1102     this->nextToken();
1103     std::vector<std::unique_ptr<ASTVarDeclarations>> decls;
1104     while (this->peek().fKind != Token::RBRACE) {
1105         std::unique_ptr<ASTVarDeclarations> decl = this->varDeclarations();
1106         if (!decl) {
1107             return nullptr;
1108         }
1109         decls.push_back(std::move(decl));
1110     }
1111     this->nextToken();
1112     std::vector<std::unique_ptr<ASTExpression>> sizes;
1113     StringFragment instanceName;
1114     Token instanceNameToken;
1115     if (this->checkNext(Token::IDENTIFIER, &instanceNameToken)) {
1116         while (this->checkNext(Token::LBRACKET)) {
1117             if (this->peek().fKind != Token::RBRACKET) {
1118                 std::unique_ptr<ASTExpression> size = this->expression();
1119                 if (!size) {
1120                     return nullptr;
1121                 }
1122                 sizes.push_back(std::move(size));
1123             } else {
1124                 sizes.push_back(nullptr);
1125             }
1126             this->expect(Token::RBRACKET, "']'");
1127         }
1128         instanceName = this->text(instanceNameToken);
1129     }
1130     this->expect(Token::SEMICOLON, "';'");
1131     return std::unique_ptr<ASTDeclaration>(new ASTInterfaceBlock(name.fOffset, mods,
1132                                                                  this->text(name),
1133                                                                  std::move(decls),
1134                                                                  instanceName,
1135                                                                  std::move(sizes)));
1136 }
1137 
1138 /* IF LPAREN expression RPAREN statement (ELSE statement)? */
ifStatement()1139 std::unique_ptr<ASTIfStatement> Parser::ifStatement() {
1140     Token start;
1141     bool isStatic = this->checkNext(Token::STATIC_IF, &start);
1142     if (!isStatic && !this->expect(Token::IF, "'if'", &start)) {
1143         return nullptr;
1144     }
1145     if (!this->expect(Token::LPAREN, "'('")) {
1146         return nullptr;
1147     }
1148     std::unique_ptr<ASTExpression> test(this->expression());
1149     if (!test) {
1150         return nullptr;
1151     }
1152     if (!this->expect(Token::RPAREN, "')'")) {
1153         return nullptr;
1154     }
1155     std::unique_ptr<ASTStatement> ifTrue(this->statement());
1156     if (!ifTrue) {
1157         return nullptr;
1158     }
1159     std::unique_ptr<ASTStatement> ifFalse;
1160     if (this->checkNext(Token::ELSE)) {
1161         ifFalse = this->statement();
1162         if (!ifFalse) {
1163             return nullptr;
1164         }
1165     }
1166     return std::unique_ptr<ASTIfStatement>(new ASTIfStatement(start.fOffset,
1167                                                               isStatic,
1168                                                               std::move(test),
1169                                                               std::move(ifTrue),
1170                                                               std::move(ifFalse)));
1171 }
1172 
1173 /* DO statement WHILE LPAREN expression RPAREN SEMICOLON */
doStatement()1174 std::unique_ptr<ASTDoStatement> Parser::doStatement() {
1175     Token start;
1176     if (!this->expect(Token::DO, "'do'", &start)) {
1177         return nullptr;
1178     }
1179     std::unique_ptr<ASTStatement> statement(this->statement());
1180     if (!statement) {
1181         return nullptr;
1182     }
1183     if (!this->expect(Token::WHILE, "'while'")) {
1184         return nullptr;
1185     }
1186     if (!this->expect(Token::LPAREN, "'('")) {
1187         return nullptr;
1188     }
1189     std::unique_ptr<ASTExpression> test(this->expression());
1190     if (!test) {
1191         return nullptr;
1192     }
1193     if (!this->expect(Token::RPAREN, "')'")) {
1194         return nullptr;
1195     }
1196     if (!this->expect(Token::SEMICOLON, "';'")) {
1197         return nullptr;
1198     }
1199     return std::unique_ptr<ASTDoStatement>(new ASTDoStatement(start.fOffset,
1200                                                               std::move(statement),
1201                                                               std::move(test)));
1202 }
1203 
1204 /* WHILE LPAREN expression RPAREN STATEMENT */
whileStatement()1205 std::unique_ptr<ASTWhileStatement> Parser::whileStatement() {
1206     Token start;
1207     if (!this->expect(Token::WHILE, "'while'", &start)) {
1208         return nullptr;
1209     }
1210     if (!this->expect(Token::LPAREN, "'('")) {
1211         return nullptr;
1212     }
1213     std::unique_ptr<ASTExpression> test(this->expression());
1214     if (!test) {
1215         return nullptr;
1216     }
1217     if (!this->expect(Token::RPAREN, "')'")) {
1218         return nullptr;
1219     }
1220     std::unique_ptr<ASTStatement> statement(this->statement());
1221     if (!statement) {
1222         return nullptr;
1223     }
1224     return std::unique_ptr<ASTWhileStatement>(new ASTWhileStatement(start.fOffset,
1225                                                                     std::move(test),
1226                                                                     std::move(statement)));
1227 }
1228 
1229 /* CASE expression COLON statement* */
switchCase()1230 std::unique_ptr<ASTSwitchCase> Parser::switchCase() {
1231     Token start;
1232     if (!this->expect(Token::CASE, "'case'", &start)) {
1233         return nullptr;
1234     }
1235     std::unique_ptr<ASTExpression> value = this->expression();
1236     if (!value) {
1237         return nullptr;
1238     }
1239     if (!this->expect(Token::COLON, "':'")) {
1240         return nullptr;
1241     }
1242     std::vector<std::unique_ptr<ASTStatement>> statements;
1243     while (this->peek().fKind != Token::RBRACE && this->peek().fKind != Token::CASE &&
1244            this->peek().fKind != Token::DEFAULT) {
1245         std::unique_ptr<ASTStatement> s = this->statement();
1246         if (!s) {
1247             return nullptr;
1248         }
1249         statements.push_back(std::move(s));
1250     }
1251     return std::unique_ptr<ASTSwitchCase>(new ASTSwitchCase(start.fOffset, std::move(value),
1252                                                             std::move(statements)));
1253 }
1254 
1255 /* SWITCH LPAREN expression RPAREN LBRACE switchCase* (DEFAULT COLON statement*)? RBRACE */
switchStatement()1256 std::unique_ptr<ASTStatement> Parser::switchStatement() {
1257     Token start;
1258     bool isStatic = this->checkNext(Token::STATIC_SWITCH, &start);
1259     if (!isStatic && !this->expect(Token::SWITCH, "'switch'", &start)) {
1260         return nullptr;
1261     }
1262     if (!this->expect(Token::LPAREN, "'('")) {
1263         return nullptr;
1264     }
1265     std::unique_ptr<ASTExpression> value(this->expression());
1266     if (!value) {
1267         return nullptr;
1268     }
1269     if (!this->expect(Token::RPAREN, "')'")) {
1270         return nullptr;
1271     }
1272     if (!this->expect(Token::LBRACE, "'{'")) {
1273         return nullptr;
1274     }
1275     std::vector<std::unique_ptr<ASTSwitchCase>> cases;
1276     while (this->peek().fKind == Token::CASE) {
1277         std::unique_ptr<ASTSwitchCase> c = this->switchCase();
1278         if (!c) {
1279             return nullptr;
1280         }
1281         cases.push_back(std::move(c));
1282     }
1283     // Requiring default: to be last (in defiance of C and GLSL) was a deliberate decision. Other
1284     // parts of the compiler may rely upon this assumption.
1285     if (this->peek().fKind == Token::DEFAULT) {
1286         Token defaultStart;
1287         SkAssertResult(this->expect(Token::DEFAULT, "'default'", &defaultStart));
1288         if (!this->expect(Token::COLON, "':'")) {
1289             return nullptr;
1290         }
1291         std::vector<std::unique_ptr<ASTStatement>> statements;
1292         while (this->peek().fKind != Token::RBRACE) {
1293             std::unique_ptr<ASTStatement> s = this->statement();
1294             if (!s) {
1295                 return nullptr;
1296             }
1297             statements.push_back(std::move(s));
1298         }
1299         cases.emplace_back(new ASTSwitchCase(defaultStart.fOffset, nullptr,
1300                                              std::move(statements)));
1301     }
1302     if (!this->expect(Token::RBRACE, "'}'")) {
1303         return nullptr;
1304     }
1305     return std::unique_ptr<ASTStatement>(new ASTSwitchStatement(start.fOffset,
1306                                                                 isStatic,
1307                                                                 std::move(value),
1308                                                                 std::move(cases)));
1309 }
1310 
1311 /* FOR LPAREN (declaration | expression)? SEMICOLON expression? SEMICOLON expression? RPAREN
1312    STATEMENT */
forStatement()1313 std::unique_ptr<ASTForStatement> Parser::forStatement() {
1314     Token start;
1315     if (!this->expect(Token::FOR, "'for'", &start)) {
1316         return nullptr;
1317     }
1318     if (!this->expect(Token::LPAREN, "'('")) {
1319         return nullptr;
1320     }
1321     std::unique_ptr<ASTStatement> initializer;
1322     Token nextToken = this->peek();
1323     switch (nextToken.fKind) {
1324         case Token::SEMICOLON:
1325             this->nextToken();
1326             break;
1327         case Token::CONST: {
1328             std::unique_ptr<ASTVarDeclarations> vd = this->varDeclarations();
1329             if (!vd) {
1330                 return nullptr;
1331             }
1332             initializer = std::unique_ptr<ASTStatement>(new ASTVarDeclarationStatement(
1333                                                                                     std::move(vd)));
1334             break;
1335         }
1336         case Token::IDENTIFIER: {
1337             if (this->isType(this->text(nextToken))) {
1338                 std::unique_ptr<ASTVarDeclarations> vd = this->varDeclarations();
1339                 if (!vd) {
1340                     return nullptr;
1341                 }
1342                 initializer = std::unique_ptr<ASTStatement>(new ASTVarDeclarationStatement(
1343                                                                                     std::move(vd)));
1344                 break;
1345             }
1346         } // fall through
1347         default:
1348             initializer = this->expressionStatement();
1349     }
1350     std::unique_ptr<ASTExpression> test;
1351     if (this->peek().fKind != Token::SEMICOLON) {
1352         test = this->expression();
1353         if (!test) {
1354             return nullptr;
1355         }
1356     }
1357     if (!this->expect(Token::SEMICOLON, "';'")) {
1358         return nullptr;
1359     }
1360     std::unique_ptr<ASTExpression> next;
1361     if (this->peek().fKind != Token::RPAREN) {
1362         next = this->expression();
1363         if (!next) {
1364             return nullptr;
1365         }
1366     }
1367     if (!this->expect(Token::RPAREN, "')'")) {
1368         return nullptr;
1369     }
1370     std::unique_ptr<ASTStatement> statement(this->statement());
1371     if (!statement) {
1372         return nullptr;
1373     }
1374     return std::unique_ptr<ASTForStatement>(new ASTForStatement(start.fOffset,
1375                                                                 std::move(initializer),
1376                                                                 std::move(test), std::move(next),
1377                                                                 std::move(statement)));
1378 }
1379 
1380 /* RETURN expression? SEMICOLON */
returnStatement()1381 std::unique_ptr<ASTReturnStatement> Parser::returnStatement() {
1382     Token start;
1383     if (!this->expect(Token::RETURN, "'return'", &start)) {
1384         return nullptr;
1385     }
1386     std::unique_ptr<ASTExpression> expression;
1387     if (this->peek().fKind != Token::SEMICOLON) {
1388         expression = this->expression();
1389         if (!expression) {
1390             return nullptr;
1391         }
1392     }
1393     if (!this->expect(Token::SEMICOLON, "';'")) {
1394         return nullptr;
1395     }
1396     return std::unique_ptr<ASTReturnStatement>(new ASTReturnStatement(start.fOffset,
1397                                                                       std::move(expression)));
1398 }
1399 
1400 /* BREAK SEMICOLON */
breakStatement()1401 std::unique_ptr<ASTBreakStatement> Parser::breakStatement() {
1402     Token start;
1403     if (!this->expect(Token::BREAK, "'break'", &start)) {
1404         return nullptr;
1405     }
1406     if (!this->expect(Token::SEMICOLON, "';'")) {
1407         return nullptr;
1408     }
1409     return std::unique_ptr<ASTBreakStatement>(new ASTBreakStatement(start.fOffset));
1410 }
1411 
1412 /* CONTINUE SEMICOLON */
continueStatement()1413 std::unique_ptr<ASTContinueStatement> Parser::continueStatement() {
1414     Token start;
1415     if (!this->expect(Token::CONTINUE, "'continue'", &start)) {
1416         return nullptr;
1417     }
1418     if (!this->expect(Token::SEMICOLON, "';'")) {
1419         return nullptr;
1420     }
1421     return std::unique_ptr<ASTContinueStatement>(new ASTContinueStatement(start.fOffset));
1422 }
1423 
1424 /* DISCARD SEMICOLON */
discardStatement()1425 std::unique_ptr<ASTDiscardStatement> Parser::discardStatement() {
1426     Token start;
1427     if (!this->expect(Token::DISCARD, "'continue'", &start)) {
1428         return nullptr;
1429     }
1430     if (!this->expect(Token::SEMICOLON, "';'")) {
1431         return nullptr;
1432     }
1433     return std::unique_ptr<ASTDiscardStatement>(new ASTDiscardStatement(start.fOffset));
1434 }
1435 
1436 /* LBRACE statement* RBRACE */
block()1437 std::unique_ptr<ASTBlock> Parser::block() {
1438     AutoDepth depth(this);
1439     if (!depth.checkValid()) {
1440         return nullptr;
1441     }
1442     Token start;
1443     if (!this->expect(Token::LBRACE, "'{'", &start)) {
1444         return nullptr;
1445     }
1446     std::vector<std::unique_ptr<ASTStatement>> statements;
1447     for (;;) {
1448         switch (this->peek().fKind) {
1449             case Token::RBRACE:
1450                 this->nextToken();
1451                 return std::unique_ptr<ASTBlock>(new ASTBlock(start.fOffset,
1452                                                               std::move(statements)));
1453             case Token::END_OF_FILE:
1454                 this->error(this->peek(), "expected '}', but found end of file");
1455                 return nullptr;
1456             default: {
1457                 std::unique_ptr<ASTStatement> statement = this->statement();
1458                 if (!statement) {
1459                     return nullptr;
1460                 }
1461                 statements.push_back(std::move(statement));
1462             }
1463         }
1464     }
1465 }
1466 
1467 /* expression SEMICOLON */
expressionStatement()1468 std::unique_ptr<ASTExpressionStatement> Parser::expressionStatement() {
1469     std::unique_ptr<ASTExpression> expr = this->expression();
1470     if (expr) {
1471         if (this->expect(Token::SEMICOLON, "';'")) {
1472             ASTExpressionStatement* result = new ASTExpressionStatement(std::move(expr));
1473             return std::unique_ptr<ASTExpressionStatement>(result);
1474         }
1475     }
1476     return nullptr;
1477 }
1478 
1479 /* commaExpression */
expression()1480 std::unique_ptr<ASTExpression> Parser::expression() {
1481     AutoDepth depth(this);
1482     if (!depth.checkValid()) {
1483         return nullptr;
1484     }
1485     return this->commaExpression();
1486 }
1487 
1488 /* assignmentExpression (COMMA assignmentExpression)* */
commaExpression()1489 std::unique_ptr<ASTExpression> Parser::commaExpression() {
1490     std::unique_ptr<ASTExpression> result = this->assignmentExpression();
1491     if (!result) {
1492         return nullptr;
1493     }
1494     Token t;
1495     while (this->checkNext(Token::COMMA, &t)) {
1496         std::unique_ptr<ASTExpression> right = this->commaExpression();
1497         if (!right) {
1498             return nullptr;
1499         }
1500         result.reset(new ASTBinaryExpression(std::move(result), std::move(t), std::move(right)));
1501     }
1502     return result;
1503 }
1504 
1505 /* ternaryExpression ((EQEQ | STAREQ | SLASHEQ | PERCENTEQ | PLUSEQ | MINUSEQ | SHLEQ | SHREQ |
1506    BITWISEANDEQ | BITWISEXOREQ | BITWISEOREQ | LOGICALANDEQ | LOGICALXOREQ | LOGICALOREQ)
1507    assignmentExpression)*
1508  */
assignmentExpression()1509 std::unique_ptr<ASTExpression> Parser::assignmentExpression() {
1510     std::unique_ptr<ASTExpression> result = this->ternaryExpression();
1511     if (!result) {
1512         return nullptr;
1513     }
1514     for (;;) {
1515         switch (this->peek().fKind) {
1516             case Token::EQ:           // fall through
1517             case Token::STAREQ:       // fall through
1518             case Token::SLASHEQ:      // fall through
1519             case Token::PERCENTEQ:    // fall through
1520             case Token::PLUSEQ:       // fall through
1521             case Token::MINUSEQ:      // fall through
1522             case Token::SHLEQ:        // fall through
1523             case Token::SHREQ:        // fall through
1524             case Token::BITWISEANDEQ: // fall through
1525             case Token::BITWISEXOREQ: // fall through
1526             case Token::BITWISEOREQ:  // fall through
1527             case Token::LOGICALANDEQ: // fall through
1528             case Token::LOGICALXOREQ: // fall through
1529             case Token::LOGICALOREQ: {
1530                 Token t = this->nextToken();
1531                 std::unique_ptr<ASTExpression> right = this->assignmentExpression();
1532                 if (!right) {
1533                     return nullptr;
1534                 }
1535                 result = std::unique_ptr<ASTExpression>(new ASTBinaryExpression(std::move(result),
1536                                                                                 std::move(t),
1537                                                                                 std::move(right)));
1538                 return result;
1539             }
1540             default:
1541                 return result;
1542         }
1543     }
1544 }
1545 
1546 /* logicalOrExpression ('?' expression ':' assignmentExpression)? */
ternaryExpression()1547 std::unique_ptr<ASTExpression> Parser::ternaryExpression() {
1548     std::unique_ptr<ASTExpression> result = this->logicalOrExpression();
1549     if (!result) {
1550         return nullptr;
1551     }
1552     if (this->checkNext(Token::QUESTION)) {
1553         std::unique_ptr<ASTExpression> trueExpr = this->expression();
1554         if (!trueExpr) {
1555             return nullptr;
1556         }
1557         if (this->expect(Token::COLON, "':'")) {
1558             std::unique_ptr<ASTExpression> falseExpr = this->assignmentExpression();
1559             return std::unique_ptr<ASTExpression>(new ASTTernaryExpression(std::move(result),
1560                                                                            std::move(trueExpr),
1561                                                                            std::move(falseExpr)));
1562         }
1563         return nullptr;
1564     }
1565     return result;
1566 }
1567 
1568 /* logicalXorExpression (LOGICALOR logicalXorExpression)* */
logicalOrExpression()1569 std::unique_ptr<ASTExpression> Parser::logicalOrExpression() {
1570     std::unique_ptr<ASTExpression> result = this->logicalXorExpression();
1571     if (!result) {
1572         return nullptr;
1573     }
1574     Token t;
1575     while (this->checkNext(Token::LOGICALOR, &t)) {
1576         std::unique_ptr<ASTExpression> right = this->logicalXorExpression();
1577         if (!right) {
1578             return nullptr;
1579         }
1580         result.reset(new ASTBinaryExpression(std::move(result), std::move(t), std::move(right)));
1581     }
1582     return result;
1583 }
1584 
1585 /* logicalAndExpression (LOGICALXOR logicalAndExpression)* */
logicalXorExpression()1586 std::unique_ptr<ASTExpression> Parser::logicalXorExpression() {
1587     std::unique_ptr<ASTExpression> result = this->logicalAndExpression();
1588     if (!result) {
1589         return nullptr;
1590     }
1591     Token t;
1592     while (this->checkNext(Token::LOGICALXOR, &t)) {
1593         std::unique_ptr<ASTExpression> right = this->logicalAndExpression();
1594         if (!right) {
1595             return nullptr;
1596         }
1597         result.reset(new ASTBinaryExpression(std::move(result), std::move(t), std::move(right)));
1598     }
1599     return result;
1600 }
1601 
1602 /* bitwiseOrExpression (LOGICALAND bitwiseOrExpression)* */
logicalAndExpression()1603 std::unique_ptr<ASTExpression> Parser::logicalAndExpression() {
1604     std::unique_ptr<ASTExpression> result = this->bitwiseOrExpression();
1605     if (!result) {
1606         return nullptr;
1607     }
1608     Token t;
1609     while (this->checkNext(Token::LOGICALAND, &t)) {
1610         std::unique_ptr<ASTExpression> right = this->bitwiseOrExpression();
1611         if (!right) {
1612             return nullptr;
1613         }
1614         result.reset(new ASTBinaryExpression(std::move(result), std::move(t), std::move(right)));
1615     }
1616     return result;
1617 }
1618 
1619 /* bitwiseXorExpression (BITWISEOR bitwiseXorExpression)* */
bitwiseOrExpression()1620 std::unique_ptr<ASTExpression> Parser::bitwiseOrExpression() {
1621     std::unique_ptr<ASTExpression> result = this->bitwiseXorExpression();
1622     if (!result) {
1623         return nullptr;
1624     }
1625     Token t;
1626     while (this->checkNext(Token::BITWISEOR, &t)) {
1627         std::unique_ptr<ASTExpression> right = this->bitwiseXorExpression();
1628         if (!right) {
1629             return nullptr;
1630         }
1631         result.reset(new ASTBinaryExpression(std::move(result), std::move(t), std::move(right)));
1632     }
1633     return result;
1634 }
1635 
1636 /* bitwiseAndExpression (BITWISEXOR bitwiseAndExpression)* */
bitwiseXorExpression()1637 std::unique_ptr<ASTExpression> Parser::bitwiseXorExpression() {
1638     std::unique_ptr<ASTExpression> result = this->bitwiseAndExpression();
1639     if (!result) {
1640         return nullptr;
1641     }
1642     Token t;
1643     while (this->checkNext(Token::BITWISEXOR, &t)) {
1644         std::unique_ptr<ASTExpression> right = this->bitwiseAndExpression();
1645         if (!right) {
1646             return nullptr;
1647         }
1648         result.reset(new ASTBinaryExpression(std::move(result), std::move(t), std::move(right)));
1649     }
1650     return result;
1651 }
1652 
1653 /* equalityExpression (BITWISEAND equalityExpression)* */
bitwiseAndExpression()1654 std::unique_ptr<ASTExpression> Parser::bitwiseAndExpression() {
1655     std::unique_ptr<ASTExpression> result = this->equalityExpression();
1656     if (!result) {
1657         return nullptr;
1658     }
1659     Token t;
1660     while (this->checkNext(Token::BITWISEAND, &t)) {
1661         std::unique_ptr<ASTExpression> right = this->equalityExpression();
1662         if (!right) {
1663             return nullptr;
1664         }
1665         result.reset(new ASTBinaryExpression(std::move(result), std::move(t), std::move(right)));
1666     }
1667     return result;
1668 }
1669 
1670 /* relationalExpression ((EQEQ | NEQ) relationalExpression)* */
equalityExpression()1671 std::unique_ptr<ASTExpression> Parser::equalityExpression() {
1672     std::unique_ptr<ASTExpression> result = this->relationalExpression();
1673     if (!result) {
1674         return nullptr;
1675     }
1676     for (;;) {
1677         switch (this->peek().fKind) {
1678             case Token::EQEQ:   // fall through
1679             case Token::NEQ: {
1680                 Token t = this->nextToken();
1681                 std::unique_ptr<ASTExpression> right = this->relationalExpression();
1682                 if (!right) {
1683                     return nullptr;
1684                 }
1685                 result.reset(new ASTBinaryExpression(std::move(result), std::move(t), std::move(right)));
1686                 break;
1687             }
1688             default:
1689                 return result;
1690         }
1691     }
1692 }
1693 
1694 /* shiftExpression ((LT | GT | LTEQ | GTEQ) shiftExpression)* */
relationalExpression()1695 std::unique_ptr<ASTExpression> Parser::relationalExpression() {
1696     std::unique_ptr<ASTExpression> result = this->shiftExpression();
1697     if (!result) {
1698         return nullptr;
1699     }
1700     for (;;) {
1701         switch (this->peek().fKind) {
1702             case Token::LT:   // fall through
1703             case Token::GT:   // fall through
1704             case Token::LTEQ: // fall through
1705             case Token::GTEQ: {
1706                 Token t = this->nextToken();
1707                 std::unique_ptr<ASTExpression> right = this->shiftExpression();
1708                 if (!right) {
1709                     return nullptr;
1710                 }
1711                 result.reset(new ASTBinaryExpression(std::move(result), std::move(t),
1712                                                      std::move(right)));
1713                 break;
1714             }
1715             default:
1716                 return result;
1717         }
1718     }
1719 }
1720 
1721 /* additiveExpression ((SHL | SHR) additiveExpression)* */
shiftExpression()1722 std::unique_ptr<ASTExpression> Parser::shiftExpression() {
1723     std::unique_ptr<ASTExpression> result = this->additiveExpression();
1724     if (!result) {
1725         return nullptr;
1726     }
1727     for (;;) {
1728         switch (this->peek().fKind) {
1729             case Token::SHL: // fall through
1730             case Token::SHR: {
1731                 Token t = this->nextToken();
1732                 std::unique_ptr<ASTExpression> right = this->additiveExpression();
1733                 if (!right) {
1734                     return nullptr;
1735                 }
1736                 result.reset(new ASTBinaryExpression(std::move(result), std::move(t),
1737                                                      std::move(right)));
1738                 break;
1739             }
1740             default:
1741                 return result;
1742         }
1743     }
1744 }
1745 
1746 /* multiplicativeExpression ((PLUS | MINUS) multiplicativeExpression)* */
additiveExpression()1747 std::unique_ptr<ASTExpression> Parser::additiveExpression() {
1748     std::unique_ptr<ASTExpression> result = this->multiplicativeExpression();
1749     if (!result) {
1750         return nullptr;
1751     }
1752     for (;;) {
1753         switch (this->peek().fKind) {
1754             case Token::PLUS: // fall through
1755             case Token::MINUS: {
1756                 Token t = this->nextToken();
1757                 std::unique_ptr<ASTExpression> right = this->multiplicativeExpression();
1758                 if (!right) {
1759                     return nullptr;
1760                 }
1761                 result.reset(new ASTBinaryExpression(std::move(result), std::move(t),
1762                                                      std::move(right)));
1763                 break;
1764             }
1765             default:
1766                 return result;
1767         }
1768     }
1769 }
1770 
1771 /* unaryExpression ((STAR | SLASH | PERCENT) unaryExpression)* */
multiplicativeExpression()1772 std::unique_ptr<ASTExpression> Parser::multiplicativeExpression() {
1773     std::unique_ptr<ASTExpression> result = this->unaryExpression();
1774     if (!result) {
1775         return nullptr;
1776     }
1777     for (;;) {
1778         switch (this->peek().fKind) {
1779             case Token::STAR: // fall through
1780             case Token::SLASH: // fall through
1781             case Token::PERCENT: {
1782                 Token t = this->nextToken();
1783                 std::unique_ptr<ASTExpression> right = this->unaryExpression();
1784                 if (!right) {
1785                     return nullptr;
1786                 }
1787                 result.reset(new ASTBinaryExpression(std::move(result), std::move(t),
1788                                                      std::move(right)));
1789                 break;
1790             }
1791             default:
1792                 return result;
1793         }
1794     }
1795 }
1796 
1797 /* postfixExpression | (PLUS | MINUS | NOT | PLUSPLUS | MINUSMINUS) unaryExpression */
unaryExpression()1798 std::unique_ptr<ASTExpression> Parser::unaryExpression() {
1799     switch (this->peek().fKind) {
1800         case Token::PLUS:       // fall through
1801         case Token::MINUS:      // fall through
1802         case Token::LOGICALNOT: // fall through
1803         case Token::BITWISENOT: // fall through
1804         case Token::PLUSPLUS:   // fall through
1805         case Token::MINUSMINUS: {
1806             AutoDepth depth(this);
1807             if (!depth.checkValid()) {
1808                 return nullptr;
1809             }
1810             Token t = this->nextToken();
1811             std::unique_ptr<ASTExpression> expr = this->unaryExpression();
1812             if (!expr) {
1813                 return nullptr;
1814             }
1815             return std::unique_ptr<ASTExpression>(new ASTPrefixExpression(std::move(t),
1816                                                                           std::move(expr)));
1817         }
1818         default:
1819             return this->postfixExpression();
1820     }
1821 }
1822 
1823 /* term suffix* */
postfixExpression()1824 std::unique_ptr<ASTExpression> Parser::postfixExpression() {
1825     std::unique_ptr<ASTExpression> result = this->term();
1826     if (!result) {
1827         return nullptr;
1828     }
1829     for (;;) {
1830         switch (this->peek().fKind) {
1831             case Token::LBRACKET:   // fall through
1832             case Token::DOT:        // fall through
1833             case Token::LPAREN:     // fall through
1834             case Token::PLUSPLUS:   // fall through
1835             case Token::MINUSMINUS: // fall through
1836             case Token::COLONCOLON: {
1837                 std::unique_ptr<ASTSuffix> s = this->suffix();
1838                 if (!s) {
1839                     return nullptr;
1840                 }
1841                 result.reset(new ASTSuffixExpression(std::move(result), std::move(s)));
1842                 break;
1843             }
1844             default:
1845                 return result;
1846         }
1847     }
1848 }
1849 
1850 /* LBRACKET expression? RBRACKET | DOT IDENTIFIER | LPAREN parameters RPAREN |
1851    PLUSPLUS | MINUSMINUS | COLONCOLON IDENTIFIER */
suffix()1852 std::unique_ptr<ASTSuffix> Parser::suffix() {
1853     Token next = this->nextToken();
1854     switch (next.fKind) {
1855         case Token::LBRACKET: {
1856             if (this->checkNext(Token::RBRACKET)) {
1857                 return std::unique_ptr<ASTSuffix>(new ASTIndexSuffix(next.fOffset));
1858             }
1859             std::unique_ptr<ASTExpression> e = this->expression();
1860             if (!e) {
1861                 return nullptr;
1862             }
1863             this->expect(Token::RBRACKET, "']' to complete array access expression");
1864             return std::unique_ptr<ASTSuffix>(new ASTIndexSuffix(std::move(e)));
1865         }
1866         case Token::DOT: // fall through
1867         case Token::COLONCOLON: {
1868             int offset = this->peek().fOffset;
1869             StringFragment text;
1870             if (this->identifier(&text)) {
1871                 return std::unique_ptr<ASTSuffix>(new ASTFieldSuffix(offset, std::move(text)));
1872             }
1873             return nullptr;
1874         }
1875         case Token::LPAREN: {
1876             std::vector<std::unique_ptr<ASTExpression>> parameters;
1877             if (this->peek().fKind != Token::RPAREN) {
1878                 for (;;) {
1879                     std::unique_ptr<ASTExpression> expr = this->assignmentExpression();
1880                     if (!expr) {
1881                         return nullptr;
1882                     }
1883                     parameters.push_back(std::move(expr));
1884                     if (!this->checkNext(Token::COMMA)) {
1885                         break;
1886                     }
1887                 }
1888             }
1889             this->expect(Token::RPAREN, "')' to complete function parameters");
1890             return std::unique_ptr<ASTSuffix>(new ASTCallSuffix(next.fOffset,
1891                                                                 std::move(parameters)));
1892         }
1893         case Token::PLUSPLUS:
1894             return std::unique_ptr<ASTSuffix>(new ASTSuffix(next.fOffset,
1895                                                             ASTSuffix::kPostIncrement_Kind));
1896         case Token::MINUSMINUS:
1897             return std::unique_ptr<ASTSuffix>(new ASTSuffix(next.fOffset,
1898                                                             ASTSuffix::kPostDecrement_Kind));
1899         default: {
1900             this->error(next,  "expected expression suffix, but found '" + this->text(next) +
1901                                          "'\n");
1902             return nullptr;
1903         }
1904     }
1905 }
1906 
1907 /* IDENTIFIER | intLiteral | floatLiteral | boolLiteral | NULL_LITERAL | '(' expression ')' */
term()1908 std::unique_ptr<ASTExpression> Parser::term() {
1909     std::unique_ptr<ASTExpression> result;
1910     Token t = this->peek();
1911     switch (t.fKind) {
1912         case Token::IDENTIFIER: {
1913             StringFragment text;
1914             if (this->identifier(&text)) {
1915                 result.reset(new ASTIdentifier(t.fOffset, std::move(text)));
1916             }
1917             break;
1918         }
1919         case Token::INT_LITERAL: {
1920             int64_t i;
1921             if (this->intLiteral(&i)) {
1922                 result.reset(new ASTIntLiteral(t.fOffset, i));
1923             }
1924             break;
1925         }
1926         case Token::FLOAT_LITERAL: {
1927             double f;
1928             if (this->floatLiteral(&f)) {
1929                 result.reset(new ASTFloatLiteral(t.fOffset, f));
1930             }
1931             break;
1932         }
1933         case Token::TRUE_LITERAL: // fall through
1934         case Token::FALSE_LITERAL: {
1935             bool b;
1936             if (this->boolLiteral(&b)) {
1937                 result.reset(new ASTBoolLiteral(t.fOffset, b));
1938             }
1939             break;
1940         }
1941         case Token::NULL_LITERAL:
1942             this->nextToken();
1943             result.reset(new ASTNullLiteral(t.fOffset));
1944             break;
1945         case Token::LPAREN: {
1946             this->nextToken();
1947             result = this->expression();
1948             if (result) {
1949                 this->expect(Token::RPAREN, "')' to complete expression");
1950             }
1951             break;
1952         }
1953         default:
1954             this->nextToken();
1955             this->error(t.fOffset,  "expected expression, but found '" + this->text(t) + "'\n");
1956             result = nullptr;
1957     }
1958     return result;
1959 }
1960 
1961 /* INT_LITERAL */
intLiteral(int64_t * dest)1962 bool Parser::intLiteral(int64_t* dest) {
1963     Token t;
1964     if (this->expect(Token::INT_LITERAL, "integer literal", &t)) {
1965         *dest = SkSL::stol(this->text(t));
1966         return true;
1967     }
1968     return false;
1969 }
1970 
1971 /* FLOAT_LITERAL */
floatLiteral(double * dest)1972 bool Parser::floatLiteral(double* dest) {
1973     Token t;
1974     if (this->expect(Token::FLOAT_LITERAL, "float literal", &t)) {
1975         *dest = SkSL::stod(this->text(t));
1976         return true;
1977     }
1978     return false;
1979 }
1980 
1981 /* TRUE_LITERAL | FALSE_LITERAL */
boolLiteral(bool * dest)1982 bool Parser::boolLiteral(bool* dest) {
1983     Token t = this->nextToken();
1984     switch (t.fKind) {
1985         case Token::TRUE_LITERAL:
1986             *dest = true;
1987             return true;
1988         case Token::FALSE_LITERAL:
1989             *dest = false;
1990             return true;
1991         default:
1992             this->error(t, "expected 'true' or 'false', but found '" + this->text(t) + "'\n");
1993             return false;
1994     }
1995 }
1996 
1997 /* IDENTIFIER */
identifier(StringFragment * dest)1998 bool Parser::identifier(StringFragment* dest) {
1999     Token t;
2000     if (this->expect(Token::IDENTIFIER, "identifier", &t)) {
2001         *dest = this->text(t);
2002         return true;
2003     }
2004     return false;
2005 }
2006 
2007 } // namespace
2008