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