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