Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 Google LLC. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "src/sksl/SkSLDSLParser.h" |
| 9 | |
| 10 | #include "include/private/SkSLString.h" |
| 11 | #include "src/sksl/SkSLCompiler.h" |
Ethan Nicholas | c845272 | 2021-10-07 10:47:32 -0400 | [diff] [blame] | 12 | #include "src/sksl/SkSLThreadContext.h" |
Ethan Nicholas | 6c302ba | 2021-09-14 09:16:12 -0400 | [diff] [blame] | 13 | #include "src/sksl/dsl/priv/DSLWriter.h" |
Ethan Nicholas | 051aeb7 | 2021-09-24 16:39:19 -0400 | [diff] [blame] | 14 | #include "src/sksl/dsl/priv/DSL_priv.h" |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 15 | |
| 16 | #include <memory> |
| 17 | |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 18 | using namespace SkSL::dsl; |
| 19 | |
| 20 | namespace SkSL { |
| 21 | |
| 22 | static constexpr int kMaxParseDepth = 50; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 23 | |
| 24 | static int parse_modifier_token(Token::Kind token) { |
| 25 | switch (token) { |
| 26 | case Token::Kind::TK_UNIFORM: return Modifiers::kUniform_Flag; |
| 27 | case Token::Kind::TK_CONST: return Modifiers::kConst_Flag; |
| 28 | case Token::Kind::TK_IN: return Modifiers::kIn_Flag; |
| 29 | case Token::Kind::TK_OUT: return Modifiers::kOut_Flag; |
| 30 | case Token::Kind::TK_INOUT: return Modifiers::kIn_Flag | Modifiers::kOut_Flag; |
| 31 | case Token::Kind::TK_FLAT: return Modifiers::kFlat_Flag; |
| 32 | case Token::Kind::TK_NOPERSPECTIVE: return Modifiers::kNoPerspective_Flag; |
| 33 | case Token::Kind::TK_HASSIDEEFFECTS: return Modifiers::kHasSideEffects_Flag; |
| 34 | case Token::Kind::TK_INLINE: return Modifiers::kInline_Flag; |
| 35 | case Token::Kind::TK_NOINLINE: return Modifiers::kNoInline_Flag; |
John Stiles | 0201431 | 2021-08-04 16:03:12 -0400 | [diff] [blame] | 36 | case Token::Kind::TK_HIGHP: return Modifiers::kHighp_Flag; |
| 37 | case Token::Kind::TK_MEDIUMP: return Modifiers::kMediump_Flag; |
| 38 | case Token::Kind::TK_LOWP: return Modifiers::kLowp_Flag; |
John Stiles | efde90d | 2021-08-12 23:06:24 -0400 | [diff] [blame] | 39 | case Token::Kind::TK_ES3: return Modifiers::kES3_Flag; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 40 | default: return 0; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | class AutoDSLDepth { |
| 45 | public: |
| 46 | AutoDSLDepth(DSLParser* p) |
| 47 | : fParser(p) |
| 48 | , fDepth(0) {} |
| 49 | |
| 50 | ~AutoDSLDepth() { |
| 51 | fParser->fDepth -= fDepth; |
| 52 | } |
| 53 | |
| 54 | bool increase() { |
| 55 | ++fDepth; |
| 56 | ++fParser->fDepth; |
| 57 | if (fParser->fDepth > kMaxParseDepth) { |
| 58 | fParser->error(fParser->peek(), String("exceeded max parse depth")); |
| 59 | return false; |
| 60 | } |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | DSLParser* fParser; |
| 66 | int fDepth; |
| 67 | }; |
| 68 | |
| 69 | class AutoDSLSymbolTable { |
| 70 | public: |
| 71 | AutoDSLSymbolTable() { |
| 72 | dsl::PushSymbolTable(); |
| 73 | } |
| 74 | |
| 75 | ~AutoDSLSymbolTable() { |
| 76 | dsl::PopSymbolTable(); |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | std::unordered_map<skstd::string_view, DSLParser::LayoutToken>* DSLParser::layoutTokens; |
| 81 | |
| 82 | void DSLParser::InitLayoutMap() { |
| 83 | layoutTokens = new std::unordered_map<skstd::string_view, LayoutToken>; |
| 84 | #define TOKEN(name, text) (*layoutTokens)[text] = LayoutToken::name |
| 85 | TOKEN(LOCATION, "location"); |
| 86 | TOKEN(OFFSET, "offset"); |
| 87 | TOKEN(BINDING, "binding"); |
| 88 | TOKEN(INDEX, "index"); |
| 89 | TOKEN(SET, "set"); |
| 90 | TOKEN(BUILTIN, "builtin"); |
| 91 | TOKEN(INPUT_ATTACHMENT_INDEX, "input_attachment_index"); |
| 92 | TOKEN(ORIGIN_UPPER_LEFT, "origin_upper_left"); |
| 93 | TOKEN(BLEND_SUPPORT_ALL_EQUATIONS, "blend_support_all_equations"); |
| 94 | TOKEN(PUSH_CONSTANT, "push_constant"); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 95 | TOKEN(SRGB_UNPREMUL, "srgb_unpremul"); |
| 96 | #undef TOKEN |
| 97 | } |
| 98 | |
| 99 | DSLParser::DSLParser(Compiler* compiler, const ProgramSettings& settings, ProgramKind kind, |
| 100 | String text) |
| 101 | : fCompiler(*compiler) |
| 102 | , fSettings(settings) |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 103 | , fKind(kind) |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 104 | , fText(std::make_unique<String>(std::move(text))) |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame] | 105 | , fPushback(Token::Kind::TK_NONE, /*offset=*/-1, /*length=*/-1, /*line=*/-1) { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 106 | // We don't want to have to worry about manually releasing all of the objects in the event that |
| 107 | // an error occurs |
| 108 | fSettings.fAssertDSLObjectsReleased = false; |
Ethan Nicholas | ae9b446 | 2021-09-16 16:23:05 -0400 | [diff] [blame] | 109 | // We manage our symbol tables manually, so no need for name mangling |
| 110 | fSettings.fDSLMangling = false; |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 111 | fLexer.start(*fText); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 112 | static const bool layoutMapInitialized = []{ InitLayoutMap(); return true; }(); |
| 113 | (void) layoutMapInitialized; |
| 114 | } |
| 115 | |
| 116 | Token DSLParser::nextRawToken() { |
| 117 | if (fPushback.fKind != Token::Kind::TK_NONE) { |
| 118 | Token result = fPushback; |
| 119 | fPushback.fKind = Token::Kind::TK_NONE; |
| 120 | return result; |
| 121 | } |
| 122 | return fLexer.next(); |
| 123 | } |
| 124 | |
| 125 | Token DSLParser::nextToken() { |
| 126 | Token token = this->nextRawToken(); |
| 127 | while (token.fKind == Token::Kind::TK_WHITESPACE || |
| 128 | token.fKind == Token::Kind::TK_LINE_COMMENT || |
| 129 | token.fKind == Token::Kind::TK_BLOCK_COMMENT) { |
| 130 | token = this->nextRawToken(); |
| 131 | } |
| 132 | return token; |
| 133 | } |
| 134 | |
| 135 | void DSLParser::pushback(Token t) { |
| 136 | SkASSERT(fPushback.fKind == Token::Kind::TK_NONE); |
| 137 | fPushback = std::move(t); |
| 138 | } |
| 139 | |
| 140 | Token DSLParser::peek() { |
| 141 | if (fPushback.fKind == Token::Kind::TK_NONE) { |
| 142 | fPushback = this->nextToken(); |
| 143 | } |
| 144 | return fPushback; |
| 145 | } |
| 146 | |
| 147 | bool DSLParser::checkNext(Token::Kind kind, Token* result) { |
| 148 | if (fPushback.fKind != Token::Kind::TK_NONE && fPushback.fKind != kind) { |
| 149 | return false; |
| 150 | } |
| 151 | Token next = this->nextToken(); |
| 152 | if (next.fKind == kind) { |
| 153 | if (result) { |
| 154 | *result = next; |
| 155 | } |
| 156 | return true; |
| 157 | } |
| 158 | this->pushback(std::move(next)); |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | bool DSLParser::expect(Token::Kind kind, const char* expected, Token* result) { |
| 163 | Token next = this->nextToken(); |
| 164 | if (next.fKind == kind) { |
| 165 | if (result) { |
| 166 | *result = std::move(next); |
| 167 | } |
| 168 | return true; |
| 169 | } else { |
| 170 | this->error(next, "expected " + String(expected) + ", but found '" + |
| 171 | this->text(next) + "'"); |
Ethan Nicholas | 5d97a96 | 2021-08-30 15:58:52 -0400 | [diff] [blame] | 172 | this->fEncounteredFatalError = true; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 173 | return false; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | bool DSLParser::expectIdentifier(Token* result) { |
| 178 | if (!this->expect(Token::Kind::TK_IDENTIFIER, "an identifier", result)) { |
| 179 | return false; |
| 180 | } |
| 181 | if (IsType(this->text(*result))) { |
| 182 | this->error(*result, "expected an identifier, but found type '" + |
| 183 | this->text(*result) + "'"); |
Ethan Nicholas | 5d97a96 | 2021-08-30 15:58:52 -0400 | [diff] [blame] | 184 | this->fEncounteredFatalError = true; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 185 | return false; |
| 186 | } |
| 187 | return true; |
| 188 | } |
| 189 | |
| 190 | skstd::string_view DSLParser::text(Token token) { |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 191 | return skstd::string_view(fText->data() + token.fOffset, token.fLength); |
| 192 | } |
| 193 | |
| 194 | PositionInfo DSLParser::position(Token t) { |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame] | 195 | return this->position(t.fLine); |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 196 | } |
| 197 | |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame] | 198 | PositionInfo DSLParser::position(int line) { |
| 199 | return PositionInfo("<unknown>", line); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | void DSLParser::error(Token token, String msg) { |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame] | 203 | this->error(token.fLine, msg); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 204 | } |
| 205 | |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame] | 206 | void DSLParser::error(int line, String msg) { |
| 207 | GetErrorReporter().error(msg.c_str(), this->position(line)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 208 | } |
| 209 | |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 210 | /* declaration* END_OF_FILE */ |
| 211 | std::unique_ptr<Program> DSLParser::program() { |
Ethan Nicholas | 5c4463e | 2021-08-29 14:31:19 -0400 | [diff] [blame] | 212 | ErrorReporter* errorReporter = &fCompiler.errorReporter(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 213 | Start(&fCompiler, fKind, fSettings); |
Ethan Nicholas | 5c4463e | 2021-08-29 14:31:19 -0400 | [diff] [blame] | 214 | SetErrorReporter(errorReporter); |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 215 | errorReporter->setSource(fText->c_str()); |
Ethan Nicholas | 051aeb7 | 2021-09-24 16:39:19 -0400 | [diff] [blame] | 216 | this->declarations(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 217 | std::unique_ptr<Program> result; |
Ethan Nicholas | 051aeb7 | 2021-09-24 16:39:19 -0400 | [diff] [blame] | 218 | if (!GetErrorReporter().errorCount()) { |
| 219 | result = dsl::ReleaseProgram(std::move(fText)); |
| 220 | } |
| 221 | errorReporter->setSource(nullptr); |
| 222 | End(); |
| 223 | return result; |
| 224 | } |
| 225 | |
| 226 | SkSL::LoadedModule DSLParser::moduleInheritingFrom(SkSL::ParsedModule baseModule) { |
| 227 | ErrorReporter* errorReporter = &fCompiler.errorReporter(); |
| 228 | StartModule(&fCompiler, fKind, fSettings, std::move(baseModule)); |
| 229 | SetErrorReporter(errorReporter); |
| 230 | errorReporter->setSource(fText->c_str()); |
| 231 | this->declarations(); |
| 232 | CurrentSymbolTable()->takeOwnershipOfString(std::move(*fText)); |
| 233 | SkSL::LoadedModule result{ fKind, CurrentSymbolTable(), |
Ethan Nicholas | c845272 | 2021-10-07 10:47:32 -0400 | [diff] [blame] | 234 | std::move(ThreadContext::ProgramElements()) }; |
Ethan Nicholas | 051aeb7 | 2021-09-24 16:39:19 -0400 | [diff] [blame] | 235 | errorReporter->setSource(nullptr); |
| 236 | End(); |
| 237 | return result; |
| 238 | } |
| 239 | |
| 240 | void DSLParser::declarations() { |
| 241 | fEncounteredFatalError = false; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 242 | bool done = false; |
| 243 | while (!done) { |
| 244 | switch (this->peek().fKind) { |
| 245 | case Token::Kind::TK_END_OF_FILE: |
| 246 | done = true; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 247 | break; |
Ethan Nicholas | 642215e | 2021-09-03 11:10:54 -0400 | [diff] [blame] | 248 | case Token::Kind::TK_DIRECTIVE: |
| 249 | this->directive(); |
| 250 | break; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 251 | case Token::Kind::TK_INVALID: { |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 252 | this->nextToken(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 253 | this->error(this->peek(), String("invalid token")); |
Ethan Nicholas | f8f1fa0 | 2021-08-29 14:12:17 -0400 | [diff] [blame] | 254 | done = true; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 255 | break; |
| 256 | } |
Ethan Nicholas | 5d97a96 | 2021-08-30 15:58:52 -0400 | [diff] [blame] | 257 | default: |
| 258 | this->declaration(); |
| 259 | done = fEncounteredFatalError; |
Ethan Nicholas | b13f369 | 2021-09-10 16:49:42 -0400 | [diff] [blame] | 260 | break; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 261 | } |
| 262 | } |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 263 | } |
| 264 | |
Ethan Nicholas | 642215e | 2021-09-03 11:10:54 -0400 | [diff] [blame] | 265 | /* DIRECTIVE(#extension) IDENTIFIER COLON IDENTIFIER */ |
| 266 | void DSLParser::directive() { |
| 267 | Token start; |
| 268 | if (!this->expect(Token::Kind::TK_DIRECTIVE, "a directive", &start)) { |
| 269 | return; |
| 270 | } |
| 271 | skstd::string_view text = this->text(start); |
| 272 | if (text == "#extension") { |
| 273 | Token name; |
| 274 | if (!this->expectIdentifier(&name)) { |
| 275 | return; |
| 276 | } |
| 277 | if (!this->expect(Token::Kind::TK_COLON, "':'")) { |
| 278 | return; |
| 279 | } |
| 280 | Token behavior; |
| 281 | if (!this->expect(Token::Kind::TK_IDENTIFIER, "an identifier", &behavior)) { |
| 282 | return; |
| 283 | } |
| 284 | skstd::string_view behaviorText = this->text(behavior); |
| 285 | if (behaviorText == "disable") { |
| 286 | return; |
| 287 | } |
| 288 | if (behaviorText != "require" && behaviorText != "enable" && behaviorText != "warn") { |
| 289 | this->error(behavior, "expected 'require', 'enable', 'warn', or 'disable'"); |
| 290 | } |
| 291 | // We don't currently do anything different between require, enable, and warn |
| 292 | dsl::AddExtension(this->text(name)); |
| 293 | } else { |
| 294 | this->error(start, "unsupported directive '" + this->text(start) + "'"); |
| 295 | } |
| 296 | } |
| 297 | |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 298 | /* modifiers (structVarDeclaration | type IDENTIFIER ((LPAREN parameter (COMMA parameter)* RPAREN |
| 299 | (block | SEMICOLON)) | SEMICOLON) | interfaceBlock) */ |
| 300 | bool DSLParser::declaration() { |
| 301 | Token lookahead = this->peek(); |
| 302 | switch (lookahead.fKind) { |
| 303 | case Token::Kind::TK_SEMICOLON: |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 304 | this->nextToken(); |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame] | 305 | this->error(lookahead, "expected a declaration, but found ';'"); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 306 | return false; |
| 307 | default: |
| 308 | break; |
| 309 | } |
| 310 | DSLModifiers modifiers = this->modifiers(); |
| 311 | lookahead = this->peek(); |
| 312 | if (lookahead.fKind == Token::Kind::TK_IDENTIFIER && !IsType(this->text(lookahead))) { |
| 313 | // we have an identifier that's not a type, could be the start of an interface block |
| 314 | return this->interfaceBlock(modifiers); |
| 315 | } |
| 316 | if (lookahead.fKind == Token::Kind::TK_SEMICOLON) { |
Ethan Nicholas | 678ec71 | 2021-09-03 06:33:47 -0400 | [diff] [blame] | 317 | this->nextToken(); |
| 318 | Declare(modifiers, position(lookahead)); |
| 319 | return true; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 320 | } |
| 321 | if (lookahead.fKind == Token::Kind::TK_STRUCT) { |
Ethan Nicholas | 6c302ba | 2021-09-14 09:16:12 -0400 | [diff] [blame] | 322 | this->structVarDeclaration(modifiers); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 323 | return true; |
| 324 | } |
John Stiles | 66aa1de | 2021-10-01 13:59:20 -0400 | [diff] [blame] | 325 | skstd::optional<DSLType> type = this->type(&modifiers); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 326 | if (!type) { |
| 327 | return false; |
| 328 | } |
| 329 | Token name; |
| 330 | if (!this->expectIdentifier(&name)) { |
| 331 | return false; |
| 332 | } |
| 333 | if (this->checkNext(Token::Kind::TK_LPAREN)) { |
| 334 | return this->functionDeclarationEnd(modifiers, *type, name); |
| 335 | } else { |
Ethan Nicholas | 6c302ba | 2021-09-14 09:16:12 -0400 | [diff] [blame] | 336 | this->globalVarDeclarationEnd(this->position(name), modifiers, *type, this->text(name)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 337 | return true; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | /* (RPAREN | VOID RPAREN | parameter (COMMA parameter)* RPAREN) (block | SEMICOLON) */ |
John Stiles | e53c721 | 2021-08-05 10:19:11 -0400 | [diff] [blame] | 342 | bool DSLParser::functionDeclarationEnd(const DSLModifiers& modifiers, |
| 343 | DSLType type, |
| 344 | const Token& name) { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 345 | SkTArray<DSLWrapper<DSLParameter>> parameters; |
| 346 | Token lookahead = this->peek(); |
| 347 | if (lookahead.fKind == Token::Kind::TK_RPAREN) { |
| 348 | // `()` means no parameters at all. |
| 349 | } else if (lookahead.fKind == Token::Kind::TK_IDENTIFIER && this->text(lookahead) == "void") { |
| 350 | // `(void)` also means no parameters at all. |
| 351 | this->nextToken(); |
| 352 | } else { |
| 353 | for (;;) { |
| 354 | skstd::optional<DSLWrapper<DSLParameter>> parameter = this->parameter(); |
| 355 | if (!parameter) { |
| 356 | return false; |
| 357 | } |
| 358 | parameters.push_back(std::move(*parameter)); |
| 359 | if (!this->checkNext(Token::Kind::TK_COMMA)) { |
| 360 | break; |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | if (!this->expect(Token::Kind::TK_RPAREN, "')'")) { |
| 365 | return false; |
| 366 | } |
| 367 | SkTArray<DSLParameter*> parameterPointers; |
| 368 | for (DSLWrapper<DSLParameter>& param : parameters) { |
| 369 | parameterPointers.push_back(¶m.get()); |
| 370 | } |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 371 | DSLFunction result(modifiers, type, this->text(name), parameterPointers, this->position(name)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 372 | if (!this->checkNext(Token::Kind::TK_SEMICOLON)) { |
| 373 | AutoDSLSymbolTable symbols; |
| 374 | for (DSLParameter* var : parameterPointers) { |
| 375 | AddToSymbolTable(*var); |
| 376 | } |
| 377 | skstd::optional<DSLBlock> body = this->block(); |
| 378 | if (!body) { |
| 379 | return false; |
| 380 | } |
Ethan Nicholas | c9d65f0 | 2021-09-10 11:57:46 -0400 | [diff] [blame] | 381 | result.define(std::move(*body), this->position(name)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 382 | } |
| 383 | return true; |
| 384 | } |
| 385 | |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 386 | SKSL_INT DSLParser::arraySize() { |
| 387 | Token next = this->peek(); |
| 388 | if (next.fKind == Token::Kind::TK_INT_LITERAL) { |
| 389 | SKSL_INT size; |
| 390 | if (this->intLiteral(&size)) { |
| 391 | if (size > INT32_MAX) { |
| 392 | this->error(next, "array size out of bounds"); |
| 393 | return 1; |
| 394 | } |
| 395 | if (size <= 0) { |
| 396 | this->error(next, "array size must be positive"); |
| 397 | return 1; |
| 398 | } |
| 399 | return size; |
| 400 | } |
| 401 | return 1; |
| 402 | } else if (this->checkNext(Token::Kind::TK_MINUS) && |
| 403 | this->checkNext(Token::Kind::TK_INT_LITERAL)) { |
| 404 | this->error(next, "array size must be positive"); |
| 405 | return 1; |
| 406 | } else { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 407 | DSLExpression expr = this->expression(); |
| 408 | if (expr.isValid()) { |
Ethan Nicholas | 51b4b86 | 2021-08-31 16:12:40 -0400 | [diff] [blame] | 409 | this->error(next, "expected int literal"); |
| 410 | } |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 411 | return 1; |
| 412 | } |
| 413 | } |
| 414 | |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame] | 415 | bool DSLParser::parseArrayDimensions(int line, DSLType* type) { |
Ethan Nicholas | 6c302ba | 2021-09-14 09:16:12 -0400 | [diff] [blame] | 416 | while (this->checkNext(Token::Kind::TK_LBRACKET)) { |
| 417 | if (this->checkNext(Token::Kind::TK_RBRACKET)) { |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame] | 418 | this->error(line, "expected array dimension"); |
Ethan Nicholas | 6c302ba | 2021-09-14 09:16:12 -0400 | [diff] [blame] | 419 | } else { |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame] | 420 | *type = Array(*type, this->arraySize(), this->position(line)); |
Ethan Nicholas | 6c302ba | 2021-09-14 09:16:12 -0400 | [diff] [blame] | 421 | if (!this->expect(Token::Kind::TK_RBRACKET, "']'")) { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 422 | return false; |
| 423 | } |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 424 | } |
Ethan Nicholas | 6c302ba | 2021-09-14 09:16:12 -0400 | [diff] [blame] | 425 | } |
| 426 | return true; |
| 427 | } |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 428 | |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame] | 429 | bool DSLParser::parseInitializer(int line, DSLExpression* initializer) { |
Ethan Nicholas | 6c302ba | 2021-09-14 09:16:12 -0400 | [diff] [blame] | 430 | if (this->checkNext(Token::Kind::TK_EQ)) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 431 | DSLExpression value = this->assignmentExpression(); |
| 432 | if (!value.hasValue()) { |
Ethan Nicholas | 6c302ba | 2021-09-14 09:16:12 -0400 | [diff] [blame] | 433 | return false; |
| 434 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 435 | initializer->swap(value); |
Ethan Nicholas | 6c302ba | 2021-09-14 09:16:12 -0400 | [diff] [blame] | 436 | } |
| 437 | return true; |
| 438 | } |
| 439 | |
| 440 | /* (LBRACKET expression? RBRACKET)* (EQ assignmentExpression)? (COMMA IDENTIFER |
| 441 | (LBRACKET expression? RBRACKET)* (EQ assignmentExpression)?)* SEMICOLON */ |
| 442 | void DSLParser::globalVarDeclarationEnd(PositionInfo pos, const dsl::DSLModifiers& mods, |
| 443 | dsl::DSLType baseType, skstd::string_view name) { |
| 444 | using namespace dsl; |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame] | 445 | int line = this->peek().fLine; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 446 | DSLType type = baseType; |
| 447 | DSLExpression initializer; |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame] | 448 | if (!this->parseArrayDimensions(line, &type)) { |
Ethan Nicholas | 6c302ba | 2021-09-14 09:16:12 -0400 | [diff] [blame] | 449 | return; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 450 | } |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame] | 451 | if (!this->parseInitializer(line, &initializer)) { |
John Stiles | b05bbd0 | 2021-09-24 15:11:16 -0400 | [diff] [blame] | 452 | return; |
| 453 | } |
Ethan Nicholas | 6c302ba | 2021-09-14 09:16:12 -0400 | [diff] [blame] | 454 | DSLGlobalVar first(mods, type, name, std::move(initializer), pos); |
| 455 | Declare(first); |
| 456 | AddToSymbolTable(first); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 457 | |
| 458 | while (this->checkNext(Token::Kind::TK_COMMA)) { |
| 459 | type = baseType; |
| 460 | Token identifierName; |
| 461 | if (!this->expectIdentifier(&identifierName)) { |
Ethan Nicholas | 6c302ba | 2021-09-14 09:16:12 -0400 | [diff] [blame] | 462 | return; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 463 | } |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame] | 464 | if (!this->parseArrayDimensions(line, &type)) { |
Ethan Nicholas | 6c302ba | 2021-09-14 09:16:12 -0400 | [diff] [blame] | 465 | return; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 466 | } |
Ethan Nicholas | 0ed278b | 2021-09-03 13:06:31 -0400 | [diff] [blame] | 467 | DSLExpression anotherInitializer; |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame] | 468 | if (!this->parseInitializer(line, &anotherInitializer)) { |
Ethan Nicholas | 6c302ba | 2021-09-14 09:16:12 -0400 | [diff] [blame] | 469 | return; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 470 | } |
John Stiles | b05bbd0 | 2021-09-24 15:11:16 -0400 | [diff] [blame] | 471 | DSLGlobalVar next(mods, type, this->text(identifierName), std::move(anotherInitializer), |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame] | 472 | this->position(line)); |
Ethan Nicholas | 6c302ba | 2021-09-14 09:16:12 -0400 | [diff] [blame] | 473 | Declare(next); |
Ethan Nicholas | c973d26 | 2021-09-17 16:10:29 -0400 | [diff] [blame] | 474 | AddToSymbolTable(next, this->position(identifierName)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 475 | } |
Ethan Nicholas | b9c6489 | 2021-09-02 10:31:25 -0400 | [diff] [blame] | 476 | this->expect(Token::Kind::TK_SEMICOLON, "';'"); |
Ethan Nicholas | 6c302ba | 2021-09-14 09:16:12 -0400 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /* (LBRACKET expression? RBRACKET)* (EQ assignmentExpression)? (COMMA IDENTIFER |
| 480 | (LBRACKET expression? RBRACKET)* (EQ assignmentExpression)?)* SEMICOLON */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 481 | DSLStatement DSLParser::localVarDeclarationEnd(PositionInfo pos, const dsl::DSLModifiers& mods, |
| 482 | dsl::DSLType baseType, skstd::string_view name) { |
Ethan Nicholas | 6c302ba | 2021-09-14 09:16:12 -0400 | [diff] [blame] | 483 | using namespace dsl; |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame] | 484 | int line = this->peek().fLine; |
Ethan Nicholas | 6c302ba | 2021-09-14 09:16:12 -0400 | [diff] [blame] | 485 | DSLType type = baseType; |
| 486 | DSLExpression initializer; |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame] | 487 | if (!this->parseArrayDimensions(line, &type)) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 488 | return {}; |
Ethan Nicholas | 6c302ba | 2021-09-14 09:16:12 -0400 | [diff] [blame] | 489 | } |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame] | 490 | if (!this->parseInitializer(line, &initializer)) { |
John Stiles | b05bbd0 | 2021-09-24 15:11:16 -0400 | [diff] [blame] | 491 | return {}; |
| 492 | } |
Ethan Nicholas | 6c302ba | 2021-09-14 09:16:12 -0400 | [diff] [blame] | 493 | DSLVar first(mods, type, name, std::move(initializer), pos); |
| 494 | DSLStatement result = Declare(first); |
| 495 | AddToSymbolTable(first); |
| 496 | |
| 497 | while (this->checkNext(Token::Kind::TK_COMMA)) { |
| 498 | type = baseType; |
| 499 | Token identifierName; |
| 500 | if (!this->expectIdentifier(&identifierName)) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 501 | return result; |
Ethan Nicholas | 6c302ba | 2021-09-14 09:16:12 -0400 | [diff] [blame] | 502 | } |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame] | 503 | if (!this->parseArrayDimensions(line, &type)) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 504 | return result; |
Ethan Nicholas | 6c302ba | 2021-09-14 09:16:12 -0400 | [diff] [blame] | 505 | } |
| 506 | DSLExpression anotherInitializer; |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame] | 507 | if (!this->parseInitializer(line, &anotherInitializer)) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 508 | return result; |
Ethan Nicholas | 6c302ba | 2021-09-14 09:16:12 -0400 | [diff] [blame] | 509 | } |
John Stiles | b05bbd0 | 2021-09-24 15:11:16 -0400 | [diff] [blame] | 510 | DSLVar next(mods, type, this->text(identifierName), std::move(anotherInitializer), |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame] | 511 | this->position(line)); |
Ethan Nicholas | 6c302ba | 2021-09-14 09:16:12 -0400 | [diff] [blame] | 512 | DSLWriter::AddVarDeclaration(result, next); |
Ethan Nicholas | c973d26 | 2021-09-17 16:10:29 -0400 | [diff] [blame] | 513 | AddToSymbolTable(next, this->position(identifierName)); |
Ethan Nicholas | 6c302ba | 2021-09-14 09:16:12 -0400 | [diff] [blame] | 514 | } |
| 515 | this->expect(Token::Kind::TK_SEMICOLON, "';'"); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 516 | return result; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | /* (varDeclarations | expressionStatement) */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 520 | DSLStatement DSLParser::varDeclarationsOrExpressionStatement() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 521 | Token nextToken = this->peek(); |
| 522 | if (nextToken.fKind == Token::Kind::TK_CONST) { |
| 523 | // Statements that begin with `const` might be variable declarations, but can't be legal |
| 524 | // SkSL expression-statements. (SkSL constructors don't take a `const` modifier.) |
| 525 | return this->varDeclarations(); |
| 526 | } |
| 527 | |
John Stiles | 0201431 | 2021-08-04 16:03:12 -0400 | [diff] [blame] | 528 | if (nextToken.fKind == Token::Kind::TK_HIGHP || |
| 529 | nextToken.fKind == Token::Kind::TK_MEDIUMP || |
| 530 | nextToken.fKind == Token::Kind::TK_LOWP || |
| 531 | IsType(this->text(nextToken))) { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 532 | // Statements that begin with a typename are most often variable declarations, but |
| 533 | // occasionally the type is part of a constructor, and these are actually expression- |
| 534 | // statements in disguise. First, attempt the common case: parse it as a vardecl. |
| 535 | Checkpoint checkpoint(this); |
| 536 | VarDeclarationsPrefix prefix; |
| 537 | if (this->varDeclarationsPrefix(&prefix)) { |
| 538 | checkpoint.accept(); |
Ethan Nicholas | 6c302ba | 2021-09-14 09:16:12 -0400 | [diff] [blame] | 539 | return this->localVarDeclarationEnd(prefix.fPosition, prefix.fModifiers, prefix.fType, |
| 540 | this->text(prefix.fName)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | // If this statement wasn't actually a vardecl after all, rewind and try parsing it as an |
| 544 | // expression-statement instead. |
| 545 | checkpoint.rewind(); |
| 546 | } |
| 547 | return this->expressionStatement(); |
| 548 | } |
| 549 | |
| 550 | // Helper function for varDeclarations(). If this function succeeds, we assume that the rest of the |
| 551 | // statement is a variable-declaration statement, not an expression-statement. |
| 552 | bool DSLParser::varDeclarationsPrefix(VarDeclarationsPrefix* prefixData) { |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 553 | prefixData->fPosition = this->position(this->peek()); |
| 554 | prefixData->fModifiers = this->modifiers(); |
John Stiles | 66aa1de | 2021-10-01 13:59:20 -0400 | [diff] [blame] | 555 | skstd::optional<DSLType> type = this->type(&prefixData->fModifiers); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 556 | if (!type) { |
| 557 | return false; |
| 558 | } |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 559 | prefixData->fType = *type; |
| 560 | return this->expectIdentifier(&prefixData->fName); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | /* modifiers type IDENTIFIER varDeclarationEnd */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 564 | DSLStatement DSLParser::varDeclarations() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 565 | VarDeclarationsPrefix prefix; |
| 566 | if (!this->varDeclarationsPrefix(&prefix)) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 567 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 568 | } |
Ethan Nicholas | 6c302ba | 2021-09-14 09:16:12 -0400 | [diff] [blame] | 569 | return this->localVarDeclarationEnd(prefix.fPosition, prefix.fModifiers, prefix.fType, |
| 570 | this->text(prefix.fName)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | /* STRUCT IDENTIFIER LBRACE varDeclaration* RBRACE */ |
| 574 | skstd::optional<DSLType> DSLParser::structDeclaration() { |
| 575 | AutoDSLDepth depth(this); |
| 576 | if (!depth.increase()) { |
| 577 | return skstd::nullopt; |
| 578 | } |
| 579 | if (!this->expect(Token::Kind::TK_STRUCT, "'struct'")) { |
| 580 | return skstd::nullopt; |
| 581 | } |
| 582 | Token name; |
| 583 | if (!this->expectIdentifier(&name)) { |
| 584 | return skstd::nullopt; |
| 585 | } |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 586 | if (!this->expect(Token::Kind::TK_LBRACE, "'{'")) { |
| 587 | return skstd::nullopt; |
| 588 | } |
| 589 | SkTArray<DSLField> fields; |
| 590 | while (!this->checkNext(Token::Kind::TK_RBRACE)) { |
| 591 | DSLModifiers modifiers = this->modifiers(); |
John Stiles | 66aa1de | 2021-10-01 13:59:20 -0400 | [diff] [blame] | 592 | skstd::optional<DSLType> type = this->type(&modifiers); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 593 | if (!type) { |
| 594 | return skstd::nullopt; |
| 595 | } |
| 596 | |
| 597 | do { |
| 598 | DSLType actualType = *type; |
| 599 | Token memberName; |
| 600 | if (!this->expectIdentifier(&memberName)) { |
| 601 | return skstd::nullopt; |
| 602 | } |
| 603 | |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 604 | while (this->checkNext(Token::Kind::TK_LBRACKET)) { |
| 605 | actualType = dsl::Array(actualType, this->arraySize(), this->position(memberName)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 606 | if (!this->expect(Token::Kind::TK_RBRACKET, "']'")) { |
| 607 | return skstd::nullopt; |
| 608 | } |
| 609 | } |
Ethan Nicholas | 0c8a598 | 2021-08-31 11:48:54 -0400 | [diff] [blame] | 610 | fields.push_back(DSLField(modifiers, std::move(actualType), this->text(memberName), |
| 611 | this->position(memberName))); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 612 | } while (this->checkNext(Token::Kind::TK_COMMA)); |
| 613 | if (!this->expect(Token::Kind::TK_SEMICOLON, "';'")) { |
| 614 | return skstd::nullopt; |
| 615 | } |
| 616 | } |
| 617 | if (fields.empty()) { |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame] | 618 | this->error(name, "struct '" + this->text(name) + "' must contain at least one field"); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 619 | } |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 620 | return dsl::Struct(this->text(name), SkMakeSpan(fields), this->position(name)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | /* structDeclaration ((IDENTIFIER varDeclarationEnd) | SEMICOLON) */ |
John Stiles | e53c721 | 2021-08-05 10:19:11 -0400 | [diff] [blame] | 624 | SkTArray<dsl::DSLGlobalVar> DSLParser::structVarDeclaration(const DSLModifiers& modifiers) { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 625 | skstd::optional<DSLType> type = this->structDeclaration(); |
| 626 | if (!type) { |
| 627 | return {}; |
| 628 | } |
| 629 | Token name; |
| 630 | if (this->checkNext(Token::Kind::TK_IDENTIFIER, &name)) { |
Ethan Nicholas | 6c302ba | 2021-09-14 09:16:12 -0400 | [diff] [blame] | 631 | this->globalVarDeclarationEnd(this->position(name), modifiers, std::move(*type), |
| 632 | this->text(name)); |
Ethan Nicholas | 3e7cd00 | 2021-09-15 16:19:03 -0400 | [diff] [blame] | 633 | } else { |
| 634 | this->expect(Token::Kind::TK_SEMICOLON, "';'"); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 635 | } |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 636 | return {}; |
| 637 | } |
| 638 | |
| 639 | /* modifiers type IDENTIFIER (LBRACKET INT_LITERAL RBRACKET)? */ |
| 640 | skstd::optional<DSLWrapper<DSLParameter>> DSLParser::parameter() { |
Ethan Nicholas | 6389bfa | 2021-10-01 16:33:43 -0400 | [diff] [blame] | 641 | DSLModifiers modifiers = this->modifiers(); |
John Stiles | 66aa1de | 2021-10-01 13:59:20 -0400 | [diff] [blame] | 642 | skstd::optional<DSLType> type = this->type(&modifiers); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 643 | if (!type) { |
| 644 | return skstd::nullopt; |
| 645 | } |
| 646 | Token name; |
| 647 | if (!this->expectIdentifier(&name)) { |
| 648 | return skstd::nullopt; |
| 649 | } |
| 650 | while (this->checkNext(Token::Kind::TK_LBRACKET)) { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 651 | Token sizeToken; |
| 652 | if (!this->expect(Token::Kind::TK_INT_LITERAL, "a positive integer", &sizeToken)) { |
| 653 | return skstd::nullopt; |
| 654 | } |
| 655 | skstd::string_view arraySizeFrag = this->text(sizeToken); |
| 656 | SKSL_INT arraySize; |
| 657 | if (!SkSL::stoi(arraySizeFrag, &arraySize)) { |
| 658 | this->error(sizeToken, "array size is too large: " + arraySizeFrag); |
Ethan Nicholas | 709ecd5 | 2021-09-01 15:48:42 -0400 | [diff] [blame] | 659 | arraySize = 1; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 660 | } |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 661 | type = Array(*type, arraySize, this->position(name)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 662 | if (!this->expect(Token::Kind::TK_RBRACKET, "']'")) { |
| 663 | return skstd::nullopt; |
| 664 | } |
| 665 | } |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 666 | return {{DSLParameter(modifiers, *type, this->text(name), this->position(name))}}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | /** EQ INT_LITERAL */ |
| 670 | int DSLParser::layoutInt() { |
| 671 | if (!this->expect(Token::Kind::TK_EQ, "'='")) { |
| 672 | return -1; |
| 673 | } |
| 674 | Token resultToken; |
| 675 | if (!this->expect(Token::Kind::TK_INT_LITERAL, "a non-negative integer", &resultToken)) { |
| 676 | return -1; |
| 677 | } |
| 678 | skstd::string_view resultFrag = this->text(resultToken); |
| 679 | SKSL_INT resultValue; |
| 680 | if (!SkSL::stoi(resultFrag, &resultValue)) { |
| 681 | this->error(resultToken, "value in layout is too large: " + resultFrag); |
| 682 | return -1; |
| 683 | } |
| 684 | return resultValue; |
| 685 | } |
| 686 | |
| 687 | /** EQ IDENTIFIER */ |
| 688 | skstd::string_view DSLParser::layoutIdentifier() { |
| 689 | if (!this->expect(Token::Kind::TK_EQ, "'='")) { |
| 690 | return {}; |
| 691 | } |
| 692 | Token resultToken; |
| 693 | if (!this->expectIdentifier(&resultToken)) { |
| 694 | return {}; |
| 695 | } |
| 696 | return this->text(resultToken); |
| 697 | } |
| 698 | |
| 699 | /* LAYOUT LPAREN IDENTIFIER (EQ INT_LITERAL)? (COMMA IDENTIFIER (EQ INT_LITERAL)?)* RPAREN */ |
| 700 | DSLLayout DSLParser::layout() { |
| 701 | DSLLayout result; |
| 702 | if (this->checkNext(Token::Kind::TK_LAYOUT)) { |
| 703 | if (!this->expect(Token::Kind::TK_LPAREN, "'('")) { |
| 704 | return result; |
| 705 | } |
| 706 | for (;;) { |
| 707 | Token t = this->nextToken(); |
| 708 | String text(this->text(t)); |
| 709 | auto found = layoutTokens->find(text); |
| 710 | if (found != layoutTokens->end()) { |
| 711 | switch (found->second) { |
| 712 | case LayoutToken::ORIGIN_UPPER_LEFT: |
Ethan Nicholas | df803aa | 2021-08-29 14:22:45 -0400 | [diff] [blame] | 713 | result.originUpperLeft(this->position(t)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 714 | break; |
| 715 | case LayoutToken::PUSH_CONSTANT: |
Ethan Nicholas | df803aa | 2021-08-29 14:22:45 -0400 | [diff] [blame] | 716 | result.pushConstant(this->position(t)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 717 | break; |
| 718 | case LayoutToken::BLEND_SUPPORT_ALL_EQUATIONS: |
Ethan Nicholas | df803aa | 2021-08-29 14:22:45 -0400 | [diff] [blame] | 719 | result.blendSupportAllEquations(this->position(t)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 720 | break; |
| 721 | case LayoutToken::SRGB_UNPREMUL: |
Ethan Nicholas | df803aa | 2021-08-29 14:22:45 -0400 | [diff] [blame] | 722 | result.srgbUnpremul(this->position(t)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 723 | break; |
| 724 | case LayoutToken::LOCATION: |
Ethan Nicholas | df803aa | 2021-08-29 14:22:45 -0400 | [diff] [blame] | 725 | result.location(this->layoutInt(), this->position(t)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 726 | break; |
| 727 | case LayoutToken::OFFSET: |
Ethan Nicholas | df803aa | 2021-08-29 14:22:45 -0400 | [diff] [blame] | 728 | result.offset(this->layoutInt(), this->position(t)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 729 | break; |
| 730 | case LayoutToken::BINDING: |
Ethan Nicholas | df803aa | 2021-08-29 14:22:45 -0400 | [diff] [blame] | 731 | result.binding(this->layoutInt(), this->position(t)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 732 | break; |
| 733 | case LayoutToken::INDEX: |
Ethan Nicholas | df803aa | 2021-08-29 14:22:45 -0400 | [diff] [blame] | 734 | result.index(this->layoutInt(), this->position(t)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 735 | break; |
| 736 | case LayoutToken::SET: |
Ethan Nicholas | df803aa | 2021-08-29 14:22:45 -0400 | [diff] [blame] | 737 | result.set(this->layoutInt(), this->position(t)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 738 | break; |
| 739 | case LayoutToken::BUILTIN: |
Ethan Nicholas | df803aa | 2021-08-29 14:22:45 -0400 | [diff] [blame] | 740 | result.builtin(this->layoutInt(), this->position(t)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 741 | break; |
| 742 | case LayoutToken::INPUT_ATTACHMENT_INDEX: |
Ethan Nicholas | df803aa | 2021-08-29 14:22:45 -0400 | [diff] [blame] | 743 | result.inputAttachmentIndex(this->layoutInt(), this->position(t)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 744 | break; |
| 745 | default: |
| 746 | this->error(t, "'" + text + "' is not a valid layout qualifier"); |
| 747 | break; |
| 748 | } |
| 749 | } else { |
| 750 | this->error(t, "'" + text + "' is not a valid layout qualifier"); |
| 751 | } |
| 752 | if (this->checkNext(Token::Kind::TK_RPAREN)) { |
| 753 | break; |
| 754 | } |
| 755 | if (!this->expect(Token::Kind::TK_COMMA, "','")) { |
| 756 | break; |
| 757 | } |
| 758 | } |
| 759 | } |
| 760 | return result; |
| 761 | } |
| 762 | |
| 763 | /* layout? (UNIFORM | CONST | IN | OUT | INOUT | LOWP | MEDIUMP | HIGHP | FLAT | NOPERSPECTIVE | |
| 764 | VARYING | INLINE)* */ |
| 765 | DSLModifiers DSLParser::modifiers() { |
| 766 | DSLLayout layout = this->layout(); |
| 767 | int flags = 0; |
| 768 | for (;;) { |
| 769 | // TODO(ethannicholas): handle duplicate / incompatible flags |
| 770 | int tokenFlag = parse_modifier_token(peek().fKind); |
| 771 | if (!tokenFlag) { |
| 772 | break; |
| 773 | } |
| 774 | flags |= tokenFlag; |
| 775 | this->nextToken(); |
| 776 | } |
| 777 | return DSLModifiers(std::move(layout), flags); |
| 778 | } |
| 779 | |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 780 | /* ifStatement | forStatement | doStatement | whileStatement | block | expression */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 781 | DSLStatement DSLParser::statement() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 782 | Token start = this->nextToken(); |
| 783 | AutoDSLDepth depth(this); |
| 784 | if (!depth.increase()) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 785 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 786 | } |
| 787 | this->pushback(start); |
| 788 | switch (start.fKind) { |
| 789 | case Token::Kind::TK_IF: // fall through |
| 790 | case Token::Kind::TK_STATIC_IF: |
| 791 | return this->ifStatement(); |
| 792 | case Token::Kind::TK_FOR: |
| 793 | return this->forStatement(); |
| 794 | case Token::Kind::TK_DO: |
| 795 | return this->doStatement(); |
| 796 | case Token::Kind::TK_WHILE: |
| 797 | return this->whileStatement(); |
| 798 | case Token::Kind::TK_SWITCH: // fall through |
| 799 | case Token::Kind::TK_STATIC_SWITCH: |
| 800 | return this->switchStatement(); |
| 801 | case Token::Kind::TK_RETURN: |
| 802 | return this->returnStatement(); |
| 803 | case Token::Kind::TK_BREAK: |
| 804 | return this->breakStatement(); |
| 805 | case Token::Kind::TK_CONTINUE: |
| 806 | return this->continueStatement(); |
| 807 | case Token::Kind::TK_DISCARD: |
| 808 | return this->discardStatement(); |
| 809 | case Token::Kind::TK_LBRACE: { |
| 810 | skstd::optional<DSLBlock> result = this->block(); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 811 | return result ? DSLStatement(std::move(*result)) : DSLStatement(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 812 | } |
| 813 | case Token::Kind::TK_SEMICOLON: |
| 814 | this->nextToken(); |
| 815 | return dsl::Block(); |
John Stiles | 0201431 | 2021-08-04 16:03:12 -0400 | [diff] [blame] | 816 | case Token::Kind::TK_HIGHP: |
| 817 | case Token::Kind::TK_MEDIUMP: |
| 818 | case Token::Kind::TK_LOWP: |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 819 | case Token::Kind::TK_CONST: |
| 820 | case Token::Kind::TK_IDENTIFIER: |
| 821 | return this->varDeclarationsOrExpressionStatement(); |
| 822 | default: |
| 823 | return this->expressionStatement(); |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | /* IDENTIFIER(type) (LBRACKET intLiteral? RBRACKET)* QUESTION? */ |
John Stiles | 66aa1de | 2021-10-01 13:59:20 -0400 | [diff] [blame] | 828 | skstd::optional<DSLType> DSLParser::type(DSLModifiers* modifiers) { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 829 | Token type; |
| 830 | if (!this->expect(Token::Kind::TK_IDENTIFIER, "a type", &type)) { |
| 831 | return skstd::nullopt; |
| 832 | } |
| 833 | if (!IsType(this->text(type))) { |
| 834 | this->error(type, ("no type named '" + this->text(type) + "'").c_str()); |
| 835 | return skstd::nullopt; |
| 836 | } |
Ethan Nicholas | a248a9a | 2021-09-01 16:40:25 -0400 | [diff] [blame] | 837 | DSLType result(this->text(type), modifiers, this->position(type)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 838 | while (this->checkNext(Token::Kind::TK_LBRACKET)) { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 839 | if (this->peek().fKind != Token::Kind::TK_RBRACKET) { |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 840 | result = Array(result, this->arraySize(), this->position(type)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 841 | } else { |
| 842 | this->error(this->peek(), "expected array dimension"); |
| 843 | } |
| 844 | this->expect(Token::Kind::TK_RBRACKET, "']'"); |
| 845 | } |
| 846 | return result; |
| 847 | } |
| 848 | |
| 849 | /* IDENTIFIER LBRACE |
| 850 | varDeclaration+ |
| 851 | RBRACE (IDENTIFIER (LBRACKET expression? RBRACKET)*)? SEMICOLON */ |
John Stiles | e53c721 | 2021-08-05 10:19:11 -0400 | [diff] [blame] | 852 | bool DSLParser::interfaceBlock(const dsl::DSLModifiers& modifiers) { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 853 | Token typeName; |
| 854 | if (!this->expectIdentifier(&typeName)) { |
| 855 | return false; |
| 856 | } |
| 857 | if (peek().fKind != Token::Kind::TK_LBRACE) { |
| 858 | // we only get into interfaceBlock if we found a top-level identifier which was not a type. |
| 859 | // 99% of the time, the user was not actually intending to create an interface block, so |
| 860 | // it's better to report it as an unknown type |
| 861 | this->error(typeName, "no type named '" + this->text(typeName) + "'"); |
| 862 | return false; |
| 863 | } |
| 864 | this->nextToken(); |
| 865 | SkTArray<dsl::Field> fields; |
| 866 | while (!this->checkNext(Token::Kind::TK_RBRACE)) { |
Ethan Nicholas | 5c4463e | 2021-08-29 14:31:19 -0400 | [diff] [blame] | 867 | DSLModifiers fieldModifiers = this->modifiers(); |
John Stiles | 66aa1de | 2021-10-01 13:59:20 -0400 | [diff] [blame] | 868 | skstd::optional<dsl::DSLType> type = this->type(&fieldModifiers); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 869 | if (!type) { |
| 870 | return false; |
| 871 | } |
| 872 | do { |
| 873 | Token fieldName; |
| 874 | if (!this->expect(Token::Kind::TK_IDENTIFIER, "an identifier", &fieldName)) { |
| 875 | return false; |
| 876 | } |
| 877 | DSLType actualType = *type; |
| 878 | if (this->checkNext(Token::Kind::TK_LBRACKET)) { |
| 879 | Token sizeToken = this->peek(); |
| 880 | if (sizeToken.fKind != Token::Kind::TK_RBRACKET) { |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 881 | actualType = Array(std::move(actualType), this->arraySize(), |
| 882 | this->position(typeName)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 883 | } else { |
| 884 | this->error(sizeToken, "unsized arrays are not permitted"); |
| 885 | } |
| 886 | this->expect(Token::Kind::TK_RBRACKET, "']'"); |
| 887 | } |
| 888 | if (!this->expect(Token::Kind::TK_SEMICOLON, "';'")) { |
| 889 | return false; |
| 890 | } |
Ethan Nicholas | 2763323 | 2021-08-29 13:51:44 -0400 | [diff] [blame] | 891 | fields.push_back(dsl::Field(fieldModifiers, std::move(actualType), |
| 892 | this->text(fieldName), this->position(fieldName))); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 893 | } |
| 894 | while (this->checkNext(Token::Kind::TK_COMMA)); |
| 895 | } |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 896 | skstd::string_view instanceName; |
| 897 | Token instanceNameToken; |
| 898 | SKSL_INT arraySize = 0; |
| 899 | if (this->checkNext(Token::Kind::TK_IDENTIFIER, &instanceNameToken)) { |
| 900 | instanceName = this->text(instanceNameToken); |
| 901 | if (this->checkNext(Token::Kind::TK_LBRACKET)) { |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 902 | arraySize = this->arraySize(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 903 | this->expect(Token::Kind::TK_RBRACKET, "']'"); |
| 904 | } |
| 905 | } |
Ethan Nicholas | e110f6e | 2021-08-29 14:03:06 -0400 | [diff] [blame] | 906 | this->expect(Token::Kind::TK_SEMICOLON, "';'"); |
John Stiles | d0665d9 | 2021-09-17 09:14:28 -0400 | [diff] [blame] | 907 | if (fields.empty()) { |
| 908 | this->error(typeName, "interface block '" + this->text(typeName) + |
| 909 | "' must contain at least one member"); |
| 910 | } else { |
| 911 | dsl::InterfaceBlock(modifiers, this->text(typeName), std::move(fields), instanceName, |
| 912 | arraySize, this->position(typeName)); |
| 913 | } |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 914 | return true; |
| 915 | } |
| 916 | |
| 917 | /* IF LPAREN expression RPAREN statement (ELSE statement)? */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 918 | DSLStatement DSLParser::ifStatement() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 919 | Token start; |
| 920 | bool isStatic = this->checkNext(Token::Kind::TK_STATIC_IF, &start); |
| 921 | if (!isStatic && !this->expect(Token::Kind::TK_IF, "'if'", &start)) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 922 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 923 | } |
| 924 | if (!this->expect(Token::Kind::TK_LPAREN, "'('")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 925 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 926 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 927 | DSLExpression test = this->expression(); |
| 928 | if (!test.hasValue()) { |
| 929 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 930 | } |
| 931 | if (!this->expect(Token::Kind::TK_RPAREN, "')'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 932 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 933 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 934 | DSLStatement ifTrue = this->statement(); |
| 935 | if (!ifTrue.hasValue()) { |
| 936 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 937 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 938 | DSLStatement ifFalse; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 939 | if (this->checkNext(Token::Kind::TK_ELSE)) { |
| 940 | ifFalse = this->statement(); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 941 | if (!ifFalse.hasValue()) { |
| 942 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 943 | } |
| 944 | } |
| 945 | if (isStatic) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 946 | return StaticIf(std::move(test), std::move(ifTrue), |
| 947 | ifFalse.hasValue() ? std::move(ifFalse) : DSLStatement(), this->position(start)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 948 | } else { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 949 | return If(std::move(test), std::move(ifTrue), |
| 950 | ifFalse.hasValue() ? std::move(ifFalse) : DSLStatement(), this->position(start)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 951 | } |
| 952 | } |
| 953 | |
| 954 | /* DO statement WHILE LPAREN expression RPAREN SEMICOLON */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 955 | DSLStatement DSLParser::doStatement() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 956 | Token start; |
| 957 | if (!this->expect(Token::Kind::TK_DO, "'do'", &start)) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 958 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 959 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 960 | DSLStatement statement = this->statement(); |
| 961 | if (!statement.hasValue()) { |
| 962 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 963 | } |
| 964 | if (!this->expect(Token::Kind::TK_WHILE, "'while'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 965 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 966 | } |
| 967 | if (!this->expect(Token::Kind::TK_LPAREN, "'('")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 968 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 969 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 970 | DSLExpression test = this->expression(); |
| 971 | if (!test.hasValue()) { |
| 972 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 973 | } |
| 974 | if (!this->expect(Token::Kind::TK_RPAREN, "')'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 975 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 976 | } |
| 977 | if (!this->expect(Token::Kind::TK_SEMICOLON, "';'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 978 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 979 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 980 | return Do(std::move(statement), std::move(test), this->position(start)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | /* WHILE LPAREN expression RPAREN STATEMENT */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 984 | DSLStatement DSLParser::whileStatement() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 985 | Token start; |
| 986 | if (!this->expect(Token::Kind::TK_WHILE, "'while'", &start)) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 987 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 988 | } |
| 989 | if (!this->expect(Token::Kind::TK_LPAREN, "'('")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 990 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 991 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 992 | DSLExpression test = this->expression(); |
| 993 | if (!test.hasValue()) { |
| 994 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 995 | } |
| 996 | if (!this->expect(Token::Kind::TK_RPAREN, "')'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 997 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 998 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 999 | DSLStatement statement = this->statement(); |
| 1000 | if (!statement.hasValue()) { |
| 1001 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1002 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1003 | return While(std::move(test), std::move(statement), this->position(start)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | /* CASE expression COLON statement* */ |
| 1007 | skstd::optional<DSLCase> DSLParser::switchCase() { |
| 1008 | Token start; |
| 1009 | if (!this->expect(Token::Kind::TK_CASE, "'case'", &start)) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1010 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1011 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1012 | DSLExpression value = this->expression(); |
| 1013 | if (!value.hasValue()) { |
| 1014 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1015 | } |
| 1016 | if (!this->expect(Token::Kind::TK_COLON, "':'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1017 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1018 | } |
| 1019 | SkTArray<DSLStatement> statements; |
| 1020 | while (this->peek().fKind != Token::Kind::TK_RBRACE && |
| 1021 | this->peek().fKind != Token::Kind::TK_CASE && |
| 1022 | this->peek().fKind != Token::Kind::TK_DEFAULT) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1023 | DSLStatement s = this->statement(); |
| 1024 | if (!s.hasValue()) { |
| 1025 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1026 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1027 | statements.push_back(std::move(s)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1028 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1029 | return DSLCase(std::move(value), std::move(statements)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | /* SWITCH LPAREN expression RPAREN LBRACE switchCase* (DEFAULT COLON statement*)? RBRACE */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1033 | DSLStatement DSLParser::switchStatement() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1034 | Token start; |
| 1035 | bool isStatic = this->checkNext(Token::Kind::TK_STATIC_SWITCH, &start); |
| 1036 | if (!isStatic && !this->expect(Token::Kind::TK_SWITCH, "'switch'", &start)) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1037 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1038 | } |
| 1039 | if (!this->expect(Token::Kind::TK_LPAREN, "'('")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1040 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1041 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1042 | DSLExpression value = this->expression(); |
| 1043 | if (!value.hasValue()) { |
| 1044 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1045 | } |
| 1046 | if (!this->expect(Token::Kind::TK_RPAREN, "')'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1047 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1048 | } |
| 1049 | if (!this->expect(Token::Kind::TK_LBRACE, "'{'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1050 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1051 | } |
| 1052 | SkTArray<DSLCase> cases; |
| 1053 | while (this->peek().fKind == Token::Kind::TK_CASE) { |
| 1054 | skstd::optional<DSLCase> c = this->switchCase(); |
| 1055 | if (!c) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1056 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1057 | } |
| 1058 | cases.push_back(std::move(*c)); |
| 1059 | } |
| 1060 | // Requiring default: to be last (in defiance of C and GLSL) was a deliberate decision. Other |
| 1061 | // parts of the compiler may rely upon this assumption. |
| 1062 | if (this->peek().fKind == Token::Kind::TK_DEFAULT) { |
| 1063 | SkTArray<DSLStatement> statements; |
| 1064 | Token defaultStart; |
| 1065 | SkAssertResult(this->expect(Token::Kind::TK_DEFAULT, "'default'", &defaultStart)); |
| 1066 | if (!this->expect(Token::Kind::TK_COLON, "':'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1067 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1068 | } |
| 1069 | while (this->peek().fKind != Token::Kind::TK_RBRACE) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1070 | DSLStatement s = this->statement(); |
| 1071 | if (!s.hasValue()) { |
| 1072 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1073 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1074 | statements.push_back(std::move(s)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1075 | } |
Ethan Nicholas | 360db87 | 2021-09-03 17:00:09 -0400 | [diff] [blame] | 1076 | cases.push_back(DSLCase(DSLExpression(), std::move(statements), this->position(start))); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1077 | } |
| 1078 | if (!this->expect(Token::Kind::TK_RBRACE, "'}'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1079 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1080 | } |
| 1081 | if (isStatic) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1082 | return StaticSwitch(std::move(value), std::move(cases), this->position(start)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1083 | } else { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1084 | return Switch(std::move(value), std::move(cases), this->position(start)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | /* FOR LPAREN (declaration | expression)? SEMICOLON expression? SEMICOLON expression? RPAREN |
| 1089 | STATEMENT */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1090 | dsl::DSLStatement DSLParser::forStatement() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1091 | Token start; |
| 1092 | if (!this->expect(Token::Kind::TK_FOR, "'for'", &start)) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1093 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1094 | } |
| 1095 | if (!this->expect(Token::Kind::TK_LPAREN, "'('")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1096 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1097 | } |
| 1098 | AutoDSLSymbolTable symbols; |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1099 | dsl::DSLStatement initializer; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1100 | Token nextToken = this->peek(); |
| 1101 | if (nextToken.fKind == Token::Kind::TK_SEMICOLON) { |
| 1102 | // An empty init-statement. |
| 1103 | this->nextToken(); |
| 1104 | } else { |
| 1105 | // The init-statement must be an expression or variable declaration. |
| 1106 | initializer = this->varDeclarationsOrExpressionStatement(); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1107 | if (!initializer.hasValue()) { |
| 1108 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1109 | } |
| 1110 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1111 | dsl::DSLExpression test; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1112 | if (this->peek().fKind != Token::Kind::TK_SEMICOLON) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1113 | dsl::DSLExpression testValue = this->expression(); |
| 1114 | if (!testValue.hasValue()) { |
| 1115 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1116 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1117 | test.swap(testValue); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1118 | } |
| 1119 | if (!this->expect(Token::Kind::TK_SEMICOLON, "';'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1120 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1121 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1122 | dsl::DSLExpression next; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1123 | if (this->peek().fKind != Token::Kind::TK_RPAREN) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1124 | dsl::DSLExpression nextValue = this->expression(); |
| 1125 | if (!nextValue.hasValue()) { |
| 1126 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1127 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1128 | next.swap(nextValue); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1129 | } |
| 1130 | if (!this->expect(Token::Kind::TK_RPAREN, "')'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1131 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1132 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1133 | dsl::DSLStatement statement = this->statement(); |
| 1134 | if (!statement.hasValue()) { |
| 1135 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1136 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1137 | return For(initializer.hasValue() ? std::move(initializer) : DSLStatement(), |
| 1138 | test.hasValue() ? std::move(test) : DSLExpression(), |
| 1139 | next.hasValue() ? std::move(next) : DSLExpression(), |
| 1140 | std::move(statement), |
Ethan Nicholas | 360db87 | 2021-09-03 17:00:09 -0400 | [diff] [blame] | 1141 | this->position(start)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | /* RETURN expression? SEMICOLON */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1145 | DSLStatement DSLParser::returnStatement() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1146 | Token start; |
| 1147 | if (!this->expect(Token::Kind::TK_RETURN, "'return'", &start)) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1148 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1149 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1150 | DSLExpression expression; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1151 | if (this->peek().fKind != Token::Kind::TK_SEMICOLON) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1152 | DSLExpression next = this->expression(); |
| 1153 | if (!next.hasValue()) { |
| 1154 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1155 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1156 | expression.swap(next); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1157 | } |
| 1158 | if (!this->expect(Token::Kind::TK_SEMICOLON, "';'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1159 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1160 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1161 | return Return(expression.hasValue() ? std::move(expression) : DSLExpression(), |
| 1162 | this->position(start)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | /* BREAK SEMICOLON */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1166 | DSLStatement DSLParser::breakStatement() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1167 | Token start; |
| 1168 | if (!this->expect(Token::Kind::TK_BREAK, "'break'", &start)) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1169 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1170 | } |
| 1171 | if (!this->expect(Token::Kind::TK_SEMICOLON, "';'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1172 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1173 | } |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 1174 | return Break(this->position(start)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | /* CONTINUE SEMICOLON */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1178 | DSLStatement DSLParser::continueStatement() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1179 | Token start; |
| 1180 | if (!this->expect(Token::Kind::TK_CONTINUE, "'continue'", &start)) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1181 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1182 | } |
| 1183 | if (!this->expect(Token::Kind::TK_SEMICOLON, "';'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1184 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1185 | } |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 1186 | return Continue(this->position(start)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | /* DISCARD SEMICOLON */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1190 | DSLStatement DSLParser::discardStatement() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1191 | Token start; |
| 1192 | if (!this->expect(Token::Kind::TK_DISCARD, "'continue'", &start)) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1193 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1194 | } |
| 1195 | if (!this->expect(Token::Kind::TK_SEMICOLON, "';'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1196 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1197 | } |
Ethan Nicholas | 360db87 | 2021-09-03 17:00:09 -0400 | [diff] [blame] | 1198 | return Discard(this->position(start)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | /* LBRACE statement* RBRACE */ |
| 1202 | skstd::optional<DSLBlock> DSLParser::block() { |
| 1203 | Token start; |
| 1204 | if (!this->expect(Token::Kind::TK_LBRACE, "'{'", &start)) { |
| 1205 | return skstd::nullopt; |
| 1206 | } |
| 1207 | AutoDSLDepth depth(this); |
| 1208 | if (!depth.increase()) { |
| 1209 | return skstd::nullopt; |
| 1210 | } |
| 1211 | AutoDSLSymbolTable symbols; |
Ethan Nicholas | 96dbf74 | 2021-09-10 15:59:11 -0400 | [diff] [blame] | 1212 | StatementArray statements; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1213 | for (;;) { |
| 1214 | switch (this->peek().fKind) { |
| 1215 | case Token::Kind::TK_RBRACE: |
| 1216 | this->nextToken(); |
| 1217 | return DSLBlock(std::move(statements), CurrentSymbolTable()); |
| 1218 | case Token::Kind::TK_END_OF_FILE: |
| 1219 | this->error(this->peek(), "expected '}', but found end of file"); |
| 1220 | return skstd::nullopt; |
| 1221 | default: { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1222 | DSLStatement statement = this->statement(); |
| 1223 | if (!statement.hasValue()) { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1224 | return skstd::nullopt; |
| 1225 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1226 | statements.push_back(statement.release()); |
Ethan Nicholas | b13f369 | 2021-09-10 16:49:42 -0400 | [diff] [blame] | 1227 | break; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1228 | } |
| 1229 | } |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | /* expression SEMICOLON */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1234 | DSLStatement DSLParser::expressionStatement() { |
| 1235 | DSLExpression expr = this->expression(); |
| 1236 | if (expr.hasValue()) { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1237 | if (!this->expect(Token::Kind::TK_SEMICOLON, "';'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1238 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1239 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1240 | return DSLStatement(std::move(expr)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1241 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1242 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | /* assignmentExpression (COMMA assignmentExpression)* */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1246 | DSLExpression DSLParser::expression() { |
| 1247 | DSLExpression result = this->assignmentExpression(); |
| 1248 | if (!result.hasValue()) { |
| 1249 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1250 | } |
| 1251 | Token t; |
| 1252 | AutoDSLDepth depth(this); |
| 1253 | while (this->checkNext(Token::Kind::TK_COMMA, &t)) { |
| 1254 | if (!depth.increase()) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1255 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1256 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1257 | DSLExpression right = this->assignmentExpression(); |
| 1258 | if (!right.hasValue()) { |
| 1259 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1260 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1261 | DSLExpression next = dsl::operator,(std::move(result), std::move(right)); |
| 1262 | result.swap(next); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1263 | } |
| 1264 | return result; |
| 1265 | } |
| 1266 | |
| 1267 | #define OPERATOR_RIGHT(op, exprType) \ |
| 1268 | do { \ |
| 1269 | this->nextToken(); \ |
| 1270 | if (!depth.increase()) { \ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1271 | return {}; \ |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1272 | } \ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1273 | DSLExpression right = this->exprType(); \ |
| 1274 | if (!right.hasValue()) { \ |
| 1275 | return {}; \ |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1276 | } \ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1277 | DSLExpression next = std::move(result) op std::move(right); \ |
| 1278 | result.swap(next); \ |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1279 | } while (false) |
| 1280 | |
| 1281 | /* ternaryExpression ((EQEQ | STAREQ | SLASHEQ | PERCENTEQ | PLUSEQ | MINUSEQ | SHLEQ | SHREQ | |
| 1282 | BITWISEANDEQ | BITWISEXOREQ | BITWISEOREQ | LOGICALANDEQ | LOGICALXOREQ | LOGICALOREQ) |
| 1283 | assignmentExpression)* |
| 1284 | */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1285 | DSLExpression DSLParser::assignmentExpression() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1286 | AutoDSLDepth depth(this); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1287 | DSLExpression result = this->ternaryExpression(); |
| 1288 | if (!result.hasValue()) { |
| 1289 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1290 | } |
| 1291 | for (;;) { |
| 1292 | switch (this->peek().fKind) { |
| 1293 | case Token::Kind::TK_EQ: OPERATOR_RIGHT(=, assignmentExpression); break; |
| 1294 | case Token::Kind::TK_STAREQ: OPERATOR_RIGHT(*=, assignmentExpression); break; |
| 1295 | case Token::Kind::TK_SLASHEQ: OPERATOR_RIGHT(/=, assignmentExpression); break; |
| 1296 | case Token::Kind::TK_PERCENTEQ: OPERATOR_RIGHT(%=, assignmentExpression); break; |
| 1297 | case Token::Kind::TK_PLUSEQ: OPERATOR_RIGHT(+=, assignmentExpression); break; |
| 1298 | case Token::Kind::TK_MINUSEQ: OPERATOR_RIGHT(-=, assignmentExpression); break; |
| 1299 | case Token::Kind::TK_SHLEQ: OPERATOR_RIGHT(<<=, assignmentExpression); break; |
| 1300 | case Token::Kind::TK_SHREQ: OPERATOR_RIGHT(>>=, assignmentExpression); break; |
| 1301 | case Token::Kind::TK_BITWISEANDEQ: OPERATOR_RIGHT(&=, assignmentExpression); break; |
| 1302 | case Token::Kind::TK_BITWISEXOREQ: OPERATOR_RIGHT(^=, assignmentExpression); break; |
| 1303 | case Token::Kind::TK_BITWISEOREQ: OPERATOR_RIGHT(|=, assignmentExpression); break; |
| 1304 | default: |
| 1305 | return result; |
| 1306 | } |
| 1307 | } |
| 1308 | } |
| 1309 | |
| 1310 | /* logicalOrExpression ('?' expression ':' assignmentExpression)? */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1311 | DSLExpression DSLParser::ternaryExpression() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1312 | AutoDSLDepth depth(this); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1313 | DSLExpression base = this->logicalOrExpression(); |
| 1314 | if (!base.hasValue()) { |
| 1315 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1316 | } |
| 1317 | if (this->checkNext(Token::Kind::TK_QUESTION)) { |
| 1318 | if (!depth.increase()) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1319 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1320 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1321 | DSLExpression trueExpr = this->expression(); |
| 1322 | if (!trueExpr.hasValue()) { |
| 1323 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1324 | } |
| 1325 | if (this->expect(Token::Kind::TK_COLON, "':'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1326 | DSLExpression falseExpr = this->assignmentExpression(); |
| 1327 | if (!falseExpr.hasValue()) { |
| 1328 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1329 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1330 | return Select(std::move(base), std::move(trueExpr), std::move(falseExpr)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1331 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1332 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1333 | } |
| 1334 | return base; |
| 1335 | } |
| 1336 | |
| 1337 | /* logicalXorExpression (LOGICALOR logicalXorExpression)* */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1338 | DSLExpression DSLParser::logicalOrExpression() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1339 | AutoDSLDepth depth(this); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1340 | DSLExpression result = this->logicalXorExpression(); |
| 1341 | if (!result.hasValue()) { |
| 1342 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1343 | } |
| 1344 | while (this->peek().fKind == Token::Kind::TK_LOGICALOR) { |
| 1345 | OPERATOR_RIGHT(||, logicalXorExpression); |
| 1346 | } |
| 1347 | return result; |
| 1348 | } |
| 1349 | |
| 1350 | /* logicalAndExpression (LOGICALXOR logicalAndExpression)* */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1351 | DSLExpression DSLParser::logicalXorExpression() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1352 | AutoDSLDepth depth(this); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1353 | DSLExpression result = this->logicalAndExpression(); |
| 1354 | if (!result.hasValue()) { |
| 1355 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1356 | } |
| 1357 | while (this->checkNext(Token::Kind::TK_LOGICALXOR)) { |
| 1358 | if (!depth.increase()) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1359 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1360 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1361 | DSLExpression right = this->logicalAndExpression(); |
| 1362 | if (!right.hasValue()) { |
| 1363 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1364 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1365 | DSLExpression next = LogicalXor(std::move(result), std::move(right)); |
| 1366 | result.swap(next); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1367 | } |
| 1368 | return result; |
| 1369 | } |
| 1370 | |
| 1371 | /* bitwiseOrExpression (LOGICALAND bitwiseOrExpression)* */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1372 | DSLExpression DSLParser::logicalAndExpression() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1373 | AutoDSLDepth depth(this); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1374 | DSLExpression result = this->bitwiseOrExpression(); |
| 1375 | if (!result.hasValue()) { |
| 1376 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1377 | } |
| 1378 | while (this->peek().fKind == Token::Kind::TK_LOGICALAND) { |
| 1379 | OPERATOR_RIGHT(&&, bitwiseOrExpression); |
| 1380 | } |
| 1381 | return result; |
| 1382 | } |
| 1383 | |
| 1384 | /* bitwiseXorExpression (BITWISEOR bitwiseXorExpression)* */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1385 | DSLExpression DSLParser::bitwiseOrExpression() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1386 | AutoDSLDepth depth(this); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1387 | DSLExpression result = this->bitwiseXorExpression(); |
| 1388 | if (!result.hasValue()) { |
| 1389 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1390 | } |
| 1391 | while (this->peek().fKind == Token::Kind::TK_BITWISEOR) { |
| 1392 | OPERATOR_RIGHT(|, bitwiseXorExpression); |
| 1393 | } |
| 1394 | return result; |
| 1395 | } |
| 1396 | |
| 1397 | /* bitwiseAndExpression (BITWISEXOR bitwiseAndExpression)* */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1398 | DSLExpression DSLParser::bitwiseXorExpression() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1399 | AutoDSLDepth depth(this); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1400 | DSLExpression result = this->bitwiseAndExpression(); |
| 1401 | if (!result.hasValue()) { |
| 1402 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1403 | } |
| 1404 | while (this->peek().fKind == Token::Kind::TK_BITWISEXOR) { |
| 1405 | OPERATOR_RIGHT(^, bitwiseAndExpression); |
| 1406 | } |
| 1407 | return result; |
| 1408 | } |
| 1409 | |
| 1410 | /* equalityExpression (BITWISEAND equalityExpression)* */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1411 | DSLExpression DSLParser::bitwiseAndExpression() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1412 | AutoDSLDepth depth(this); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1413 | DSLExpression result = this->equalityExpression(); |
| 1414 | if (!result.hasValue()) { |
| 1415 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1416 | } |
| 1417 | while (this->peek().fKind == Token::Kind::TK_BITWISEAND) { |
| 1418 | OPERATOR_RIGHT(&, equalityExpression); |
| 1419 | } |
| 1420 | return result; |
| 1421 | } |
| 1422 | |
| 1423 | /* relationalExpression ((EQEQ | NEQ) relationalExpression)* */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1424 | DSLExpression DSLParser::equalityExpression() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1425 | AutoDSLDepth depth(this); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1426 | DSLExpression result = this->relationalExpression(); |
| 1427 | if (!result.hasValue()) { |
| 1428 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1429 | } |
| 1430 | for (;;) { |
| 1431 | switch (this->peek().fKind) { |
| 1432 | case Token::Kind::TK_EQEQ: OPERATOR_RIGHT(==, relationalExpression); break; |
| 1433 | case Token::Kind::TK_NEQ: OPERATOR_RIGHT(!=, relationalExpression); break; |
| 1434 | default: return result; |
| 1435 | } |
| 1436 | } |
| 1437 | } |
| 1438 | |
| 1439 | /* shiftExpression ((LT | GT | LTEQ | GTEQ) shiftExpression)* */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1440 | DSLExpression DSLParser::relationalExpression() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1441 | AutoDSLDepth depth(this); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1442 | DSLExpression result = this->shiftExpression(); |
| 1443 | if (!result.hasValue()) { |
| 1444 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1445 | } |
| 1446 | for (;;) { |
| 1447 | switch (this->peek().fKind) { |
| 1448 | case Token::Kind::TK_LT: OPERATOR_RIGHT(<, shiftExpression); break; |
| 1449 | case Token::Kind::TK_GT: OPERATOR_RIGHT(>, shiftExpression); break; |
| 1450 | case Token::Kind::TK_LTEQ: OPERATOR_RIGHT(<=, shiftExpression); break; |
| 1451 | case Token::Kind::TK_GTEQ: OPERATOR_RIGHT(>=, shiftExpression); break; |
| 1452 | default: return result; |
| 1453 | } |
| 1454 | } |
| 1455 | } |
| 1456 | |
| 1457 | /* additiveExpression ((SHL | SHR) additiveExpression)* */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1458 | DSLExpression DSLParser::shiftExpression() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1459 | AutoDSLDepth depth(this); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1460 | DSLExpression result = this->additiveExpression(); |
| 1461 | if (!result.hasValue()) { |
| 1462 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1463 | } |
| 1464 | for (;;) { |
| 1465 | switch (this->peek().fKind) { |
| 1466 | case Token::Kind::TK_SHL: OPERATOR_RIGHT(<<, additiveExpression); break; |
| 1467 | case Token::Kind::TK_SHR: OPERATOR_RIGHT(>>, additiveExpression); break; |
| 1468 | default: return result; |
| 1469 | } |
| 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | /* multiplicativeExpression ((PLUS | MINUS) multiplicativeExpression)* */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1474 | DSLExpression DSLParser::additiveExpression() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1475 | AutoDSLDepth depth(this); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1476 | DSLExpression result = this->multiplicativeExpression(); |
| 1477 | if (!result.hasValue()) { |
| 1478 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1479 | } |
| 1480 | for (;;) { |
| 1481 | switch (this->peek().fKind) { |
| 1482 | case Token::Kind::TK_PLUS: OPERATOR_RIGHT(+, multiplicativeExpression); break; |
| 1483 | case Token::Kind::TK_MINUS: OPERATOR_RIGHT(-, multiplicativeExpression); break; |
| 1484 | default: return result; |
| 1485 | } |
| 1486 | } |
| 1487 | } |
| 1488 | |
| 1489 | /* unaryExpression ((STAR | SLASH | PERCENT) unaryExpression)* */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1490 | DSLExpression DSLParser::multiplicativeExpression() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1491 | AutoDSLDepth depth(this); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1492 | DSLExpression result = this->unaryExpression(); |
| 1493 | if (!result.hasValue()) { |
| 1494 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1495 | } |
| 1496 | for (;;) { |
| 1497 | switch (this->peek().fKind) { |
| 1498 | case Token::Kind::TK_STAR: OPERATOR_RIGHT(*, unaryExpression); break; |
| 1499 | case Token::Kind::TK_SLASH: OPERATOR_RIGHT(/, unaryExpression); break; |
| 1500 | case Token::Kind::TK_PERCENT: OPERATOR_RIGHT(%, unaryExpression); break; |
| 1501 | default: return result; |
| 1502 | } |
| 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | /* postfixExpression | (PLUS | MINUS | NOT | PLUSPLUS | MINUSMINUS) unaryExpression */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1507 | DSLExpression DSLParser::unaryExpression() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1508 | AutoDSLDepth depth(this); |
| 1509 | Token next = this->peek(); |
| 1510 | switch (next.fKind) { |
| 1511 | case Token::Kind::TK_PLUS: |
| 1512 | case Token::Kind::TK_MINUS: |
| 1513 | case Token::Kind::TK_LOGICALNOT: |
| 1514 | case Token::Kind::TK_BITWISENOT: |
| 1515 | case Token::Kind::TK_PLUSPLUS: |
| 1516 | case Token::Kind::TK_MINUSMINUS: { |
| 1517 | if (!depth.increase()) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1518 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1519 | } |
| 1520 | this->nextToken(); |
Ethan Nicholas | 0dc1e0f | 2021-09-17 12:52:55 -0400 | [diff] [blame] | 1521 | DSLExpression expr = this->unaryExpression(); |
| 1522 | if (!expr.hasValue()) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1523 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1524 | } |
| 1525 | switch (next.fKind) { |
Ethan Nicholas | 0dc1e0f | 2021-09-17 12:52:55 -0400 | [diff] [blame] | 1526 | case Token::Kind::TK_PLUS: return {{ +std::move(expr)}}; |
| 1527 | case Token::Kind::TK_MINUS: return {{ -std::move(expr)}}; |
| 1528 | case Token::Kind::TK_LOGICALNOT: return {{ !std::move(expr)}}; |
| 1529 | case Token::Kind::TK_BITWISENOT: return {{ ~std::move(expr)}}; |
| 1530 | case Token::Kind::TK_PLUSPLUS: return {{++std::move(expr)}}; |
| 1531 | case Token::Kind::TK_MINUSMINUS: return {{--std::move(expr)}}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1532 | default: SkUNREACHABLE; |
| 1533 | } |
| 1534 | } |
| 1535 | default: |
| 1536 | return this->postfixExpression(); |
| 1537 | } |
| 1538 | } |
| 1539 | |
| 1540 | /* term suffix* */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1541 | DSLExpression DSLParser::postfixExpression() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1542 | AutoDSLDepth depth(this); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1543 | DSLExpression result = this->term(); |
| 1544 | if (!result.hasValue()) { |
| 1545 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1546 | } |
| 1547 | for (;;) { |
| 1548 | Token t = this->peek(); |
| 1549 | switch (t.fKind) { |
| 1550 | case Token::Kind::TK_FLOAT_LITERAL: |
| 1551 | if (this->text(t)[0] != '.') { |
| 1552 | return result; |
| 1553 | } |
| 1554 | [[fallthrough]]; |
| 1555 | case Token::Kind::TK_LBRACKET: |
| 1556 | case Token::Kind::TK_DOT: |
| 1557 | case Token::Kind::TK_LPAREN: |
| 1558 | case Token::Kind::TK_PLUSPLUS: |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1559 | case Token::Kind::TK_MINUSMINUS: { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1560 | if (!depth.increase()) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1561 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1562 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1563 | DSLExpression next = this->suffix(std::move(result)); |
| 1564 | if (!next.hasValue()) { |
| 1565 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1566 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1567 | result.swap(next); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1568 | break; |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1569 | } |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1570 | default: |
| 1571 | return result; |
| 1572 | } |
| 1573 | } |
| 1574 | } |
| 1575 | |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame] | 1576 | DSLExpression DSLParser::swizzle(int line, DSLExpression base, |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1577 | skstd::string_view swizzleMask) { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1578 | SkASSERT(swizzleMask.length() > 0); |
| 1579 | if (!base.type().isVector() && !base.type().isScalar()) { |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame] | 1580 | return base.field(swizzleMask, this->position(line)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1581 | } |
| 1582 | int length = swizzleMask.length(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1583 | SkSL::SwizzleComponent::Type components[4]; |
| 1584 | for (int i = 0; i < length; ++i) { |
Ethan Nicholas | be8f73d | 2021-08-28 19:50:03 -0400 | [diff] [blame] | 1585 | if (i >= 4) { |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame] | 1586 | this->error(line, "too many components in swizzle mask"); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1587 | return DSLExpression::Poison(); |
Ethan Nicholas | be8f73d | 2021-08-28 19:50:03 -0400 | [diff] [blame] | 1588 | } |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1589 | switch (swizzleMask[i]) { |
| 1590 | case '0': components[i] = SwizzleComponent::ZERO; break; |
| 1591 | case '1': components[i] = SwizzleComponent::ONE; break; |
Ethan Nicholas | b61a243 | 2021-09-02 16:38:43 -0400 | [diff] [blame] | 1592 | case 'r': components[i] = SwizzleComponent::R; break; |
| 1593 | case 'x': components[i] = SwizzleComponent::X; break; |
| 1594 | case 's': components[i] = SwizzleComponent::S; break; |
| 1595 | case 'L': components[i] = SwizzleComponent::UL; break; |
| 1596 | case 'g': components[i] = SwizzleComponent::G; break; |
| 1597 | case 'y': components[i] = SwizzleComponent::Y; break; |
| 1598 | case 't': components[i] = SwizzleComponent::T; break; |
| 1599 | case 'T': components[i] = SwizzleComponent::UT; break; |
| 1600 | case 'b': components[i] = SwizzleComponent::B; break; |
| 1601 | case 'z': components[i] = SwizzleComponent::Z; break; |
| 1602 | case 'p': components[i] = SwizzleComponent::P; break; |
| 1603 | case 'R': components[i] = SwizzleComponent::UR; break; |
| 1604 | case 'a': components[i] = SwizzleComponent::A; break; |
| 1605 | case 'w': components[i] = SwizzleComponent::W; break; |
| 1606 | case 'q': components[i] = SwizzleComponent::Q; break; |
| 1607 | case 'B': components[i] = SwizzleComponent::UB; break; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1608 | default: |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame] | 1609 | this->error(line, |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1610 | String::printf("invalid swizzle component '%c'", swizzleMask[i]).c_str()); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1611 | return DSLExpression::Poison(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1612 | } |
| 1613 | } |
| 1614 | switch (length) { |
| 1615 | case 1: return dsl::Swizzle(std::move(base), components[0]); |
| 1616 | case 2: return dsl::Swizzle(std::move(base), components[0], components[1]); |
| 1617 | case 3: return dsl::Swizzle(std::move(base), components[0], components[1], components[2]); |
| 1618 | case 4: return dsl::Swizzle(std::move(base), components[0], components[1], components[2], |
| 1619 | components[3]); |
| 1620 | default: SkUNREACHABLE; |
| 1621 | } |
| 1622 | } |
| 1623 | |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame] | 1624 | dsl::DSLExpression DSLParser::call(int line, dsl::DSLExpression base, ExpressionArray args) { |
| 1625 | return DSLExpression(base(std::move(args), this->position(line)), this->position(line)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | /* LBRACKET expression? RBRACKET | DOT IDENTIFIER | LPAREN arguments RPAREN | |
| 1629 | PLUSPLUS | MINUSMINUS | COLONCOLON IDENTIFIER | FLOAT_LITERAL [IDENTIFIER] */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1630 | DSLExpression DSLParser::suffix(DSLExpression base) { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1631 | Token next = this->nextToken(); |
| 1632 | AutoDSLDepth depth(this); |
| 1633 | if (!depth.increase()) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1634 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1635 | } |
| 1636 | switch (next.fKind) { |
| 1637 | case Token::Kind::TK_LBRACKET: { |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 1638 | if (this->checkNext(Token::Kind::TK_RBRACKET)) { |
| 1639 | this->error(next, "missing index in '[]'"); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1640 | return DSLExpression::Poison(); |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 1641 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1642 | DSLExpression index = this->expression(); |
| 1643 | if (!index.hasValue()) { |
| 1644 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1645 | } |
| 1646 | this->expect(Token::Kind::TK_RBRACKET, "']' to complete array access expression"); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1647 | DSLPossibleExpression result = base[std::move(index)]; |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 1648 | if (!result.valid()) { |
| 1649 | result.reportErrors(this->position(next)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1650 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1651 | return std::move(result); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1652 | } |
| 1653 | case Token::Kind::TK_DOT: { |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame] | 1654 | int line = this->peek().fLine; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1655 | skstd::string_view text; |
| 1656 | if (this->identifier(&text)) { |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame] | 1657 | return this->swizzle(line, std::move(base), text); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1658 | } |
| 1659 | [[fallthrough]]; |
| 1660 | } |
| 1661 | case Token::Kind::TK_FLOAT_LITERAL: { |
| 1662 | // Swizzles that start with a constant number, e.g. '.000r', will be tokenized as |
| 1663 | // floating point literals, possibly followed by an identifier. Handle that here. |
| 1664 | skstd::string_view field = this->text(next); |
| 1665 | SkASSERT(field[0] == '.'); |
| 1666 | field.remove_prefix(1); |
| 1667 | // use the next *raw* token so we don't ignore whitespace - we only care about |
| 1668 | // identifiers that directly follow the float |
| 1669 | Token id = this->nextRawToken(); |
| 1670 | if (id.fKind == Token::Kind::TK_IDENTIFIER) { |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame] | 1671 | return this->swizzle(next.fLine, std::move(base), field + this->text(id)); |
Ethan Nicholas | bf4a7d5 | 2021-09-09 09:32:13 -0400 | [diff] [blame] | 1672 | } else if (field.empty()) { |
| 1673 | this->error(next, "expected field name or swizzle mask after '.'"); |
| 1674 | return {{DSLExpression::Poison()}}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1675 | } |
| 1676 | this->pushback(id); |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame] | 1677 | return this->swizzle(next.fLine, std::move(base), field); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1678 | } |
| 1679 | case Token::Kind::TK_LPAREN: { |
Ethan Nicholas | 9a1f92e | 2021-09-09 15:03:22 -0400 | [diff] [blame] | 1680 | ExpressionArray args; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1681 | if (this->peek().fKind != Token::Kind::TK_RPAREN) { |
| 1682 | for (;;) { |
Ethan Nicholas | f62934b | 2021-09-20 10:18:58 -0400 | [diff] [blame] | 1683 | DSLExpression expr = this->assignmentExpression(); |
| 1684 | if (!expr.hasValue()) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1685 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1686 | } |
Ethan Nicholas | f62934b | 2021-09-20 10:18:58 -0400 | [diff] [blame] | 1687 | args.push_back(expr.release()); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1688 | if (!this->checkNext(Token::Kind::TK_COMMA)) { |
| 1689 | break; |
| 1690 | } |
| 1691 | } |
| 1692 | } |
| 1693 | this->expect(Token::Kind::TK_RPAREN, "')' to complete function arguments"); |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame] | 1694 | return this->call(next.fLine, std::move(base), std::move(args)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1695 | } |
| 1696 | case Token::Kind::TK_PLUSPLUS: |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1697 | return std::move(base)++; |
| 1698 | case Token::Kind::TK_MINUSMINUS: |
| 1699 | return std::move(base)--; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1700 | default: { |
| 1701 | this->error(next, "expected expression suffix, but found '" + this->text(next) + "'"); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1702 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1703 | } |
| 1704 | } |
| 1705 | } |
| 1706 | |
| 1707 | /* IDENTIFIER | intLiteral | floatLiteral | boolLiteral | '(' expression ')' */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1708 | DSLExpression DSLParser::term() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1709 | Token t = this->peek(); |
| 1710 | switch (t.fKind) { |
| 1711 | case Token::Kind::TK_IDENTIFIER: { |
| 1712 | skstd::string_view text; |
| 1713 | if (this->identifier(&text)) { |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 1714 | return dsl::Symbol(text, this->position(t)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1715 | } |
| 1716 | break; |
| 1717 | } |
| 1718 | case Token::Kind::TK_INT_LITERAL: { |
| 1719 | SKSL_INT i; |
Ethan Nicholas | 0459a93 | 2021-09-01 14:54:44 -0400 | [diff] [blame] | 1720 | if (!this->intLiteral(&i)) { |
| 1721 | i = 0; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1722 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1723 | return DSLExpression(i, this->position(t)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1724 | } |
| 1725 | case Token::Kind::TK_FLOAT_LITERAL: { |
| 1726 | SKSL_FLOAT f; |
Ethan Nicholas | 0459a93 | 2021-09-01 14:54:44 -0400 | [diff] [blame] | 1727 | if (!this->floatLiteral(&f)) { |
| 1728 | f = 0.0f; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1729 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1730 | return DSLExpression(f, this->position(t)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1731 | } |
| 1732 | case Token::Kind::TK_TRUE_LITERAL: // fall through |
| 1733 | case Token::Kind::TK_FALSE_LITERAL: { |
| 1734 | bool b; |
Ethan Nicholas | 0459a93 | 2021-09-01 14:54:44 -0400 | [diff] [blame] | 1735 | SkAssertResult(this->boolLiteral(&b)); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1736 | return DSLExpression(b, this->position(t)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1737 | } |
| 1738 | case Token::Kind::TK_LPAREN: { |
| 1739 | this->nextToken(); |
| 1740 | AutoDSLDepth depth(this); |
| 1741 | if (!depth.increase()) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1742 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1743 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1744 | DSLExpression result = this->expression(); |
| 1745 | if (result.hasValue()) { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1746 | this->expect(Token::Kind::TK_RPAREN, "')' to complete expression"); |
| 1747 | return result; |
| 1748 | } |
| 1749 | break; |
| 1750 | } |
| 1751 | default: |
| 1752 | this->nextToken(); |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame] | 1753 | this->error(t, "expected expression, but found '" + this->text(t) + "'"); |
Ethan Nicholas | ad284fe | 2021-09-01 10:17:48 -0400 | [diff] [blame] | 1754 | fEncounteredFatalError = true; |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame] | 1755 | break; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1756 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1757 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1758 | } |
| 1759 | |
| 1760 | /* INT_LITERAL */ |
| 1761 | bool DSLParser::intLiteral(SKSL_INT* dest) { |
| 1762 | Token t; |
| 1763 | if (!this->expect(Token::Kind::TK_INT_LITERAL, "integer literal", &t)) { |
| 1764 | return false; |
| 1765 | } |
| 1766 | skstd::string_view s = this->text(t); |
| 1767 | if (!SkSL::stoi(s, dest)) { |
| 1768 | this->error(t, "integer is too large: " + s); |
| 1769 | return false; |
| 1770 | } |
| 1771 | return true; |
| 1772 | } |
| 1773 | |
| 1774 | /* FLOAT_LITERAL */ |
| 1775 | bool DSLParser::floatLiteral(SKSL_FLOAT* dest) { |
| 1776 | Token t; |
| 1777 | if (!this->expect(Token::Kind::TK_FLOAT_LITERAL, "float literal", &t)) { |
| 1778 | return false; |
| 1779 | } |
| 1780 | skstd::string_view s = this->text(t); |
| 1781 | if (!SkSL::stod(s, dest)) { |
| 1782 | this->error(t, "floating-point value is too large: " + s); |
| 1783 | return false; |
| 1784 | } |
| 1785 | return true; |
| 1786 | } |
| 1787 | |
| 1788 | /* TRUE_LITERAL | FALSE_LITERAL */ |
| 1789 | bool DSLParser::boolLiteral(bool* dest) { |
| 1790 | Token t = this->nextToken(); |
| 1791 | switch (t.fKind) { |
| 1792 | case Token::Kind::TK_TRUE_LITERAL: |
| 1793 | *dest = true; |
| 1794 | return true; |
| 1795 | case Token::Kind::TK_FALSE_LITERAL: |
| 1796 | *dest = false; |
| 1797 | return true; |
| 1798 | default: |
| 1799 | this->error(t, "expected 'true' or 'false', but found '" + this->text(t) + "'"); |
| 1800 | return false; |
| 1801 | } |
| 1802 | } |
| 1803 | |
| 1804 | /* IDENTIFIER */ |
| 1805 | bool DSLParser::identifier(skstd::string_view* dest) { |
| 1806 | Token t; |
| 1807 | if (this->expect(Token::Kind::TK_IDENTIFIER, "identifier", &t)) { |
| 1808 | *dest = this->text(t); |
| 1809 | return true; |
| 1810 | } |
| 1811 | return false; |
| 1812 | } |
| 1813 | |
| 1814 | } // namespace SkSL |