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