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