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