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