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