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