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