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 | |
Mike Klein | 4b432fa | 2019-06-06 11:44:05 -0500 | [diff] [blame] | 8 | #include "src/sksl/SkSLParser.h" |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 9 | |
| 10 | #include <memory> |
| 11 | #include "stdio.h" |
| 12 | |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 13 | #include "include/private/SkSLModifiers.h" |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 14 | #include "src/sksl/SkSLASTNode.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/sksl/ir/SkSLSymbolTable.h" |
| 16 | #include "src/sksl/ir/SkSLType.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 17 | |
Ethan Nicholas | b93af7e | 2018-07-24 11:28:52 -0400 | [diff] [blame] | 18 | #ifndef SKSL_STANDALONE |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "include/private/SkOnce.h" |
Ethan Nicholas | b93af7e | 2018-07-24 11:28:52 -0400 | [diff] [blame] | 20 | #endif |
| 21 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 22 | namespace SkSL { |
| 23 | |
John Stiles | 8d05659 | 2020-11-10 10:03:50 -0500 | [diff] [blame] | 24 | static constexpr int kMaxParseDepth = 50; |
John Stiles | d39aec9 | 2020-12-03 14:37:16 -0500 | [diff] [blame] | 25 | static constexpr int kMaxStructDepth = 8; |
John Stiles | a695d62 | 2020-11-10 10:04:26 -0500 | [diff] [blame] | 26 | |
| 27 | static bool struct_is_too_deeply_nested(const Type& type, int limit) { |
| 28 | if (limit < 0) { |
| 29 | return true; |
| 30 | } |
| 31 | |
John Stiles | c0c5106 | 2020-12-03 17:16:29 -0500 | [diff] [blame] | 32 | if (type.isStruct()) { |
John Stiles | a695d62 | 2020-11-10 10:04:26 -0500 | [diff] [blame] | 33 | for (const Type::Field& f : type.fields()) { |
| 34 | if (struct_is_too_deeply_nested(*f.fType, limit - 1)) { |
| 35 | return true; |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | return false; |
| 41 | } |
ethannicholas | cad6416 | 2016-10-27 10:54:02 -0700 | [diff] [blame] | 42 | |
John Stiles | 7d5b90a | 2021-01-21 14:26:51 -0500 | [diff] [blame] | 43 | static int parse_modifier_token(Token::Kind token) { |
| 44 | switch (token) { |
| 45 | case Token::Kind::TK_UNIFORM: return Modifiers::kUniform_Flag; |
| 46 | case Token::Kind::TK_CONST: return Modifiers::kConst_Flag; |
| 47 | case Token::Kind::TK_IN: return Modifiers::kIn_Flag; |
| 48 | case Token::Kind::TK_OUT: return Modifiers::kOut_Flag; |
| 49 | case Token::Kind::TK_INOUT: return Modifiers::kIn_Flag | Modifiers::kOut_Flag; |
| 50 | case Token::Kind::TK_FLAT: return Modifiers::kFlat_Flag; |
| 51 | case Token::Kind::TK_NOPERSPECTIVE: return Modifiers::kNoPerspective_Flag; |
John Stiles | 7d5b90a | 2021-01-21 14:26:51 -0500 | [diff] [blame] | 52 | case Token::Kind::TK_HASSIDEEFFECTS: return Modifiers::kHasSideEffects_Flag; |
John Stiles | 7d5b90a | 2021-01-21 14:26:51 -0500 | [diff] [blame] | 53 | case Token::Kind::TK_INLINE: return Modifiers::kInline_Flag; |
John Stiles | 0dd1a77 | 2021-03-09 22:14:27 -0500 | [diff] [blame] | 54 | case Token::Kind::TK_NOINLINE: return Modifiers::kNoInline_Flag; |
John Stiles | 7d5b90a | 2021-01-21 14:26:51 -0500 | [diff] [blame] | 55 | default: return 0; |
| 56 | } |
| 57 | } |
| 58 | |
ethannicholas | cad6416 | 2016-10-27 10:54:02 -0700 | [diff] [blame] | 59 | class AutoDepth { |
| 60 | public: |
| 61 | AutoDepth(Parser* p) |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 62 | : fParser(p) |
| 63 | , fDepth(0) {} |
ethannicholas | cad6416 | 2016-10-27 10:54:02 -0700 | [diff] [blame] | 64 | |
| 65 | ~AutoDepth() { |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 66 | fParser->fDepth -= fDepth; |
ethannicholas | cad6416 | 2016-10-27 10:54:02 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 69 | bool increase() { |
| 70 | ++fDepth; |
| 71 | ++fParser->fDepth; |
John Stiles | 8d05659 | 2020-11-10 10:03:50 -0500 | [diff] [blame] | 72 | if (fParser->fDepth > kMaxParseDepth) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 73 | fParser->error(fParser->peek(), String("exceeded max parse depth")); |
ethannicholas | cad6416 | 2016-10-27 10:54:02 -0700 | [diff] [blame] | 74 | return false; |
| 75 | } |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | private: |
| 80 | Parser* fParser; |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 81 | int fDepth; |
ethannicholas | cad6416 | 2016-10-27 10:54:02 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
Brian Salomon | 140f3da | 2018-08-23 13:51:27 +0000 | [diff] [blame] | 84 | std::unordered_map<String, Parser::LayoutToken>* Parser::layoutTokens; |
Ethan Nicholas | b93af7e | 2018-07-24 11:28:52 -0400 | [diff] [blame] | 85 | |
| 86 | void Parser::InitLayoutMap() { |
Brian Salomon | 140f3da | 2018-08-23 13:51:27 +0000 | [diff] [blame] | 87 | layoutTokens = new std::unordered_map<String, LayoutToken>; |
Brian Salomon | 2335644 | 2018-11-30 15:33:19 -0500 | [diff] [blame] | 88 | #define TOKEN(name, text) (*layoutTokens)[text] = LayoutToken::name |
Ethan Nicholas | b93af7e | 2018-07-24 11:28:52 -0400 | [diff] [blame] | 89 | TOKEN(LOCATION, "location"); |
| 90 | TOKEN(OFFSET, "offset"); |
| 91 | TOKEN(BINDING, "binding"); |
| 92 | TOKEN(INDEX, "index"); |
| 93 | TOKEN(SET, "set"); |
| 94 | TOKEN(BUILTIN, "builtin"); |
| 95 | TOKEN(INPUT_ATTACHMENT_INDEX, "input_attachment_index"); |
| 96 | TOKEN(ORIGIN_UPPER_LEFT, "origin_upper_left"); |
Ethan Nicholas | b93af7e | 2018-07-24 11:28:52 -0400 | [diff] [blame] | 97 | TOKEN(BLEND_SUPPORT_ALL_EQUATIONS, "blend_support_all_equations"); |
Ethan Nicholas | b93af7e | 2018-07-24 11:28:52 -0400 | [diff] [blame] | 98 | TOKEN(PUSH_CONSTANT, "push_constant"); |
| 99 | TOKEN(POINTS, "points"); |
| 100 | TOKEN(LINES, "lines"); |
| 101 | TOKEN(LINE_STRIP, "line_strip"); |
| 102 | TOKEN(LINES_ADJACENCY, "lines_adjacency"); |
| 103 | TOKEN(TRIANGLES, "triangles"); |
| 104 | TOKEN(TRIANGLE_STRIP, "triangle_strip"); |
| 105 | TOKEN(TRIANGLES_ADJACENCY, "triangles_adjacency"); |
| 106 | TOKEN(MAX_VERTICES, "max_vertices"); |
| 107 | TOKEN(INVOCATIONS, "invocations"); |
Brian Osman | b32d66b | 2020-04-30 17:12:03 -0400 | [diff] [blame] | 108 | TOKEN(SRGB_UNPREMUL, "srgb_unpremul"); |
Ethan Nicholas | b93af7e | 2018-07-24 11:28:52 -0400 | [diff] [blame] | 109 | #undef TOKEN |
| 110 | } |
| 111 | |
Ethan Nicholas | 6823b50 | 2021-06-15 11:42:07 -0400 | [diff] [blame] | 112 | Parser::Parser(skstd::string_view text, SymbolTable& symbols, ErrorReporter& errors) |
| 113 | : fText(text) |
John Stiles | 7cbb09c2 | 2021-01-07 16:07:00 -0500 | [diff] [blame] | 114 | , fPushback(Token::Kind::TK_NONE, -1, -1) |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 115 | , fSymbols(symbols) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 116 | , fErrors(errors) { |
Ethan Nicholas | 6823b50 | 2021-06-15 11:42:07 -0400 | [diff] [blame] | 117 | fLexer.start(text); |
Brian Salomon | 3b83afe | 2018-08-23 11:04:36 -0400 | [diff] [blame] | 118 | static const bool layoutMapInitialized = []{ return (void)InitLayoutMap(), true; }(); |
| 119 | (void) layoutMapInitialized; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 120 | } |
| 121 | |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 122 | template <typename... Args> |
| 123 | ASTNode::ID Parser::createNode(Args&&... args) { |
| 124 | ASTNode::ID result(fFile->fNodes.size()); |
| 125 | fFile->fNodes.emplace_back(&fFile->fNodes, std::forward<Args>(args)...); |
| 126 | return result; |
| 127 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 128 | |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 129 | ASTNode::ID Parser::addChild(ASTNode::ID target, ASTNode::ID child) { |
| 130 | fFile->fNodes[target.fValue].addChild(child); |
| 131 | return child; |
| 132 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 133 | |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 134 | void Parser::createEmptyChild(ASTNode::ID target) { |
| 135 | ASTNode::ID child(fFile->fNodes.size()); |
| 136 | fFile->fNodes.emplace_back(); |
| 137 | fFile->fNodes[target.fValue].addChild(child); |
| 138 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 139 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 140 | /* (directive | section | declaration)* END_OF_FILE */ |
Ethan Nicholas | ba9a04f | 2020-11-06 09:28:04 -0500 | [diff] [blame] | 141 | std::unique_ptr<ASTFile> Parser::compilationUnit() { |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 142 | fFile = std::make_unique<ASTFile>(); |
John Stiles | 18a45b3 | 2021-03-29 18:07:32 -0400 | [diff] [blame] | 143 | fFile->fNodes.reserve(fText.size() / 10); // a typical program is approx 10:1 for chars:nodes |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 144 | ASTNode::ID result = this->createNode(/*offset=*/0, ASTNode::Kind::kFile); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 145 | fFile->fRoot = result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 146 | for (;;) { |
| 147 | switch (this->peek().fKind) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 148 | case Token::Kind::TK_END_OF_FILE: |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 149 | return std::move(fFile); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 150 | case Token::Kind::TK_DIRECTIVE: { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 151 | ASTNode::ID dir = this->directive(); |
| 152 | if (fErrors.errorCount()) { |
| 153 | return nullptr; |
| 154 | } |
| 155 | if (dir) { |
| 156 | getNode(result).addChild(dir); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 157 | } |
| 158 | break; |
| 159 | } |
John Stiles | 7cbb09c2 | 2021-01-07 16:07:00 -0500 | [diff] [blame] | 160 | case Token::Kind::TK_INVALID: { |
| 161 | this->error(this->peek(), String("invalid token")); |
| 162 | return nullptr; |
| 163 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 164 | default: { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 165 | ASTNode::ID decl = this->declaration(); |
| 166 | if (fErrors.errorCount()) { |
| 167 | return nullptr; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 168 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 169 | if (decl) { |
| 170 | getNode(result).addChild(decl); |
| 171 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 175 | return std::move(fFile); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 178 | Token Parser::nextRawToken() { |
John Stiles | 7cbb09c2 | 2021-01-07 16:07:00 -0500 | [diff] [blame] | 179 | if (fPushback.fKind != Token::Kind::TK_NONE) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 180 | Token result = fPushback; |
John Stiles | 7cbb09c2 | 2021-01-07 16:07:00 -0500 | [diff] [blame] | 181 | fPushback.fKind = Token::Kind::TK_NONE; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 182 | return result; |
| 183 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 184 | Token result = fLexer.next(); |
| 185 | return result; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | Token Parser::nextToken() { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 189 | Token token = this->nextRawToken(); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 190 | while (token.fKind == Token::Kind::TK_WHITESPACE || |
| 191 | token.fKind == Token::Kind::TK_LINE_COMMENT || |
| 192 | token.fKind == Token::Kind::TK_BLOCK_COMMENT) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 193 | token = this->nextRawToken(); |
| 194 | } |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 195 | return token; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void Parser::pushback(Token t) { |
John Stiles | 7cbb09c2 | 2021-01-07 16:07:00 -0500 | [diff] [blame] | 199 | SkASSERT(fPushback.fKind == Token::Kind::TK_NONE); |
Ethan Nicholas | 08b79b7 | 2017-08-14 10:35:37 -0400 | [diff] [blame] | 200 | fPushback = std::move(t); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | Token Parser::peek() { |
John Stiles | 7cbb09c2 | 2021-01-07 16:07:00 -0500 | [diff] [blame] | 204 | if (fPushback.fKind == Token::Kind::TK_NONE) { |
Brian Osman | 634624a | 2017-08-15 11:14:30 -0400 | [diff] [blame] | 205 | fPushback = this->nextToken(); |
| 206 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 207 | return fPushback; |
| 208 | } |
| 209 | |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 210 | bool Parser::checkNext(Token::Kind kind, Token* result) { |
John Stiles | 7cbb09c2 | 2021-01-07 16:07:00 -0500 | [diff] [blame] | 211 | if (fPushback.fKind != Token::Kind::TK_NONE && fPushback.fKind != kind) { |
Brian Osman | 634624a | 2017-08-15 11:14:30 -0400 | [diff] [blame] | 212 | return false; |
| 213 | } |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 214 | Token next = this->nextToken(); |
| 215 | if (next.fKind == kind) { |
| 216 | if (result) { |
| 217 | *result = next; |
| 218 | } |
| 219 | return true; |
| 220 | } |
Ethan Nicholas | 08b79b7 | 2017-08-14 10:35:37 -0400 | [diff] [blame] | 221 | this->pushback(std::move(next)); |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 222 | return false; |
| 223 | } |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 224 | |
| 225 | bool Parser::expect(Token::Kind kind, const char* expected, Token* result) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 226 | Token next = this->nextToken(); |
| 227 | if (next.fKind == kind) { |
| 228 | if (result) { |
Brian Osman | 634624a | 2017-08-15 11:14:30 -0400 | [diff] [blame] | 229 | *result = std::move(next); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 230 | } |
| 231 | return true; |
| 232 | } else { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 233 | this->error(next, "expected " + String(expected) + ", but found '" + |
| 234 | this->text(next) + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 235 | return false; |
| 236 | } |
| 237 | } |
| 238 | |
John Stiles | 2630ea3 | 2020-12-04 10:51:21 -0500 | [diff] [blame] | 239 | bool Parser::expectIdentifier(Token* result) { |
| 240 | if (!this->expect(Token::Kind::TK_IDENTIFIER, "an identifier", result)) { |
| 241 | return false; |
| 242 | } |
| 243 | if (this->isType(this->text(*result))) { |
| 244 | this->error(*result, "expected an identifier, but found type '" + |
| 245 | this->text(*result) + "'"); |
| 246 | return false; |
| 247 | } |
| 248 | return true; |
| 249 | } |
| 250 | |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 251 | skstd::string_view Parser::text(Token token) { |
| 252 | return skstd::string_view(fText.begin() + token.fOffset, token.fLength); |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 253 | } |
| 254 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 255 | void Parser::error(Token token, String msg) { |
| 256 | this->error(token.fOffset, msg); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 259 | void Parser::error(int offset, String msg) { |
| 260 | fErrors.error(offset, msg); |
| 261 | } |
| 262 | |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 263 | bool Parser::isType(skstd::string_view name) { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 264 | const Symbol* s = fSymbols[name]; |
John Stiles | 2630ea3 | 2020-12-04 10:51:21 -0500 | [diff] [blame] | 265 | return s && s->is<Type>(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 266 | } |
| 267 | |
John Stiles | 80b02af | 2021-02-12 17:07:51 -0500 | [diff] [blame] | 268 | bool Parser::isArrayType(ASTNode::ID type) { |
| 269 | const ASTNode& node = this->getNode(type); |
| 270 | SkASSERT(node.fKind == ASTNode::Kind::kType); |
| 271 | return node.begin() != node.end(); |
| 272 | } |
| 273 | |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 274 | /* DIRECTIVE(#version) INT_LITERAL ("es" | "compatibility")? | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 275 | DIRECTIVE(#extension) IDENTIFIER COLON IDENTIFIER */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 276 | ASTNode::ID Parser::directive() { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 277 | Token start; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 278 | if (!this->expect(Token::Kind::TK_DIRECTIVE, "a directive", &start)) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 279 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 280 | } |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 281 | skstd::string_view text = this->text(start); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 282 | if (text == "#extension") { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 283 | Token name; |
John Stiles | 2630ea3 | 2020-12-04 10:51:21 -0500 | [diff] [blame] | 284 | if (!this->expectIdentifier(&name)) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 285 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 286 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 287 | if (!this->expect(Token::Kind::TK_COLON, "':'")) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 288 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 289 | } |
| 290 | // FIXME: need to start paying attention to this token |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 291 | if (!this->expect(Token::Kind::TK_IDENTIFIER, "an identifier")) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 292 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 293 | } |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 294 | return this->createNode(start.fOffset, ASTNode::Kind::kExtension, this->text(name)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 295 | } else { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 296 | this->error(start, "unsupported directive '" + this->text(start) + "'"); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 297 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
Brian Osman | c9145f3 | 2021-07-08 13:40:10 -0400 | [diff] [blame^] | 301 | /* modifiers (interfaceBlock | structVarDeclaration | SEMICOLON | |
John Stiles | 147cfda | 2021-05-21 14:11:38 -0400 | [diff] [blame] | 302 | type IDENTIFIER (varDeclarationEnd | LPAREN functionDeclarationEnd))) */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 303 | ASTNode::ID Parser::declaration() { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 304 | Token lookahead = this->peek(); |
Ethan Nicholas | da6320c | 2020-09-02 14:08:23 -0400 | [diff] [blame] | 305 | switch (lookahead.fKind) { |
Ethan Nicholas | da6320c | 2020-09-02 14:08:23 -0400 | [diff] [blame] | 306 | case Token::Kind::TK_SEMICOLON: |
| 307 | this->error(lookahead.fOffset, "expected a declaration, but found ';'"); |
| 308 | return ASTNode::ID::Invalid(); |
| 309 | default: |
| 310 | break; |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 311 | } |
| 312 | Modifiers modifiers = this->modifiers(); |
| 313 | lookahead = this->peek(); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 314 | if (lookahead.fKind == Token::Kind::TK_IDENTIFIER && !this->isType(this->text(lookahead))) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 315 | // we have an identifier that's not a type, could be the start of an interface block |
| 316 | return this->interfaceBlock(modifiers); |
| 317 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 318 | if (lookahead.fKind == Token::Kind::TK_STRUCT) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 319 | return this->structVarDeclaration(modifiers); |
| 320 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 321 | if (lookahead.fKind == Token::Kind::TK_SEMICOLON) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 322 | this->nextToken(); |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 323 | return this->createNode(lookahead.fOffset, ASTNode::Kind::kModifiers, modifiers); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 324 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 325 | ASTNode::ID type = this->type(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 326 | if (!type) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 327 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 328 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 329 | Token name; |
John Stiles | 2630ea3 | 2020-12-04 10:51:21 -0500 | [diff] [blame] | 330 | if (!this->expectIdentifier(&name)) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 331 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 332 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 333 | if (this->checkNext(Token::Kind::TK_LPAREN)) { |
John Stiles | 147cfda | 2021-05-21 14:11:38 -0400 | [diff] [blame] | 334 | return this->functionDeclarationEnd(modifiers, type, name); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 335 | } else { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 336 | return this->varDeclarationEnd(modifiers, type, this->text(name)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | |
John Stiles | 147cfda | 2021-05-21 14:11:38 -0400 | [diff] [blame] | 340 | /* (RPAREN | VOID RPAREN | parameter (COMMA parameter)* RPAREN) (block | SEMICOLON) */ |
| 341 | ASTNode::ID Parser::functionDeclarationEnd(Modifiers modifiers, |
| 342 | ASTNode::ID type, |
| 343 | const Token& name) { |
| 344 | ASTNode::ID result = this->createNode(name.fOffset, ASTNode::Kind::kFunction); |
| 345 | ASTNode::FunctionData fd(modifiers, this->text(name), 0); |
| 346 | getNode(result).addChild(type); |
| 347 | Token lookahead = this->peek(); |
| 348 | if (lookahead.fKind == Token::Kind::TK_RPAREN) { |
| 349 | // `()` means no parameters at all. |
| 350 | } else if (lookahead.fKind == Token::Kind::TK_IDENTIFIER && this->text(lookahead) == "void") { |
| 351 | // `(void)` also means no parameters at all. |
| 352 | this->nextToken(); |
| 353 | } else { |
| 354 | for (;;) { |
| 355 | ASTNode::ID parameter = this->parameter(); |
| 356 | if (!parameter) { |
| 357 | return ASTNode::ID::Invalid(); |
| 358 | } |
| 359 | ++fd.fParameterCount; |
| 360 | getNode(result).addChild(parameter); |
| 361 | if (!this->checkNext(Token::Kind::TK_COMMA)) { |
| 362 | break; |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | getNode(result).setFunctionData(fd); |
| 367 | if (!this->expect(Token::Kind::TK_RPAREN, "')'")) { |
| 368 | return ASTNode::ID::Invalid(); |
| 369 | } |
| 370 | ASTNode::ID body; |
| 371 | if (!this->checkNext(Token::Kind::TK_SEMICOLON)) { |
| 372 | body = this->block(); |
| 373 | if (!body) { |
| 374 | return ASTNode::ID::Invalid(); |
| 375 | } |
| 376 | getNode(result).addChild(body); |
| 377 | } |
| 378 | return result; |
| 379 | } |
| 380 | |
John Stiles | 76389b7 | 2021-01-25 13:49:05 -0500 | [diff] [blame] | 381 | /* (varDeclarations | expressionStatement) */ |
| 382 | ASTNode::ID Parser::varDeclarationsOrExpressionStatement() { |
John Stiles | 89b484a | 2021-04-19 14:57:27 -0400 | [diff] [blame] | 383 | Token nextToken = this->peek(); |
| 384 | if (nextToken.fKind == Token::Kind::TK_CONST) { |
| 385 | // Statements that begin with `const` might be variable declarations, but can't be legal |
| 386 | // SkSL expression-statements. (SkSL constructors don't take a `const` modifier.) |
| 387 | return this->varDeclarations(); |
| 388 | } |
| 389 | |
| 390 | if (this->isType(this->text(nextToken))) { |
John Stiles | 76389b7 | 2021-01-25 13:49:05 -0500 | [diff] [blame] | 391 | // Statements that begin with a typename are most often variable declarations, but |
| 392 | // occasionally the type is part of a constructor, and these are actually expression- |
| 393 | // statements in disguise. First, attempt the common case: parse it as a vardecl. |
| 394 | Checkpoint checkpoint(this); |
John Stiles | 8dabeac | 2021-02-12 16:05:00 -0500 | [diff] [blame] | 395 | VarDeclarationsPrefix prefix; |
| 396 | if (this->varDeclarationsPrefix(&prefix)) { |
| 397 | return this->varDeclarationEnd(prefix.modifiers, prefix.type, this->text(prefix.name)); |
John Stiles | 76389b7 | 2021-01-25 13:49:05 -0500 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | // If this statement wasn't actually a vardecl after all, rewind and try parsing it as an |
| 401 | // expression-statement instead. |
| 402 | checkpoint.rewind(); |
| 403 | } |
| 404 | |
| 405 | return this->expressionStatement(); |
| 406 | } |
| 407 | |
John Stiles | 8dabeac | 2021-02-12 16:05:00 -0500 | [diff] [blame] | 408 | // Helper function for varDeclarations(). If this function succeeds, we assume that the rest of the |
| 409 | // statement is a variable-declaration statement, not an expression-statement. |
| 410 | bool Parser::varDeclarationsPrefix(VarDeclarationsPrefix* prefixData) { |
| 411 | prefixData->modifiers = this->modifiers(); |
| 412 | prefixData->type = this->type(); |
| 413 | if (!prefixData->type) { |
| 414 | return false; |
| 415 | } |
| 416 | return this->expectIdentifier(&prefixData->name); |
| 417 | } |
| 418 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 419 | /* modifiers type IDENTIFIER varDeclarationEnd */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 420 | ASTNode::ID Parser::varDeclarations() { |
John Stiles | 8dabeac | 2021-02-12 16:05:00 -0500 | [diff] [blame] | 421 | VarDeclarationsPrefix prefix; |
| 422 | if (!this->varDeclarationsPrefix(&prefix)) { |
John Stiles | 76389b7 | 2021-01-25 13:49:05 -0500 | [diff] [blame] | 423 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 424 | } |
John Stiles | 8dabeac | 2021-02-12 16:05:00 -0500 | [diff] [blame] | 425 | return this->varDeclarationEnd(prefix.modifiers, prefix.type, this->text(prefix.name)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | /* STRUCT IDENTIFIER LBRACE varDeclaration* RBRACE */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 429 | ASTNode::ID Parser::structDeclaration() { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 430 | if (!this->expect(Token::Kind::TK_STRUCT, "'struct'")) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 431 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 432 | } |
| 433 | Token name; |
John Stiles | 2630ea3 | 2020-12-04 10:51:21 -0500 | [diff] [blame] | 434 | if (!this->expectIdentifier(&name)) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 435 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 436 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 437 | if (!this->expect(Token::Kind::TK_LBRACE, "'{'")) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 438 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 439 | } |
| 440 | std::vector<Type::Field> fields; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 441 | while (this->peek().fKind != Token::Kind::TK_RBRACE) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 442 | ASTNode::ID decls = this->varDeclarations(); |
| 443 | if (!decls) { |
| 444 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 445 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 446 | ASTNode& declsNode = getNode(decls); |
John Stiles | ece1bf0 | 2021-03-08 11:15:55 -0500 | [diff] [blame] | 447 | const Modifiers& modifiers = declsNode.begin()->getModifiers(); |
John Stiles | 13fc260 | 2020-10-09 17:42:31 -0400 | [diff] [blame] | 448 | if (modifiers.fFlags != Modifiers::kNo_Flag) { |
| 449 | String desc = modifiers.description(); |
| 450 | desc.pop_back(); // remove trailing space |
| 451 | this->error(declsNode.fOffset, |
| 452 | "modifier '" + desc + "' is not permitted on a struct field"); |
| 453 | } |
| 454 | |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 455 | const Symbol* symbol = fSymbols[(declsNode.begin() + 1)->getStringView()]; |
John Stiles | 13fc260 | 2020-10-09 17:42:31 -0400 | [diff] [blame] | 456 | SkASSERT(symbol); |
| 457 | const Type* type = &symbol->as<Type>(); |
John Stiles | 1d75778 | 2020-11-17 09:43:22 -0500 | [diff] [blame] | 458 | if (type->isOpaque()) { |
| 459 | this->error(declsNode.fOffset, |
| 460 | "opaque type '" + type->name() + "' is not permitted in a struct"); |
| 461 | } |
John Stiles | 13fc260 | 2020-10-09 17:42:31 -0400 | [diff] [blame] | 462 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 463 | for (auto iter = declsNode.begin() + 2; iter != declsNode.end(); ++iter) { |
| 464 | ASTNode& var = *iter; |
John Stiles | ece1bf0 | 2021-03-08 11:15:55 -0500 | [diff] [blame] | 465 | const ASTNode::VarData& vd = var.getVarData(); |
John Stiles | 6bef6a7 | 2020-12-02 14:26:04 -0500 | [diff] [blame] | 466 | |
John Stiles | d39aec9 | 2020-12-03 14:37:16 -0500 | [diff] [blame] | 467 | // Read array size if one is present. |
| 468 | if (vd.fIsArray) { |
| 469 | const ASTNode& size = *var.begin(); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 470 | if (!size || size.fKind != ASTNode::Kind::kInt) { |
| 471 | this->error(declsNode.fOffset, "array size in struct field must be a constant"); |
| 472 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 473 | } |
John Stiles | 6bef6a7 | 2020-12-02 14:26:04 -0500 | [diff] [blame] | 474 | if (size.getInt() <= 0 || size.getInt() > INT_MAX) { |
| 475 | this->error(declsNode.fOffset, "array size is invalid"); |
| 476 | return ASTNode::ID::Invalid(); |
| 477 | } |
John Stiles | d39aec9 | 2020-12-03 14:37:16 -0500 | [diff] [blame] | 478 | // Add the array dimensions to our type. |
| 479 | int arraySize = size.getInt(); |
John Stiles | a217950 | 2020-12-03 14:37:57 -0500 | [diff] [blame] | 480 | type = fSymbols.addArrayDimension(type, arraySize); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 481 | } |
John Stiles | 13fc260 | 2020-10-09 17:42:31 -0400 | [diff] [blame] | 482 | |
| 483 | fields.push_back(Type::Field(modifiers, vd.fName, type)); |
John Stiles | d39aec9 | 2020-12-03 14:37:16 -0500 | [diff] [blame] | 484 | if (vd.fIsArray ? var.begin()->fNext : var.fFirstChild) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 485 | this->error(declsNode.fOffset, "initializers are not permitted on struct fields"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 489 | if (!this->expect(Token::Kind::TK_RBRACE, "'}'")) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 490 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 491 | } |
Brian Osman | 6c3b23f | 2021-02-12 13:09:58 -0500 | [diff] [blame] | 492 | if (fields.empty()) { |
| 493 | this->error(name.fOffset, |
| 494 | "struct '" + this->text(name) + "' must contain at least one field"); |
| 495 | return ASTNode::ID::Invalid(); |
| 496 | } |
Ethan Nicholas | d2e0960 | 2021-06-10 11:21:59 -0400 | [diff] [blame] | 497 | std::unique_ptr<Type> newType = Type::MakeStructType(name.fOffset, String(this->text(name)), |
| 498 | fields); |
John Stiles | a695d62 | 2020-11-10 10:04:26 -0500 | [diff] [blame] | 499 | if (struct_is_too_deeply_nested(*newType, kMaxStructDepth)) { |
| 500 | this->error(name.fOffset, "struct '" + this->text(name) + "' is too deeply nested"); |
| 501 | return ASTNode::ID::Invalid(); |
| 502 | } |
| 503 | fSymbols.add(std::move(newType)); |
Brian Osman | 00fea5b | 2021-01-28 09:46:30 -0500 | [diff] [blame] | 504 | return this->createNode(name.fOffset, ASTNode::Kind::kType, this->text(name)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | /* structDeclaration ((IDENTIFIER varDeclarationEnd) | SEMICOLON) */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 508 | ASTNode::ID Parser::structVarDeclaration(Modifiers modifiers) { |
| 509 | ASTNode::ID type = this->structDeclaration(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 510 | if (!type) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 511 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 512 | } |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 513 | Token name; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 514 | if (this->checkNext(Token::Kind::TK_IDENTIFIER, &name)) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 515 | return this->varDeclarationEnd(modifiers, std::move(type), this->text(name)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 516 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 517 | this->expect(Token::Kind::TK_SEMICOLON, "';'"); |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 518 | return type; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 519 | } |
| 520 | |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 521 | /* (LBRACKET expression? RBRACKET)* (EQ assignmentExpression)? (COMMA IDENTIFER |
| 522 | (LBRACKET expression? RBRACKET)* (EQ assignmentExpression)?)* SEMICOLON */ |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 523 | ASTNode::ID Parser::varDeclarationEnd(Modifiers mods, ASTNode::ID type, skstd::string_view name) { |
John Stiles | 08070f6 | 2020-11-17 10:45:49 -0500 | [diff] [blame] | 524 | int offset = this->peek().fOffset; |
| 525 | ASTNode::ID result = this->createNode(offset, ASTNode::Kind::kVarDeclarations); |
| 526 | this->addChild(result, this->createNode(offset, ASTNode::Kind::kModifiers, mods)); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 527 | getNode(result).addChild(type); |
John Stiles | d39aec9 | 2020-12-03 14:37:16 -0500 | [diff] [blame] | 528 | |
John Stiles | 80b02af | 2021-02-12 17:07:51 -0500 | [diff] [blame] | 529 | auto parseArrayDimensions = [&](ASTNode::ID currentVar, ASTNode::VarData* vd) -> bool { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 530 | while (this->checkNext(Token::Kind::TK_LBRACKET)) { |
John Stiles | 80b02af | 2021-02-12 17:07:51 -0500 | [diff] [blame] | 531 | if (vd->fIsArray || this->isArrayType(type)) { |
John Stiles | d39aec9 | 2020-12-03 14:37:16 -0500 | [diff] [blame] | 532 | this->error(this->peek(), "multi-dimensional arrays are not supported"); |
| 533 | return false; |
| 534 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 535 | if (this->checkNext(Token::Kind::TK_RBRACKET)) { |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 536 | this->createEmptyChild(currentVar); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 537 | } else { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 538 | ASTNode::ID size = this->expression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 539 | if (!size) { |
John Stiles | d39aec9 | 2020-12-03 14:37:16 -0500 | [diff] [blame] | 540 | return false; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 541 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 542 | getNode(currentVar).addChild(size); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 543 | if (!this->expect(Token::Kind::TK_RBRACKET, "']'")) { |
John Stiles | d39aec9 | 2020-12-03 14:37:16 -0500 | [diff] [blame] | 544 | return false; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 545 | } |
| 546 | } |
John Stiles | d39aec9 | 2020-12-03 14:37:16 -0500 | [diff] [blame] | 547 | vd->fIsArray = true; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 548 | } |
John Stiles | d39aec9 | 2020-12-03 14:37:16 -0500 | [diff] [blame] | 549 | return true; |
| 550 | }; |
| 551 | |
| 552 | auto parseInitializer = [this](ASTNode::ID currentVar) -> bool { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 553 | if (this->checkNext(Token::Kind::TK_EQ)) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 554 | ASTNode::ID value = this->assignmentExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 555 | if (!value) { |
John Stiles | d39aec9 | 2020-12-03 14:37:16 -0500 | [diff] [blame] | 556 | return false; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 557 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 558 | getNode(currentVar).addChild(value); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 559 | } |
John Stiles | d39aec9 | 2020-12-03 14:37:16 -0500 | [diff] [blame] | 560 | return true; |
| 561 | }; |
| 562 | |
| 563 | ASTNode::ID currentVar = this->createNode(offset, ASTNode::Kind::kVarDeclaration); |
| 564 | ASTNode::VarData vd{name, /*isArray=*/false}; |
| 565 | |
| 566 | getNode(result).addChild(currentVar); |
| 567 | if (!parseArrayDimensions(currentVar, &vd)) { |
| 568 | return ASTNode::ID::Invalid(); |
| 569 | } |
| 570 | getNode(currentVar).setVarData(vd); |
| 571 | if (!parseInitializer(currentVar)) { |
| 572 | return ASTNode::ID::Invalid(); |
| 573 | } |
| 574 | |
| 575 | while (this->checkNext(Token::Kind::TK_COMMA)) { |
| 576 | Token identifierName; |
John Stiles | 2630ea3 | 2020-12-04 10:51:21 -0500 | [diff] [blame] | 577 | if (!this->expectIdentifier(&identifierName)) { |
John Stiles | d39aec9 | 2020-12-03 14:37:16 -0500 | [diff] [blame] | 578 | return ASTNode::ID::Invalid(); |
| 579 | } |
| 580 | |
| 581 | currentVar = ASTNode::ID(fFile->fNodes.size()); |
| 582 | vd = ASTNode::VarData{this->text(identifierName), /*isArray=*/false}; |
| 583 | fFile->fNodes.emplace_back(&fFile->fNodes, offset, ASTNode::Kind::kVarDeclaration); |
| 584 | |
| 585 | getNode(result).addChild(currentVar); |
| 586 | if (!parseArrayDimensions(currentVar, &vd)) { |
| 587 | return ASTNode::ID::Invalid(); |
| 588 | } |
| 589 | getNode(currentVar).setVarData(vd); |
| 590 | if (!parseInitializer(currentVar)) { |
| 591 | return ASTNode::ID::Invalid(); |
| 592 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 593 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 594 | if (!this->expect(Token::Kind::TK_SEMICOLON, "';'")) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 595 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 596 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 597 | return result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | /* modifiers type IDENTIFIER (LBRACKET INT_LITERAL RBRACKET)? */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 601 | ASTNode::ID Parser::parameter() { |
Ethan Nicholas | c6f5e10 | 2017-03-31 14:53:17 -0400 | [diff] [blame] | 602 | Modifiers modifiers = this->modifiersWithDefaults(0); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 603 | ASTNode::ID type = this->type(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 604 | if (!type) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 605 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 606 | } |
| 607 | Token name; |
John Stiles | 2630ea3 | 2020-12-04 10:51:21 -0500 | [diff] [blame] | 608 | if (!this->expectIdentifier(&name)) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 609 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 610 | } |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 611 | ASTNode::ID result = this->createNode(name.fOffset, ASTNode::Kind::kParameter); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 612 | ASTNode::ParameterData pd(modifiers, this->text(name), 0); |
| 613 | getNode(result).addChild(type); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 614 | while (this->checkNext(Token::Kind::TK_LBRACKET)) { |
John Stiles | 80b02af | 2021-02-12 17:07:51 -0500 | [diff] [blame] | 615 | if (pd.fIsArray || this->isArrayType(type)) { |
John Stiles | d39aec9 | 2020-12-03 14:37:16 -0500 | [diff] [blame] | 616 | this->error(this->peek(), "multi-dimensional arrays are not supported"); |
| 617 | return ASTNode::ID::Invalid(); |
| 618 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 619 | Token sizeToken; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 620 | if (!this->expect(Token::Kind::TK_INT_LITERAL, "a positive integer", &sizeToken)) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 621 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 622 | } |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 623 | skstd::string_view arraySizeFrag = this->text(sizeToken); |
John Stiles | f94348f | 2020-12-23 18:58:43 -0500 | [diff] [blame] | 624 | SKSL_INT arraySize; |
| 625 | if (!SkSL::stoi(arraySizeFrag, &arraySize)) { |
| 626 | this->error(sizeToken, "array size is too large: " + arraySizeFrag); |
| 627 | return ASTNode::ID::Invalid(); |
| 628 | } |
| 629 | this->addChild(result, this->createNode(sizeToken.fOffset, ASTNode::Kind::kInt, arraySize)); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 630 | if (!this->expect(Token::Kind::TK_RBRACKET, "']'")) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 631 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 632 | } |
John Stiles | d39aec9 | 2020-12-03 14:37:16 -0500 | [diff] [blame] | 633 | pd.fIsArray = true; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 634 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 635 | getNode(result).setParameterData(pd); |
| 636 | return result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 637 | } |
| 638 | |
Ethan Nicholas | d608c09 | 2017-10-26 09:30:08 -0400 | [diff] [blame] | 639 | /** EQ INT_LITERAL */ |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 640 | int Parser::layoutInt() { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 641 | if (!this->expect(Token::Kind::TK_EQ, "'='")) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 642 | return -1; |
| 643 | } |
| 644 | Token resultToken; |
John Stiles | f94348f | 2020-12-23 18:58:43 -0500 | [diff] [blame] | 645 | if (!this->expect(Token::Kind::TK_INT_LITERAL, "a non-negative integer", &resultToken)) { |
| 646 | return -1; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 647 | } |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 648 | skstd::string_view resultFrag = this->text(resultToken); |
John Stiles | f94348f | 2020-12-23 18:58:43 -0500 | [diff] [blame] | 649 | SKSL_INT resultValue; |
| 650 | if (!SkSL::stoi(resultFrag, &resultValue)) { |
| 651 | this->error(resultToken, "value in layout is too large: " + resultFrag); |
| 652 | return -1; |
| 653 | } |
| 654 | return resultValue; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Ethan Nicholas | d608c09 | 2017-10-26 09:30:08 -0400 | [diff] [blame] | 657 | /** EQ IDENTIFIER */ |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 658 | skstd::string_view Parser::layoutIdentifier() { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 659 | if (!this->expect(Token::Kind::TK_EQ, "'='")) { |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 660 | return skstd::string_view(); |
Ethan Nicholas | d608c09 | 2017-10-26 09:30:08 -0400 | [diff] [blame] | 661 | } |
| 662 | Token resultToken; |
John Stiles | 2630ea3 | 2020-12-04 10:51:21 -0500 | [diff] [blame] | 663 | if (!this->expectIdentifier(&resultToken)) { |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 664 | return skstd::string_view(); |
Ethan Nicholas | d608c09 | 2017-10-26 09:30:08 -0400 | [diff] [blame] | 665 | } |
| 666 | return this->text(resultToken); |
| 667 | } |
| 668 | |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 669 | /* LAYOUT LPAREN IDENTIFIER (EQ INT_LITERAL)? (COMMA IDENTIFIER (EQ INT_LITERAL)?)* RPAREN */ |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 670 | Layout Parser::layout() { |
Ethan Nicholas | 39204fd | 2017-11-27 13:12:30 -0500 | [diff] [blame] | 671 | int flags = 0; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 672 | int location = -1; |
Ethan Nicholas | 1967177 | 2016-11-28 16:30:17 -0500 | [diff] [blame] | 673 | int offset = -1; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 674 | int binding = -1; |
| 675 | int index = -1; |
| 676 | int set = -1; |
| 677 | int builtin = -1; |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 678 | int inputAttachmentIndex = -1; |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 679 | Layout::Primitive primitive = Layout::kUnspecified_Primitive; |
| 680 | int maxVertices = -1; |
| 681 | int invocations = -1; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 682 | if (this->checkNext(Token::Kind::TK_LAYOUT)) { |
| 683 | if (!this->expect(Token::Kind::TK_LPAREN, "'('")) { |
Ethan Nicholas | 39204fd | 2017-11-27 13:12:30 -0500 | [diff] [blame] | 684 | return Layout(flags, location, offset, binding, index, set, builtin, |
Brian Osman | 8c26479 | 2021-07-01 16:41:27 -0400 | [diff] [blame] | 685 | inputAttachmentIndex, primitive, maxVertices, invocations); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 686 | } |
| 687 | for (;;) { |
| 688 | Token t = this->nextToken(); |
Ethan Nicholas | d2e0960 | 2021-06-10 11:21:59 -0400 | [diff] [blame] | 689 | String text(this->text(t)); |
Brian Osman | a77ed8b | 2021-02-23 12:54:22 -0500 | [diff] [blame] | 690 | auto setFlag = [&](Layout::Flag f) { |
| 691 | if (flags & f) { |
| 692 | this->error(t, "layout qualifier '" + text + "' appears more than once"); |
| 693 | } |
| 694 | flags |= f; |
| 695 | }; |
| 696 | auto setPrimitive = [&](Layout::Primitive p) { |
| 697 | if (flags & Layout::kPrimitive_Flag) { |
| 698 | this->error(t, "only one primitive-type layout qualifier is allowed"); |
| 699 | } |
| 700 | flags |= Layout::kPrimitive_Flag; |
| 701 | primitive = p; |
| 702 | }; |
| 703 | |
Ethan Nicholas | b93af7e | 2018-07-24 11:28:52 -0400 | [diff] [blame] | 704 | auto found = layoutTokens->find(text); |
| 705 | if (found != layoutTokens->end()) { |
| 706 | switch (found->second) { |
Brian Osman | a77ed8b | 2021-02-23 12:54:22 -0500 | [diff] [blame] | 707 | case LayoutToken::ORIGIN_UPPER_LEFT: |
| 708 | setFlag(Layout::kOriginUpperLeft_Flag); |
| 709 | break; |
Brian Osman | a77ed8b | 2021-02-23 12:54:22 -0500 | [diff] [blame] | 710 | case LayoutToken::PUSH_CONSTANT: |
| 711 | setFlag(Layout::kPushConstant_Flag); |
| 712 | break; |
| 713 | case LayoutToken::BLEND_SUPPORT_ALL_EQUATIONS: |
| 714 | setFlag(Layout::kBlendSupportAllEquations_Flag); |
| 715 | break; |
Brian Osman | a77ed8b | 2021-02-23 12:54:22 -0500 | [diff] [blame] | 716 | case LayoutToken::SRGB_UNPREMUL: |
| 717 | setFlag(Layout::kSRGBUnpremul_Flag); |
| 718 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 719 | case LayoutToken::LOCATION: |
Brian Osman | a77ed8b | 2021-02-23 12:54:22 -0500 | [diff] [blame] | 720 | setFlag(Layout::kLocation_Flag); |
Ethan Nicholas | 3614d9a | 2017-02-15 12:33:30 -0500 | [diff] [blame] | 721 | location = this->layoutInt(); |
| 722 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 723 | case LayoutToken::OFFSET: |
Brian Osman | a77ed8b | 2021-02-23 12:54:22 -0500 | [diff] [blame] | 724 | setFlag(Layout::kOffset_Flag); |
Ethan Nicholas | 3614d9a | 2017-02-15 12:33:30 -0500 | [diff] [blame] | 725 | offset = this->layoutInt(); |
| 726 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 727 | case LayoutToken::BINDING: |
Brian Osman | a77ed8b | 2021-02-23 12:54:22 -0500 | [diff] [blame] | 728 | setFlag(Layout::kBinding_Flag); |
Ethan Nicholas | 3614d9a | 2017-02-15 12:33:30 -0500 | [diff] [blame] | 729 | binding = this->layoutInt(); |
| 730 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 731 | case LayoutToken::INDEX: |
Brian Osman | a77ed8b | 2021-02-23 12:54:22 -0500 | [diff] [blame] | 732 | setFlag(Layout::kIndex_Flag); |
Ethan Nicholas | 3614d9a | 2017-02-15 12:33:30 -0500 | [diff] [blame] | 733 | index = this->layoutInt(); |
| 734 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 735 | case LayoutToken::SET: |
Brian Osman | a77ed8b | 2021-02-23 12:54:22 -0500 | [diff] [blame] | 736 | setFlag(Layout::kSet_Flag); |
Ethan Nicholas | 3614d9a | 2017-02-15 12:33:30 -0500 | [diff] [blame] | 737 | set = this->layoutInt(); |
| 738 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 739 | case LayoutToken::BUILTIN: |
Brian Osman | a77ed8b | 2021-02-23 12:54:22 -0500 | [diff] [blame] | 740 | setFlag(Layout::kBuiltin_Flag); |
Ethan Nicholas | 3614d9a | 2017-02-15 12:33:30 -0500 | [diff] [blame] | 741 | builtin = this->layoutInt(); |
| 742 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 743 | case LayoutToken::INPUT_ATTACHMENT_INDEX: |
Brian Osman | a77ed8b | 2021-02-23 12:54:22 -0500 | [diff] [blame] | 744 | setFlag(Layout::kInputAttachmentIndex_Flag); |
Ethan Nicholas | 3614d9a | 2017-02-15 12:33:30 -0500 | [diff] [blame] | 745 | inputAttachmentIndex = this->layoutInt(); |
| 746 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 747 | case LayoutToken::POINTS: |
Brian Osman | a77ed8b | 2021-02-23 12:54:22 -0500 | [diff] [blame] | 748 | setPrimitive(Layout::kPoints_Primitive); |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 749 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 750 | case LayoutToken::LINES: |
Brian Osman | a77ed8b | 2021-02-23 12:54:22 -0500 | [diff] [blame] | 751 | setPrimitive(Layout::kLines_Primitive); |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 752 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 753 | case LayoutToken::LINE_STRIP: |
Brian Osman | a77ed8b | 2021-02-23 12:54:22 -0500 | [diff] [blame] | 754 | setPrimitive(Layout::kLineStrip_Primitive); |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 755 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 756 | case LayoutToken::LINES_ADJACENCY: |
Brian Osman | a77ed8b | 2021-02-23 12:54:22 -0500 | [diff] [blame] | 757 | setPrimitive(Layout::kLinesAdjacency_Primitive); |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 758 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 759 | case LayoutToken::TRIANGLES: |
Brian Osman | a77ed8b | 2021-02-23 12:54:22 -0500 | [diff] [blame] | 760 | setPrimitive(Layout::kTriangles_Primitive); |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 761 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 762 | case LayoutToken::TRIANGLE_STRIP: |
Brian Osman | a77ed8b | 2021-02-23 12:54:22 -0500 | [diff] [blame] | 763 | setPrimitive(Layout::kTriangleStrip_Primitive); |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 764 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 765 | case LayoutToken::TRIANGLES_ADJACENCY: |
Brian Osman | a77ed8b | 2021-02-23 12:54:22 -0500 | [diff] [blame] | 766 | setPrimitive(Layout::kTrianglesAdjacency_Primitive); |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 767 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 768 | case LayoutToken::MAX_VERTICES: |
Brian Osman | a77ed8b | 2021-02-23 12:54:22 -0500 | [diff] [blame] | 769 | setFlag(Layout::kMaxVertices_Flag); |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 770 | maxVertices = this->layoutInt(); |
| 771 | break; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 772 | case LayoutToken::INVOCATIONS: |
Brian Osman | a77ed8b | 2021-02-23 12:54:22 -0500 | [diff] [blame] | 773 | setFlag(Layout::kInvocations_Flag); |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 774 | invocations = this->layoutInt(); |
| 775 | break; |
Ethan Nicholas | 78aceb2 | 2018-08-31 16:13:58 -0400 | [diff] [blame] | 776 | default: |
Brian Osman | a77ed8b | 2021-02-23 12:54:22 -0500 | [diff] [blame] | 777 | this->error(t, "'" + text + "' is not a valid layout qualifier"); |
Ethan Nicholas | d608c09 | 2017-10-26 09:30:08 -0400 | [diff] [blame] | 778 | break; |
Ethan Nicholas | 3614d9a | 2017-02-15 12:33:30 -0500 | [diff] [blame] | 779 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 780 | } else { |
Brian Osman | a77ed8b | 2021-02-23 12:54:22 -0500 | [diff] [blame] | 781 | this->error(t, "'" + text + "' is not a valid layout qualifier"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 782 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 783 | if (this->checkNext(Token::Kind::TK_RPAREN)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 784 | break; |
| 785 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 786 | if (!this->expect(Token::Kind::TK_COMMA, "','")) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 787 | break; |
| 788 | } |
| 789 | } |
| 790 | } |
Ethan Nicholas | 39204fd | 2017-11-27 13:12:30 -0500 | [diff] [blame] | 791 | return Layout(flags, location, offset, binding, index, set, builtin, inputAttachmentIndex, |
Brian Osman | 8c26479 | 2021-07-01 16:41:27 -0400 | [diff] [blame] | 792 | primitive, maxVertices, invocations); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 793 | } |
| 794 | |
Brian Osman | e49703f | 2021-04-19 11:15:24 -0400 | [diff] [blame] | 795 | /* layout? (UNIFORM | CONST | IN | OUT | INOUT | FLAT | NOPERSPECTIVE | INLINE)* */ |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 796 | Modifiers Parser::modifiers() { |
| 797 | Layout layout = this->layout(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 798 | int flags = 0; |
| 799 | for (;;) { |
| 800 | // TODO: handle duplicate / incompatible flags |
John Stiles | 7d5b90a | 2021-01-21 14:26:51 -0500 | [diff] [blame] | 801 | int tokenFlag = parse_modifier_token(peek().fKind); |
| 802 | if (!tokenFlag) { |
| 803 | break; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 804 | } |
John Stiles | 7d5b90a | 2021-01-21 14:26:51 -0500 | [diff] [blame] | 805 | flags |= tokenFlag; |
| 806 | this->nextToken(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 807 | } |
John Stiles | 7d5b90a | 2021-01-21 14:26:51 -0500 | [diff] [blame] | 808 | return Modifiers(layout, flags); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 809 | } |
| 810 | |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 811 | Modifiers Parser::modifiersWithDefaults(int defaultFlags) { |
| 812 | Modifiers result = this->modifiers(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 813 | if (!result.fFlags) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 814 | return Modifiers(result.fLayout, defaultFlags); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 815 | } |
| 816 | return result; |
| 817 | } |
| 818 | |
| 819 | /* ifStatement | forStatement | doStatement | whileStatement | block | expression */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 820 | ASTNode::ID Parser::statement() { |
Ethan Nicholas | 4e3b011 | 2019-06-07 16:49:07 -0400 | [diff] [blame] | 821 | Token start = this->nextToken(); |
| 822 | AutoDepth depth(this); |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 823 | if (!depth.increase()) { |
Ethan Nicholas | 4e3b011 | 2019-06-07 16:49:07 -0400 | [diff] [blame] | 824 | return ASTNode::ID::Invalid(); |
| 825 | } |
| 826 | this->pushback(start); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 827 | switch (start.fKind) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 828 | case Token::Kind::TK_IF: // fall through |
| 829 | case Token::Kind::TK_STATIC_IF: |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 830 | return this->ifStatement(); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 831 | case Token::Kind::TK_FOR: |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 832 | return this->forStatement(); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 833 | case Token::Kind::TK_DO: |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 834 | return this->doStatement(); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 835 | case Token::Kind::TK_WHILE: |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 836 | return this->whileStatement(); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 837 | case Token::Kind::TK_SWITCH: // fall through |
| 838 | case Token::Kind::TK_STATIC_SWITCH: |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 839 | return this->switchStatement(); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 840 | case Token::Kind::TK_RETURN: |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 841 | return this->returnStatement(); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 842 | case Token::Kind::TK_BREAK: |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 843 | return this->breakStatement(); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 844 | case Token::Kind::TK_CONTINUE: |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 845 | return this->continueStatement(); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 846 | case Token::Kind::TK_DISCARD: |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 847 | return this->discardStatement(); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 848 | case Token::Kind::TK_LBRACE: |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 849 | return this->block(); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 850 | case Token::Kind::TK_SEMICOLON: |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 851 | this->nextToken(); |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 852 | return this->createNode(start.fOffset, ASTNode::Kind::kBlock); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 853 | case Token::Kind::TK_CONST: |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 854 | case Token::Kind::TK_IDENTIFIER: |
John Stiles | 76389b7 | 2021-01-25 13:49:05 -0500 | [diff] [blame] | 855 | return this->varDeclarationsOrExpressionStatement(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 856 | default: |
| 857 | return this->expressionStatement(); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 858 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 859 | } |
| 860 | |
Ethan Nicholas | ee1c8a7 | 2019-02-22 10:50:47 -0500 | [diff] [blame] | 861 | /* IDENTIFIER(type) (LBRACKET intLiteral? RBRACKET)* QUESTION? */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 862 | ASTNode::ID Parser::type() { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 863 | Token type; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 864 | if (!this->expect(Token::Kind::TK_IDENTIFIER, "a type", &type)) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 865 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 866 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 867 | if (!this->isType(this->text(type))) { |
| 868 | this->error(type, ("no type named '" + this->text(type) + "'").c_str()); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 869 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 870 | } |
Brian Osman | 00fea5b | 2021-01-28 09:46:30 -0500 | [diff] [blame] | 871 | ASTNode::ID result = this->createNode(type.fOffset, ASTNode::Kind::kType, this->text(type)); |
John Stiles | d39aec9 | 2020-12-03 14:37:16 -0500 | [diff] [blame] | 872 | bool isArray = false; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 873 | while (this->checkNext(Token::Kind::TK_LBRACKET)) { |
John Stiles | d39aec9 | 2020-12-03 14:37:16 -0500 | [diff] [blame] | 874 | if (isArray) { |
| 875 | this->error(this->peek(), "multi-dimensional arrays are not supported"); |
| 876 | return ASTNode::ID::Invalid(); |
| 877 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 878 | if (this->peek().fKind != Token::Kind::TK_RBRACKET) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 879 | SKSL_INT i; |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 880 | if (this->intLiteral(&i)) { |
John Stiles | 08070f6 | 2020-11-17 10:45:49 -0500 | [diff] [blame] | 881 | this->addChild(result, this->createNode(this->peek().fOffset, |
| 882 | ASTNode::Kind::kInt, i)); |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 883 | } else { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 884 | return ASTNode::ID::Invalid(); |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 885 | } |
| 886 | } else { |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 887 | this->createEmptyChild(result); |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 888 | } |
John Stiles | d39aec9 | 2020-12-03 14:37:16 -0500 | [diff] [blame] | 889 | isArray = true; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 890 | this->expect(Token::Kind::TK_RBRACKET, "']'"); |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 891 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 892 | return result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 893 | } |
| 894 | |
John Stiles | 57a996b | 2020-04-19 19:05:12 -0700 | [diff] [blame] | 895 | /* IDENTIFIER LBRACE |
| 896 | varDeclaration+ |
| 897 | RBRACE (IDENTIFIER (LBRACKET expression? RBRACKET)*)? SEMICOLON */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 898 | ASTNode::ID Parser::interfaceBlock(Modifiers mods) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 899 | Token name; |
John Stiles | 2630ea3 | 2020-12-04 10:51:21 -0500 | [diff] [blame] | 900 | if (!this->expectIdentifier(&name)) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 901 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 902 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 903 | if (peek().fKind != Token::Kind::TK_LBRACE) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 904 | // 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] | 905 | // 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] | 906 | // it's better to report it as an unknown type |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 907 | this->error(name, "no type named '" + this->text(name) + "'"); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 908 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 909 | } |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 910 | ASTNode::ID result = this->createNode(name.fOffset, ASTNode::Kind::kInterfaceBlock); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 911 | ASTNode::InterfaceBlockData id(mods, this->text(name), 0, "", 0); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 912 | this->nextToken(); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 913 | while (this->peek().fKind != Token::Kind::TK_RBRACE) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 914 | ASTNode::ID decl = this->varDeclarations(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 915 | if (!decl) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 916 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 917 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 918 | getNode(result).addChild(decl); |
| 919 | ++id.fDeclarationCount; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 920 | } |
John Stiles | 57a996b | 2020-04-19 19:05:12 -0700 | [diff] [blame] | 921 | if (id.fDeclarationCount == 0) { |
| 922 | this->error(name, "interface block '" + this->text(name) + |
| 923 | "' must contain at least one member"); |
| 924 | return ASTNode::ID::Invalid(); |
| 925 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 926 | this->nextToken(); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 927 | std::vector<ASTNode> sizes; |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 928 | skstd::string_view instanceName; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 929 | Token instanceNameToken; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 930 | if (this->checkNext(Token::Kind::TK_IDENTIFIER, &instanceNameToken)) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 931 | id.fInstanceName = this->text(instanceNameToken); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 932 | while (this->checkNext(Token::Kind::TK_LBRACKET)) { |
John Stiles | d39aec9 | 2020-12-03 14:37:16 -0500 | [diff] [blame] | 933 | if (id.fIsArray) { |
| 934 | this->error(this->peek(), "multi-dimensional arrays are not supported"); |
| 935 | return false; |
| 936 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 937 | if (this->peek().fKind != Token::Kind::TK_RBRACKET) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 938 | ASTNode::ID size = this->expression(); |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 939 | if (!size) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 940 | return ASTNode::ID::Invalid(); |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 941 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 942 | getNode(result).addChild(size); |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 943 | } else { |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 944 | this->createEmptyChild(result); |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 945 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 946 | this->expect(Token::Kind::TK_RBRACKET, "']'"); |
John Stiles | d39aec9 | 2020-12-03 14:37:16 -0500 | [diff] [blame] | 947 | id.fIsArray = true; |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 948 | } |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 949 | instanceName = this->text(instanceNameToken); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 950 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 951 | getNode(result).setInterfaceBlockData(id); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 952 | this->expect(Token::Kind::TK_SEMICOLON, "';'"); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 953 | return result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | /* IF LPAREN expression RPAREN statement (ELSE statement)? */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 957 | ASTNode::ID Parser::ifStatement() { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 958 | Token start; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 959 | bool isStatic = this->checkNext(Token::Kind::TK_STATIC_IF, &start); |
| 960 | if (!isStatic && !this->expect(Token::Kind::TK_IF, "'if'", &start)) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 961 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 962 | } |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 963 | ASTNode::ID result = this->createNode(start.fOffset, ASTNode::Kind::kIf, isStatic); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 964 | if (!this->expect(Token::Kind::TK_LPAREN, "'('")) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 965 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 966 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 967 | ASTNode::ID test = this->expression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 968 | if (!test) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 969 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 970 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 971 | getNode(result).addChild(test); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 972 | if (!this->expect(Token::Kind::TK_RPAREN, "')'")) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 973 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 974 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 975 | ASTNode::ID ifTrue = this->statement(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 976 | if (!ifTrue) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 977 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 978 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 979 | getNode(result).addChild(ifTrue); |
| 980 | ASTNode::ID ifFalse; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 981 | if (this->checkNext(Token::Kind::TK_ELSE)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 982 | ifFalse = this->statement(); |
| 983 | if (!ifFalse) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 984 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 985 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 986 | getNode(result).addChild(ifFalse); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 987 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 988 | return result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | /* DO statement WHILE LPAREN expression RPAREN SEMICOLON */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 992 | ASTNode::ID Parser::doStatement() { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 993 | Token start; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 994 | if (!this->expect(Token::Kind::TK_DO, "'do'", &start)) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 995 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 996 | } |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 997 | ASTNode::ID result = this->createNode(start.fOffset, ASTNode::Kind::kDo); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 998 | ASTNode::ID statement = this->statement(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 999 | if (!statement) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1000 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1001 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1002 | getNode(result).addChild(statement); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1003 | if (!this->expect(Token::Kind::TK_WHILE, "'while'")) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1004 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1005 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1006 | if (!this->expect(Token::Kind::TK_LPAREN, "'('")) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1007 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1008 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1009 | ASTNode::ID test = this->expression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1010 | if (!test) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1011 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1012 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1013 | getNode(result).addChild(test); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1014 | if (!this->expect(Token::Kind::TK_RPAREN, "')'")) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1015 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1016 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1017 | if (!this->expect(Token::Kind::TK_SEMICOLON, "';'")) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1018 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1019 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1020 | return result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | /* WHILE LPAREN expression RPAREN STATEMENT */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1024 | ASTNode::ID Parser::whileStatement() { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1025 | Token start; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1026 | if (!this->expect(Token::Kind::TK_WHILE, "'while'", &start)) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1027 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1028 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1029 | if (!this->expect(Token::Kind::TK_LPAREN, "'('")) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1030 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1031 | } |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 1032 | ASTNode::ID result = this->createNode(start.fOffset, ASTNode::Kind::kWhile); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1033 | ASTNode::ID test = this->expression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1034 | if (!test) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1035 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1036 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1037 | getNode(result).addChild(test); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1038 | if (!this->expect(Token::Kind::TK_RPAREN, "')'")) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1039 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1040 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1041 | ASTNode::ID statement = this->statement(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1042 | if (!statement) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1043 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1044 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1045 | getNode(result).addChild(statement); |
| 1046 | return result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1047 | } |
| 1048 | |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1049 | /* CASE expression COLON statement* */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1050 | ASTNode::ID Parser::switchCase() { |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1051 | Token start; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1052 | if (!this->expect(Token::Kind::TK_CASE, "'case'", &start)) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1053 | return ASTNode::ID::Invalid(); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1054 | } |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 1055 | ASTNode::ID result = this->createNode(start.fOffset, ASTNode::Kind::kSwitchCase); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1056 | ASTNode::ID value = this->expression(); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1057 | if (!value) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1058 | return ASTNode::ID::Invalid(); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1059 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1060 | if (!this->expect(Token::Kind::TK_COLON, "':'")) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1061 | return ASTNode::ID::Invalid(); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1062 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1063 | getNode(result).addChild(value); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1064 | while (this->peek().fKind != Token::Kind::TK_RBRACE && |
| 1065 | this->peek().fKind != Token::Kind::TK_CASE && |
| 1066 | this->peek().fKind != Token::Kind::TK_DEFAULT) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1067 | ASTNode::ID s = this->statement(); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1068 | if (!s) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1069 | return ASTNode::ID::Invalid(); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1070 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1071 | getNode(result).addChild(s); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1072 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1073 | return result; |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | /* SWITCH LPAREN expression RPAREN LBRACE switchCase* (DEFAULT COLON statement*)? RBRACE */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1077 | ASTNode::ID Parser::switchStatement() { |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1078 | Token start; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1079 | bool isStatic = this->checkNext(Token::Kind::TK_STATIC_SWITCH, &start); |
| 1080 | if (!isStatic && !this->expect(Token::Kind::TK_SWITCH, "'switch'", &start)) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1081 | return ASTNode::ID::Invalid(); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1082 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1083 | if (!this->expect(Token::Kind::TK_LPAREN, "'('")) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1084 | return ASTNode::ID::Invalid(); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1085 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1086 | ASTNode::ID value = this->expression(); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1087 | if (!value) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1088 | return ASTNode::ID::Invalid(); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1089 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1090 | if (!this->expect(Token::Kind::TK_RPAREN, "')'")) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1091 | return ASTNode::ID::Invalid(); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1092 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1093 | if (!this->expect(Token::Kind::TK_LBRACE, "'{'")) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1094 | return ASTNode::ID::Invalid(); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1095 | } |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 1096 | ASTNode::ID result = this->createNode(start.fOffset, ASTNode::Kind::kSwitch, isStatic); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1097 | getNode(result).addChild(value); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1098 | while (this->peek().fKind == Token::Kind::TK_CASE) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1099 | ASTNode::ID c = this->switchCase(); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1100 | if (!c) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1101 | return ASTNode::ID::Invalid(); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1102 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1103 | getNode(result).addChild(c); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1104 | } |
| 1105 | // Requiring default: to be last (in defiance of C and GLSL) was a deliberate decision. Other |
| 1106 | // parts of the compiler may rely upon this assumption. |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1107 | if (this->peek().fKind == Token::Kind::TK_DEFAULT) { |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1108 | Token defaultStart; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1109 | SkAssertResult(this->expect(Token::Kind::TK_DEFAULT, "'default'", &defaultStart)); |
| 1110 | if (!this->expect(Token::Kind::TK_COLON, "':'")) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1111 | return ASTNode::ID::Invalid(); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1112 | } |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 1113 | ASTNode::ID defaultCase = this->addChild( |
| 1114 | result, this->createNode(defaultStart.fOffset, ASTNode::Kind::kSwitchCase)); |
| 1115 | this->createEmptyChild(defaultCase); // empty test to signify default case |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1116 | while (this->peek().fKind != Token::Kind::TK_RBRACE) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1117 | ASTNode::ID s = this->statement(); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1118 | if (!s) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1119 | return ASTNode::ID::Invalid(); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1120 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1121 | getNode(defaultCase).addChild(s); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1122 | } |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1123 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1124 | if (!this->expect(Token::Kind::TK_RBRACE, "'}'")) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1125 | return ASTNode::ID::Invalid(); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1126 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1127 | return result; |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1128 | } |
| 1129 | |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1130 | /* FOR LPAREN (declaration | expression)? SEMICOLON expression? SEMICOLON expression? RPAREN |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1131 | STATEMENT */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1132 | ASTNode::ID Parser::forStatement() { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1133 | Token start; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1134 | if (!this->expect(Token::Kind::TK_FOR, "'for'", &start)) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1135 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1136 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1137 | if (!this->expect(Token::Kind::TK_LPAREN, "'('")) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1138 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1139 | } |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 1140 | ASTNode::ID result = this->createNode(start.fOffset, ASTNode::Kind::kFor); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1141 | Token nextToken = this->peek(); |
John Stiles | 89b484a | 2021-04-19 14:57:27 -0400 | [diff] [blame] | 1142 | if (nextToken.fKind == Token::Kind::TK_SEMICOLON) { |
| 1143 | // An empty init-statement. |
| 1144 | this->nextToken(); |
| 1145 | this->createEmptyChild(result); |
| 1146 | } else { |
| 1147 | // The init-statement must be an expression or variable declaration. |
| 1148 | ASTNode::ID initializer = this->varDeclarationsOrExpressionStatement(); |
| 1149 | if (!initializer) { |
| 1150 | return ASTNode::ID::Invalid(); |
ethannicholas | a54401d | 2016-10-14 08:37:32 -0700 | [diff] [blame] | 1151 | } |
John Stiles | 89b484a | 2021-04-19 14:57:27 -0400 | [diff] [blame] | 1152 | getNode(result).addChild(initializer); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1153 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1154 | ASTNode::ID test; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1155 | if (this->peek().fKind != Token::Kind::TK_SEMICOLON) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1156 | test = this->expression(); |
| 1157 | if (!test) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1158 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1159 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1160 | getNode(result).addChild(test); |
| 1161 | } else { |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 1162 | this->createEmptyChild(result); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1163 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1164 | if (!this->expect(Token::Kind::TK_SEMICOLON, "';'")) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1165 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1166 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1167 | ASTNode::ID next; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1168 | if (this->peek().fKind != Token::Kind::TK_RPAREN) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1169 | next = this->expression(); |
| 1170 | if (!next) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1171 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1172 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1173 | getNode(result).addChild(next); |
| 1174 | } else { |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 1175 | this->createEmptyChild(result); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1176 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1177 | if (!this->expect(Token::Kind::TK_RPAREN, "')'")) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1178 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1179 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1180 | ASTNode::ID statement = this->statement(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1181 | if (!statement) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1182 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1183 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1184 | getNode(result).addChild(statement); |
| 1185 | return result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | /* RETURN expression? SEMICOLON */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1189 | ASTNode::ID Parser::returnStatement() { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1190 | Token start; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1191 | if (!this->expect(Token::Kind::TK_RETURN, "'return'", &start)) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1192 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1193 | } |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 1194 | ASTNode::ID result = this->createNode(start.fOffset, ASTNode::Kind::kReturn); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1195 | if (this->peek().fKind != Token::Kind::TK_SEMICOLON) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1196 | ASTNode::ID expression = this->expression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1197 | if (!expression) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1198 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1199 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1200 | getNode(result).addChild(expression); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1201 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1202 | if (!this->expect(Token::Kind::TK_SEMICOLON, "';'")) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1203 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1204 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1205 | return result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | /* BREAK SEMICOLON */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1209 | ASTNode::ID Parser::breakStatement() { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1210 | Token start; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1211 | if (!this->expect(Token::Kind::TK_BREAK, "'break'", &start)) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1212 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1213 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1214 | if (!this->expect(Token::Kind::TK_SEMICOLON, "';'")) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1215 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1216 | } |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 1217 | return this->createNode(start.fOffset, ASTNode::Kind::kBreak); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | /* CONTINUE SEMICOLON */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1221 | ASTNode::ID Parser::continueStatement() { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1222 | Token start; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1223 | if (!this->expect(Token::Kind::TK_CONTINUE, "'continue'", &start)) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1224 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1225 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1226 | if (!this->expect(Token::Kind::TK_SEMICOLON, "';'")) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1227 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1228 | } |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 1229 | return this->createNode(start.fOffset, ASTNode::Kind::kContinue); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | /* DISCARD SEMICOLON */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1233 | ASTNode::ID Parser::discardStatement() { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1234 | Token start; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1235 | if (!this->expect(Token::Kind::TK_DISCARD, "'continue'", &start)) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1236 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1237 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1238 | if (!this->expect(Token::Kind::TK_SEMICOLON, "';'")) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1239 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1240 | } |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 1241 | return this->createNode(start.fOffset, ASTNode::Kind::kDiscard); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | /* LBRACE statement* RBRACE */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1245 | ASTNode::ID Parser::block() { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1246 | Token start; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1247 | if (!this->expect(Token::Kind::TK_LBRACE, "'{'", &start)) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1248 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1249 | } |
Ethan Nicholas | 4e3b011 | 2019-06-07 16:49:07 -0400 | [diff] [blame] | 1250 | AutoDepth depth(this); |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 1251 | if (!depth.increase()) { |
Ethan Nicholas | 4e3b011 | 2019-06-07 16:49:07 -0400 | [diff] [blame] | 1252 | return ASTNode::ID::Invalid(); |
| 1253 | } |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 1254 | ASTNode::ID result = this->createNode(start.fOffset, ASTNode::Kind::kBlock); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1255 | for (;;) { |
| 1256 | switch (this->peek().fKind) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1257 | case Token::Kind::TK_RBRACE: |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1258 | this->nextToken(); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1259 | return result; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1260 | case Token::Kind::TK_END_OF_FILE: |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1261 | this->error(this->peek(), "expected '}', but found end of file"); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1262 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1263 | default: { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1264 | ASTNode::ID statement = this->statement(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1265 | if (!statement) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1266 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1267 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1268 | getNode(result).addChild(statement); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1269 | } |
| 1270 | } |
| 1271 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1272 | return result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | /* expression SEMICOLON */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1276 | ASTNode::ID Parser::expressionStatement() { |
| 1277 | ASTNode::ID expr = this->expression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1278 | if (expr) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1279 | if (this->expect(Token::Kind::TK_SEMICOLON, "';'")) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1280 | return expr; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1281 | } |
| 1282 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1283 | return ASTNode::ID::Invalid(); |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | /* assignmentExpression (COMMA assignmentExpression)* */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1287 | ASTNode::ID Parser::expression() { |
| 1288 | ASTNode::ID result = this->assignmentExpression(); |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 1289 | if (!result) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1290 | return ASTNode::ID::Invalid(); |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 1291 | } |
| 1292 | Token t; |
Ethan Nicholas | b67d056 | 2020-04-30 16:10:00 -0400 | [diff] [blame] | 1293 | AutoDepth depth(this); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1294 | while (this->checkNext(Token::Kind::TK_COMMA, &t)) { |
Ethan Nicholas | b67d056 | 2020-04-30 16:10:00 -0400 | [diff] [blame] | 1295 | if (!depth.increase()) { |
| 1296 | return ASTNode::ID::Invalid(); |
| 1297 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1298 | ASTNode::ID right = this->assignmentExpression(); |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 1299 | if (!right) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1300 | return ASTNode::ID::Invalid(); |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 1301 | } |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 1302 | ASTNode::ID newResult = this->createNode(t.fOffset, ASTNode::Kind::kBinary, |
| 1303 | Operator(t.fKind)); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1304 | getNode(newResult).addChild(result); |
| 1305 | getNode(newResult).addChild(right); |
| 1306 | result = newResult; |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 1307 | } |
| 1308 | return result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | /* ternaryExpression ((EQEQ | STAREQ | SLASHEQ | PERCENTEQ | PLUSEQ | MINUSEQ | SHLEQ | SHREQ | |
| 1312 | BITWISEANDEQ | BITWISEXOREQ | BITWISEOREQ | LOGICALANDEQ | LOGICALXOREQ | LOGICALOREQ) |
| 1313 | assignmentExpression)* |
| 1314 | */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1315 | ASTNode::ID Parser::assignmentExpression() { |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 1316 | AutoDepth depth(this); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1317 | ASTNode::ID result = this->ternaryExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1318 | if (!result) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1319 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1320 | } |
| 1321 | for (;;) { |
| 1322 | switch (this->peek().fKind) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1323 | case Token::Kind::TK_EQ: // fall through |
| 1324 | case Token::Kind::TK_STAREQ: // fall through |
| 1325 | case Token::Kind::TK_SLASHEQ: // fall through |
| 1326 | case Token::Kind::TK_PERCENTEQ: // fall through |
| 1327 | case Token::Kind::TK_PLUSEQ: // fall through |
| 1328 | case Token::Kind::TK_MINUSEQ: // fall through |
| 1329 | case Token::Kind::TK_SHLEQ: // fall through |
| 1330 | case Token::Kind::TK_SHREQ: // fall through |
| 1331 | case Token::Kind::TK_BITWISEANDEQ: // fall through |
| 1332 | case Token::Kind::TK_BITWISEXOREQ: // fall through |
John Stiles | 8b3b159 | 2020-11-23 11:06:44 -0500 | [diff] [blame] | 1333 | case Token::Kind::TK_BITWISEOREQ: { |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 1334 | if (!depth.increase()) { |
| 1335 | return ASTNode::ID::Invalid(); |
| 1336 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1337 | Token t = this->nextToken(); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1338 | ASTNode::ID right = this->assignmentExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1339 | if (!right) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1340 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1341 | } |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 1342 | ASTNode::ID newResult = this->createNode(getNode(result).fOffset, |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 1343 | ASTNode::Kind::kBinary, Operator(t.fKind)); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1344 | getNode(newResult).addChild(result); |
| 1345 | getNode(newResult).addChild(right); |
| 1346 | result = newResult; |
| 1347 | break; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1348 | } |
| 1349 | default: |
| 1350 | return result; |
| 1351 | } |
| 1352 | } |
| 1353 | } |
| 1354 | |
| 1355 | /* logicalOrExpression ('?' expression ':' assignmentExpression)? */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1356 | ASTNode::ID Parser::ternaryExpression() { |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 1357 | AutoDepth depth(this); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1358 | ASTNode::ID base = this->logicalOrExpression(); |
| 1359 | if (!base) { |
| 1360 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1361 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1362 | if (this->checkNext(Token::Kind::TK_QUESTION)) { |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 1363 | if (!depth.increase()) { |
| 1364 | return ASTNode::ID::Invalid(); |
| 1365 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1366 | ASTNode::ID trueExpr = this->expression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1367 | if (!trueExpr) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1368 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1369 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1370 | if (this->expect(Token::Kind::TK_COLON, "':'")) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1371 | ASTNode::ID falseExpr = this->assignmentExpression(); |
| 1372 | if (!falseExpr) { |
| 1373 | return ASTNode::ID::Invalid(); |
| 1374 | } |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 1375 | ASTNode::ID ternary = this->createNode(getNode(base).fOffset, ASTNode::Kind::kTernary); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1376 | getNode(ternary).addChild(base); |
| 1377 | getNode(ternary).addChild(trueExpr); |
| 1378 | getNode(ternary).addChild(falseExpr); |
| 1379 | return ternary; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1380 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1381 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1382 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1383 | return base; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | /* logicalXorExpression (LOGICALOR logicalXorExpression)* */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1387 | ASTNode::ID Parser::logicalOrExpression() { |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 1388 | AutoDepth depth(this); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1389 | ASTNode::ID result = this->logicalXorExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1390 | if (!result) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1391 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1392 | } |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 1393 | Token t; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1394 | while (this->checkNext(Token::Kind::TK_LOGICALOR, &t)) { |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 1395 | if (!depth.increase()) { |
| 1396 | return ASTNode::ID::Invalid(); |
| 1397 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1398 | ASTNode::ID right = this->logicalXorExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1399 | if (!right) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1400 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1401 | } |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 1402 | ASTNode::ID newResult = this->createNode(getNode(result).fOffset, ASTNode::Kind::kBinary, |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 1403 | Operator(t.fKind)); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1404 | getNode(newResult).addChild(result); |
| 1405 | getNode(newResult).addChild(right); |
| 1406 | result = newResult; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1407 | } |
| 1408 | return result; |
| 1409 | } |
| 1410 | |
| 1411 | /* logicalAndExpression (LOGICALXOR logicalAndExpression)* */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1412 | ASTNode::ID Parser::logicalXorExpression() { |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 1413 | AutoDepth depth(this); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1414 | ASTNode::ID result = this->logicalAndExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1415 | if (!result) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1416 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1417 | } |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 1418 | Token t; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1419 | while (this->checkNext(Token::Kind::TK_LOGICALXOR, &t)) { |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 1420 | if (!depth.increase()) { |
| 1421 | return ASTNode::ID::Invalid(); |
| 1422 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1423 | ASTNode::ID right = this->logicalAndExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1424 | if (!right) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1425 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1426 | } |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 1427 | ASTNode::ID newResult = this->createNode(getNode(result).fOffset, ASTNode::Kind::kBinary, |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 1428 | Operator(t.fKind)); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1429 | getNode(newResult).addChild(result); |
| 1430 | getNode(newResult).addChild(right); |
| 1431 | result = newResult; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1432 | } |
| 1433 | return result; |
| 1434 | } |
| 1435 | |
| 1436 | /* bitwiseOrExpression (LOGICALAND bitwiseOrExpression)* */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1437 | ASTNode::ID Parser::logicalAndExpression() { |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 1438 | AutoDepth depth(this); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1439 | ASTNode::ID result = this->bitwiseOrExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1440 | if (!result) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1441 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1442 | } |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 1443 | Token t; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1444 | while (this->checkNext(Token::Kind::TK_LOGICALAND, &t)) { |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 1445 | if (!depth.increase()) { |
| 1446 | return ASTNode::ID::Invalid(); |
| 1447 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1448 | ASTNode::ID right = this->bitwiseOrExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1449 | if (!right) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1450 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1451 | } |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 1452 | ASTNode::ID newResult = this->createNode(getNode(result).fOffset, ASTNode::Kind::kBinary, |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 1453 | Operator(t.fKind)); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1454 | getNode(newResult).addChild(result); |
| 1455 | getNode(newResult).addChild(right); |
| 1456 | result = newResult; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1457 | } |
| 1458 | return result; |
| 1459 | } |
| 1460 | |
| 1461 | /* bitwiseXorExpression (BITWISEOR bitwiseXorExpression)* */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1462 | ASTNode::ID Parser::bitwiseOrExpression() { |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 1463 | AutoDepth depth(this); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1464 | ASTNode::ID result = this->bitwiseXorExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1465 | if (!result) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1466 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1467 | } |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 1468 | Token t; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1469 | while (this->checkNext(Token::Kind::TK_BITWISEOR, &t)) { |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 1470 | if (!depth.increase()) { |
| 1471 | return ASTNode::ID::Invalid(); |
| 1472 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1473 | ASTNode::ID right = this->bitwiseXorExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1474 | if (!right) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1475 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1476 | } |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 1477 | ASTNode::ID newResult = this->createNode(getNode(result).fOffset, ASTNode::Kind::kBinary, |
| 1478 | Operator(t.fKind)); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1479 | getNode(newResult).addChild(result); |
| 1480 | getNode(newResult).addChild(right); |
| 1481 | result = newResult; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1482 | } |
| 1483 | return result; |
| 1484 | } |
| 1485 | |
| 1486 | /* bitwiseAndExpression (BITWISEXOR bitwiseAndExpression)* */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1487 | ASTNode::ID Parser::bitwiseXorExpression() { |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 1488 | AutoDepth depth(this); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1489 | ASTNode::ID result = this->bitwiseAndExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1490 | if (!result) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1491 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1492 | } |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 1493 | Token t; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1494 | while (this->checkNext(Token::Kind::TK_BITWISEXOR, &t)) { |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 1495 | if (!depth.increase()) { |
| 1496 | return ASTNode::ID::Invalid(); |
| 1497 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1498 | ASTNode::ID right = this->bitwiseAndExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1499 | if (!right) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1500 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1501 | } |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 1502 | ASTNode::ID newResult = this->createNode(getNode(result).fOffset, ASTNode::Kind::kBinary, |
| 1503 | Operator(t.fKind)); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1504 | getNode(newResult).addChild(result); |
| 1505 | getNode(newResult).addChild(right); |
| 1506 | result = newResult; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1507 | } |
| 1508 | return result; |
| 1509 | } |
| 1510 | |
| 1511 | /* equalityExpression (BITWISEAND equalityExpression)* */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1512 | ASTNode::ID Parser::bitwiseAndExpression() { |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 1513 | AutoDepth depth(this); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1514 | ASTNode::ID result = this->equalityExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1515 | if (!result) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1516 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1517 | } |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 1518 | Token t; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1519 | while (this->checkNext(Token::Kind::TK_BITWISEAND, &t)) { |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 1520 | if (!depth.increase()) { |
| 1521 | return ASTNode::ID::Invalid(); |
| 1522 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1523 | ASTNode::ID right = this->equalityExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1524 | if (!right) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1525 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1526 | } |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 1527 | ASTNode::ID newResult = this->createNode(getNode(result).fOffset, ASTNode::Kind::kBinary, |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 1528 | Operator(t.fKind)); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1529 | getNode(newResult).addChild(result); |
| 1530 | getNode(newResult).addChild(right); |
| 1531 | result = newResult; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1532 | } |
| 1533 | return result; |
| 1534 | } |
| 1535 | |
| 1536 | /* relationalExpression ((EQEQ | NEQ) relationalExpression)* */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1537 | ASTNode::ID Parser::equalityExpression() { |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 1538 | AutoDepth depth(this); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1539 | ASTNode::ID result = this->relationalExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1540 | if (!result) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1541 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1542 | } |
| 1543 | for (;;) { |
| 1544 | switch (this->peek().fKind) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1545 | case Token::Kind::TK_EQEQ: // fall through |
| 1546 | case Token::Kind::TK_NEQ: { |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 1547 | if (!depth.increase()) { |
| 1548 | return ASTNode::ID::Invalid(); |
| 1549 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1550 | Token t = this->nextToken(); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1551 | ASTNode::ID right = this->relationalExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1552 | if (!right) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1553 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1554 | } |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 1555 | ASTNode::ID newResult = this->createNode(getNode(result).fOffset, |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 1556 | ASTNode::Kind::kBinary, Operator(t.fKind)); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1557 | getNode(newResult).addChild(result); |
| 1558 | getNode(newResult).addChild(right); |
| 1559 | result = newResult; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1560 | break; |
| 1561 | } |
| 1562 | default: |
| 1563 | return result; |
| 1564 | } |
| 1565 | } |
| 1566 | } |
| 1567 | |
| 1568 | /* shiftExpression ((LT | GT | LTEQ | GTEQ) shiftExpression)* */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1569 | ASTNode::ID Parser::relationalExpression() { |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 1570 | AutoDepth depth(this); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1571 | ASTNode::ID result = this->shiftExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1572 | if (!result) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1573 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1574 | } |
| 1575 | for (;;) { |
| 1576 | switch (this->peek().fKind) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1577 | case Token::Kind::TK_LT: // fall through |
| 1578 | case Token::Kind::TK_GT: // fall through |
| 1579 | case Token::Kind::TK_LTEQ: // fall through |
| 1580 | case Token::Kind::TK_GTEQ: { |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 1581 | if (!depth.increase()) { |
| 1582 | return ASTNode::ID::Invalid(); |
| 1583 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1584 | Token t = this->nextToken(); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1585 | ASTNode::ID right = this->shiftExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1586 | if (!right) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1587 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1588 | } |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 1589 | ASTNode::ID newResult = this->createNode(getNode(result).fOffset, |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 1590 | ASTNode::Kind::kBinary, Operator(t.fKind)); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1591 | getNode(newResult).addChild(result); |
| 1592 | getNode(newResult).addChild(right); |
| 1593 | result = newResult; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1594 | break; |
| 1595 | } |
| 1596 | default: |
| 1597 | return result; |
| 1598 | } |
| 1599 | } |
| 1600 | } |
| 1601 | |
| 1602 | /* additiveExpression ((SHL | SHR) additiveExpression)* */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1603 | ASTNode::ID Parser::shiftExpression() { |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 1604 | AutoDepth depth(this); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1605 | ASTNode::ID result = this->additiveExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1606 | if (!result) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1607 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1608 | } |
| 1609 | for (;;) { |
| 1610 | switch (this->peek().fKind) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1611 | case Token::Kind::TK_SHL: // fall through |
| 1612 | case Token::Kind::TK_SHR: { |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 1613 | if (!depth.increase()) { |
| 1614 | return ASTNode::ID::Invalid(); |
| 1615 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1616 | Token t = this->nextToken(); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1617 | ASTNode::ID right = this->additiveExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1618 | if (!right) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1619 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1620 | } |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 1621 | ASTNode::ID newResult = this->createNode(getNode(result).fOffset, |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 1622 | ASTNode::Kind::kBinary, Operator(t.fKind)); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1623 | getNode(newResult).addChild(result); |
| 1624 | getNode(newResult).addChild(right); |
| 1625 | result = newResult; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1626 | break; |
| 1627 | } |
| 1628 | default: |
| 1629 | return result; |
| 1630 | } |
| 1631 | } |
| 1632 | } |
| 1633 | |
| 1634 | /* multiplicativeExpression ((PLUS | MINUS) multiplicativeExpression)* */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1635 | ASTNode::ID Parser::additiveExpression() { |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 1636 | AutoDepth depth(this); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1637 | ASTNode::ID result = this->multiplicativeExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1638 | if (!result) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1639 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1640 | } |
| 1641 | for (;;) { |
| 1642 | switch (this->peek().fKind) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1643 | case Token::Kind::TK_PLUS: // fall through |
| 1644 | case Token::Kind::TK_MINUS: { |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 1645 | if (!depth.increase()) { |
| 1646 | return ASTNode::ID::Invalid(); |
| 1647 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1648 | Token t = this->nextToken(); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1649 | ASTNode::ID right = this->multiplicativeExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1650 | if (!right) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1651 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1652 | } |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 1653 | ASTNode::ID newResult = this->createNode(getNode(result).fOffset, |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 1654 | ASTNode::Kind::kBinary, Operator(t.fKind)); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1655 | getNode(newResult).addChild(result); |
| 1656 | getNode(newResult).addChild(right); |
| 1657 | result = newResult; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1658 | break; |
| 1659 | } |
| 1660 | default: |
| 1661 | return result; |
| 1662 | } |
| 1663 | } |
| 1664 | } |
| 1665 | |
| 1666 | /* unaryExpression ((STAR | SLASH | PERCENT) unaryExpression)* */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1667 | ASTNode::ID Parser::multiplicativeExpression() { |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 1668 | AutoDepth depth(this); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1669 | ASTNode::ID result = this->unaryExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1670 | if (!result) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1671 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1672 | } |
| 1673 | for (;;) { |
| 1674 | switch (this->peek().fKind) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1675 | case Token::Kind::TK_STAR: // fall through |
| 1676 | case Token::Kind::TK_SLASH: // fall through |
| 1677 | case Token::Kind::TK_PERCENT: { |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 1678 | if (!depth.increase()) { |
| 1679 | return ASTNode::ID::Invalid(); |
| 1680 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1681 | Token t = this->nextToken(); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1682 | ASTNode::ID right = this->unaryExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1683 | if (!right) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1684 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1685 | } |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 1686 | ASTNode::ID newResult = this->createNode(getNode(result).fOffset, |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 1687 | ASTNode::Kind::kBinary, Operator(t.fKind)); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1688 | getNode(newResult).addChild(result); |
| 1689 | getNode(newResult).addChild(right); |
| 1690 | result = newResult; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1691 | break; |
| 1692 | } |
| 1693 | default: |
| 1694 | return result; |
| 1695 | } |
| 1696 | } |
| 1697 | } |
| 1698 | |
| 1699 | /* postfixExpression | (PLUS | MINUS | NOT | PLUSPLUS | MINUSMINUS) unaryExpression */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1700 | ASTNode::ID Parser::unaryExpression() { |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 1701 | AutoDepth depth(this); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1702 | switch (this->peek().fKind) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1703 | case Token::Kind::TK_PLUS: // fall through |
| 1704 | case Token::Kind::TK_MINUS: // fall through |
| 1705 | case Token::Kind::TK_LOGICALNOT: // fall through |
| 1706 | case Token::Kind::TK_BITWISENOT: // fall through |
| 1707 | case Token::Kind::TK_PLUSPLUS: // fall through |
| 1708 | case Token::Kind::TK_MINUSMINUS: { |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 1709 | if (!depth.increase()) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1710 | return ASTNode::ID::Invalid(); |
Ethan Nicholas | 6dcc325 | 2019-02-20 15:18:36 -0500 | [diff] [blame] | 1711 | } |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 1712 | Token t = this->nextToken(); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1713 | ASTNode::ID expr = this->unaryExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1714 | if (!expr) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1715 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1716 | } |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 1717 | ASTNode::ID result = this->createNode(t.fOffset, ASTNode::Kind::kPrefix, |
| 1718 | Operator(t.fKind)); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1719 | getNode(result).addChild(expr); |
| 1720 | return result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1721 | } |
| 1722 | default: |
| 1723 | return this->postfixExpression(); |
| 1724 | } |
| 1725 | } |
| 1726 | |
| 1727 | /* term suffix* */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1728 | ASTNode::ID Parser::postfixExpression() { |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 1729 | AutoDepth depth(this); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1730 | ASTNode::ID result = this->term(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1731 | if (!result) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1732 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1733 | } |
| 1734 | for (;;) { |
Ethan Nicholas | 5a9a0b3 | 2019-09-17 16:18:22 -0400 | [diff] [blame] | 1735 | Token t = this->peek(); |
| 1736 | switch (t.fKind) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1737 | case Token::Kind::TK_FLOAT_LITERAL: |
Ethan Nicholas | 5a9a0b3 | 2019-09-17 16:18:22 -0400 | [diff] [blame] | 1738 | if (this->text(t)[0] != '.') { |
| 1739 | return result; |
| 1740 | } |
John Stiles | 30212b7 | 2020-06-11 17:55:07 -0400 | [diff] [blame] | 1741 | [[fallthrough]]; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1742 | case Token::Kind::TK_LBRACKET: |
| 1743 | case Token::Kind::TK_DOT: |
| 1744 | case Token::Kind::TK_LPAREN: |
| 1745 | case Token::Kind::TK_PLUSPLUS: |
| 1746 | case Token::Kind::TK_MINUSMINUS: |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 1747 | if (!depth.increase()) { |
| 1748 | return ASTNode::ID::Invalid(); |
| 1749 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1750 | result = this->suffix(result); |
Ethan Nicholas | 0c8582e | 2019-07-19 09:26:46 -0400 | [diff] [blame] | 1751 | if (!result) { |
| 1752 | return ASTNode::ID::Invalid(); |
| 1753 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1754 | break; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1755 | default: |
| 1756 | return result; |
| 1757 | } |
| 1758 | } |
| 1759 | } |
| 1760 | |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1761 | /* LBRACKET expression? RBRACKET | DOT IDENTIFIER | LPAREN parameters RPAREN | |
Brian Osman | c9145f3 | 2021-07-08 13:40:10 -0400 | [diff] [blame^] | 1762 | PLUSPLUS | MINUSMINUS | FLOAT_LITERAL [IDENTIFIER] */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1763 | ASTNode::ID Parser::suffix(ASTNode::ID base) { |
Ethan Nicholas | 0c8582e | 2019-07-19 09:26:46 -0400 | [diff] [blame] | 1764 | SkASSERT(base); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1765 | Token next = this->nextToken(); |
Ethan Nicholas | 4e3b011 | 2019-06-07 16:49:07 -0400 | [diff] [blame] | 1766 | AutoDepth depth(this); |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 1767 | if (!depth.increase()) { |
Ethan Nicholas | 4e3b011 | 2019-06-07 16:49:07 -0400 | [diff] [blame] | 1768 | return ASTNode::ID::Invalid(); |
| 1769 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1770 | switch (next.fKind) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1771 | case Token::Kind::TK_LBRACKET: { |
| 1772 | if (this->checkNext(Token::Kind::TK_RBRACKET)) { |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 1773 | ASTNode::ID result = this->createNode(next.fOffset, ASTNode::Kind::kIndex); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1774 | getNode(result).addChild(base); |
| 1775 | return result; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1776 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1777 | ASTNode::ID e = this->expression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1778 | if (!e) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1779 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1780 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1781 | this->expect(Token::Kind::TK_RBRACKET, "']' to complete array access expression"); |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 1782 | ASTNode::ID result = this->createNode(next.fOffset, ASTNode::Kind::kIndex); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1783 | getNode(result).addChild(base); |
| 1784 | getNode(result).addChild(e); |
| 1785 | return result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1786 | } |
Brian Osman | 6518d77 | 2020-09-10 16:50:06 -0400 | [diff] [blame] | 1787 | case Token::Kind::TK_DOT: { |
| 1788 | int offset = this->peek().fOffset; |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 1789 | skstd::string_view text; |
Brian Osman | 6518d77 | 2020-09-10 16:50:06 -0400 | [diff] [blame] | 1790 | if (this->identifier(&text)) { |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 1791 | ASTNode::ID result = this->createNode(offset, ASTNode::Kind::kField, |
| 1792 | std::move(text)); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1793 | getNode(result).addChild(base); |
| 1794 | return result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1795 | } |
Brian Osman | 6518d77 | 2020-09-10 16:50:06 -0400 | [diff] [blame] | 1796 | [[fallthrough]]; |
Ethan Nicholas | e455f65 | 2019-09-13 12:52:55 -0400 | [diff] [blame] | 1797 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1798 | case Token::Kind::TK_FLOAT_LITERAL: { |
Ethan Nicholas | e455f65 | 2019-09-13 12:52:55 -0400 | [diff] [blame] | 1799 | // Swizzles that start with a constant number, e.g. '.000r', will be tokenized as |
| 1800 | // floating point literals, possibly followed by an identifier. Handle that here. |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 1801 | skstd::string_view field = this->text(next); |
Ethan Nicholas | d2e0960 | 2021-06-10 11:21:59 -0400 | [diff] [blame] | 1802 | SkASSERT(field[0] == '.'); |
| 1803 | field.remove_prefix(1); |
| 1804 | for (auto iter = field.begin(); iter != field.end(); ++iter) { |
| 1805 | if (*iter != '0' && *iter != '1') { |
Ethan Nicholas | e455f65 | 2019-09-13 12:52:55 -0400 | [diff] [blame] | 1806 | this->error(next, "invalid swizzle"); |
| 1807 | return ASTNode::ID::Invalid(); |
| 1808 | } |
| 1809 | } |
| 1810 | // use the next *raw* token so we don't ignore whitespace - we only care about |
| 1811 | // identifiers that directly follow the float |
| 1812 | Token id = this->nextRawToken(); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1813 | if (id.fKind == Token::Kind::TK_IDENTIFIER) { |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 1814 | field = skstd::string_view(field.data(), field.length() + id.fLength); |
Ethan Nicholas | e455f65 | 2019-09-13 12:52:55 -0400 | [diff] [blame] | 1815 | } else { |
| 1816 | this->pushback(id); |
| 1817 | } |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 1818 | ASTNode::ID result = this->createNode(next.fOffset, ASTNode::Kind::kField, field); |
Ethan Nicholas | e455f65 | 2019-09-13 12:52:55 -0400 | [diff] [blame] | 1819 | getNode(result).addChild(base); |
| 1820 | return result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1821 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1822 | case Token::Kind::TK_LPAREN: { |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 1823 | ASTNode::ID result = this->createNode(next.fOffset, ASTNode::Kind::kCall); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1824 | getNode(result).addChild(base); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1825 | if (this->peek().fKind != Token::Kind::TK_RPAREN) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1826 | for (;;) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1827 | ASTNode::ID expr = this->assignmentExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1828 | if (!expr) { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1829 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1830 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1831 | getNode(result).addChild(expr); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1832 | if (!this->checkNext(Token::Kind::TK_COMMA)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1833 | break; |
| 1834 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1835 | } |
| 1836 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1837 | this->expect(Token::Kind::TK_RPAREN, "')' to complete function parameters"); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1838 | return result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1839 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1840 | case Token::Kind::TK_PLUSPLUS: // fall through |
| 1841 | case Token::Kind::TK_MINUSMINUS: { |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 1842 | ASTNode::ID result = this->createNode(next.fOffset, ASTNode::Kind::kPostfix, |
| 1843 | Operator(next.fKind)); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1844 | getNode(result).addChild(base); |
| 1845 | return result; |
| 1846 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1847 | default: { |
Ethan Nicholas | 0c8582e | 2019-07-19 09:26:46 -0400 | [diff] [blame] | 1848 | this->error(next, "expected expression suffix, but found '" + this->text(next) + "'"); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1849 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1850 | } |
| 1851 | } |
| 1852 | } |
| 1853 | |
Brian Osman | 33c64a4 | 2020-12-23 10:26:38 -0500 | [diff] [blame] | 1854 | /* IDENTIFIER | intLiteral | floatLiteral | boolLiteral | '(' expression ')' */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1855 | ASTNode::ID Parser::term() { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1856 | Token t = this->peek(); |
| 1857 | switch (t.fKind) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1858 | case Token::Kind::TK_IDENTIFIER: { |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 1859 | skstd::string_view text; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1860 | if (this->identifier(&text)) { |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 1861 | return this->createNode(t.fOffset, ASTNode::Kind::kIdentifier, std::move(text)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1862 | } |
John Stiles | 30212b7 | 2020-06-11 17:55:07 -0400 | [diff] [blame] | 1863 | break; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1864 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1865 | case Token::Kind::TK_INT_LITERAL: { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1866 | SKSL_INT i; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1867 | if (this->intLiteral(&i)) { |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 1868 | return this->createNode(t.fOffset, ASTNode::Kind::kInt, i); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1869 | } |
| 1870 | break; |
| 1871 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1872 | case Token::Kind::TK_FLOAT_LITERAL: { |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1873 | SKSL_FLOAT f; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1874 | if (this->floatLiteral(&f)) { |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 1875 | return this->createNode(t.fOffset, ASTNode::Kind::kFloat, f); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1876 | } |
| 1877 | break; |
| 1878 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1879 | case Token::Kind::TK_TRUE_LITERAL: // fall through |
| 1880 | case Token::Kind::TK_FALSE_LITERAL: { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1881 | bool b; |
| 1882 | if (this->boolLiteral(&b)) { |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 1883 | return this->createNode(t.fOffset, ASTNode::Kind::kBool, b); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1884 | } |
| 1885 | break; |
| 1886 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1887 | case Token::Kind::TK_LPAREN: { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1888 | this->nextToken(); |
Ethan Nicholas | 4e3b011 | 2019-06-07 16:49:07 -0400 | [diff] [blame] | 1889 | AutoDepth depth(this); |
Ethan Nicholas | cf4deab | 2019-09-13 16:28:14 -0400 | [diff] [blame] | 1890 | if (!depth.increase()) { |
Ethan Nicholas | 4e3b011 | 2019-06-07 16:49:07 -0400 | [diff] [blame] | 1891 | return ASTNode::ID::Invalid(); |
| 1892 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1893 | ASTNode::ID result = this->expression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1894 | if (result) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1895 | this->expect(Token::Kind::TK_RPAREN, "')' to complete expression"); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1896 | return result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1897 | } |
| 1898 | break; |
| 1899 | } |
| 1900 | default: |
| 1901 | this->nextToken(); |
John Stiles | f94348f | 2020-12-23 18:58:43 -0500 | [diff] [blame] | 1902 | this->error(t.fOffset, "expected expression, but found '" + this->text(t) + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1903 | } |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1904 | return ASTNode::ID::Invalid(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1905 | } |
| 1906 | |
| 1907 | /* INT_LITERAL */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1908 | bool Parser::intLiteral(SKSL_INT* dest) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1909 | Token t; |
John Stiles | f94348f | 2020-12-23 18:58:43 -0500 | [diff] [blame] | 1910 | if (!this->expect(Token::Kind::TK_INT_LITERAL, "integer literal", &t)) { |
| 1911 | return false; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1912 | } |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 1913 | skstd::string_view s = this->text(t); |
John Stiles | f94348f | 2020-12-23 18:58:43 -0500 | [diff] [blame] | 1914 | if (!SkSL::stoi(s, dest)) { |
| 1915 | this->error(t, "integer is too large: " + s); |
| 1916 | return false; |
| 1917 | } |
| 1918 | return true; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1919 | } |
| 1920 | |
John Stiles | f94348f | 2020-12-23 18:58:43 -0500 | [diff] [blame] | 1921 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1922 | /* FLOAT_LITERAL */ |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 1923 | bool Parser::floatLiteral(SKSL_FLOAT* dest) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1924 | Token t; |
John Stiles | f94348f | 2020-12-23 18:58:43 -0500 | [diff] [blame] | 1925 | if (!this->expect(Token::Kind::TK_FLOAT_LITERAL, "float literal", &t)) { |
| 1926 | return false; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1927 | } |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 1928 | skstd::string_view s = this->text(t); |
John Stiles | f94348f | 2020-12-23 18:58:43 -0500 | [diff] [blame] | 1929 | if (!SkSL::stod(s, dest)) { |
| 1930 | this->error(t, "floating-point value is too large: " + s); |
| 1931 | return false; |
| 1932 | } |
| 1933 | return true; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1934 | } |
| 1935 | |
| 1936 | /* TRUE_LITERAL | FALSE_LITERAL */ |
| 1937 | bool Parser::boolLiteral(bool* dest) { |
| 1938 | Token t = this->nextToken(); |
| 1939 | switch (t.fKind) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1940 | case Token::Kind::TK_TRUE_LITERAL: |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1941 | *dest = true; |
| 1942 | return true; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1943 | case Token::Kind::TK_FALSE_LITERAL: |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1944 | *dest = false; |
| 1945 | return true; |
| 1946 | default: |
Ethan Nicholas | 0c8582e | 2019-07-19 09:26:46 -0400 | [diff] [blame] | 1947 | this->error(t, "expected 'true' or 'false', but found '" + this->text(t) + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1948 | return false; |
| 1949 | } |
| 1950 | } |
| 1951 | |
| 1952 | /* IDENTIFIER */ |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 1953 | bool Parser::identifier(skstd::string_view* dest) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1954 | Token t; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 1955 | if (this->expect(Token::Kind::TK_IDENTIFIER, "identifier", &t)) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1956 | *dest = this->text(t); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1957 | return true; |
| 1958 | } |
| 1959 | return false; |
| 1960 | } |
| 1961 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 1962 | } // namespace SkSL |