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