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))) |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame^] | 106 | , fPushback(Token::Kind::TK_NONE, /*offset=*/-1, /*length=*/-1, /*line=*/-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) { |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame^] | 196 | return this->position(t.fLine); |
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) { |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame^] | 200 | return PositionInfo("<unknown>", offset); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | void DSLParser::error(Token token, String msg) { |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame^] | 204 | this->error(token.fLine, 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(); |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame^] | 306 | this->error(lookahead, "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; |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame^] | 446 | int line = this->peek().fLine; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 447 | DSLType type = baseType; |
| 448 | DSLExpression initializer; |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame^] | 449 | if (!this->parseArrayDimensions(line, &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 | } |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame^] | 452 | if (!this->parseInitializer(line, &initializer)) { |
John Stiles | b05bbd0 | 2021-09-24 15:11:16 -0400 | [diff] [blame] | 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 | } |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame^] | 465 | if (!this->parseArrayDimensions(line, &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; |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame^] | 469 | if (!this->parseInitializer(line, &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), |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame^] | 473 | this->position(line)); |
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; |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame^] | 485 | int line = this->peek().fLine; |
Ethan Nicholas | 6c302ba | 2021-09-14 09:16:12 -0400 | [diff] [blame] | 486 | DSLType type = baseType; |
| 487 | DSLExpression initializer; |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame^] | 488 | if (!this->parseArrayDimensions(line, &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 | } |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame^] | 491 | if (!this->parseInitializer(line, &initializer)) { |
John Stiles | b05bbd0 | 2021-09-24 15:11:16 -0400 | [diff] [blame] | 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 | } |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame^] | 504 | if (!this->parseArrayDimensions(line, &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; |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame^] | 508 | if (!this->parseInitializer(line, &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), |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame^] | 512 | this->position(line)); |
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()) { |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame^] | 620 | this->error(name, "struct '" + this->text(name) + "' must contain at least one field"); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 621 | } |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 622 | return dsl::Struct(this->text(name), SkMakeSpan(fields), this->position(name)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | /* structDeclaration ((IDENTIFIER varDeclarationEnd) | SEMICOLON) */ |
John Stiles | e53c721 | 2021-08-05 10:19:11 -0400 | [diff] [blame] | 626 | SkTArray<dsl::DSLGlobalVar> DSLParser::structVarDeclaration(const DSLModifiers& modifiers) { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 627 | skstd::optional<DSLType> type = this->structDeclaration(); |
| 628 | if (!type) { |
| 629 | return {}; |
| 630 | } |
| 631 | Token name; |
| 632 | if (this->checkNext(Token::Kind::TK_IDENTIFIER, &name)) { |
Ethan Nicholas | 6c302ba | 2021-09-14 09:16:12 -0400 | [diff] [blame] | 633 | this->globalVarDeclarationEnd(this->position(name), modifiers, std::move(*type), |
| 634 | this->text(name)); |
Ethan Nicholas | 3e7cd00 | 2021-09-15 16:19:03 -0400 | [diff] [blame] | 635 | } else { |
| 636 | this->expect(Token::Kind::TK_SEMICOLON, "';'"); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 637 | } |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 638 | return {}; |
| 639 | } |
| 640 | |
| 641 | /* modifiers type IDENTIFIER (LBRACKET INT_LITERAL RBRACKET)? */ |
| 642 | skstd::optional<DSLWrapper<DSLParameter>> DSLParser::parameter() { |
| 643 | DSLModifiers modifiers = this->modifiersWithDefaults(0); |
John Stiles | 4adb66f | 2021-08-05 10:15:16 -0400 | [diff] [blame] | 644 | skstd::optional<DSLType> type = this->type(modifiers); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 645 | if (!type) { |
| 646 | return skstd::nullopt; |
| 647 | } |
| 648 | Token name; |
| 649 | if (!this->expectIdentifier(&name)) { |
| 650 | return skstd::nullopt; |
| 651 | } |
| 652 | while (this->checkNext(Token::Kind::TK_LBRACKET)) { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 653 | Token sizeToken; |
| 654 | if (!this->expect(Token::Kind::TK_INT_LITERAL, "a positive integer", &sizeToken)) { |
| 655 | return skstd::nullopt; |
| 656 | } |
| 657 | skstd::string_view arraySizeFrag = this->text(sizeToken); |
| 658 | SKSL_INT arraySize; |
| 659 | if (!SkSL::stoi(arraySizeFrag, &arraySize)) { |
| 660 | this->error(sizeToken, "array size is too large: " + arraySizeFrag); |
Ethan Nicholas | 709ecd5 | 2021-09-01 15:48:42 -0400 | [diff] [blame] | 661 | arraySize = 1; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 662 | } |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 663 | type = Array(*type, arraySize, this->position(name)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 664 | if (!this->expect(Token::Kind::TK_RBRACKET, "']'")) { |
| 665 | return skstd::nullopt; |
| 666 | } |
| 667 | } |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 668 | return {{DSLParameter(modifiers, *type, this->text(name), this->position(name))}}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | /** EQ INT_LITERAL */ |
| 672 | int DSLParser::layoutInt() { |
| 673 | if (!this->expect(Token::Kind::TK_EQ, "'='")) { |
| 674 | return -1; |
| 675 | } |
| 676 | Token resultToken; |
| 677 | if (!this->expect(Token::Kind::TK_INT_LITERAL, "a non-negative integer", &resultToken)) { |
| 678 | return -1; |
| 679 | } |
| 680 | skstd::string_view resultFrag = this->text(resultToken); |
| 681 | SKSL_INT resultValue; |
| 682 | if (!SkSL::stoi(resultFrag, &resultValue)) { |
| 683 | this->error(resultToken, "value in layout is too large: " + resultFrag); |
| 684 | return -1; |
| 685 | } |
| 686 | return resultValue; |
| 687 | } |
| 688 | |
| 689 | /** EQ IDENTIFIER */ |
| 690 | skstd::string_view DSLParser::layoutIdentifier() { |
| 691 | if (!this->expect(Token::Kind::TK_EQ, "'='")) { |
| 692 | return {}; |
| 693 | } |
| 694 | Token resultToken; |
| 695 | if (!this->expectIdentifier(&resultToken)) { |
| 696 | return {}; |
| 697 | } |
| 698 | return this->text(resultToken); |
| 699 | } |
| 700 | |
| 701 | /* LAYOUT LPAREN IDENTIFIER (EQ INT_LITERAL)? (COMMA IDENTIFIER (EQ INT_LITERAL)?)* RPAREN */ |
| 702 | DSLLayout DSLParser::layout() { |
| 703 | DSLLayout result; |
| 704 | if (this->checkNext(Token::Kind::TK_LAYOUT)) { |
| 705 | if (!this->expect(Token::Kind::TK_LPAREN, "'('")) { |
| 706 | return result; |
| 707 | } |
| 708 | for (;;) { |
| 709 | Token t = this->nextToken(); |
| 710 | String text(this->text(t)); |
| 711 | auto found = layoutTokens->find(text); |
| 712 | if (found != layoutTokens->end()) { |
| 713 | switch (found->second) { |
| 714 | case LayoutToken::ORIGIN_UPPER_LEFT: |
Ethan Nicholas | df803aa | 2021-08-29 14:22:45 -0400 | [diff] [blame] | 715 | result.originUpperLeft(this->position(t)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 716 | break; |
| 717 | case LayoutToken::PUSH_CONSTANT: |
Ethan Nicholas | df803aa | 2021-08-29 14:22:45 -0400 | [diff] [blame] | 718 | result.pushConstant(this->position(t)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 719 | break; |
| 720 | case LayoutToken::BLEND_SUPPORT_ALL_EQUATIONS: |
Ethan Nicholas | df803aa | 2021-08-29 14:22:45 -0400 | [diff] [blame] | 721 | result.blendSupportAllEquations(this->position(t)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 722 | break; |
| 723 | case LayoutToken::SRGB_UNPREMUL: |
Ethan Nicholas | df803aa | 2021-08-29 14:22:45 -0400 | [diff] [blame] | 724 | result.srgbUnpremul(this->position(t)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 725 | break; |
| 726 | case LayoutToken::LOCATION: |
Ethan Nicholas | df803aa | 2021-08-29 14:22:45 -0400 | [diff] [blame] | 727 | result.location(this->layoutInt(), this->position(t)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 728 | break; |
| 729 | case LayoutToken::OFFSET: |
Ethan Nicholas | df803aa | 2021-08-29 14:22:45 -0400 | [diff] [blame] | 730 | result.offset(this->layoutInt(), this->position(t)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 731 | break; |
| 732 | case LayoutToken::BINDING: |
Ethan Nicholas | df803aa | 2021-08-29 14:22:45 -0400 | [diff] [blame] | 733 | result.binding(this->layoutInt(), this->position(t)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 734 | break; |
| 735 | case LayoutToken::INDEX: |
Ethan Nicholas | df803aa | 2021-08-29 14:22:45 -0400 | [diff] [blame] | 736 | result.index(this->layoutInt(), this->position(t)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 737 | break; |
| 738 | case LayoutToken::SET: |
Ethan Nicholas | df803aa | 2021-08-29 14:22:45 -0400 | [diff] [blame] | 739 | result.set(this->layoutInt(), this->position(t)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 740 | break; |
| 741 | case LayoutToken::BUILTIN: |
Ethan Nicholas | df803aa | 2021-08-29 14:22:45 -0400 | [diff] [blame] | 742 | result.builtin(this->layoutInt(), this->position(t)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 743 | break; |
| 744 | case LayoutToken::INPUT_ATTACHMENT_INDEX: |
Ethan Nicholas | df803aa | 2021-08-29 14:22:45 -0400 | [diff] [blame] | 745 | result.inputAttachmentIndex(this->layoutInt(), this->position(t)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 746 | break; |
| 747 | default: |
| 748 | this->error(t, "'" + text + "' is not a valid layout qualifier"); |
| 749 | break; |
| 750 | } |
| 751 | } else { |
| 752 | this->error(t, "'" + text + "' is not a valid layout qualifier"); |
| 753 | } |
| 754 | if (this->checkNext(Token::Kind::TK_RPAREN)) { |
| 755 | break; |
| 756 | } |
| 757 | if (!this->expect(Token::Kind::TK_COMMA, "','")) { |
| 758 | break; |
| 759 | } |
| 760 | } |
| 761 | } |
| 762 | return result; |
| 763 | } |
| 764 | |
| 765 | /* layout? (UNIFORM | CONST | IN | OUT | INOUT | LOWP | MEDIUMP | HIGHP | FLAT | NOPERSPECTIVE | |
| 766 | VARYING | INLINE)* */ |
| 767 | DSLModifiers DSLParser::modifiers() { |
| 768 | DSLLayout layout = this->layout(); |
| 769 | int flags = 0; |
| 770 | for (;;) { |
| 771 | // TODO(ethannicholas): handle duplicate / incompatible flags |
| 772 | int tokenFlag = parse_modifier_token(peek().fKind); |
| 773 | if (!tokenFlag) { |
| 774 | break; |
| 775 | } |
| 776 | flags |= tokenFlag; |
| 777 | this->nextToken(); |
| 778 | } |
| 779 | return DSLModifiers(std::move(layout), flags); |
| 780 | } |
| 781 | |
| 782 | DSLModifiers DSLParser::modifiersWithDefaults(int defaultFlags) { |
| 783 | DSLModifiers result = this->modifiers(); |
| 784 | if (defaultFlags && !result.flags()) { |
| 785 | return DSLModifiers(result.layout(), defaultFlags); |
| 786 | } |
| 787 | return result; |
| 788 | } |
| 789 | |
| 790 | /* ifStatement | forStatement | doStatement | whileStatement | block | expression */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 791 | DSLStatement DSLParser::statement() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 792 | Token start = this->nextToken(); |
| 793 | AutoDSLDepth depth(this); |
| 794 | if (!depth.increase()) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 795 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 796 | } |
| 797 | this->pushback(start); |
| 798 | switch (start.fKind) { |
| 799 | case Token::Kind::TK_IF: // fall through |
| 800 | case Token::Kind::TK_STATIC_IF: |
| 801 | return this->ifStatement(); |
| 802 | case Token::Kind::TK_FOR: |
| 803 | return this->forStatement(); |
| 804 | case Token::Kind::TK_DO: |
| 805 | return this->doStatement(); |
| 806 | case Token::Kind::TK_WHILE: |
| 807 | return this->whileStatement(); |
| 808 | case Token::Kind::TK_SWITCH: // fall through |
| 809 | case Token::Kind::TK_STATIC_SWITCH: |
| 810 | return this->switchStatement(); |
| 811 | case Token::Kind::TK_RETURN: |
| 812 | return this->returnStatement(); |
| 813 | case Token::Kind::TK_BREAK: |
| 814 | return this->breakStatement(); |
| 815 | case Token::Kind::TK_CONTINUE: |
| 816 | return this->continueStatement(); |
| 817 | case Token::Kind::TK_DISCARD: |
| 818 | return this->discardStatement(); |
| 819 | case Token::Kind::TK_LBRACE: { |
| 820 | skstd::optional<DSLBlock> result = this->block(); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 821 | return result ? DSLStatement(std::move(*result)) : DSLStatement(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 822 | } |
| 823 | case Token::Kind::TK_SEMICOLON: |
| 824 | this->nextToken(); |
| 825 | return dsl::Block(); |
John Stiles | 0201431 | 2021-08-04 16:03:12 -0400 | [diff] [blame] | 826 | case Token::Kind::TK_HIGHP: |
| 827 | case Token::Kind::TK_MEDIUMP: |
| 828 | case Token::Kind::TK_LOWP: |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 829 | case Token::Kind::TK_CONST: |
| 830 | case Token::Kind::TK_IDENTIFIER: |
| 831 | return this->varDeclarationsOrExpressionStatement(); |
| 832 | default: |
| 833 | return this->expressionStatement(); |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | /* IDENTIFIER(type) (LBRACKET intLiteral? RBRACKET)* QUESTION? */ |
John Stiles | e53c721 | 2021-08-05 10:19:11 -0400 | [diff] [blame] | 838 | skstd::optional<DSLType> DSLParser::type(const DSLModifiers& modifiers) { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 839 | Token type; |
| 840 | if (!this->expect(Token::Kind::TK_IDENTIFIER, "a type", &type)) { |
| 841 | return skstd::nullopt; |
| 842 | } |
| 843 | if (!IsType(this->text(type))) { |
| 844 | this->error(type, ("no type named '" + this->text(type) + "'").c_str()); |
| 845 | return skstd::nullopt; |
| 846 | } |
Ethan Nicholas | a248a9a | 2021-09-01 16:40:25 -0400 | [diff] [blame] | 847 | DSLType result(this->text(type), modifiers, this->position(type)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 848 | while (this->checkNext(Token::Kind::TK_LBRACKET)) { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 849 | if (this->peek().fKind != Token::Kind::TK_RBRACKET) { |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 850 | result = Array(result, this->arraySize(), this->position(type)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 851 | } else { |
| 852 | this->error(this->peek(), "expected array dimension"); |
| 853 | } |
| 854 | this->expect(Token::Kind::TK_RBRACKET, "']'"); |
| 855 | } |
| 856 | return result; |
| 857 | } |
| 858 | |
| 859 | /* IDENTIFIER LBRACE |
| 860 | varDeclaration+ |
| 861 | RBRACE (IDENTIFIER (LBRACKET expression? RBRACKET)*)? SEMICOLON */ |
John Stiles | e53c721 | 2021-08-05 10:19:11 -0400 | [diff] [blame] | 862 | bool DSLParser::interfaceBlock(const dsl::DSLModifiers& modifiers) { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 863 | Token typeName; |
| 864 | if (!this->expectIdentifier(&typeName)) { |
| 865 | return false; |
| 866 | } |
| 867 | if (peek().fKind != Token::Kind::TK_LBRACE) { |
| 868 | // we only get into interfaceBlock if we found a top-level identifier which was not a type. |
| 869 | // 99% of the time, the user was not actually intending to create an interface block, so |
| 870 | // it's better to report it as an unknown type |
| 871 | this->error(typeName, "no type named '" + this->text(typeName) + "'"); |
| 872 | return false; |
| 873 | } |
| 874 | this->nextToken(); |
| 875 | SkTArray<dsl::Field> fields; |
| 876 | while (!this->checkNext(Token::Kind::TK_RBRACE)) { |
Ethan Nicholas | 5c4463e | 2021-08-29 14:31:19 -0400 | [diff] [blame] | 877 | DSLModifiers fieldModifiers = this->modifiers(); |
| 878 | skstd::optional<dsl::DSLType> type = this->type(fieldModifiers); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 879 | if (!type) { |
| 880 | return false; |
| 881 | } |
| 882 | do { |
| 883 | Token fieldName; |
| 884 | if (!this->expect(Token::Kind::TK_IDENTIFIER, "an identifier", &fieldName)) { |
| 885 | return false; |
| 886 | } |
| 887 | DSLType actualType = *type; |
| 888 | if (this->checkNext(Token::Kind::TK_LBRACKET)) { |
| 889 | Token sizeToken = this->peek(); |
| 890 | if (sizeToken.fKind != Token::Kind::TK_RBRACKET) { |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 891 | actualType = Array(std::move(actualType), this->arraySize(), |
| 892 | this->position(typeName)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 893 | } else { |
| 894 | this->error(sizeToken, "unsized arrays are not permitted"); |
| 895 | } |
| 896 | this->expect(Token::Kind::TK_RBRACKET, "']'"); |
| 897 | } |
| 898 | if (!this->expect(Token::Kind::TK_SEMICOLON, "';'")) { |
| 899 | return false; |
| 900 | } |
Ethan Nicholas | 2763323 | 2021-08-29 13:51:44 -0400 | [diff] [blame] | 901 | fields.push_back(dsl::Field(fieldModifiers, std::move(actualType), |
| 902 | this->text(fieldName), this->position(fieldName))); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 903 | } |
| 904 | while (this->checkNext(Token::Kind::TK_COMMA)); |
| 905 | } |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 906 | skstd::string_view instanceName; |
| 907 | Token instanceNameToken; |
| 908 | SKSL_INT arraySize = 0; |
| 909 | if (this->checkNext(Token::Kind::TK_IDENTIFIER, &instanceNameToken)) { |
| 910 | instanceName = this->text(instanceNameToken); |
| 911 | if (this->checkNext(Token::Kind::TK_LBRACKET)) { |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 912 | arraySize = this->arraySize(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 913 | this->expect(Token::Kind::TK_RBRACKET, "']'"); |
| 914 | } |
| 915 | } |
Ethan Nicholas | e110f6e | 2021-08-29 14:03:06 -0400 | [diff] [blame] | 916 | this->expect(Token::Kind::TK_SEMICOLON, "';'"); |
John Stiles | d0665d9 | 2021-09-17 09:14:28 -0400 | [diff] [blame] | 917 | if (fields.empty()) { |
| 918 | this->error(typeName, "interface block '" + this->text(typeName) + |
| 919 | "' must contain at least one member"); |
| 920 | } else { |
| 921 | dsl::InterfaceBlock(modifiers, this->text(typeName), std::move(fields), instanceName, |
| 922 | arraySize, this->position(typeName)); |
| 923 | } |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 924 | return true; |
| 925 | } |
| 926 | |
| 927 | /* IF LPAREN expression RPAREN statement (ELSE statement)? */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 928 | DSLStatement DSLParser::ifStatement() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 929 | Token start; |
| 930 | bool isStatic = this->checkNext(Token::Kind::TK_STATIC_IF, &start); |
| 931 | if (!isStatic && !this->expect(Token::Kind::TK_IF, "'if'", &start)) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 932 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 933 | } |
| 934 | if (!this->expect(Token::Kind::TK_LPAREN, "'('")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 935 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 936 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 937 | DSLExpression test = this->expression(); |
| 938 | if (!test.hasValue()) { |
| 939 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 940 | } |
| 941 | if (!this->expect(Token::Kind::TK_RPAREN, "')'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 942 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 943 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 944 | DSLStatement ifTrue = this->statement(); |
| 945 | if (!ifTrue.hasValue()) { |
| 946 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 947 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 948 | DSLStatement ifFalse; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 949 | if (this->checkNext(Token::Kind::TK_ELSE)) { |
| 950 | ifFalse = this->statement(); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 951 | if (!ifFalse.hasValue()) { |
| 952 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 953 | } |
| 954 | } |
| 955 | if (isStatic) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 956 | return StaticIf(std::move(test), std::move(ifTrue), |
| 957 | ifFalse.hasValue() ? std::move(ifFalse) : DSLStatement(), this->position(start)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 958 | } else { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 959 | return If(std::move(test), std::move(ifTrue), |
| 960 | ifFalse.hasValue() ? std::move(ifFalse) : DSLStatement(), this->position(start)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 961 | } |
| 962 | } |
| 963 | |
| 964 | /* DO statement WHILE LPAREN expression RPAREN SEMICOLON */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 965 | DSLStatement DSLParser::doStatement() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 966 | Token start; |
| 967 | if (!this->expect(Token::Kind::TK_DO, "'do'", &start)) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 968 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 969 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 970 | DSLStatement statement = this->statement(); |
| 971 | if (!statement.hasValue()) { |
| 972 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 973 | } |
| 974 | if (!this->expect(Token::Kind::TK_WHILE, "'while'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 975 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 976 | } |
| 977 | if (!this->expect(Token::Kind::TK_LPAREN, "'('")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 978 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 979 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 980 | DSLExpression test = this->expression(); |
| 981 | if (!test.hasValue()) { |
| 982 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 983 | } |
| 984 | if (!this->expect(Token::Kind::TK_RPAREN, "')'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 985 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 986 | } |
| 987 | if (!this->expect(Token::Kind::TK_SEMICOLON, "';'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 988 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 989 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 990 | return Do(std::move(statement), std::move(test), this->position(start)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | /* WHILE LPAREN expression RPAREN STATEMENT */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 994 | DSLStatement DSLParser::whileStatement() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 995 | Token start; |
| 996 | if (!this->expect(Token::Kind::TK_WHILE, "'while'", &start)) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 997 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 998 | } |
| 999 | if (!this->expect(Token::Kind::TK_LPAREN, "'('")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1000 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1001 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1002 | DSLExpression test = this->expression(); |
| 1003 | if (!test.hasValue()) { |
| 1004 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1005 | } |
| 1006 | if (!this->expect(Token::Kind::TK_RPAREN, "')'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1007 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1008 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1009 | DSLStatement statement = this->statement(); |
| 1010 | if (!statement.hasValue()) { |
| 1011 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1012 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1013 | return While(std::move(test), std::move(statement), this->position(start)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | /* CASE expression COLON statement* */ |
| 1017 | skstd::optional<DSLCase> DSLParser::switchCase() { |
| 1018 | Token start; |
| 1019 | if (!this->expect(Token::Kind::TK_CASE, "'case'", &start)) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1020 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1021 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1022 | DSLExpression value = this->expression(); |
| 1023 | if (!value.hasValue()) { |
| 1024 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1025 | } |
| 1026 | if (!this->expect(Token::Kind::TK_COLON, "':'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1027 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1028 | } |
| 1029 | SkTArray<DSLStatement> statements; |
| 1030 | while (this->peek().fKind != Token::Kind::TK_RBRACE && |
| 1031 | this->peek().fKind != Token::Kind::TK_CASE && |
| 1032 | this->peek().fKind != Token::Kind::TK_DEFAULT) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1033 | DSLStatement s = this->statement(); |
| 1034 | if (!s.hasValue()) { |
| 1035 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1036 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1037 | statements.push_back(std::move(s)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1038 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1039 | return DSLCase(std::move(value), std::move(statements)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | /* SWITCH LPAREN expression RPAREN LBRACE switchCase* (DEFAULT COLON statement*)? RBRACE */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1043 | DSLStatement DSLParser::switchStatement() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1044 | Token start; |
| 1045 | bool isStatic = this->checkNext(Token::Kind::TK_STATIC_SWITCH, &start); |
| 1046 | if (!isStatic && !this->expect(Token::Kind::TK_SWITCH, "'switch'", &start)) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1047 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1048 | } |
| 1049 | if (!this->expect(Token::Kind::TK_LPAREN, "'('")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1050 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1051 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1052 | DSLExpression value = this->expression(); |
| 1053 | if (!value.hasValue()) { |
| 1054 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1055 | } |
| 1056 | if (!this->expect(Token::Kind::TK_RPAREN, "')'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1057 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1058 | } |
| 1059 | if (!this->expect(Token::Kind::TK_LBRACE, "'{'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1060 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1061 | } |
| 1062 | SkTArray<DSLCase> cases; |
| 1063 | while (this->peek().fKind == Token::Kind::TK_CASE) { |
| 1064 | skstd::optional<DSLCase> c = this->switchCase(); |
| 1065 | if (!c) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1066 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1067 | } |
| 1068 | cases.push_back(std::move(*c)); |
| 1069 | } |
| 1070 | // Requiring default: to be last (in defiance of C and GLSL) was a deliberate decision. Other |
| 1071 | // parts of the compiler may rely upon this assumption. |
| 1072 | if (this->peek().fKind == Token::Kind::TK_DEFAULT) { |
| 1073 | SkTArray<DSLStatement> statements; |
| 1074 | Token defaultStart; |
| 1075 | SkAssertResult(this->expect(Token::Kind::TK_DEFAULT, "'default'", &defaultStart)); |
| 1076 | if (!this->expect(Token::Kind::TK_COLON, "':'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1077 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1078 | } |
| 1079 | while (this->peek().fKind != Token::Kind::TK_RBRACE) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1080 | DSLStatement s = this->statement(); |
| 1081 | if (!s.hasValue()) { |
| 1082 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1083 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1084 | statements.push_back(std::move(s)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1085 | } |
Ethan Nicholas | 360db87 | 2021-09-03 17:00:09 -0400 | [diff] [blame] | 1086 | cases.push_back(DSLCase(DSLExpression(), std::move(statements), this->position(start))); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1087 | } |
| 1088 | if (!this->expect(Token::Kind::TK_RBRACE, "'}'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1089 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1090 | } |
| 1091 | if (isStatic) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1092 | return StaticSwitch(std::move(value), std::move(cases), this->position(start)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1093 | } else { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1094 | return Switch(std::move(value), std::move(cases), this->position(start)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | /* FOR LPAREN (declaration | expression)? SEMICOLON expression? SEMICOLON expression? RPAREN |
| 1099 | STATEMENT */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1100 | dsl::DSLStatement DSLParser::forStatement() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1101 | Token start; |
| 1102 | if (!this->expect(Token::Kind::TK_FOR, "'for'", &start)) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1103 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1104 | } |
| 1105 | if (!this->expect(Token::Kind::TK_LPAREN, "'('")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1106 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1107 | } |
| 1108 | AutoDSLSymbolTable symbols; |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1109 | dsl::DSLStatement initializer; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1110 | Token nextToken = this->peek(); |
| 1111 | if (nextToken.fKind == Token::Kind::TK_SEMICOLON) { |
| 1112 | // An empty init-statement. |
| 1113 | this->nextToken(); |
| 1114 | } else { |
| 1115 | // The init-statement must be an expression or variable declaration. |
| 1116 | initializer = this->varDeclarationsOrExpressionStatement(); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1117 | if (!initializer.hasValue()) { |
| 1118 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1119 | } |
| 1120 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1121 | dsl::DSLExpression test; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1122 | if (this->peek().fKind != Token::Kind::TK_SEMICOLON) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1123 | dsl::DSLExpression testValue = this->expression(); |
| 1124 | if (!testValue.hasValue()) { |
| 1125 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1126 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1127 | test.swap(testValue); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1128 | } |
| 1129 | if (!this->expect(Token::Kind::TK_SEMICOLON, "';'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1130 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1131 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1132 | dsl::DSLExpression next; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1133 | if (this->peek().fKind != Token::Kind::TK_RPAREN) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1134 | dsl::DSLExpression nextValue = this->expression(); |
| 1135 | if (!nextValue.hasValue()) { |
| 1136 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1137 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1138 | next.swap(nextValue); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1139 | } |
| 1140 | if (!this->expect(Token::Kind::TK_RPAREN, "')'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1141 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1142 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1143 | dsl::DSLStatement statement = this->statement(); |
| 1144 | if (!statement.hasValue()) { |
| 1145 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1146 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1147 | return For(initializer.hasValue() ? std::move(initializer) : DSLStatement(), |
| 1148 | test.hasValue() ? std::move(test) : DSLExpression(), |
| 1149 | next.hasValue() ? std::move(next) : DSLExpression(), |
| 1150 | std::move(statement), |
Ethan Nicholas | 360db87 | 2021-09-03 17:00:09 -0400 | [diff] [blame] | 1151 | this->position(start)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | /* RETURN expression? SEMICOLON */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1155 | DSLStatement DSLParser::returnStatement() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1156 | Token start; |
| 1157 | if (!this->expect(Token::Kind::TK_RETURN, "'return'", &start)) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1158 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1159 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1160 | DSLExpression expression; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1161 | if (this->peek().fKind != Token::Kind::TK_SEMICOLON) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1162 | DSLExpression next = this->expression(); |
| 1163 | if (!next.hasValue()) { |
| 1164 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1165 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1166 | expression.swap(next); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1167 | } |
| 1168 | if (!this->expect(Token::Kind::TK_SEMICOLON, "';'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1169 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1170 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1171 | return Return(expression.hasValue() ? std::move(expression) : DSLExpression(), |
| 1172 | this->position(start)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | /* BREAK SEMICOLON */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1176 | DSLStatement DSLParser::breakStatement() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1177 | Token start; |
| 1178 | if (!this->expect(Token::Kind::TK_BREAK, "'break'", &start)) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1179 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1180 | } |
| 1181 | if (!this->expect(Token::Kind::TK_SEMICOLON, "';'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1182 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1183 | } |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 1184 | return Break(this->position(start)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | /* CONTINUE SEMICOLON */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1188 | DSLStatement DSLParser::continueStatement() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1189 | Token start; |
| 1190 | if (!this->expect(Token::Kind::TK_CONTINUE, "'continue'", &start)) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1191 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1192 | } |
| 1193 | if (!this->expect(Token::Kind::TK_SEMICOLON, "';'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1194 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1195 | } |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 1196 | return Continue(this->position(start)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | /* DISCARD SEMICOLON */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1200 | DSLStatement DSLParser::discardStatement() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1201 | Token start; |
| 1202 | if (!this->expect(Token::Kind::TK_DISCARD, "'continue'", &start)) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1203 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1204 | } |
| 1205 | if (!this->expect(Token::Kind::TK_SEMICOLON, "';'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1206 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1207 | } |
Ethan Nicholas | 360db87 | 2021-09-03 17:00:09 -0400 | [diff] [blame] | 1208 | return Discard(this->position(start)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | /* LBRACE statement* RBRACE */ |
| 1212 | skstd::optional<DSLBlock> DSLParser::block() { |
| 1213 | Token start; |
| 1214 | if (!this->expect(Token::Kind::TK_LBRACE, "'{'", &start)) { |
| 1215 | return skstd::nullopt; |
| 1216 | } |
| 1217 | AutoDSLDepth depth(this); |
| 1218 | if (!depth.increase()) { |
| 1219 | return skstd::nullopt; |
| 1220 | } |
| 1221 | AutoDSLSymbolTable symbols; |
Ethan Nicholas | 96dbf74 | 2021-09-10 15:59:11 -0400 | [diff] [blame] | 1222 | StatementArray statements; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1223 | for (;;) { |
| 1224 | switch (this->peek().fKind) { |
| 1225 | case Token::Kind::TK_RBRACE: |
| 1226 | this->nextToken(); |
| 1227 | return DSLBlock(std::move(statements), CurrentSymbolTable()); |
| 1228 | case Token::Kind::TK_END_OF_FILE: |
| 1229 | this->error(this->peek(), "expected '}', but found end of file"); |
| 1230 | return skstd::nullopt; |
| 1231 | default: { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1232 | DSLStatement statement = this->statement(); |
| 1233 | if (!statement.hasValue()) { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1234 | return skstd::nullopt; |
| 1235 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1236 | statements.push_back(statement.release()); |
Ethan Nicholas | b13f369 | 2021-09-10 16:49:42 -0400 | [diff] [blame] | 1237 | break; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1238 | } |
| 1239 | } |
| 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | /* expression SEMICOLON */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1244 | DSLStatement DSLParser::expressionStatement() { |
| 1245 | DSLExpression expr = this->expression(); |
| 1246 | if (expr.hasValue()) { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1247 | if (!this->expect(Token::Kind::TK_SEMICOLON, "';'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1248 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1249 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1250 | return DSLStatement(std::move(expr)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1251 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1252 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | /* assignmentExpression (COMMA assignmentExpression)* */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1256 | DSLExpression DSLParser::expression() { |
| 1257 | DSLExpression result = this->assignmentExpression(); |
| 1258 | if (!result.hasValue()) { |
| 1259 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1260 | } |
| 1261 | Token t; |
| 1262 | AutoDSLDepth depth(this); |
| 1263 | while (this->checkNext(Token::Kind::TK_COMMA, &t)) { |
| 1264 | if (!depth.increase()) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1265 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1266 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1267 | DSLExpression right = this->assignmentExpression(); |
| 1268 | if (!right.hasValue()) { |
| 1269 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1270 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1271 | DSLExpression next = dsl::operator,(std::move(result), std::move(right)); |
| 1272 | result.swap(next); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1273 | } |
| 1274 | return result; |
| 1275 | } |
| 1276 | |
| 1277 | #define OPERATOR_RIGHT(op, exprType) \ |
| 1278 | do { \ |
| 1279 | this->nextToken(); \ |
| 1280 | if (!depth.increase()) { \ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1281 | return {}; \ |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1282 | } \ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1283 | DSLExpression right = this->exprType(); \ |
| 1284 | if (!right.hasValue()) { \ |
| 1285 | return {}; \ |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1286 | } \ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1287 | DSLExpression next = std::move(result) op std::move(right); \ |
| 1288 | result.swap(next); \ |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1289 | } while (false) |
| 1290 | |
| 1291 | /* ternaryExpression ((EQEQ | STAREQ | SLASHEQ | PERCENTEQ | PLUSEQ | MINUSEQ | SHLEQ | SHREQ | |
| 1292 | BITWISEANDEQ | BITWISEXOREQ | BITWISEOREQ | LOGICALANDEQ | LOGICALXOREQ | LOGICALOREQ) |
| 1293 | assignmentExpression)* |
| 1294 | */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1295 | DSLExpression DSLParser::assignmentExpression() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1296 | AutoDSLDepth depth(this); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1297 | DSLExpression result = this->ternaryExpression(); |
| 1298 | if (!result.hasValue()) { |
| 1299 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1300 | } |
| 1301 | for (;;) { |
| 1302 | switch (this->peek().fKind) { |
| 1303 | case Token::Kind::TK_EQ: OPERATOR_RIGHT(=, assignmentExpression); break; |
| 1304 | case Token::Kind::TK_STAREQ: OPERATOR_RIGHT(*=, assignmentExpression); break; |
| 1305 | case Token::Kind::TK_SLASHEQ: OPERATOR_RIGHT(/=, assignmentExpression); break; |
| 1306 | case Token::Kind::TK_PERCENTEQ: OPERATOR_RIGHT(%=, assignmentExpression); break; |
| 1307 | case Token::Kind::TK_PLUSEQ: OPERATOR_RIGHT(+=, assignmentExpression); break; |
| 1308 | case Token::Kind::TK_MINUSEQ: OPERATOR_RIGHT(-=, assignmentExpression); break; |
| 1309 | case Token::Kind::TK_SHLEQ: OPERATOR_RIGHT(<<=, assignmentExpression); break; |
| 1310 | case Token::Kind::TK_SHREQ: OPERATOR_RIGHT(>>=, assignmentExpression); break; |
| 1311 | case Token::Kind::TK_BITWISEANDEQ: OPERATOR_RIGHT(&=, assignmentExpression); break; |
| 1312 | case Token::Kind::TK_BITWISEXOREQ: OPERATOR_RIGHT(^=, assignmentExpression); break; |
| 1313 | case Token::Kind::TK_BITWISEOREQ: OPERATOR_RIGHT(|=, assignmentExpression); break; |
| 1314 | default: |
| 1315 | return result; |
| 1316 | } |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | /* logicalOrExpression ('?' expression ':' assignmentExpression)? */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1321 | DSLExpression DSLParser::ternaryExpression() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1322 | AutoDSLDepth depth(this); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1323 | DSLExpression base = this->logicalOrExpression(); |
| 1324 | if (!base.hasValue()) { |
| 1325 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1326 | } |
| 1327 | if (this->checkNext(Token::Kind::TK_QUESTION)) { |
| 1328 | if (!depth.increase()) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1329 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1330 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1331 | DSLExpression trueExpr = this->expression(); |
| 1332 | if (!trueExpr.hasValue()) { |
| 1333 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1334 | } |
| 1335 | if (this->expect(Token::Kind::TK_COLON, "':'")) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1336 | DSLExpression falseExpr = this->assignmentExpression(); |
| 1337 | if (!falseExpr.hasValue()) { |
| 1338 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1339 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1340 | return Select(std::move(base), std::move(trueExpr), std::move(falseExpr)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1341 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1342 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1343 | } |
| 1344 | return base; |
| 1345 | } |
| 1346 | |
| 1347 | /* logicalXorExpression (LOGICALOR logicalXorExpression)* */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1348 | DSLExpression DSLParser::logicalOrExpression() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1349 | AutoDSLDepth depth(this); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1350 | DSLExpression result = this->logicalXorExpression(); |
| 1351 | if (!result.hasValue()) { |
| 1352 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1353 | } |
| 1354 | while (this->peek().fKind == Token::Kind::TK_LOGICALOR) { |
| 1355 | OPERATOR_RIGHT(||, logicalXorExpression); |
| 1356 | } |
| 1357 | return result; |
| 1358 | } |
| 1359 | |
| 1360 | /* logicalAndExpression (LOGICALXOR logicalAndExpression)* */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1361 | DSLExpression DSLParser::logicalXorExpression() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1362 | AutoDSLDepth depth(this); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1363 | DSLExpression result = this->logicalAndExpression(); |
| 1364 | if (!result.hasValue()) { |
| 1365 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1366 | } |
| 1367 | while (this->checkNext(Token::Kind::TK_LOGICALXOR)) { |
| 1368 | if (!depth.increase()) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1369 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1370 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1371 | DSLExpression right = this->logicalAndExpression(); |
| 1372 | if (!right.hasValue()) { |
| 1373 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1374 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1375 | DSLExpression next = LogicalXor(std::move(result), std::move(right)); |
| 1376 | result.swap(next); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1377 | } |
| 1378 | return result; |
| 1379 | } |
| 1380 | |
| 1381 | /* bitwiseOrExpression (LOGICALAND bitwiseOrExpression)* */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1382 | DSLExpression DSLParser::logicalAndExpression() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1383 | AutoDSLDepth depth(this); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1384 | DSLExpression result = this->bitwiseOrExpression(); |
| 1385 | if (!result.hasValue()) { |
| 1386 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1387 | } |
| 1388 | while (this->peek().fKind == Token::Kind::TK_LOGICALAND) { |
| 1389 | OPERATOR_RIGHT(&&, bitwiseOrExpression); |
| 1390 | } |
| 1391 | return result; |
| 1392 | } |
| 1393 | |
| 1394 | /* bitwiseXorExpression (BITWISEOR bitwiseXorExpression)* */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1395 | DSLExpression DSLParser::bitwiseOrExpression() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1396 | AutoDSLDepth depth(this); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1397 | DSLExpression result = this->bitwiseXorExpression(); |
| 1398 | if (!result.hasValue()) { |
| 1399 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1400 | } |
| 1401 | while (this->peek().fKind == Token::Kind::TK_BITWISEOR) { |
| 1402 | OPERATOR_RIGHT(|, bitwiseXorExpression); |
| 1403 | } |
| 1404 | return result; |
| 1405 | } |
| 1406 | |
| 1407 | /* bitwiseAndExpression (BITWISEXOR bitwiseAndExpression)* */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1408 | DSLExpression DSLParser::bitwiseXorExpression() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1409 | AutoDSLDepth depth(this); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1410 | DSLExpression result = this->bitwiseAndExpression(); |
| 1411 | if (!result.hasValue()) { |
| 1412 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1413 | } |
| 1414 | while (this->peek().fKind == Token::Kind::TK_BITWISEXOR) { |
| 1415 | OPERATOR_RIGHT(^, bitwiseAndExpression); |
| 1416 | } |
| 1417 | return result; |
| 1418 | } |
| 1419 | |
| 1420 | /* equalityExpression (BITWISEAND equalityExpression)* */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1421 | DSLExpression DSLParser::bitwiseAndExpression() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1422 | AutoDSLDepth depth(this); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1423 | DSLExpression result = this->equalityExpression(); |
| 1424 | if (!result.hasValue()) { |
| 1425 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1426 | } |
| 1427 | while (this->peek().fKind == Token::Kind::TK_BITWISEAND) { |
| 1428 | OPERATOR_RIGHT(&, equalityExpression); |
| 1429 | } |
| 1430 | return result; |
| 1431 | } |
| 1432 | |
| 1433 | /* relationalExpression ((EQEQ | NEQ) relationalExpression)* */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1434 | DSLExpression DSLParser::equalityExpression() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1435 | AutoDSLDepth depth(this); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1436 | DSLExpression result = this->relationalExpression(); |
| 1437 | if (!result.hasValue()) { |
| 1438 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1439 | } |
| 1440 | for (;;) { |
| 1441 | switch (this->peek().fKind) { |
| 1442 | case Token::Kind::TK_EQEQ: OPERATOR_RIGHT(==, relationalExpression); break; |
| 1443 | case Token::Kind::TK_NEQ: OPERATOR_RIGHT(!=, relationalExpression); break; |
| 1444 | default: return result; |
| 1445 | } |
| 1446 | } |
| 1447 | } |
| 1448 | |
| 1449 | /* shiftExpression ((LT | GT | LTEQ | GTEQ) shiftExpression)* */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1450 | DSLExpression DSLParser::relationalExpression() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1451 | AutoDSLDepth depth(this); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1452 | DSLExpression result = this->shiftExpression(); |
| 1453 | if (!result.hasValue()) { |
| 1454 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1455 | } |
| 1456 | for (;;) { |
| 1457 | switch (this->peek().fKind) { |
| 1458 | case Token::Kind::TK_LT: OPERATOR_RIGHT(<, shiftExpression); break; |
| 1459 | case Token::Kind::TK_GT: OPERATOR_RIGHT(>, shiftExpression); break; |
| 1460 | case Token::Kind::TK_LTEQ: OPERATOR_RIGHT(<=, shiftExpression); break; |
| 1461 | case Token::Kind::TK_GTEQ: OPERATOR_RIGHT(>=, shiftExpression); break; |
| 1462 | default: return result; |
| 1463 | } |
| 1464 | } |
| 1465 | } |
| 1466 | |
| 1467 | /* additiveExpression ((SHL | SHR) additiveExpression)* */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1468 | DSLExpression DSLParser::shiftExpression() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1469 | AutoDSLDepth depth(this); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1470 | DSLExpression result = this->additiveExpression(); |
| 1471 | if (!result.hasValue()) { |
| 1472 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1473 | } |
| 1474 | for (;;) { |
| 1475 | switch (this->peek().fKind) { |
| 1476 | case Token::Kind::TK_SHL: OPERATOR_RIGHT(<<, additiveExpression); break; |
| 1477 | case Token::Kind::TK_SHR: OPERATOR_RIGHT(>>, additiveExpression); break; |
| 1478 | default: return result; |
| 1479 | } |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | /* multiplicativeExpression ((PLUS | MINUS) multiplicativeExpression)* */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1484 | DSLExpression DSLParser::additiveExpression() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1485 | AutoDSLDepth depth(this); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1486 | DSLExpression result = this->multiplicativeExpression(); |
| 1487 | if (!result.hasValue()) { |
| 1488 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1489 | } |
| 1490 | for (;;) { |
| 1491 | switch (this->peek().fKind) { |
| 1492 | case Token::Kind::TK_PLUS: OPERATOR_RIGHT(+, multiplicativeExpression); break; |
| 1493 | case Token::Kind::TK_MINUS: OPERATOR_RIGHT(-, multiplicativeExpression); break; |
| 1494 | default: return result; |
| 1495 | } |
| 1496 | } |
| 1497 | } |
| 1498 | |
| 1499 | /* unaryExpression ((STAR | SLASH | PERCENT) unaryExpression)* */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1500 | DSLExpression DSLParser::multiplicativeExpression() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1501 | AutoDSLDepth depth(this); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1502 | DSLExpression result = this->unaryExpression(); |
| 1503 | if (!result.hasValue()) { |
| 1504 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1505 | } |
| 1506 | for (;;) { |
| 1507 | switch (this->peek().fKind) { |
| 1508 | case Token::Kind::TK_STAR: OPERATOR_RIGHT(*, unaryExpression); break; |
| 1509 | case Token::Kind::TK_SLASH: OPERATOR_RIGHT(/, unaryExpression); break; |
| 1510 | case Token::Kind::TK_PERCENT: OPERATOR_RIGHT(%, unaryExpression); break; |
| 1511 | default: return result; |
| 1512 | } |
| 1513 | } |
| 1514 | } |
| 1515 | |
| 1516 | /* postfixExpression | (PLUS | MINUS | NOT | PLUSPLUS | MINUSMINUS) unaryExpression */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1517 | DSLExpression DSLParser::unaryExpression() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1518 | AutoDSLDepth depth(this); |
| 1519 | Token next = this->peek(); |
| 1520 | switch (next.fKind) { |
| 1521 | case Token::Kind::TK_PLUS: |
| 1522 | case Token::Kind::TK_MINUS: |
| 1523 | case Token::Kind::TK_LOGICALNOT: |
| 1524 | case Token::Kind::TK_BITWISENOT: |
| 1525 | case Token::Kind::TK_PLUSPLUS: |
| 1526 | case Token::Kind::TK_MINUSMINUS: { |
| 1527 | if (!depth.increase()) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1528 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1529 | } |
| 1530 | this->nextToken(); |
Ethan Nicholas | 0dc1e0f | 2021-09-17 12:52:55 -0400 | [diff] [blame] | 1531 | DSLExpression expr = this->unaryExpression(); |
| 1532 | if (!expr.hasValue()) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1533 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1534 | } |
| 1535 | switch (next.fKind) { |
Ethan Nicholas | 0dc1e0f | 2021-09-17 12:52:55 -0400 | [diff] [blame] | 1536 | case Token::Kind::TK_PLUS: return {{ +std::move(expr)}}; |
| 1537 | case Token::Kind::TK_MINUS: return {{ -std::move(expr)}}; |
| 1538 | case Token::Kind::TK_LOGICALNOT: return {{ !std::move(expr)}}; |
| 1539 | case Token::Kind::TK_BITWISENOT: return {{ ~std::move(expr)}}; |
| 1540 | case Token::Kind::TK_PLUSPLUS: return {{++std::move(expr)}}; |
| 1541 | case Token::Kind::TK_MINUSMINUS: return {{--std::move(expr)}}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1542 | default: SkUNREACHABLE; |
| 1543 | } |
| 1544 | } |
| 1545 | default: |
| 1546 | return this->postfixExpression(); |
| 1547 | } |
| 1548 | } |
| 1549 | |
| 1550 | /* term suffix* */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1551 | DSLExpression DSLParser::postfixExpression() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1552 | AutoDSLDepth depth(this); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1553 | DSLExpression result = this->term(); |
| 1554 | if (!result.hasValue()) { |
| 1555 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1556 | } |
| 1557 | for (;;) { |
| 1558 | Token t = this->peek(); |
| 1559 | switch (t.fKind) { |
| 1560 | case Token::Kind::TK_FLOAT_LITERAL: |
| 1561 | if (this->text(t)[0] != '.') { |
| 1562 | return result; |
| 1563 | } |
| 1564 | [[fallthrough]]; |
| 1565 | case Token::Kind::TK_LBRACKET: |
| 1566 | case Token::Kind::TK_DOT: |
| 1567 | case Token::Kind::TK_LPAREN: |
| 1568 | case Token::Kind::TK_PLUSPLUS: |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1569 | case Token::Kind::TK_MINUSMINUS: { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1570 | if (!depth.increase()) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1571 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1572 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1573 | DSLExpression next = this->suffix(std::move(result)); |
| 1574 | if (!next.hasValue()) { |
| 1575 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1576 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1577 | result.swap(next); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1578 | break; |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1579 | } |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1580 | default: |
| 1581 | return result; |
| 1582 | } |
| 1583 | } |
| 1584 | } |
| 1585 | |
Brian Osman | cc91452 | 2021-09-24 18:58:37 +0000 | [diff] [blame] | 1586 | DSLExpression DSLParser::swizzle(int offset, DSLExpression base, |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1587 | skstd::string_view swizzleMask) { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1588 | SkASSERT(swizzleMask.length() > 0); |
| 1589 | if (!base.type().isVector() && !base.type().isScalar()) { |
Brian Osman | cc91452 | 2021-09-24 18:58:37 +0000 | [diff] [blame] | 1590 | return base.field(swizzleMask, this->position(offset)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1591 | } |
| 1592 | int length = swizzleMask.length(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1593 | SkSL::SwizzleComponent::Type components[4]; |
| 1594 | for (int i = 0; i < length; ++i) { |
Ethan Nicholas | be8f73d | 2021-08-28 19:50:03 -0400 | [diff] [blame] | 1595 | if (i >= 4) { |
Brian Osman | cc91452 | 2021-09-24 18:58:37 +0000 | [diff] [blame] | 1596 | this->error(offset, "too many components in swizzle mask"); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1597 | return DSLExpression::Poison(); |
Ethan Nicholas | be8f73d | 2021-08-28 19:50:03 -0400 | [diff] [blame] | 1598 | } |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1599 | switch (swizzleMask[i]) { |
| 1600 | case '0': components[i] = SwizzleComponent::ZERO; break; |
| 1601 | case '1': components[i] = SwizzleComponent::ONE; break; |
Ethan Nicholas | b61a243 | 2021-09-02 16:38:43 -0400 | [diff] [blame] | 1602 | case 'r': components[i] = SwizzleComponent::R; break; |
| 1603 | case 'x': components[i] = SwizzleComponent::X; break; |
| 1604 | case 's': components[i] = SwizzleComponent::S; break; |
| 1605 | case 'L': components[i] = SwizzleComponent::UL; break; |
| 1606 | case 'g': components[i] = SwizzleComponent::G; break; |
| 1607 | case 'y': components[i] = SwizzleComponent::Y; break; |
| 1608 | case 't': components[i] = SwizzleComponent::T; break; |
| 1609 | case 'T': components[i] = SwizzleComponent::UT; break; |
| 1610 | case 'b': components[i] = SwizzleComponent::B; break; |
| 1611 | case 'z': components[i] = SwizzleComponent::Z; break; |
| 1612 | case 'p': components[i] = SwizzleComponent::P; break; |
| 1613 | case 'R': components[i] = SwizzleComponent::UR; break; |
| 1614 | case 'a': components[i] = SwizzleComponent::A; break; |
| 1615 | case 'w': components[i] = SwizzleComponent::W; break; |
| 1616 | case 'q': components[i] = SwizzleComponent::Q; break; |
| 1617 | case 'B': components[i] = SwizzleComponent::UB; break; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1618 | default: |
Brian Osman | cc91452 | 2021-09-24 18:58:37 +0000 | [diff] [blame] | 1619 | this->error(offset, |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1620 | String::printf("invalid swizzle component '%c'", swizzleMask[i]).c_str()); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1621 | return DSLExpression::Poison(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1622 | } |
| 1623 | } |
| 1624 | switch (length) { |
| 1625 | case 1: return dsl::Swizzle(std::move(base), components[0]); |
| 1626 | case 2: return dsl::Swizzle(std::move(base), components[0], components[1]); |
| 1627 | case 3: return dsl::Swizzle(std::move(base), components[0], components[1], components[2]); |
| 1628 | case 4: return dsl::Swizzle(std::move(base), components[0], components[1], components[2], |
| 1629 | components[3]); |
| 1630 | default: SkUNREACHABLE; |
| 1631 | } |
| 1632 | } |
| 1633 | |
Brian Osman | cc91452 | 2021-09-24 18:58:37 +0000 | [diff] [blame] | 1634 | dsl::DSLExpression DSLParser::call(int offset, dsl::DSLExpression base, ExpressionArray args) { |
| 1635 | return DSLExpression(base(std::move(args), this->position(offset)), this->position(offset)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1636 | } |
| 1637 | |
| 1638 | /* LBRACKET expression? RBRACKET | DOT IDENTIFIER | LPAREN arguments RPAREN | |
| 1639 | PLUSPLUS | MINUSMINUS | COLONCOLON IDENTIFIER | FLOAT_LITERAL [IDENTIFIER] */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1640 | DSLExpression DSLParser::suffix(DSLExpression base) { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1641 | Token next = this->nextToken(); |
| 1642 | AutoDSLDepth depth(this); |
| 1643 | if (!depth.increase()) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1644 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1645 | } |
| 1646 | switch (next.fKind) { |
| 1647 | case Token::Kind::TK_LBRACKET: { |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 1648 | if (this->checkNext(Token::Kind::TK_RBRACKET)) { |
| 1649 | this->error(next, "missing index in '[]'"); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1650 | return DSLExpression::Poison(); |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 1651 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1652 | DSLExpression index = this->expression(); |
| 1653 | if (!index.hasValue()) { |
| 1654 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1655 | } |
| 1656 | this->expect(Token::Kind::TK_RBRACKET, "']' to complete array access expression"); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1657 | DSLPossibleExpression result = base[std::move(index)]; |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 1658 | if (!result.valid()) { |
| 1659 | result.reportErrors(this->position(next)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1660 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1661 | return std::move(result); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1662 | } |
| 1663 | case Token::Kind::TK_DOT: { |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame^] | 1664 | int line = this->peek().fLine; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1665 | skstd::string_view text; |
| 1666 | if (this->identifier(&text)) { |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame^] | 1667 | return this->swizzle(line, std::move(base), text); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1668 | } |
| 1669 | [[fallthrough]]; |
| 1670 | } |
| 1671 | case Token::Kind::TK_FLOAT_LITERAL: { |
| 1672 | // Swizzles that start with a constant number, e.g. '.000r', will be tokenized as |
| 1673 | // floating point literals, possibly followed by an identifier. Handle that here. |
| 1674 | skstd::string_view field = this->text(next); |
| 1675 | SkASSERT(field[0] == '.'); |
| 1676 | field.remove_prefix(1); |
| 1677 | // use the next *raw* token so we don't ignore whitespace - we only care about |
| 1678 | // identifiers that directly follow the float |
| 1679 | Token id = this->nextRawToken(); |
| 1680 | if (id.fKind == Token::Kind::TK_IDENTIFIER) { |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame^] | 1681 | return this->swizzle(next.fLine, std::move(base), field + this->text(id)); |
Ethan Nicholas | bf4a7d5 | 2021-09-09 09:32:13 -0400 | [diff] [blame] | 1682 | } else if (field.empty()) { |
| 1683 | this->error(next, "expected field name or swizzle mask after '.'"); |
| 1684 | return {{DSLExpression::Poison()}}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1685 | } |
| 1686 | this->pushback(id); |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame^] | 1687 | return this->swizzle(next.fLine, std::move(base), field); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1688 | } |
| 1689 | case Token::Kind::TK_LPAREN: { |
Ethan Nicholas | 9a1f92e | 2021-09-09 15:03:22 -0400 | [diff] [blame] | 1690 | ExpressionArray args; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1691 | if (this->peek().fKind != Token::Kind::TK_RPAREN) { |
| 1692 | for (;;) { |
Ethan Nicholas | f62934b | 2021-09-20 10:18:58 -0400 | [diff] [blame] | 1693 | DSLExpression expr = this->assignmentExpression(); |
| 1694 | if (!expr.hasValue()) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1695 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1696 | } |
Ethan Nicholas | f62934b | 2021-09-20 10:18:58 -0400 | [diff] [blame] | 1697 | args.push_back(expr.release()); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1698 | if (!this->checkNext(Token::Kind::TK_COMMA)) { |
| 1699 | break; |
| 1700 | } |
| 1701 | } |
| 1702 | } |
| 1703 | this->expect(Token::Kind::TK_RPAREN, "')' to complete function arguments"); |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame^] | 1704 | return this->call(next.fLine, std::move(base), std::move(args)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1705 | } |
| 1706 | case Token::Kind::TK_PLUSPLUS: |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1707 | return std::move(base)++; |
| 1708 | case Token::Kind::TK_MINUSMINUS: |
| 1709 | return std::move(base)--; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1710 | default: { |
| 1711 | this->error(next, "expected expression suffix, but found '" + this->text(next) + "'"); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1712 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1713 | } |
| 1714 | } |
| 1715 | } |
| 1716 | |
| 1717 | /* IDENTIFIER | intLiteral | floatLiteral | boolLiteral | '(' expression ')' */ |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1718 | DSLExpression DSLParser::term() { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1719 | Token t = this->peek(); |
| 1720 | switch (t.fKind) { |
| 1721 | case Token::Kind::TK_IDENTIFIER: { |
| 1722 | skstd::string_view text; |
| 1723 | if (this->identifier(&text)) { |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 1724 | return dsl::Symbol(text, this->position(t)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1725 | } |
| 1726 | break; |
| 1727 | } |
| 1728 | case Token::Kind::TK_INT_LITERAL: { |
| 1729 | SKSL_INT i; |
Ethan Nicholas | 0459a93 | 2021-09-01 14:54:44 -0400 | [diff] [blame] | 1730 | if (!this->intLiteral(&i)) { |
| 1731 | i = 0; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1732 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1733 | return DSLExpression(i, this->position(t)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1734 | } |
| 1735 | case Token::Kind::TK_FLOAT_LITERAL: { |
| 1736 | SKSL_FLOAT f; |
Ethan Nicholas | 0459a93 | 2021-09-01 14:54:44 -0400 | [diff] [blame] | 1737 | if (!this->floatLiteral(&f)) { |
| 1738 | f = 0.0f; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1739 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1740 | return DSLExpression(f, this->position(t)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1741 | } |
| 1742 | case Token::Kind::TK_TRUE_LITERAL: // fall through |
| 1743 | case Token::Kind::TK_FALSE_LITERAL: { |
| 1744 | bool b; |
Ethan Nicholas | 0459a93 | 2021-09-01 14:54:44 -0400 | [diff] [blame] | 1745 | SkAssertResult(this->boolLiteral(&b)); |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1746 | return DSLExpression(b, this->position(t)); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1747 | } |
| 1748 | case Token::Kind::TK_LPAREN: { |
| 1749 | this->nextToken(); |
| 1750 | AutoDSLDepth depth(this); |
| 1751 | if (!depth.increase()) { |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1752 | return {}; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1753 | } |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 1754 | DSLExpression result = this->expression(); |
| 1755 | if (result.hasValue()) { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 1756 | this->expect(Token::Kind::TK_RPAREN, "')' to complete expression"); |
| 1757 | return result; |
| 1758 | } |
| 1759 | break; |
| 1760 | } |
| 1761 | default: |
| 1762 | this->nextToken(); |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame^] | 1763 | this->error(t, "expected expression, but found '" + this->text(t) + "'"); |
Ethan Nicholas | ad284fe | 2021-09-01 10:17:48 -0400 | [diff] [blame] | 1764 | fEncounteredFatalError = true; |
Ethan Nicholas | 5fad2b88 | 2021-09-27 10:39:18 -0400 | [diff] [blame^] | 1765 | break; |
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 |