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