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