blob: 7f6551ce3fcf88de9c86af43f8d2cc75893abc86 [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
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 */
Ethan Nicholas11d53972016-11-28 11:23:23 -05007
ethannicholasb3058bd2016-07-01 08:22:01 -07008#include "stdio.h"
9#include "SkSLParser.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070010#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"
Ethan Nicholasaae47c82017-11-10 15:34:03 -050018#include "ast/SkSLASTEnum.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070019#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"
ethannicholas5961bc92016-10-12 06:39:56 -070031#include "ast/SkSLASTModifiersDeclaration.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070032#include "ast/SkSLASTParameter.h"
33#include "ast/SkSLASTPrefixExpression.h"
34#include "ast/SkSLASTReturnStatement.h"
Ethan Nicholas762466e2017-06-29 10:03:38 -040035#include "ast/SkSLASTSection.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070036#include "ast/SkSLASTStatement.h"
37#include "ast/SkSLASTSuffixExpression.h"
Ethan Nicholasaf197692017-02-27 13:26:45 -050038#include "ast/SkSLASTSwitchCase.h"
39#include "ast/SkSLASTSwitchStatement.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070040#include "ast/SkSLASTTernaryExpression.h"
41#include "ast/SkSLASTType.h"
42#include "ast/SkSLASTVarDeclaration.h"
43#include "ast/SkSLASTVarDeclarationStatement.h"
44#include "ast/SkSLASTWhileStatement.h"
45#include "ir/SkSLSymbolTable.h"
Ethan Nicholas11d53972016-11-28 11:23:23 -050046#include "ir/SkSLModifiers.h"
ethannicholasd598f792016-07-25 10:08:54 -070047#include "ir/SkSLType.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070048
Ethan Nicholasb93af7e2018-07-24 11:28:52 -040049#ifndef SKSL_STANDALONE
50#include "SkOnce.h"
51#endif
52
ethannicholasb3058bd2016-07-01 08:22:01 -070053namespace SkSL {
54
ethannicholascad64162016-10-27 10:54:02 -070055#define MAX_PARSE_DEPTH 50
56
57class AutoDepth {
58public:
59 AutoDepth(Parser* p)
60 : fParser(p) {
61 fParser->fDepth++;
62 }
63
64 ~AutoDepth() {
65 fParser->fDepth--;
66 }
67
68 bool checkValid() {
69 if (fParser->fDepth > MAX_PARSE_DEPTH) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070070 fParser->error(fParser->peek(), String("exceeded max parse depth"));
ethannicholascad64162016-10-27 10:54:02 -070071 return false;
72 }
73 return true;
74 }
75
76private:
77 Parser* fParser;
78};
79
Brian Salomon140f3da2018-08-23 13:51:27 +000080std::unordered_map<String, Parser::LayoutToken>* Parser::layoutTokens;
Ethan Nicholasb93af7e2018-07-24 11:28:52 -040081
82void Parser::InitLayoutMap() {
Brian Salomon140f3da2018-08-23 13:51:27 +000083 layoutTokens = new std::unordered_map<String, LayoutToken>;
Brian Salomon23356442018-11-30 15:33:19 -050084 #define TOKEN(name, text) (*layoutTokens)[text] = LayoutToken::name
Ethan Nicholasb93af7e2018-07-24 11:28:52 -040085 TOKEN(LOCATION, "location");
86 TOKEN(OFFSET, "offset");
87 TOKEN(BINDING, "binding");
88 TOKEN(INDEX, "index");
89 TOKEN(SET, "set");
90 TOKEN(BUILTIN, "builtin");
91 TOKEN(INPUT_ATTACHMENT_INDEX, "input_attachment_index");
92 TOKEN(ORIGIN_UPPER_LEFT, "origin_upper_left");
93 TOKEN(OVERRIDE_COVERAGE, "override_coverage");
94 TOKEN(BLEND_SUPPORT_ALL_EQUATIONS, "blend_support_all_equations");
95 TOKEN(BLEND_SUPPORT_MULTIPLY, "blend_support_multiply");
96 TOKEN(BLEND_SUPPORT_SCREEN, "blend_support_screen");
97 TOKEN(BLEND_SUPPORT_OVERLAY, "blend_support_overlay");
98 TOKEN(BLEND_SUPPORT_DARKEN, "blend_support_darken");
99 TOKEN(BLEND_SUPPORT_LIGHTEN, "blend_support_lighten");
100 TOKEN(BLEND_SUPPORT_COLORDODGE, "blend_support_colordodge");
101 TOKEN(BLEND_SUPPORT_COLORBURN, "blend_support_colorburn");
102 TOKEN(BLEND_SUPPORT_HARDLIGHT, "blend_support_hardlight");
103 TOKEN(BLEND_SUPPORT_SOFTLIGHT, "blend_support_softlight");
104 TOKEN(BLEND_SUPPORT_DIFFERENCE, "blend_support_difference");
105 TOKEN(BLEND_SUPPORT_EXCLUSION, "blend_support_exclusion");
106 TOKEN(BLEND_SUPPORT_HSL_HUE, "blend_support_hsl_hue");
107 TOKEN(BLEND_SUPPORT_HSL_SATURATION, "blend_support_hsl_saturation");
108 TOKEN(BLEND_SUPPORT_HSL_COLOR, "blend_support_hsl_color");
109 TOKEN(BLEND_SUPPORT_HSL_LUMINOSITY, "blend_support_hsl_luminosity");
110 TOKEN(PUSH_CONSTANT, "push_constant");
111 TOKEN(POINTS, "points");
112 TOKEN(LINES, "lines");
113 TOKEN(LINE_STRIP, "line_strip");
114 TOKEN(LINES_ADJACENCY, "lines_adjacency");
115 TOKEN(TRIANGLES, "triangles");
116 TOKEN(TRIANGLE_STRIP, "triangle_strip");
117 TOKEN(TRIANGLES_ADJACENCY, "triangles_adjacency");
118 TOKEN(MAX_VERTICES, "max_vertices");
119 TOKEN(INVOCATIONS, "invocations");
120 TOKEN(WHEN, "when");
121 TOKEN(KEY, "key");
Michael Ludwiga4275592018-08-31 10:52:47 -0400122 TOKEN(TRACKED, "tracked");
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400123 TOKEN(CTYPE, "ctype");
Brian Osmanf28e55d2018-10-03 16:35:54 -0400124 TOKEN(SKPMCOLOR4F, "SkPMColor4f");
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400125 TOKEN(SKRECT, "SkRect");
126 TOKEN(SKIRECT, "SkIRect");
127 TOKEN(SKPMCOLOR, "SkPMColor");
Ethan Nicholasb93af7e2018-07-24 11:28:52 -0400128 #undef TOKEN
129}
130
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700131Parser::Parser(const char* text, size_t length, SymbolTable& types, ErrorReporter& errors)
132: fText(text)
133, fPushback(Token::INVALID, -1, -1)
ethannicholasb3058bd2016-07-01 08:22:01 -0700134, fTypes(types)
135, fErrors(errors) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700136 fLexer.start(text, length);
Brian Salomon3b83afe2018-08-23 11:04:36 -0400137 static const bool layoutMapInitialized = []{ return (void)InitLayoutMap(), true; }();
138 (void) layoutMapInitialized;
ethannicholasb3058bd2016-07-01 08:22:01 -0700139}
140
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400141/* (directive | section | declaration)* END_OF_FILE */
ethannicholasb3058bd2016-07-01 08:22:01 -0700142std::vector<std::unique_ptr<ASTDeclaration>> Parser::file() {
143 std::vector<std::unique_ptr<ASTDeclaration>> result;
144 for (;;) {
145 switch (this->peek().fKind) {
146 case Token::END_OF_FILE:
147 return result;
ethannicholasb3058bd2016-07-01 08:22:01 -0700148 case Token::DIRECTIVE: {
149 std::unique_ptr<ASTDeclaration> decl = this->directive();
150 if (decl) {
151 result.push_back(std::move(decl));
152 }
153 break;
154 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400155 case Token::SECTION: {
156 std::unique_ptr<ASTDeclaration> section = this->section();
157 if (section) {
158 result.push_back(std::move(section));
159 }
160 break;
161 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700162 default: {
163 std::unique_ptr<ASTDeclaration> decl = this->declaration();
164 if (!decl) {
165 continue;
166 }
167 result.push_back(std::move(decl));
168 }
169 }
170 }
171}
172
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700173Token Parser::nextRawToken() {
174 if (fPushback.fKind != Token::INVALID) {
175 Token result = fPushback;
176 fPushback.fKind = Token::INVALID;
ethannicholasb3058bd2016-07-01 08:22:01 -0700177 return result;
178 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700179 Token result = fLexer.next();
180 return result;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400181}
182
183Token Parser::nextToken() {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700184 Token token = this->nextRawToken();
185 while (token.fKind == Token::WHITESPACE || token.fKind == Token::LINE_COMMENT ||
186 token.fKind == Token::BLOCK_COMMENT) {
187 token = this->nextRawToken();
188 }
Ethan Nicholas762466e2017-06-29 10:03:38 -0400189 return token;
ethannicholasb3058bd2016-07-01 08:22:01 -0700190}
191
192void Parser::pushback(Token t) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400193 SkASSERT(fPushback.fKind == Token::INVALID);
Ethan Nicholas08b79b72017-08-14 10:35:37 -0400194 fPushback = std::move(t);
ethannicholasb3058bd2016-07-01 08:22:01 -0700195}
196
197Token Parser::peek() {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700198 if (fPushback.fKind == Token::INVALID) {
Brian Osman634624a2017-08-15 11:14:30 -0400199 fPushback = this->nextToken();
200 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700201 return fPushback;
202}
203
Ethan Nicholas0c9d13b2017-05-08 16:18:19 -0400204bool Parser::checkNext(Token::Kind kind, Token* result) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700205 if (fPushback.fKind != Token::INVALID && fPushback.fKind != kind) {
Brian Osman634624a2017-08-15 11:14:30 -0400206 return false;
207 }
Ethan Nicholas0c9d13b2017-05-08 16:18:19 -0400208 Token next = this->nextToken();
209 if (next.fKind == kind) {
210 if (result) {
211 *result = next;
212 }
213 return true;
214 }
Ethan Nicholas08b79b72017-08-14 10:35:37 -0400215 this->pushback(std::move(next));
Ethan Nicholas0c9d13b2017-05-08 16:18:19 -0400216 return false;
217}
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500218
219bool Parser::expect(Token::Kind kind, const char* expected, Token* result) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700220 Token next = this->nextToken();
221 if (next.fKind == kind) {
222 if (result) {
Brian Osman634624a2017-08-15 11:14:30 -0400223 *result = std::move(next);
ethannicholasb3058bd2016-07-01 08:22:01 -0700224 }
225 return true;
226 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700227 this->error(next, "expected " + String(expected) + ", but found '" +
228 this->text(next) + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -0700229 return false;
230 }
231}
232
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700233StringFragment Parser::text(Token token) {
234 return StringFragment(fText + token.fOffset, token.fLength);
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500235}
236
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700237void Parser::error(Token token, String msg) {
238 this->error(token.fOffset, msg);
ethannicholasb3058bd2016-07-01 08:22:01 -0700239}
240
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700241void Parser::error(int offset, String msg) {
242 fErrors.error(offset, msg);
243}
244
245bool Parser::isType(StringFragment name) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700246 return nullptr != fTypes[name];
247}
248
Ethan Nicholas11d53972016-11-28 11:23:23 -0500249/* DIRECTIVE(#version) INT_LITERAL ("es" | "compatibility")? |
ethannicholas5961bc92016-10-12 06:39:56 -0700250 DIRECTIVE(#extension) IDENTIFIER COLON IDENTIFIER */
ethannicholasb3058bd2016-07-01 08:22:01 -0700251std::unique_ptr<ASTDeclaration> Parser::directive() {
252 Token start;
253 if (!this->expect(Token::DIRECTIVE, "a directive", &start)) {
254 return nullptr;
255 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700256 StringFragment text = this->text(start);
257 if (text == "#version") {
ethannicholasb3058bd2016-07-01 08:22:01 -0700258 this->expect(Token::INT_LITERAL, "a version number");
ethannicholas5961bc92016-10-12 06:39:56 -0700259 Token next = this->peek();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700260 StringFragment nextText = this->text(next);
261 if (nextText == "es" || nextText == "compatibility") {
ethannicholas5961bc92016-10-12 06:39:56 -0700262 this->nextToken();
263 }
264 // version is ignored for now; it will eventually become an error when we stop pretending
265 // to be GLSL
ethannicholasb3058bd2016-07-01 08:22:01 -0700266 return nullptr;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700267 } else if (text == "#extension") {
ethannicholasb3058bd2016-07-01 08:22:01 -0700268 Token name;
269 if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) {
270 return nullptr;
271 }
272 if (!this->expect(Token::COLON, "':'")) {
273 return nullptr;
274 }
275 // FIXME: need to start paying attention to this token
276 if (!this->expect(Token::IDENTIFIER, "an identifier")) {
277 return nullptr;
278 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700279 return std::unique_ptr<ASTDeclaration>(new ASTExtension(start.fOffset,
280 String(this->text(name))));
ethannicholasb3058bd2016-07-01 08:22:01 -0700281 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700282 this->error(start, "unsupported directive '" + this->text(start) + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -0700283 return nullptr;
284 }
285}
286
Ethan Nicholas762466e2017-06-29 10:03:38 -0400287/* SECTION LBRACE (LPAREN IDENTIFIER RPAREN)? <any sequence of tokens with balanced braces>
288 RBRACE */
289std::unique_ptr<ASTDeclaration> Parser::section() {
290 Token start;
291 if (!this->expect(Token::SECTION, "a section token", &start)) {
292 return nullptr;
293 }
294 String argument;
295 if (this->peek().fKind == Token::LPAREN) {
296 this->nextToken();
297 Token argToken;
298 if (!this->expect(Token::IDENTIFIER, "an identifier", &argToken)) {
299 return nullptr;
300 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700301 argument = this->text(argToken);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400302 if (!this->expect(Token::RPAREN, "')'")) {
303 return nullptr;
304 }
305 }
306 if (!this->expect(Token::LBRACE, "'{'")) {
307 return nullptr;
308 }
309 String text;
310 int level = 1;
311 for (;;) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700312 Token next = this->nextRawToken();
Ethan Nicholas762466e2017-06-29 10:03:38 -0400313 switch (next.fKind) {
314 case Token::LBRACE:
315 ++level;
316 break;
317 case Token::RBRACE:
318 --level;
319 break;
320 case Token::END_OF_FILE:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700321 this->error(start, "reached end of file while parsing section");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400322 return nullptr;
323 default:
324 break;
325 }
326 if (!level) {
327 break;
328 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700329 text += this->text(next);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400330 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700331 StringFragment name = this->text(start);
332 ++name.fChars;
333 --name.fLength;
334 return std::unique_ptr<ASTDeclaration>(new ASTSection(start.fOffset,
335 String(name),
Ethan Nicholas762466e2017-06-29 10:03:38 -0400336 argument,
337 text));
338}
339
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500340/* ENUM CLASS IDENTIFIER LBRACE (IDENTIFIER (EQ expression)? (COMMA IDENTIFIER (EQ expression))*)?
341 RBRACE */
342std::unique_ptr<ASTDeclaration> Parser::enumDeclaration() {
343 Token start;
344 if (!this->expect(Token::ENUM, "'enum'", &start)) {
345 return nullptr;
346 }
347 if (!this->expect(Token::CLASS, "'class'")) {
348 return nullptr;
349 }
350 Token name;
351 if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) {
352 return nullptr;
353 }
354 if (!this->expect(Token::LBRACE, "'{'")) {
355 return nullptr;
356 }
357 fTypes.add(this->text(name), std::unique_ptr<Symbol>(new Type(this->text(name),
358 Type::kEnum_Kind)));
359 std::vector<StringFragment> names;
360 std::vector<std::unique_ptr<ASTExpression>> values;
361 if (!this->checkNext(Token::RBRACE)) {
362 Token id;
363 if (!this->expect(Token::IDENTIFIER, "an identifier", &id)) {
364 return nullptr;
365 }
366 names.push_back(this->text(id));
367 if (this->checkNext(Token::EQ)) {
368 std::unique_ptr<ASTExpression> value = this->assignmentExpression();
369 if (!value) {
370 return nullptr;
371 }
372 values.push_back(std::move(value));
373 } else {
374 values.push_back(nullptr);
375 }
376 while (!this->checkNext(Token::RBRACE)) {
377 if (!this->expect(Token::COMMA, "','")) {
378 return nullptr;
379 }
380 if (!this->expect(Token::IDENTIFIER, "an identifier", &id)) {
381 return nullptr;
382 }
383 names.push_back(this->text(id));
384 if (this->checkNext(Token::EQ)) {
385 std::unique_ptr<ASTExpression> value = this->assignmentExpression();
386 if (!value) {
387 return nullptr;
388 }
389 values.push_back(std::move(value));
390 } else {
391 values.push_back(nullptr);
392 }
393 }
394 }
395 this->expect(Token::SEMICOLON, "';'");
396 return std::unique_ptr<ASTDeclaration>(new ASTEnum(name.fOffset, this->text(name), names,
397 std::move(values)));
398}
399
400/* enumDeclaration | modifiers (structVarDeclaration | type IDENTIFIER ((LPAREN parameter
ethannicholasb3058bd2016-07-01 08:22:01 -0700401 (COMMA parameter)* RPAREN (block | SEMICOLON)) | SEMICOLON) | interfaceBlock) */
402std::unique_ptr<ASTDeclaration> Parser::declaration() {
ethannicholasb3058bd2016-07-01 08:22:01 -0700403 Token lookahead = this->peek();
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500404 if (lookahead.fKind == Token::ENUM) {
405 return this->enumDeclaration();
406 }
407 Modifiers modifiers = this->modifiers();
408 lookahead = this->peek();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700409 if (lookahead.fKind == Token::IDENTIFIER && !this->isType(this->text(lookahead))) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700410 // we have an identifier that's not a type, could be the start of an interface block
411 return this->interfaceBlock(modifiers);
412 }
413 if (lookahead.fKind == Token::STRUCT) {
414 return this->structVarDeclaration(modifiers);
415 }
ethannicholas5961bc92016-10-12 06:39:56 -0700416 if (lookahead.fKind == Token::SEMICOLON) {
417 this->nextToken();
418 return std::unique_ptr<ASTDeclaration>(new ASTModifiersDeclaration(modifiers));
419 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700420 std::unique_ptr<ASTType> type(this->type());
421 if (!type) {
422 return nullptr;
423 }
Ethan Nicholas0c9d13b2017-05-08 16:18:19 -0400424 if (type->fKind == ASTType::kStruct_Kind && this->checkNext(Token::SEMICOLON)) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700425 return nullptr;
426 }
427 Token name;
428 if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) {
429 return nullptr;
430 }
Ethan Nicholas0c9d13b2017-05-08 16:18:19 -0400431 if (this->checkNext(Token::LPAREN)) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700432 std::vector<std::unique_ptr<ASTParameter>> parameters;
433 while (this->peek().fKind != Token::RPAREN) {
434 if (parameters.size() > 0) {
435 if (!this->expect(Token::COMMA, "','")) {
436 return nullptr;
437 }
438 }
439 std::unique_ptr<ASTParameter> parameter = this->parameter();
440 if (!parameter) {
441 return nullptr;
442 }
443 parameters.push_back(std::move(parameter));
444 }
445 this->nextToken();
446 std::unique_ptr<ASTBlock> body;
Ethan Nicholas0c9d13b2017-05-08 16:18:19 -0400447 if (!this->checkNext(Token::SEMICOLON)) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700448 body = this->block();
449 if (!body) {
450 return nullptr;
451 }
452 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700453 return std::unique_ptr<ASTDeclaration>(new ASTFunction(name.fOffset,
Ethan Nicholascb670962017-04-20 19:31:52 -0400454 modifiers,
455 std::move(type),
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700456 this->text(name),
Ethan Nicholas11d53972016-11-28 11:23:23 -0500457 std::move(parameters),
ethannicholasb3058bd2016-07-01 08:22:01 -0700458 std::move(body)));
459 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700460 return this->varDeclarationEnd(modifiers, std::move(type), this->text(name));
ethannicholasb3058bd2016-07-01 08:22:01 -0700461 }
462}
463
464/* modifiers type IDENTIFIER varDeclarationEnd */
ethannicholas14fe8cc2016-09-07 13:37:16 -0700465std::unique_ptr<ASTVarDeclarations> Parser::varDeclarations() {
Ethan Nicholas11d53972016-11-28 11:23:23 -0500466 Modifiers modifiers = this->modifiers();
ethannicholasb3058bd2016-07-01 08:22:01 -0700467 std::unique_ptr<ASTType> type(this->type());
468 if (!type) {
469 return nullptr;
470 }
471 Token name;
472 if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) {
473 return nullptr;
474 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700475 return this->varDeclarationEnd(modifiers, std::move(type), this->text(name));
ethannicholasb3058bd2016-07-01 08:22:01 -0700476}
477
478/* STRUCT IDENTIFIER LBRACE varDeclaration* RBRACE */
479std::unique_ptr<ASTType> Parser::structDeclaration() {
480 if (!this->expect(Token::STRUCT, "'struct'")) {
481 return nullptr;
482 }
483 Token name;
484 if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) {
485 return nullptr;
486 }
487 if (!this->expect(Token::LBRACE, "'{'")) {
488 return nullptr;
489 }
490 std::vector<Type::Field> fields;
491 while (this->peek().fKind != Token::RBRACE) {
ethannicholas14fe8cc2016-09-07 13:37:16 -0700492 std::unique_ptr<ASTVarDeclarations> decl = this->varDeclarations();
ethannicholasb3058bd2016-07-01 08:22:01 -0700493 if (!decl) {
494 return nullptr;
495 }
ethannicholas14fe8cc2016-09-07 13:37:16 -0700496 for (const auto& var : decl->fVars) {
ethannicholasd598f792016-07-25 10:08:54 -0700497 auto type = (const Type*) fTypes[decl->fType->fName];
ethannicholas14fe8cc2016-09-07 13:37:16 -0700498 for (int i = (int) var.fSizes.size() - 1; i >= 0; i--) {
ethannicholasdd4645b2016-10-14 12:14:46 -0700499 if (!var.fSizes[i] || var.fSizes[i]->fKind != ASTExpression::kInt_Kind) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700500 this->error(decl->fOffset, "array size in struct field must be a constant");
ethannicholasdd4645b2016-10-14 12:14:46 -0700501 return nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -0700502 }
ethannicholas14fe8cc2016-09-07 13:37:16 -0700503 uint64_t columns = ((ASTIntLiteral&) *var.fSizes[i]).fValue;
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400504 String name = type->name() + "[" + to_string(columns) + "]";
ethannicholasd598f792016-07-25 10:08:54 -0700505 type = new Type(name, Type::kArray_Kind, *type, (int) columns);
506 fTypes.takeOwnership((Type*) type);
ethannicholasb3058bd2016-07-01 08:22:01 -0700507 }
ethannicholas14fe8cc2016-09-07 13:37:16 -0700508 fields.push_back(Type::Field(decl->fModifiers, var.fName, type));
509 if (var.fValue) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700510 this->error(decl->fOffset, "initializers are not permitted on struct fields");
ethannicholasb3058bd2016-07-01 08:22:01 -0700511 }
512 }
513 }
514 if (!this->expect(Token::RBRACE, "'}'")) {
515 return nullptr;
516 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700517 fTypes.add(this->text(name), std::unique_ptr<Type>(new Type(name.fOffset, this->text(name),
518 fields)));
519 return std::unique_ptr<ASTType>(new ASTType(name.fOffset, this->text(name),
Ethan Nicholas50afc172017-02-16 14:49:57 -0500520 ASTType::kStruct_Kind, std::vector<int>()));
ethannicholasb3058bd2016-07-01 08:22:01 -0700521}
522
523/* structDeclaration ((IDENTIFIER varDeclarationEnd) | SEMICOLON) */
Ethan Nicholas11d53972016-11-28 11:23:23 -0500524std::unique_ptr<ASTVarDeclarations> Parser::structVarDeclaration(Modifiers modifiers) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700525 std::unique_ptr<ASTType> type = this->structDeclaration();
526 if (!type) {
527 return nullptr;
528 }
Ethan Nicholas0c9d13b2017-05-08 16:18:19 -0400529 Token name;
530 if (this->checkNext(Token::IDENTIFIER, &name)) {
Ethan Nicholas11d53972016-11-28 11:23:23 -0500531 std::unique_ptr<ASTVarDeclarations> result = this->varDeclarationEnd(modifiers,
532 std::move(type),
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700533 this->text(name));
ethannicholasb3058bd2016-07-01 08:22:01 -0700534 if (result) {
ethannicholas14fe8cc2016-09-07 13:37:16 -0700535 for (const auto& var : result->fVars) {
536 if (var.fValue) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700537 this->error(var.fValue->fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -0700538 "struct variables cannot be initialized");
539 }
540 }
541 }
542 return result;
543 }
544 this->expect(Token::SEMICOLON, "';'");
545 return nullptr;
546}
547
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400548/* (LBRACKET expression? RBRACKET)* (EQ assignmentExpression)? (COMMA IDENTIFER
549 (LBRACKET expression? RBRACKET)* (EQ assignmentExpression)?)* SEMICOLON */
Ethan Nicholas11d53972016-11-28 11:23:23 -0500550std::unique_ptr<ASTVarDeclarations> Parser::varDeclarationEnd(Modifiers mods,
ethannicholas14fe8cc2016-09-07 13:37:16 -0700551 std::unique_ptr<ASTType> type,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700552 StringFragment name) {
ethannicholas14fe8cc2016-09-07 13:37:16 -0700553 std::vector<ASTVarDeclaration> vars;
ethannicholasb3058bd2016-07-01 08:22:01 -0700554 std::vector<std::unique_ptr<ASTExpression>> currentVarSizes;
Ethan Nicholas0c9d13b2017-05-08 16:18:19 -0400555 while (this->checkNext(Token::LBRACKET)) {
556 if (this->checkNext(Token::RBRACKET)) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700557 currentVarSizes.push_back(nullptr);
558 } else {
559 std::unique_ptr<ASTExpression> size(this->expression());
560 if (!size) {
561 return nullptr;
562 }
563 currentVarSizes.push_back(std::move(size));
564 if (!this->expect(Token::RBRACKET, "']'")) {
565 return nullptr;
566 }
567 }
568 }
ethannicholas14fe8cc2016-09-07 13:37:16 -0700569 std::unique_ptr<ASTExpression> value;
Ethan Nicholas0c9d13b2017-05-08 16:18:19 -0400570 if (this->checkNext(Token::EQ)) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400571 value = this->assignmentExpression();
ethannicholasb3058bd2016-07-01 08:22:01 -0700572 if (!value) {
573 return nullptr;
574 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700575 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700576 vars.emplace_back(name, std::move(currentVarSizes), std::move(value));
Ethan Nicholas0c9d13b2017-05-08 16:18:19 -0400577 while (this->checkNext(Token::COMMA)) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700578 Token name;
579 if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) {
580 return nullptr;
581 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700582 currentVarSizes.clear();
ethannicholas14fe8cc2016-09-07 13:37:16 -0700583 value.reset();
Ethan Nicholas0c9d13b2017-05-08 16:18:19 -0400584 while (this->checkNext(Token::LBRACKET)) {
585 if (this->checkNext(Token::RBRACKET)) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700586 currentVarSizes.push_back(nullptr);
587 } else {
588 std::unique_ptr<ASTExpression> size(this->expression());
589 if (!size) {
590 return nullptr;
591 }
592 currentVarSizes.push_back(std::move(size));
593 if (!this->expect(Token::RBRACKET, "']'")) {
594 return nullptr;
595 }
596 }
597 }
Ethan Nicholas0c9d13b2017-05-08 16:18:19 -0400598 if (this->checkNext(Token::EQ)) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400599 value = this->assignmentExpression();
ethannicholasb3058bd2016-07-01 08:22:01 -0700600 if (!value) {
601 return nullptr;
602 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700603 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700604 vars.emplace_back(this->text(name), std::move(currentVarSizes), std::move(value));
ethannicholasb3058bd2016-07-01 08:22:01 -0700605 }
606 if (!this->expect(Token::SEMICOLON, "';'")) {
607 return nullptr;
608 }
ethannicholas14fe8cc2016-09-07 13:37:16 -0700609 return std::unique_ptr<ASTVarDeclarations>(new ASTVarDeclarations(std::move(mods),
610 std::move(type),
611 std::move(vars)));
ethannicholasb3058bd2016-07-01 08:22:01 -0700612}
613
614/* modifiers type IDENTIFIER (LBRACKET INT_LITERAL RBRACKET)? */
615std::unique_ptr<ASTParameter> Parser::parameter() {
Ethan Nicholasc6f5e102017-03-31 14:53:17 -0400616 Modifiers modifiers = this->modifiersWithDefaults(0);
ethannicholasb3058bd2016-07-01 08:22:01 -0700617 std::unique_ptr<ASTType> type = this->type();
618 if (!type) {
619 return nullptr;
620 }
621 Token name;
622 if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) {
623 return nullptr;
624 }
625 std::vector<int> sizes;
Ethan Nicholas0c9d13b2017-05-08 16:18:19 -0400626 while (this->checkNext(Token::LBRACKET)) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700627 Token sizeToken;
628 if (!this->expect(Token::INT_LITERAL, "a positive integer", &sizeToken)) {
629 return nullptr;
630 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700631 sizes.push_back(SkSL::stoi(this->text(sizeToken)));
ethannicholasb3058bd2016-07-01 08:22:01 -0700632 if (!this->expect(Token::RBRACKET, "']'")) {
633 return nullptr;
634 }
635 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700636 return std::unique_ptr<ASTParameter>(new ASTParameter(name.fOffset, modifiers, std::move(type),
637 this->text(name), std::move(sizes)));
ethannicholasb3058bd2016-07-01 08:22:01 -0700638}
639
Ethan Nicholasd608c092017-10-26 09:30:08 -0400640/** EQ INT_LITERAL */
ethannicholasb3058bd2016-07-01 08:22:01 -0700641int Parser::layoutInt() {
642 if (!this->expect(Token::EQ, "'='")) {
643 return -1;
644 }
645 Token resultToken;
646 if (this->expect(Token::INT_LITERAL, "a non-negative integer", &resultToken)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700647 return SkSL::stoi(this->text(resultToken));
ethannicholasb3058bd2016-07-01 08:22:01 -0700648 }
649 return -1;
650}
651
Ethan Nicholasd608c092017-10-26 09:30:08 -0400652/** EQ IDENTIFIER */
653StringFragment Parser::layoutIdentifier() {
654 if (!this->expect(Token::EQ, "'='")) {
655 return StringFragment();
656 }
657 Token resultToken;
658 if (!this->expect(Token::IDENTIFIER, "an identifier", &resultToken)) {
659 return StringFragment();
660 }
661 return this->text(resultToken);
662}
663
664
Ethan Nicholas762466e2017-06-29 10:03:38 -0400665/** EQ <any sequence of tokens with balanced parentheses and no top-level comma> */
666String Parser::layoutCode() {
667 if (!this->expect(Token::EQ, "'='")) {
668 return "";
669 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700670 Token start = this->nextRawToken();
Ethan Nicholas08b79b72017-08-14 10:35:37 -0400671 this->pushback(start);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400672 String code;
673 int level = 1;
674 bool done = false;
675 while (!done) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700676 Token next = this->nextRawToken();
Ethan Nicholas762466e2017-06-29 10:03:38 -0400677 switch (next.fKind) {
678 case Token::LPAREN:
679 ++level;
680 break;
681 case Token::RPAREN:
682 --level;
683 break;
684 case Token::COMMA:
685 if (level == 1) {
686 done = true;
687 }
688 break;
689 case Token::END_OF_FILE:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700690 this->error(start, "reached end of file while parsing layout");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400691 return nullptr;
692 default:
693 break;
694 }
695 if (!level) {
696 done = true;
697 }
Ethan Nicholas08b79b72017-08-14 10:35:37 -0400698 if (done) {
699 this->pushback(std::move(next));
700 }
701 else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700702 code += this->text(next);
Ethan Nicholas762466e2017-06-29 10:03:38 -0400703 }
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)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700714 if (this->text(key) == "identity") {
Ethan Nicholas762466e2017-06-29 10:03:38 -0400715 return Layout::kIdentity_Key;
716 } else {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700717 this->error(key, "unsupported layout key");
Ethan Nicholas762466e2017-06-29 10:03:38 -0400718 }
719 }
720 }
721 return Layout::kKey_Key;
722}
723
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400724Layout::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) {
Brian Osmanf28e55d2018-10-03 16:35:54 -0400731 case LayoutToken::SKPMCOLOR4F:
732 return Layout::CType::kSkPMColor4f;
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400733 case LayoutToken::SKRECT:
734 return Layout::CType::kSkRect;
735 case LayoutToken::SKIRECT:
736 return Layout::CType::kSkIRect;
737 case LayoutToken::SKPMCOLOR:
738 return Layout::CType::kSkPMColor;
739 default:
740 break;
741 }
742 }
743 this->error(t, "unsupported ctype");
744 }
745 return Layout::CType::kDefault;
746}
747
ethannicholas8ac838d2016-11-22 08:39:36 -0800748/* LAYOUT LPAREN IDENTIFIER (EQ INT_LITERAL)? (COMMA IDENTIFIER (EQ INT_LITERAL)?)* RPAREN */
Ethan Nicholas11d53972016-11-28 11:23:23 -0500749Layout Parser::layout() {
Ethan Nicholas39204fd2017-11-27 13:12:30 -0500750 int flags = 0;
ethannicholasb3058bd2016-07-01 08:22:01 -0700751 int location = -1;
Ethan Nicholas19671772016-11-28 16:30:17 -0500752 int offset = -1;
ethannicholasb3058bd2016-07-01 08:22:01 -0700753 int binding = -1;
754 int index = -1;
755 int set = -1;
756 int builtin = -1;
Greg Daniel64773e62016-11-22 09:44:03 -0500757 int inputAttachmentIndex = -1;
Ethan Nicholas11d53972016-11-28 11:23:23 -0500758 Layout::Format format = Layout::Format::kUnspecified;
Ethan Nicholas52cad152017-02-16 16:37:32 -0500759 Layout::Primitive primitive = Layout::kUnspecified_Primitive;
760 int maxVertices = -1;
761 int invocations = -1;
Ethan Nicholas762466e2017-06-29 10:03:38 -0400762 String when;
763 Layout::Key key = Layout::kNo_Key;
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400764 Layout::CType ctype = Layout::CType::kDefault;
Ethan Nicholas0c9d13b2017-05-08 16:18:19 -0400765 if (this->checkNext(Token::LAYOUT)) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700766 if (!this->expect(Token::LPAREN, "'('")) {
Ethan Nicholas39204fd2017-11-27 13:12:30 -0500767 return Layout(flags, location, offset, binding, index, set, builtin,
768 inputAttachmentIndex, format, primitive, maxVertices, invocations, when,
769 key, ctype);
ethannicholasb3058bd2016-07-01 08:22:01 -0700770 }
771 for (;;) {
772 Token t = this->nextToken();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700773 String text = this->text(t);
Ethan Nicholasb93af7e2018-07-24 11:28:52 -0400774 auto found = layoutTokens->find(text);
775 if (found != layoutTokens->end()) {
776 switch (found->second) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700777 case LayoutToken::LOCATION:
Ethan Nicholas3614d9a2017-02-15 12:33:30 -0500778 location = this->layoutInt();
779 break;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700780 case LayoutToken::OFFSET:
Ethan Nicholas3614d9a2017-02-15 12:33:30 -0500781 offset = this->layoutInt();
782 break;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700783 case LayoutToken::BINDING:
Ethan Nicholas3614d9a2017-02-15 12:33:30 -0500784 binding = this->layoutInt();
785 break;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700786 case LayoutToken::INDEX:
Ethan Nicholas3614d9a2017-02-15 12:33:30 -0500787 index = this->layoutInt();
788 break;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700789 case LayoutToken::SET:
Ethan Nicholas3614d9a2017-02-15 12:33:30 -0500790 set = this->layoutInt();
791 break;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700792 case LayoutToken::BUILTIN:
Ethan Nicholas3614d9a2017-02-15 12:33:30 -0500793 builtin = this->layoutInt();
794 break;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700795 case LayoutToken::INPUT_ATTACHMENT_INDEX:
Ethan Nicholas3614d9a2017-02-15 12:33:30 -0500796 inputAttachmentIndex = this->layoutInt();
797 break;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700798 case LayoutToken::ORIGIN_UPPER_LEFT:
Ethan Nicholas39204fd2017-11-27 13:12:30 -0500799 flags |= Layout::kOriginUpperLeft_Flag;
Ethan Nicholas3614d9a2017-02-15 12:33:30 -0500800 break;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700801 case LayoutToken::OVERRIDE_COVERAGE:
Ethan Nicholas39204fd2017-11-27 13:12:30 -0500802 flags |= Layout::kOverrideCoverage_Flag;
Ethan Nicholas3614d9a2017-02-15 12:33:30 -0500803 break;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700804 case LayoutToken::BLEND_SUPPORT_ALL_EQUATIONS:
Ethan Nicholas39204fd2017-11-27 13:12:30 -0500805 flags |= Layout::kBlendSupportAllEquations_Flag;
806 break;
807 case LayoutToken::BLEND_SUPPORT_MULTIPLY:
808 flags |= Layout::kBlendSupportMultiply_Flag;
809 break;
810 case LayoutToken::BLEND_SUPPORT_SCREEN:
811 flags |= Layout::kBlendSupportScreen_Flag;
812 break;
813 case LayoutToken::BLEND_SUPPORT_OVERLAY:
814 flags |= Layout::kBlendSupportOverlay_Flag;
815 break;
816 case LayoutToken::BLEND_SUPPORT_DARKEN:
817 flags |= Layout::kBlendSupportDarken_Flag;
818 break;
819 case LayoutToken::BLEND_SUPPORT_LIGHTEN:
820 flags |= Layout::kBlendSupportLighten_Flag;
821 break;
822 case LayoutToken::BLEND_SUPPORT_COLORDODGE:
823 flags |= Layout::kBlendSupportColorDodge_Flag;
824 break;
825 case LayoutToken::BLEND_SUPPORT_COLORBURN:
826 flags |= Layout::kBlendSupportColorBurn_Flag;
827 break;
828 case LayoutToken::BLEND_SUPPORT_HARDLIGHT:
829 flags |= Layout::kBlendSupportHardLight_Flag;
830 break;
831 case LayoutToken::BLEND_SUPPORT_SOFTLIGHT:
832 flags |= Layout::kBlendSupportSoftLight_Flag;
833 break;
834 case LayoutToken::BLEND_SUPPORT_DIFFERENCE:
835 flags |= Layout::kBlendSupportDifference_Flag;
836 break;
837 case LayoutToken::BLEND_SUPPORT_EXCLUSION:
838 flags |= Layout::kBlendSupportExclusion_Flag;
839 break;
840 case LayoutToken::BLEND_SUPPORT_HSL_HUE:
841 flags |= Layout::kBlendSupportHSLHue_Flag;
842 break;
843 case LayoutToken::BLEND_SUPPORT_HSL_SATURATION:
844 flags |= Layout::kBlendSupportHSLSaturation_Flag;
845 break;
846 case LayoutToken::BLEND_SUPPORT_HSL_COLOR:
847 flags |= Layout::kBlendSupportHSLColor_Flag;
848 break;
849 case LayoutToken::BLEND_SUPPORT_HSL_LUMINOSITY:
850 flags |= Layout::kBlendSupportHSLLuminosity_Flag;
Ethan Nicholas3614d9a2017-02-15 12:33:30 -0500851 break;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700852 case LayoutToken::PUSH_CONSTANT:
Ethan Nicholas39204fd2017-11-27 13:12:30 -0500853 flags |= Layout::kPushConstant_Flag;
Ethan Nicholas3614d9a2017-02-15 12:33:30 -0500854 break;
Michael Ludwiga4275592018-08-31 10:52:47 -0400855 case LayoutToken::TRACKED:
856 flags |= Layout::kTracked_Flag;
857 break;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700858 case LayoutToken::POINTS:
Ethan Nicholas52cad152017-02-16 16:37:32 -0500859 primitive = Layout::kPoints_Primitive;
860 break;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700861 case LayoutToken::LINES:
Ethan Nicholas52cad152017-02-16 16:37:32 -0500862 primitive = Layout::kLines_Primitive;
863 break;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700864 case LayoutToken::LINE_STRIP:
Ethan Nicholas52cad152017-02-16 16:37:32 -0500865 primitive = Layout::kLineStrip_Primitive;
866 break;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700867 case LayoutToken::LINES_ADJACENCY:
Ethan Nicholas52cad152017-02-16 16:37:32 -0500868 primitive = Layout::kLinesAdjacency_Primitive;
869 break;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700870 case LayoutToken::TRIANGLES:
Ethan Nicholas52cad152017-02-16 16:37:32 -0500871 primitive = Layout::kTriangles_Primitive;
872 break;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700873 case LayoutToken::TRIANGLE_STRIP:
Ethan Nicholas52cad152017-02-16 16:37:32 -0500874 primitive = Layout::kTriangleStrip_Primitive;
875 break;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700876 case LayoutToken::TRIANGLES_ADJACENCY:
Ethan Nicholas52cad152017-02-16 16:37:32 -0500877 primitive = Layout::kTrianglesAdjacency_Primitive;
878 break;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700879 case LayoutToken::MAX_VERTICES:
Ethan Nicholas52cad152017-02-16 16:37:32 -0500880 maxVertices = this->layoutInt();
881 break;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700882 case LayoutToken::INVOCATIONS:
Ethan Nicholas52cad152017-02-16 16:37:32 -0500883 invocations = this->layoutInt();
884 break;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700885 case LayoutToken::WHEN:
Ethan Nicholas762466e2017-06-29 10:03:38 -0400886 when = this->layoutCode();
887 break;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700888 case LayoutToken::KEY:
Ethan Nicholas762466e2017-06-29 10:03:38 -0400889 key = this->layoutKey();
890 break;
Ethan Nicholasd608c092017-10-26 09:30:08 -0400891 case LayoutToken::CTYPE:
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400892 ctype = this->layoutCType();
893 break;
894 default:
895 this->error(t, ("'" + text + "' is not a valid layout qualifier").c_str());
Ethan Nicholasd608c092017-10-26 09:30:08 -0400896 break;
Ethan Nicholas3614d9a2017-02-15 12:33:30 -0500897 }
Ethan Nicholasb93af7e2018-07-24 11:28:52 -0400898 } else if (Layout::ReadFormat(text, &format)) {
Brian Salomon2a51de82016-11-16 12:06:01 -0500899 // AST::ReadFormat stored the result in 'format'.
ethannicholasb3058bd2016-07-01 08:22:01 -0700900 } else {
Ethan Nicholasb93af7e2018-07-24 11:28:52 -0400901 this->error(t, ("'" + text + "' is not a valid layout qualifier").c_str());
ethannicholasb3058bd2016-07-01 08:22:01 -0700902 }
Ethan Nicholas0c9d13b2017-05-08 16:18:19 -0400903 if (this->checkNext(Token::RPAREN)) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700904 break;
905 }
906 if (!this->expect(Token::COMMA, "','")) {
907 break;
908 }
909 }
910 }
Ethan Nicholas39204fd2017-11-27 13:12:30 -0500911 return Layout(flags, location, offset, binding, index, set, builtin, inputAttachmentIndex,
912 format, primitive, maxVertices, invocations, when, key, ctype);
ethannicholasb3058bd2016-07-01 08:22:01 -0700913}
914
Brian Salomonf9f45122016-11-29 11:59:17 -0500915/* layout? (UNIFORM | CONST | IN | OUT | INOUT | LOWP | MEDIUMP | HIGHP | FLAT | NOPERSPECTIVE |
Ethan Nicholasa7ceb502019-01-11 10:31:48 -0500916 READONLY | WRITEONLY | COHERENT | VOLATILE | RESTRICT | BUFFER | PLS | PLSIN |
917 PLSOUT)* */
Ethan Nicholas11d53972016-11-28 11:23:23 -0500918Modifiers Parser::modifiers() {
919 Layout layout = this->layout();
ethannicholasb3058bd2016-07-01 08:22:01 -0700920 int flags = 0;
921 for (;;) {
922 // TODO: handle duplicate / incompatible flags
923 switch (peek().fKind) {
924 case Token::UNIFORM:
925 this->nextToken();
Ethan Nicholas11d53972016-11-28 11:23:23 -0500926 flags |= Modifiers::kUniform_Flag;
ethannicholasb3058bd2016-07-01 08:22:01 -0700927 break;
928 case Token::CONST:
929 this->nextToken();
Ethan Nicholas11d53972016-11-28 11:23:23 -0500930 flags |= Modifiers::kConst_Flag;
ethannicholasb3058bd2016-07-01 08:22:01 -0700931 break;
932 case Token::IN:
933 this->nextToken();
Ethan Nicholas11d53972016-11-28 11:23:23 -0500934 flags |= Modifiers::kIn_Flag;
ethannicholasb3058bd2016-07-01 08:22:01 -0700935 break;
936 case Token::OUT:
937 this->nextToken();
Ethan Nicholas11d53972016-11-28 11:23:23 -0500938 flags |= Modifiers::kOut_Flag;
ethannicholasb3058bd2016-07-01 08:22:01 -0700939 break;
940 case Token::INOUT:
941 this->nextToken();
Ethan Nicholas11d53972016-11-28 11:23:23 -0500942 flags |= Modifiers::kIn_Flag;
943 flags |= Modifiers::kOut_Flag;
ethannicholasb3058bd2016-07-01 08:22:01 -0700944 break;
945 case Token::LOWP:
946 this->nextToken();
Ethan Nicholas11d53972016-11-28 11:23:23 -0500947 flags |= Modifiers::kLowp_Flag;
ethannicholasb3058bd2016-07-01 08:22:01 -0700948 break;
949 case Token::MEDIUMP:
950 this->nextToken();
Ethan Nicholas11d53972016-11-28 11:23:23 -0500951 flags |= Modifiers::kMediump_Flag;
ethannicholasb3058bd2016-07-01 08:22:01 -0700952 break;
953 case Token::HIGHP:
954 this->nextToken();
Ethan Nicholas11d53972016-11-28 11:23:23 -0500955 flags |= Modifiers::kHighp_Flag;
ethannicholasb3058bd2016-07-01 08:22:01 -0700956 break;
ethannicholasf789b382016-08-03 12:43:36 -0700957 case Token::FLAT:
958 this->nextToken();
Ethan Nicholas11d53972016-11-28 11:23:23 -0500959 flags |= Modifiers::kFlat_Flag;
ethannicholasf789b382016-08-03 12:43:36 -0700960 break;
961 case Token::NOPERSPECTIVE:
962 this->nextToken();
Ethan Nicholas11d53972016-11-28 11:23:23 -0500963 flags |= Modifiers::kNoPerspective_Flag;
ethannicholasf789b382016-08-03 12:43:36 -0700964 break;
Brian Salomonf9f45122016-11-29 11:59:17 -0500965 case Token::READONLY:
966 this->nextToken();
967 flags |= Modifiers::kReadOnly_Flag;
968 break;
969 case Token::WRITEONLY:
970 this->nextToken();
971 flags |= Modifiers::kWriteOnly_Flag;
972 break;
973 case Token::COHERENT:
974 this->nextToken();
975 flags |= Modifiers::kCoherent_Flag;
976 break;
977 case Token::VOLATILE:
978 this->nextToken();
979 flags |= Modifiers::kVolatile_Flag;
980 break;
981 case Token::RESTRICT:
982 this->nextToken();
983 flags |= Modifiers::kRestrict_Flag;
984 break;
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400985 case Token::BUFFER:
986 this->nextToken();
987 flags |= Modifiers::kBuffer_Flag;
988 break;
Ethan Nicholascb670962017-04-20 19:31:52 -0400989 case Token::HASSIDEEFFECTS:
990 this->nextToken();
991 flags |= Modifiers::kHasSideEffects_Flag;
992 break;
Ethan Nicholasa7ceb502019-01-11 10:31:48 -0500993 case Token::PLS:
994 this->nextToken();
995 flags |= Modifiers::kPLS_Flag;
996 break;
997 case Token::PLSIN:
998 this->nextToken();
999 flags |= Modifiers::kPLSIn_Flag;
1000 break;
1001 case Token::PLSOUT:
1002 this->nextToken();
1003 flags |= Modifiers::kPLSOut_Flag;
1004 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07001005 default:
Ethan Nicholas11d53972016-11-28 11:23:23 -05001006 return Modifiers(layout, flags);
ethannicholasb3058bd2016-07-01 08:22:01 -07001007 }
1008 }
1009}
1010
Ethan Nicholas11d53972016-11-28 11:23:23 -05001011Modifiers Parser::modifiersWithDefaults(int defaultFlags) {
1012 Modifiers result = this->modifiers();
ethannicholasb3058bd2016-07-01 08:22:01 -07001013 if (!result.fFlags) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001014 return Modifiers(result.fLayout, defaultFlags);
ethannicholasb3058bd2016-07-01 08:22:01 -07001015 }
1016 return result;
1017}
1018
1019/* ifStatement | forStatement | doStatement | whileStatement | block | expression */
1020std::unique_ptr<ASTStatement> Parser::statement() {
1021 Token start = this->peek();
1022 switch (start.fKind) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001023 case Token::IF: // fall through
1024 case Token::STATIC_IF:
ethannicholasb3058bd2016-07-01 08:22:01 -07001025 return this->ifStatement();
1026 case Token::FOR:
1027 return this->forStatement();
1028 case Token::DO:
1029 return this->doStatement();
1030 case Token::WHILE:
1031 return this->whileStatement();
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001032 case Token::SWITCH: // fall through
1033 case Token::STATIC_SWITCH:
Ethan Nicholasaf197692017-02-27 13:26:45 -05001034 return this->switchStatement();
ethannicholasb3058bd2016-07-01 08:22:01 -07001035 case Token::RETURN:
1036 return this->returnStatement();
1037 case Token::BREAK:
1038 return this->breakStatement();
1039 case Token::CONTINUE:
1040 return this->continueStatement();
1041 case Token::DISCARD:
1042 return this->discardStatement();
1043 case Token::LBRACE:
1044 return this->block();
1045 case Token::SEMICOLON:
Ethan Nicholas11d53972016-11-28 11:23:23 -05001046 this->nextToken();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001047 return std::unique_ptr<ASTStatement>(new ASTBlock(start.fOffset,
ethannicholas0730be72016-09-01 07:59:02 -07001048 std::vector<std::unique_ptr<ASTStatement>>()));
ethannicholasb3058bd2016-07-01 08:22:01 -07001049 case Token::CONST: // fall through
1050 case Token::HIGHP: // fall through
1051 case Token::MEDIUMP: // fall through
1052 case Token::LOWP: {
ethannicholas14fe8cc2016-09-07 13:37:16 -07001053 auto decl = this->varDeclarations();
ethannicholasb3058bd2016-07-01 08:22:01 -07001054 if (!decl) {
1055 return nullptr;
1056 }
1057 return std::unique_ptr<ASTStatement>(new ASTVarDeclarationStatement(std::move(decl)));
1058 }
1059 case Token::IDENTIFIER:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001060 if (this->isType(this->text(start))) {
ethannicholas14fe8cc2016-09-07 13:37:16 -07001061 auto decl = this->varDeclarations();
ethannicholasb3058bd2016-07-01 08:22:01 -07001062 if (!decl) {
1063 return nullptr;
1064 }
1065 return std::unique_ptr<ASTStatement>(new ASTVarDeclarationStatement(
1066 std::move(decl)));
1067 }
1068 // fall through
1069 default:
1070 return this->expressionStatement();
Ethan Nicholas11d53972016-11-28 11:23:23 -05001071 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001072}
1073
Ethan Nicholas50afc172017-02-16 14:49:57 -05001074/* IDENTIFIER(type) (LBRACKET intLiteral? RBRACKET)* */
ethannicholasb3058bd2016-07-01 08:22:01 -07001075std::unique_ptr<ASTType> Parser::type() {
1076 Token type;
1077 if (!this->expect(Token::IDENTIFIER, "a type", &type)) {
1078 return nullptr;
1079 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001080 if (!this->isType(this->text(type))) {
1081 this->error(type, ("no type named '" + this->text(type) + "'").c_str());
ethannicholasb3058bd2016-07-01 08:22:01 -07001082 return nullptr;
1083 }
Ethan Nicholas50afc172017-02-16 14:49:57 -05001084 std::vector<int> sizes;
Ethan Nicholas0c9d13b2017-05-08 16:18:19 -04001085 while (this->checkNext(Token::LBRACKET)) {
Ethan Nicholas50afc172017-02-16 14:49:57 -05001086 if (this->peek().fKind != Token::RBRACKET) {
1087 int64_t i;
1088 if (this->intLiteral(&i)) {
1089 sizes.push_back(i);
1090 } else {
1091 return nullptr;
1092 }
1093 } else {
1094 sizes.push_back(-1);
1095 }
1096 this->expect(Token::RBRACKET, "']'");
1097 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001098 return std::unique_ptr<ASTType>(new ASTType(type.fOffset, this->text(type),
Ethan Nicholas50afc172017-02-16 14:49:57 -05001099 ASTType::kIdentifier_Kind, sizes));
ethannicholasb3058bd2016-07-01 08:22:01 -07001100}
1101
Ethan Nicholas50afc172017-02-16 14:49:57 -05001102/* IDENTIFIER LBRACE varDeclaration* RBRACE (IDENTIFIER (LBRACKET expression? RBRACKET)*)? */
Ethan Nicholas11d53972016-11-28 11:23:23 -05001103std::unique_ptr<ASTDeclaration> Parser::interfaceBlock(Modifiers mods) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001104 Token name;
1105 if (!this->expect(Token::IDENTIFIER, "an identifier", &name)) {
1106 return nullptr;
1107 }
1108 if (peek().fKind != Token::LBRACE) {
1109 // we only get into interfaceBlock if we found a top-level identifier which was not a type.
Ethan Nicholas11d53972016-11-28 11:23:23 -05001110 // 99% of the time, the user was not actually intending to create an interface block, so
ethannicholasb3058bd2016-07-01 08:22:01 -07001111 // it's better to report it as an unknown type
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001112 this->error(name, "no type named '" + this->text(name) + "'");
ethannicholasb3058bd2016-07-01 08:22:01 -07001113 return nullptr;
1114 }
1115 this->nextToken();
Ethan Nicholas11d53972016-11-28 11:23:23 -05001116 std::vector<std::unique_ptr<ASTVarDeclarations>> decls;
ethannicholasb3058bd2016-07-01 08:22:01 -07001117 while (this->peek().fKind != Token::RBRACE) {
ethannicholas14fe8cc2016-09-07 13:37:16 -07001118 std::unique_ptr<ASTVarDeclarations> decl = this->varDeclarations();
ethannicholasb3058bd2016-07-01 08:22:01 -07001119 if (!decl) {
1120 return nullptr;
1121 }
1122 decls.push_back(std::move(decl));
1123 }
1124 this->nextToken();
Ethan Nicholas50afc172017-02-16 14:49:57 -05001125 std::vector<std::unique_ptr<ASTExpression>> sizes;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001126 StringFragment instanceName;
1127 Token instanceNameToken;
1128 if (this->checkNext(Token::IDENTIFIER, &instanceNameToken)) {
Ethan Nicholas0c9d13b2017-05-08 16:18:19 -04001129 while (this->checkNext(Token::LBRACKET)) {
Ethan Nicholas50afc172017-02-16 14:49:57 -05001130 if (this->peek().fKind != Token::RBRACKET) {
1131 std::unique_ptr<ASTExpression> size = this->expression();
1132 if (!size) {
1133 return nullptr;
1134 }
1135 sizes.push_back(std::move(size));
1136 } else {
1137 sizes.push_back(nullptr);
1138 }
1139 this->expect(Token::RBRACKET, "']'");
1140 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001141 instanceName = this->text(instanceNameToken);
ethannicholasb3058bd2016-07-01 08:22:01 -07001142 }
1143 this->expect(Token::SEMICOLON, "';'");
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001144 return std::unique_ptr<ASTDeclaration>(new ASTInterfaceBlock(name.fOffset, mods,
1145 this->text(name),
Brian Osman634624a2017-08-15 11:14:30 -04001146 std::move(decls),
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001147 instanceName,
Ethan Nicholas50afc172017-02-16 14:49:57 -05001148 std::move(sizes)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001149}
1150
1151/* IF LPAREN expression RPAREN statement (ELSE statement)? */
1152std::unique_ptr<ASTIfStatement> Parser::ifStatement() {
1153 Token start;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001154 bool isStatic = this->checkNext(Token::STATIC_IF, &start);
1155 if (!isStatic && !this->expect(Token::IF, "'if'", &start)) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001156 return nullptr;
1157 }
1158 if (!this->expect(Token::LPAREN, "'('")) {
1159 return nullptr;
1160 }
1161 std::unique_ptr<ASTExpression> test(this->expression());
1162 if (!test) {
1163 return nullptr;
1164 }
1165 if (!this->expect(Token::RPAREN, "')'")) {
1166 return nullptr;
1167 }
1168 std::unique_ptr<ASTStatement> ifTrue(this->statement());
1169 if (!ifTrue) {
1170 return nullptr;
1171 }
1172 std::unique_ptr<ASTStatement> ifFalse;
Ethan Nicholas0c9d13b2017-05-08 16:18:19 -04001173 if (this->checkNext(Token::ELSE)) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001174 ifFalse = this->statement();
1175 if (!ifFalse) {
1176 return nullptr;
1177 }
1178 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001179 return std::unique_ptr<ASTIfStatement>(new ASTIfStatement(start.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001180 isStatic,
1181 std::move(test),
Ethan Nicholas11d53972016-11-28 11:23:23 -05001182 std::move(ifTrue),
ethannicholasb3058bd2016-07-01 08:22:01 -07001183 std::move(ifFalse)));
1184}
1185
1186/* DO statement WHILE LPAREN expression RPAREN SEMICOLON */
1187std::unique_ptr<ASTDoStatement> Parser::doStatement() {
1188 Token start;
1189 if (!this->expect(Token::DO, "'do'", &start)) {
1190 return nullptr;
1191 }
1192 std::unique_ptr<ASTStatement> statement(this->statement());
1193 if (!statement) {
1194 return nullptr;
1195 }
1196 if (!this->expect(Token::WHILE, "'while'")) {
1197 return nullptr;
1198 }
1199 if (!this->expect(Token::LPAREN, "'('")) {
1200 return nullptr;
1201 }
1202 std::unique_ptr<ASTExpression> test(this->expression());
1203 if (!test) {
1204 return nullptr;
1205 }
1206 if (!this->expect(Token::RPAREN, "')'")) {
1207 return nullptr;
1208 }
1209 if (!this->expect(Token::SEMICOLON, "';'")) {
1210 return nullptr;
1211 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001212 return std::unique_ptr<ASTDoStatement>(new ASTDoStatement(start.fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -07001213 std::move(statement),
1214 std::move(test)));
1215}
1216
1217/* WHILE LPAREN expression RPAREN STATEMENT */
1218std::unique_ptr<ASTWhileStatement> Parser::whileStatement() {
1219 Token start;
1220 if (!this->expect(Token::WHILE, "'while'", &start)) {
1221 return nullptr;
1222 }
1223 if (!this->expect(Token::LPAREN, "'('")) {
1224 return nullptr;
1225 }
1226 std::unique_ptr<ASTExpression> test(this->expression());
1227 if (!test) {
1228 return nullptr;
1229 }
1230 if (!this->expect(Token::RPAREN, "')'")) {
1231 return nullptr;
1232 }
1233 std::unique_ptr<ASTStatement> statement(this->statement());
1234 if (!statement) {
1235 return nullptr;
1236 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001237 return std::unique_ptr<ASTWhileStatement>(new ASTWhileStatement(start.fOffset,
Ethan Nicholas11d53972016-11-28 11:23:23 -05001238 std::move(test),
ethannicholasb3058bd2016-07-01 08:22:01 -07001239 std::move(statement)));
1240}
1241
Ethan Nicholasaf197692017-02-27 13:26:45 -05001242/* CASE expression COLON statement* */
1243std::unique_ptr<ASTSwitchCase> Parser::switchCase() {
1244 Token start;
1245 if (!this->expect(Token::CASE, "'case'", &start)) {
1246 return nullptr;
1247 }
1248 std::unique_ptr<ASTExpression> value = this->expression();
1249 if (!value) {
1250 return nullptr;
1251 }
1252 if (!this->expect(Token::COLON, "':'")) {
1253 return nullptr;
1254 }
1255 std::vector<std::unique_ptr<ASTStatement>> statements;
1256 while (this->peek().fKind != Token::RBRACE && this->peek().fKind != Token::CASE &&
1257 this->peek().fKind != Token::DEFAULT) {
1258 std::unique_ptr<ASTStatement> s = this->statement();
1259 if (!s) {
1260 return nullptr;
1261 }
1262 statements.push_back(std::move(s));
1263 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001264 return std::unique_ptr<ASTSwitchCase>(new ASTSwitchCase(start.fOffset, std::move(value),
Ethan Nicholasaf197692017-02-27 13:26:45 -05001265 std::move(statements)));
1266}
1267
1268/* SWITCH LPAREN expression RPAREN LBRACE switchCase* (DEFAULT COLON statement*)? RBRACE */
1269std::unique_ptr<ASTStatement> Parser::switchStatement() {
1270 Token start;
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001271 bool isStatic = this->checkNext(Token::STATIC_SWITCH, &start);
1272 if (!isStatic && !this->expect(Token::SWITCH, "'switch'", &start)) {
Ethan Nicholasaf197692017-02-27 13:26:45 -05001273 return nullptr;
1274 }
1275 if (!this->expect(Token::LPAREN, "'('")) {
1276 return nullptr;
1277 }
1278 std::unique_ptr<ASTExpression> value(this->expression());
1279 if (!value) {
1280 return nullptr;
1281 }
1282 if (!this->expect(Token::RPAREN, "')'")) {
1283 return nullptr;
1284 }
1285 if (!this->expect(Token::LBRACE, "'{'")) {
1286 return nullptr;
1287 }
1288 std::vector<std::unique_ptr<ASTSwitchCase>> cases;
1289 while (this->peek().fKind == Token::CASE) {
1290 std::unique_ptr<ASTSwitchCase> c = this->switchCase();
1291 if (!c) {
1292 return nullptr;
1293 }
1294 cases.push_back(std::move(c));
1295 }
1296 // Requiring default: to be last (in defiance of C and GLSL) was a deliberate decision. Other
1297 // parts of the compiler may rely upon this assumption.
1298 if (this->peek().fKind == Token::DEFAULT) {
1299 Token defaultStart;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001300 SkAssertResult(this->expect(Token::DEFAULT, "'default'", &defaultStart));
Ethan Nicholasaf197692017-02-27 13:26:45 -05001301 if (!this->expect(Token::COLON, "':'")) {
1302 return nullptr;
1303 }
1304 std::vector<std::unique_ptr<ASTStatement>> statements;
1305 while (this->peek().fKind != Token::RBRACE) {
1306 std::unique_ptr<ASTStatement> s = this->statement();
1307 if (!s) {
1308 return nullptr;
1309 }
1310 statements.push_back(std::move(s));
1311 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001312 cases.emplace_back(new ASTSwitchCase(defaultStart.fOffset, nullptr,
Ethan Nicholasaf197692017-02-27 13:26:45 -05001313 std::move(statements)));
1314 }
1315 if (!this->expect(Token::RBRACE, "'}'")) {
1316 return nullptr;
1317 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001318 return std::unique_ptr<ASTStatement>(new ASTSwitchStatement(start.fOffset,
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001319 isStatic,
Ethan Nicholasaf197692017-02-27 13:26:45 -05001320 std::move(value),
1321 std::move(cases)));
1322}
1323
Ethan Nicholas11d53972016-11-28 11:23:23 -05001324/* FOR LPAREN (declaration | expression)? SEMICOLON expression? SEMICOLON expression? RPAREN
ethannicholasb3058bd2016-07-01 08:22:01 -07001325 STATEMENT */
1326std::unique_ptr<ASTForStatement> Parser::forStatement() {
1327 Token start;
1328 if (!this->expect(Token::FOR, "'for'", &start)) {
1329 return nullptr;
1330 }
1331 if (!this->expect(Token::LPAREN, "'('")) {
1332 return nullptr;
1333 }
1334 std::unique_ptr<ASTStatement> initializer;
1335 Token nextToken = this->peek();
1336 switch (nextToken.fKind) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001337 case Token::SEMICOLON:
ethannicholas22f939e2016-10-13 13:25:34 -07001338 this->nextToken();
ethannicholasb3058bd2016-07-01 08:22:01 -07001339 break;
ethannicholasa54401d2016-10-14 08:37:32 -07001340 case Token::CONST: {
1341 std::unique_ptr<ASTVarDeclarations> vd = this->varDeclarations();
1342 if (!vd) {
1343 return nullptr;
1344 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001345 initializer = std::unique_ptr<ASTStatement>(new ASTVarDeclarationStatement(
ethannicholasa54401d2016-10-14 08:37:32 -07001346 std::move(vd)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001347 break;
ethannicholasa54401d2016-10-14 08:37:32 -07001348 }
1349 case Token::IDENTIFIER: {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001350 if (this->isType(this->text(nextToken))) {
ethannicholasa54401d2016-10-14 08:37:32 -07001351 std::unique_ptr<ASTVarDeclarations> vd = this->varDeclarations();
1352 if (!vd) {
1353 return nullptr;
1354 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001355 initializer = std::unique_ptr<ASTStatement>(new ASTVarDeclarationStatement(
ethannicholasa54401d2016-10-14 08:37:32 -07001356 std::move(vd)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001357 break;
1358 }
ethannicholasa54401d2016-10-14 08:37:32 -07001359 } // fall through
ethannicholasb3058bd2016-07-01 08:22:01 -07001360 default:
1361 initializer = this->expressionStatement();
1362 }
1363 std::unique_ptr<ASTExpression> test;
1364 if (this->peek().fKind != Token::SEMICOLON) {
1365 test = this->expression();
1366 if (!test) {
1367 return nullptr;
1368 }
1369 }
1370 if (!this->expect(Token::SEMICOLON, "';'")) {
1371 return nullptr;
1372 }
1373 std::unique_ptr<ASTExpression> next;
ethannicholas22f939e2016-10-13 13:25:34 -07001374 if (this->peek().fKind != Token::RPAREN) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001375 next = this->expression();
1376 if (!next) {
1377 return nullptr;
1378 }
1379 }
1380 if (!this->expect(Token::RPAREN, "')'")) {
1381 return nullptr;
1382 }
1383 std::unique_ptr<ASTStatement> statement(this->statement());
1384 if (!statement) {
1385 return nullptr;
1386 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001387 return std::unique_ptr<ASTForStatement>(new ASTForStatement(start.fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -07001388 std::move(initializer),
1389 std::move(test), std::move(next),
1390 std::move(statement)));
1391}
1392
1393/* RETURN expression? SEMICOLON */
1394std::unique_ptr<ASTReturnStatement> Parser::returnStatement() {
1395 Token start;
1396 if (!this->expect(Token::RETURN, "'return'", &start)) {
1397 return nullptr;
1398 }
1399 std::unique_ptr<ASTExpression> expression;
1400 if (this->peek().fKind != Token::SEMICOLON) {
1401 expression = this->expression();
1402 if (!expression) {
1403 return nullptr;
1404 }
1405 }
1406 if (!this->expect(Token::SEMICOLON, "';'")) {
1407 return nullptr;
1408 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001409 return std::unique_ptr<ASTReturnStatement>(new ASTReturnStatement(start.fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -07001410 std::move(expression)));
1411}
1412
1413/* BREAK SEMICOLON */
1414std::unique_ptr<ASTBreakStatement> Parser::breakStatement() {
1415 Token start;
1416 if (!this->expect(Token::BREAK, "'break'", &start)) {
1417 return nullptr;
1418 }
1419 if (!this->expect(Token::SEMICOLON, "';'")) {
1420 return nullptr;
1421 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001422 return std::unique_ptr<ASTBreakStatement>(new ASTBreakStatement(start.fOffset));
ethannicholasb3058bd2016-07-01 08:22:01 -07001423}
1424
1425/* CONTINUE SEMICOLON */
1426std::unique_ptr<ASTContinueStatement> Parser::continueStatement() {
1427 Token start;
1428 if (!this->expect(Token::CONTINUE, "'continue'", &start)) {
1429 return nullptr;
1430 }
1431 if (!this->expect(Token::SEMICOLON, "';'")) {
1432 return nullptr;
1433 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001434 return std::unique_ptr<ASTContinueStatement>(new ASTContinueStatement(start.fOffset));
ethannicholasb3058bd2016-07-01 08:22:01 -07001435}
1436
1437/* DISCARD SEMICOLON */
1438std::unique_ptr<ASTDiscardStatement> Parser::discardStatement() {
1439 Token start;
1440 if (!this->expect(Token::DISCARD, "'continue'", &start)) {
1441 return nullptr;
1442 }
1443 if (!this->expect(Token::SEMICOLON, "';'")) {
1444 return nullptr;
1445 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001446 return std::unique_ptr<ASTDiscardStatement>(new ASTDiscardStatement(start.fOffset));
ethannicholasb3058bd2016-07-01 08:22:01 -07001447}
1448
1449/* LBRACE statement* RBRACE */
1450std::unique_ptr<ASTBlock> Parser::block() {
ethannicholascad64162016-10-27 10:54:02 -07001451 AutoDepth depth(this);
1452 if (!depth.checkValid()) {
1453 return nullptr;
1454 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001455 Token start;
1456 if (!this->expect(Token::LBRACE, "'{'", &start)) {
1457 return nullptr;
1458 }
1459 std::vector<std::unique_ptr<ASTStatement>> statements;
1460 for (;;) {
1461 switch (this->peek().fKind) {
Ethan Nicholas11d53972016-11-28 11:23:23 -05001462 case Token::RBRACE:
ethannicholasb3058bd2016-07-01 08:22:01 -07001463 this->nextToken();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001464 return std::unique_ptr<ASTBlock>(new ASTBlock(start.fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -07001465 std::move(statements)));
Ethan Nicholas11d53972016-11-28 11:23:23 -05001466 case Token::END_OF_FILE:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001467 this->error(this->peek(), "expected '}', but found end of file");
ethannicholasb3058bd2016-07-01 08:22:01 -07001468 return nullptr;
1469 default: {
1470 std::unique_ptr<ASTStatement> statement = this->statement();
1471 if (!statement) {
1472 return nullptr;
1473 }
1474 statements.push_back(std::move(statement));
1475 }
1476 }
1477 }
1478}
1479
1480/* expression SEMICOLON */
1481std::unique_ptr<ASTExpressionStatement> Parser::expressionStatement() {
1482 std::unique_ptr<ASTExpression> expr = this->expression();
1483 if (expr) {
1484 if (this->expect(Token::SEMICOLON, "';'")) {
1485 ASTExpressionStatement* result = new ASTExpressionStatement(std::move(expr));
1486 return std::unique_ptr<ASTExpressionStatement>(result);
1487 }
1488 }
1489 return nullptr;
1490}
1491
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001492/* commaExpression */
ethannicholasb3058bd2016-07-01 08:22:01 -07001493std::unique_ptr<ASTExpression> Parser::expression() {
ethannicholascad64162016-10-27 10:54:02 -07001494 AutoDepth depth(this);
1495 if (!depth.checkValid()) {
1496 return nullptr;
1497 }
Ethan Nicholas4b330df2017-05-17 10:52:55 -04001498 return this->commaExpression();
1499}
1500
1501/* assignmentExpression (COMMA assignmentExpression)* */
1502std::unique_ptr<ASTExpression> Parser::commaExpression() {
1503 std::unique_ptr<ASTExpression> result = this->assignmentExpression();
1504 if (!result) {
1505 return nullptr;
1506 }
1507 Token t;
1508 while (this->checkNext(Token::COMMA, &t)) {
1509 std::unique_ptr<ASTExpression> right = this->commaExpression();
1510 if (!right) {
1511 return nullptr;
1512 }
Brian Osman634624a2017-08-15 11:14:30 -04001513 result.reset(new ASTBinaryExpression(std::move(result), std::move(t), std::move(right)));
Ethan Nicholas4b330df2017-05-17 10:52:55 -04001514 }
1515 return result;
ethannicholasb3058bd2016-07-01 08:22:01 -07001516}
1517
1518/* ternaryExpression ((EQEQ | STAREQ | SLASHEQ | PERCENTEQ | PLUSEQ | MINUSEQ | SHLEQ | SHREQ |
1519 BITWISEANDEQ | BITWISEXOREQ | BITWISEOREQ | LOGICALANDEQ | LOGICALXOREQ | LOGICALOREQ)
1520 assignmentExpression)*
1521 */
1522std::unique_ptr<ASTExpression> Parser::assignmentExpression() {
1523 std::unique_ptr<ASTExpression> result = this->ternaryExpression();
1524 if (!result) {
1525 return nullptr;
1526 }
1527 for (;;) {
1528 switch (this->peek().fKind) {
1529 case Token::EQ: // fall through
1530 case Token::STAREQ: // fall through
1531 case Token::SLASHEQ: // fall through
1532 case Token::PERCENTEQ: // fall through
1533 case Token::PLUSEQ: // fall through
1534 case Token::MINUSEQ: // fall through
1535 case Token::SHLEQ: // fall through
1536 case Token::SHREQ: // fall through
1537 case Token::BITWISEANDEQ: // fall through
1538 case Token::BITWISEXOREQ: // fall through
1539 case Token::BITWISEOREQ: // fall through
1540 case Token::LOGICALANDEQ: // fall through
1541 case Token::LOGICALXOREQ: // fall through
1542 case Token::LOGICALOREQ: {
1543 Token t = this->nextToken();
1544 std::unique_ptr<ASTExpression> right = this->assignmentExpression();
1545 if (!right) {
1546 return nullptr;
1547 }
Ethan Nicholas11d53972016-11-28 11:23:23 -05001548 result = std::unique_ptr<ASTExpression>(new ASTBinaryExpression(std::move(result),
Brian Osman634624a2017-08-15 11:14:30 -04001549 std::move(t),
ethannicholasb3058bd2016-07-01 08:22:01 -07001550 std::move(right)));
Ethan Nicholas371e29c2018-03-19 13:40:37 -04001551 return result;
ethannicholasb3058bd2016-07-01 08:22:01 -07001552 }
1553 default:
1554 return result;
1555 }
1556 }
1557}
1558
1559/* logicalOrExpression ('?' expression ':' assignmentExpression)? */
1560std::unique_ptr<ASTExpression> Parser::ternaryExpression() {
1561 std::unique_ptr<ASTExpression> result = this->logicalOrExpression();
1562 if (!result) {
1563 return nullptr;
1564 }
Ethan Nicholas0c9d13b2017-05-08 16:18:19 -04001565 if (this->checkNext(Token::QUESTION)) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001566 std::unique_ptr<ASTExpression> trueExpr = this->expression();
1567 if (!trueExpr) {
1568 return nullptr;
1569 }
1570 if (this->expect(Token::COLON, "':'")) {
1571 std::unique_ptr<ASTExpression> falseExpr = this->assignmentExpression();
Ethan Nicholas11d53972016-11-28 11:23:23 -05001572 return std::unique_ptr<ASTExpression>(new ASTTernaryExpression(std::move(result),
1573 std::move(trueExpr),
ethannicholasb3058bd2016-07-01 08:22:01 -07001574 std::move(falseExpr)));
1575 }
1576 return nullptr;
1577 }
1578 return result;
1579}
1580
1581/* logicalXorExpression (LOGICALOR logicalXorExpression)* */
1582std::unique_ptr<ASTExpression> Parser::logicalOrExpression() {
1583 std::unique_ptr<ASTExpression> result = this->logicalXorExpression();
1584 if (!result) {
1585 return nullptr;
1586 }
Ethan Nicholas0c9d13b2017-05-08 16:18:19 -04001587 Token t;
1588 while (this->checkNext(Token::LOGICALOR, &t)) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001589 std::unique_ptr<ASTExpression> right = this->logicalXorExpression();
1590 if (!right) {
1591 return nullptr;
1592 }
Brian Osman634624a2017-08-15 11:14:30 -04001593 result.reset(new ASTBinaryExpression(std::move(result), std::move(t), std::move(right)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001594 }
1595 return result;
1596}
1597
1598/* logicalAndExpression (LOGICALXOR logicalAndExpression)* */
1599std::unique_ptr<ASTExpression> Parser::logicalXorExpression() {
1600 std::unique_ptr<ASTExpression> result = this->logicalAndExpression();
1601 if (!result) {
1602 return nullptr;
1603 }
Ethan Nicholas0c9d13b2017-05-08 16:18:19 -04001604 Token t;
1605 while (this->checkNext(Token::LOGICALXOR, &t)) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001606 std::unique_ptr<ASTExpression> right = this->logicalAndExpression();
1607 if (!right) {
1608 return nullptr;
1609 }
Brian Osman634624a2017-08-15 11:14:30 -04001610 result.reset(new ASTBinaryExpression(std::move(result), std::move(t), std::move(right)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001611 }
1612 return result;
1613}
1614
1615/* bitwiseOrExpression (LOGICALAND bitwiseOrExpression)* */
1616std::unique_ptr<ASTExpression> Parser::logicalAndExpression() {
1617 std::unique_ptr<ASTExpression> result = this->bitwiseOrExpression();
1618 if (!result) {
1619 return nullptr;
1620 }
Ethan Nicholas0c9d13b2017-05-08 16:18:19 -04001621 Token t;
1622 while (this->checkNext(Token::LOGICALAND, &t)) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001623 std::unique_ptr<ASTExpression> right = this->bitwiseOrExpression();
1624 if (!right) {
1625 return nullptr;
1626 }
Brian Osman634624a2017-08-15 11:14:30 -04001627 result.reset(new ASTBinaryExpression(std::move(result), std::move(t), std::move(right)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001628 }
1629 return result;
1630}
1631
1632/* bitwiseXorExpression (BITWISEOR bitwiseXorExpression)* */
1633std::unique_ptr<ASTExpression> Parser::bitwiseOrExpression() {
1634 std::unique_ptr<ASTExpression> result = this->bitwiseXorExpression();
1635 if (!result) {
1636 return nullptr;
1637 }
Ethan Nicholas0c9d13b2017-05-08 16:18:19 -04001638 Token t;
1639 while (this->checkNext(Token::BITWISEOR, &t)) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001640 std::unique_ptr<ASTExpression> right = this->bitwiseXorExpression();
1641 if (!right) {
1642 return nullptr;
1643 }
Brian Osman634624a2017-08-15 11:14:30 -04001644 result.reset(new ASTBinaryExpression(std::move(result), std::move(t), std::move(right)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001645 }
1646 return result;
1647}
1648
1649/* bitwiseAndExpression (BITWISEXOR bitwiseAndExpression)* */
1650std::unique_ptr<ASTExpression> Parser::bitwiseXorExpression() {
1651 std::unique_ptr<ASTExpression> result = this->bitwiseAndExpression();
1652 if (!result) {
1653 return nullptr;
1654 }
Ethan Nicholas0c9d13b2017-05-08 16:18:19 -04001655 Token t;
1656 while (this->checkNext(Token::BITWISEXOR, &t)) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001657 std::unique_ptr<ASTExpression> right = this->bitwiseAndExpression();
1658 if (!right) {
1659 return nullptr;
1660 }
Brian Osman634624a2017-08-15 11:14:30 -04001661 result.reset(new ASTBinaryExpression(std::move(result), std::move(t), std::move(right)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001662 }
1663 return result;
1664}
1665
1666/* equalityExpression (BITWISEAND equalityExpression)* */
1667std::unique_ptr<ASTExpression> Parser::bitwiseAndExpression() {
1668 std::unique_ptr<ASTExpression> result = this->equalityExpression();
1669 if (!result) {
1670 return nullptr;
1671 }
Ethan Nicholas0c9d13b2017-05-08 16:18:19 -04001672 Token t;
1673 while (this->checkNext(Token::BITWISEAND, &t)) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001674 std::unique_ptr<ASTExpression> right = this->equalityExpression();
1675 if (!right) {
1676 return nullptr;
1677 }
Brian Osman634624a2017-08-15 11:14:30 -04001678 result.reset(new ASTBinaryExpression(std::move(result), std::move(t), std::move(right)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001679 }
1680 return result;
1681}
1682
1683/* relationalExpression ((EQEQ | NEQ) relationalExpression)* */
1684std::unique_ptr<ASTExpression> Parser::equalityExpression() {
1685 std::unique_ptr<ASTExpression> result = this->relationalExpression();
1686 if (!result) {
1687 return nullptr;
1688 }
1689 for (;;) {
1690 switch (this->peek().fKind) {
1691 case Token::EQEQ: // fall through
1692 case Token::NEQ: {
1693 Token t = this->nextToken();
1694 std::unique_ptr<ASTExpression> right = this->relationalExpression();
1695 if (!right) {
1696 return nullptr;
1697 }
Brian Osman634624a2017-08-15 11:14:30 -04001698 result.reset(new ASTBinaryExpression(std::move(result), std::move(t), std::move(right)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001699 break;
1700 }
1701 default:
1702 return result;
1703 }
1704 }
1705}
1706
1707/* shiftExpression ((LT | GT | LTEQ | GTEQ) shiftExpression)* */
1708std::unique_ptr<ASTExpression> Parser::relationalExpression() {
1709 std::unique_ptr<ASTExpression> result = this->shiftExpression();
1710 if (!result) {
1711 return nullptr;
1712 }
1713 for (;;) {
1714 switch (this->peek().fKind) {
1715 case Token::LT: // fall through
1716 case Token::GT: // fall through
1717 case Token::LTEQ: // fall through
1718 case Token::GTEQ: {
1719 Token t = this->nextToken();
1720 std::unique_ptr<ASTExpression> right = this->shiftExpression();
1721 if (!right) {
1722 return nullptr;
1723 }
Brian Osman634624a2017-08-15 11:14:30 -04001724 result.reset(new ASTBinaryExpression(std::move(result), std::move(t),
1725 std::move(right)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001726 break;
1727 }
1728 default:
1729 return result;
1730 }
1731 }
1732}
1733
1734/* additiveExpression ((SHL | SHR) additiveExpression)* */
1735std::unique_ptr<ASTExpression> Parser::shiftExpression() {
1736 std::unique_ptr<ASTExpression> result = this->additiveExpression();
1737 if (!result) {
1738 return nullptr;
1739 }
1740 for (;;) {
1741 switch (this->peek().fKind) {
1742 case Token::SHL: // fall through
1743 case Token::SHR: {
1744 Token t = this->nextToken();
1745 std::unique_ptr<ASTExpression> right = this->additiveExpression();
1746 if (!right) {
1747 return nullptr;
1748 }
Brian Osman634624a2017-08-15 11:14:30 -04001749 result.reset(new ASTBinaryExpression(std::move(result), std::move(t),
1750 std::move(right)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001751 break;
1752 }
1753 default:
1754 return result;
1755 }
1756 }
1757}
1758
1759/* multiplicativeExpression ((PLUS | MINUS) multiplicativeExpression)* */
1760std::unique_ptr<ASTExpression> Parser::additiveExpression() {
1761 std::unique_ptr<ASTExpression> result = this->multiplicativeExpression();
1762 if (!result) {
1763 return nullptr;
1764 }
1765 for (;;) {
1766 switch (this->peek().fKind) {
1767 case Token::PLUS: // fall through
1768 case Token::MINUS: {
1769 Token t = this->nextToken();
1770 std::unique_ptr<ASTExpression> right = this->multiplicativeExpression();
1771 if (!right) {
1772 return nullptr;
1773 }
Brian Osman634624a2017-08-15 11:14:30 -04001774 result.reset(new ASTBinaryExpression(std::move(result), std::move(t),
1775 std::move(right)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001776 break;
1777 }
1778 default:
1779 return result;
1780 }
1781 }
1782}
1783
1784/* unaryExpression ((STAR | SLASH | PERCENT) unaryExpression)* */
1785std::unique_ptr<ASTExpression> Parser::multiplicativeExpression() {
1786 std::unique_ptr<ASTExpression> result = this->unaryExpression();
1787 if (!result) {
1788 return nullptr;
1789 }
1790 for (;;) {
1791 switch (this->peek().fKind) {
1792 case Token::STAR: // fall through
1793 case Token::SLASH: // fall through
1794 case Token::PERCENT: {
1795 Token t = this->nextToken();
1796 std::unique_ptr<ASTExpression> right = this->unaryExpression();
1797 if (!right) {
1798 return nullptr;
1799 }
Brian Osman634624a2017-08-15 11:14:30 -04001800 result.reset(new ASTBinaryExpression(std::move(result), std::move(t),
1801 std::move(right)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001802 break;
1803 }
1804 default:
1805 return result;
1806 }
1807 }
1808}
1809
1810/* postfixExpression | (PLUS | MINUS | NOT | PLUSPLUS | MINUSMINUS) unaryExpression */
1811std::unique_ptr<ASTExpression> Parser::unaryExpression() {
1812 switch (this->peek().fKind) {
ethannicholas5961bc92016-10-12 06:39:56 -07001813 case Token::PLUS: // fall through
1814 case Token::MINUS: // fall through
1815 case Token::LOGICALNOT: // fall through
1816 case Token::BITWISENOT: // fall through
1817 case Token::PLUSPLUS: // fall through
ethannicholasb3058bd2016-07-01 08:22:01 -07001818 case Token::MINUSMINUS: {
1819 Token t = this->nextToken();
1820 std::unique_ptr<ASTExpression> expr = this->unaryExpression();
1821 if (!expr) {
1822 return nullptr;
1823 }
Brian Osman634624a2017-08-15 11:14:30 -04001824 return std::unique_ptr<ASTExpression>(new ASTPrefixExpression(std::move(t),
1825 std::move(expr)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001826 }
1827 default:
1828 return this->postfixExpression();
1829 }
1830}
1831
1832/* term suffix* */
1833std::unique_ptr<ASTExpression> Parser::postfixExpression() {
1834 std::unique_ptr<ASTExpression> result = this->term();
1835 if (!result) {
1836 return nullptr;
1837 }
1838 for (;;) {
1839 switch (this->peek().fKind) {
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001840 case Token::LBRACKET: // fall through
1841 case Token::DOT: // fall through
1842 case Token::LPAREN: // fall through
1843 case Token::PLUSPLUS: // fall through
1844 case Token::MINUSMINUS: // fall through
1845 case Token::COLONCOLON: {
ethannicholasb3058bd2016-07-01 08:22:01 -07001846 std::unique_ptr<ASTSuffix> s = this->suffix();
1847 if (!s) {
1848 return nullptr;
1849 }
1850 result.reset(new ASTSuffixExpression(std::move(result), std::move(s)));
1851 break;
1852 }
1853 default:
1854 return result;
1855 }
1856 }
1857}
1858
Ethan Nicholas11d53972016-11-28 11:23:23 -05001859/* LBRACKET expression? RBRACKET | DOT IDENTIFIER | LPAREN parameters RPAREN |
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001860 PLUSPLUS | MINUSMINUS | COLONCOLON IDENTIFIER */
ethannicholasb3058bd2016-07-01 08:22:01 -07001861std::unique_ptr<ASTSuffix> Parser::suffix() {
1862 Token next = this->nextToken();
1863 switch (next.fKind) {
1864 case Token::LBRACKET: {
Ethan Nicholas0c9d13b2017-05-08 16:18:19 -04001865 if (this->checkNext(Token::RBRACKET)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001866 return std::unique_ptr<ASTSuffix>(new ASTIndexSuffix(next.fOffset));
ethannicholas5961bc92016-10-12 06:39:56 -07001867 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001868 std::unique_ptr<ASTExpression> e = this->expression();
1869 if (!e) {
1870 return nullptr;
1871 }
1872 this->expect(Token::RBRACKET, "']' to complete array access expression");
1873 return std::unique_ptr<ASTSuffix>(new ASTIndexSuffix(std::move(e)));
1874 }
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001875 case Token::DOT: // fall through
1876 case Token::COLONCOLON: {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001877 int offset = this->peek().fOffset;
1878 StringFragment text;
ethannicholasb3058bd2016-07-01 08:22:01 -07001879 if (this->identifier(&text)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001880 return std::unique_ptr<ASTSuffix>(new ASTFieldSuffix(offset, std::move(text)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001881 }
1882 return nullptr;
1883 }
1884 case Token::LPAREN: {
1885 std::vector<std::unique_ptr<ASTExpression>> parameters;
1886 if (this->peek().fKind != Token::RPAREN) {
1887 for (;;) {
Ethan Nicholas4b330df2017-05-17 10:52:55 -04001888 std::unique_ptr<ASTExpression> expr = this->assignmentExpression();
ethannicholasb3058bd2016-07-01 08:22:01 -07001889 if (!expr) {
1890 return nullptr;
1891 }
1892 parameters.push_back(std::move(expr));
Ethan Nicholas4b330df2017-05-17 10:52:55 -04001893 if (!this->checkNext(Token::COMMA)) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001894 break;
1895 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001896 }
1897 }
1898 this->expect(Token::RPAREN, "')' to complete function parameters");
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001899 return std::unique_ptr<ASTSuffix>(new ASTCallSuffix(next.fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -07001900 std::move(parameters)));
1901 }
1902 case Token::PLUSPLUS:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001903 return std::unique_ptr<ASTSuffix>(new ASTSuffix(next.fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -07001904 ASTSuffix::kPostIncrement_Kind));
1905 case Token::MINUSMINUS:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001906 return std::unique_ptr<ASTSuffix>(new ASTSuffix(next.fOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -07001907 ASTSuffix::kPostDecrement_Kind));
1908 default: {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001909 this->error(next, "expected expression suffix, but found '" + this->text(next) +
ethannicholasb3058bd2016-07-01 08:22:01 -07001910 "'\n");
1911 return nullptr;
1912 }
1913 }
1914}
1915
1916/* IDENTIFIER | intLiteral | floatLiteral | boolLiteral | '(' expression ')' */
1917std::unique_ptr<ASTExpression> Parser::term() {
1918 std::unique_ptr<ASTExpression> result;
1919 Token t = this->peek();
1920 switch (t.fKind) {
1921 case Token::IDENTIFIER: {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001922 StringFragment text;
ethannicholasb3058bd2016-07-01 08:22:01 -07001923 if (this->identifier(&text)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001924 result.reset(new ASTIdentifier(t.fOffset, std::move(text)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001925 }
1926 break;
1927 }
1928 case Token::INT_LITERAL: {
1929 int64_t i;
1930 if (this->intLiteral(&i)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001931 result.reset(new ASTIntLiteral(t.fOffset, i));
ethannicholasb3058bd2016-07-01 08:22:01 -07001932 }
1933 break;
1934 }
1935 case Token::FLOAT_LITERAL: {
1936 double f;
1937 if (this->floatLiteral(&f)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001938 result.reset(new ASTFloatLiteral(t.fOffset, f));
ethannicholasb3058bd2016-07-01 08:22:01 -07001939 }
1940 break;
1941 }
1942 case Token::TRUE_LITERAL: // fall through
1943 case Token::FALSE_LITERAL: {
1944 bool b;
1945 if (this->boolLiteral(&b)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001946 result.reset(new ASTBoolLiteral(t.fOffset, b));
ethannicholasb3058bd2016-07-01 08:22:01 -07001947 }
1948 break;
1949 }
1950 case Token::LPAREN: {
1951 this->nextToken();
1952 result = this->expression();
1953 if (result) {
1954 this->expect(Token::RPAREN, "')' to complete expression");
1955 }
1956 break;
1957 }
1958 default:
1959 this->nextToken();
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001960 this->error(t.fOffset, "expected expression, but found '" + this->text(t) + "'\n");
ethannicholasb3058bd2016-07-01 08:22:01 -07001961 result = nullptr;
1962 }
1963 return result;
1964}
1965
1966/* INT_LITERAL */
1967bool Parser::intLiteral(int64_t* dest) {
1968 Token t;
1969 if (this->expect(Token::INT_LITERAL, "integer literal", &t)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001970 *dest = SkSL::stol(this->text(t));
ethannicholasb3058bd2016-07-01 08:22:01 -07001971 return true;
1972 }
1973 return false;
1974}
1975
1976/* FLOAT_LITERAL */
1977bool Parser::floatLiteral(double* dest) {
1978 Token t;
1979 if (this->expect(Token::FLOAT_LITERAL, "float literal", &t)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001980 *dest = SkSL::stod(this->text(t));
ethannicholasb3058bd2016-07-01 08:22:01 -07001981 return true;
1982 }
1983 return false;
1984}
1985
1986/* TRUE_LITERAL | FALSE_LITERAL */
1987bool Parser::boolLiteral(bool* dest) {
1988 Token t = this->nextToken();
1989 switch (t.fKind) {
1990 case Token::TRUE_LITERAL:
1991 *dest = true;
1992 return true;
1993 case Token::FALSE_LITERAL:
1994 *dest = false;
1995 return true;
1996 default:
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001997 this->error(t, "expected 'true' or 'false', but found '" + this->text(t) + "'\n");
ethannicholasb3058bd2016-07-01 08:22:01 -07001998 return false;
1999 }
2000}
2001
2002/* IDENTIFIER */
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002003bool Parser::identifier(StringFragment* dest) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002004 Token t;
2005 if (this->expect(Token::IDENTIFIER, "identifier", &t)) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002006 *dest = this->text(t);
ethannicholasb3058bd2016-07-01 08:22:01 -07002007 return true;
2008 }
2009 return false;
2010}
2011
2012} // namespace