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 | #ifndef SKSL_DSLPARSER |
| 9 | #define SKSL_DSLPARSER |
| 10 | |
| 11 | #include <memory> |
| 12 | #include <unordered_map> |
| 13 | #include "include/core/SkStringView.h" |
| 14 | #include "include/private/SkSLProgramKind.h" |
| 15 | #include "include/private/SkTOptional.h" |
| 16 | #include "include/sksl/DSL.h" |
| 17 | #include "include/sksl/DSLSymbols.h" |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 18 | #include "src/sksl/SkSLLexer.h" |
| 19 | #include "src/sksl/ir/SkSLProgram.h" |
| 20 | |
| 21 | #if SKSL_DSL_PARSER |
| 22 | |
| 23 | namespace SkSL { |
| 24 | |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 25 | class ErrorReporter; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 26 | struct Modifiers; |
Ethan Nicholas | 051aeb7 | 2021-09-24 16:39:19 -0400 | [diff] [blame] | 27 | struct ParsedModule; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 28 | class SymbolTable; |
| 29 | |
| 30 | /** |
| 31 | * Consumes .sksl text and invokes DSL functions to instantiate the program. |
| 32 | */ |
| 33 | class DSLParser { |
| 34 | public: |
| 35 | enum class LayoutToken { |
| 36 | LOCATION, |
| 37 | OFFSET, |
| 38 | BINDING, |
| 39 | INDEX, |
| 40 | SET, |
| 41 | BUILTIN, |
| 42 | INPUT_ATTACHMENT_INDEX, |
| 43 | ORIGIN_UPPER_LEFT, |
| 44 | OVERRIDE_COVERAGE, |
| 45 | EARLY_FRAGMENT_TESTS, |
| 46 | BLEND_SUPPORT_ALL_EQUATIONS, |
| 47 | PUSH_CONSTANT, |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 48 | MARKER, |
| 49 | WHEN, |
| 50 | KEY, |
| 51 | TRACKED, |
| 52 | SRGB_UNPREMUL, |
| 53 | CTYPE, |
| 54 | SKPMCOLOR4F, |
| 55 | SKV4, |
| 56 | SKRECT, |
| 57 | SKIRECT, |
| 58 | SKPMCOLOR, |
| 59 | SKM44, |
| 60 | BOOL, |
| 61 | INT, |
| 62 | FLOAT, |
| 63 | }; |
| 64 | |
| 65 | DSLParser(Compiler* compiler, const ProgramSettings& settings, ProgramKind kind, |
| 66 | String text); |
| 67 | |
| 68 | std::unique_ptr<Program> program(); |
| 69 | |
Ethan Nicholas | 051aeb7 | 2021-09-24 16:39:19 -0400 | [diff] [blame] | 70 | SkSL::LoadedModule moduleInheritingFrom(SkSL::ParsedModule baseModule); |
| 71 | |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 72 | skstd::string_view text(Token token); |
| 73 | |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 74 | PositionInfo position(Token token); |
| 75 | |
Brian Osman | cc91452 | 2021-09-24 18:58:37 +0000 | [diff] [blame] | 76 | PositionInfo position(int offset); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 77 | |
| 78 | private: |
| 79 | static void InitLayoutMap(); |
| 80 | |
| 81 | /** |
| 82 | * Return the next token, including whitespace tokens, from the parse stream. |
| 83 | */ |
| 84 | Token nextRawToken(); |
| 85 | |
| 86 | /** |
| 87 | * Return the next non-whitespace token from the parse stream. |
| 88 | */ |
| 89 | Token nextToken(); |
| 90 | |
| 91 | /** |
| 92 | * Push a token back onto the parse stream, so that it is the next one read. Only a single level |
| 93 | * of pushback is supported (that is, it is an error to call pushback() twice in a row without |
| 94 | * an intervening nextToken()). |
| 95 | */ |
| 96 | void pushback(Token t); |
| 97 | |
| 98 | /** |
| 99 | * Returns the next non-whitespace token without consuming it from the stream. |
| 100 | */ |
| 101 | Token peek(); |
| 102 | |
| 103 | /** |
| 104 | * Checks to see if the next token is of the specified type. If so, stores it in result (if |
| 105 | * result is non-null) and returns true. Otherwise, pushes it back and returns false. |
| 106 | */ |
| 107 | bool checkNext(Token::Kind kind, Token* result = nullptr); |
| 108 | |
| 109 | /** |
| 110 | * Reads the next non-whitespace token and generates an error if it is not the expected type. |
| 111 | * The 'expected' string is part of the error message, which reads: |
| 112 | * |
| 113 | * "expected <expected>, but found '<actual text>'" |
| 114 | * |
| 115 | * If 'result' is non-null, it is set to point to the token that was read. |
| 116 | * Returns true if the read token was as expected, false otherwise. |
| 117 | */ |
| 118 | bool expect(Token::Kind kind, const char* expected, Token* result = nullptr); |
| 119 | bool expect(Token::Kind kind, String expected, Token* result = nullptr); |
| 120 | |
| 121 | /** |
| 122 | * Behaves like expect(TK_IDENTIFIER), but also verifies that identifier is not a type. |
| 123 | * If the token was actually a type, generates an error message of the form: |
| 124 | * |
| 125 | * "expected an identifier, but found type 'float2'" |
| 126 | */ |
| 127 | bool expectIdentifier(Token* result); |
| 128 | |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 129 | void error(Token token, String msg); |
Brian Osman | cc91452 | 2021-09-24 18:58:37 +0000 | [diff] [blame] | 130 | void error(int offset, String msg); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 131 | |
| 132 | SymbolTable& symbols() { |
| 133 | return *dsl::CurrentSymbolTable(); |
| 134 | } |
| 135 | |
| 136 | // these functions parse individual grammar rules from the current parse position; you probably |
| 137 | // don't need to call any of these outside of the parser. The function declarations in the .cpp |
| 138 | // file have comments describing the grammar rules. |
| 139 | |
Ethan Nicholas | 051aeb7 | 2021-09-24 16:39:19 -0400 | [diff] [blame] | 140 | void declarations(); |
| 141 | |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 142 | ASTNode::ID precision(); |
| 143 | |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 144 | SKSL_INT arraySize(); |
| 145 | |
Ethan Nicholas | 642215e | 2021-09-03 11:10:54 -0400 | [diff] [blame] | 146 | void directive(); |
| 147 | |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 148 | bool declaration(); |
| 149 | |
John Stiles | e53c721 | 2021-08-05 10:19:11 -0400 | [diff] [blame] | 150 | bool functionDeclarationEnd(const dsl::DSLModifiers& modifiers, |
| 151 | dsl::DSLType type, |
| 152 | const Token& name); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 153 | |
| 154 | struct VarDeclarationsPrefix { |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 155 | PositionInfo fPosition; |
| 156 | dsl::DSLModifiers fModifiers; |
| 157 | dsl::DSLType fType = dsl::DSLType(dsl::kVoid_Type); |
| 158 | Token fName; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | bool varDeclarationsPrefix(VarDeclarationsPrefix* prefixData); |
| 162 | |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 163 | dsl::DSLStatement varDeclarationsOrExpressionStatement(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 164 | |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 165 | dsl::DSLStatement varDeclarations(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 166 | |
| 167 | skstd::optional<dsl::DSLType> structDeclaration(); |
| 168 | |
John Stiles | e53c721 | 2021-08-05 10:19:11 -0400 | [diff] [blame] | 169 | SkTArray<dsl::DSLGlobalVar> structVarDeclaration(const dsl::DSLModifiers& modifiers); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 170 | |
Brian Osman | cc91452 | 2021-09-24 18:58:37 +0000 | [diff] [blame] | 171 | bool parseArrayDimensions(int offset, dsl::DSLType* type); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 172 | |
Brian Osman | cc91452 | 2021-09-24 18:58:37 +0000 | [diff] [blame] | 173 | bool parseInitializer(int offset, dsl::DSLExpression* initializer); |
Ethan Nicholas | 6c302ba | 2021-09-14 09:16:12 -0400 | [diff] [blame] | 174 | |
| 175 | void globalVarDeclarationEnd(PositionInfo position, const dsl::DSLModifiers& mods, |
| 176 | dsl::DSLType baseType, skstd::string_view name); |
| 177 | |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 178 | dsl::DSLStatement localVarDeclarationEnd(PositionInfo position, const dsl::DSLModifiers& mods, |
| 179 | dsl::DSLType baseType, skstd::string_view name); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 180 | |
| 181 | skstd::optional<dsl::DSLWrapper<dsl::DSLParameter>> parameter(); |
| 182 | |
| 183 | int layoutInt(); |
| 184 | |
| 185 | skstd::string_view layoutIdentifier(); |
| 186 | |
| 187 | dsl::DSLLayout layout(); |
| 188 | |
| 189 | dsl::DSLModifiers modifiers(); |
| 190 | |
| 191 | dsl::DSLModifiers modifiersWithDefaults(int defaultFlags); |
| 192 | |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 193 | dsl::DSLStatement statement(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 194 | |
John Stiles | e53c721 | 2021-08-05 10:19:11 -0400 | [diff] [blame] | 195 | skstd::optional<dsl::DSLType> type(const dsl::DSLModifiers& modifiers); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 196 | |
John Stiles | e53c721 | 2021-08-05 10:19:11 -0400 | [diff] [blame] | 197 | bool interfaceBlock(const dsl::DSLModifiers& mods); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 198 | |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 199 | dsl::DSLStatement ifStatement(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 200 | |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 201 | dsl::DSLStatement doStatement(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 202 | |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 203 | dsl::DSLStatement whileStatement(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 204 | |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 205 | dsl::DSLStatement forStatement(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 206 | |
| 207 | skstd::optional<dsl::DSLCase> switchCase(); |
| 208 | |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 209 | dsl::DSLStatement switchStatement(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 210 | |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 211 | dsl::DSLStatement returnStatement(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 212 | |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 213 | dsl::DSLStatement breakStatement(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 214 | |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 215 | dsl::DSLStatement continueStatement(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 216 | |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 217 | dsl::DSLStatement discardStatement(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 218 | |
| 219 | skstd::optional<dsl::DSLBlock> block(); |
| 220 | |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 221 | dsl::DSLStatement expressionStatement(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 222 | |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 223 | dsl::DSLExpression expression(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 224 | |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 225 | dsl::DSLExpression assignmentExpression(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 226 | |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 227 | dsl::DSLExpression ternaryExpression(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 228 | |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 229 | dsl::DSLExpression logicalOrExpression(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 230 | |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 231 | dsl::DSLExpression logicalXorExpression(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 232 | |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 233 | dsl::DSLExpression logicalAndExpression(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 234 | |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 235 | dsl::DSLExpression bitwiseOrExpression(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 236 | |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 237 | dsl::DSLExpression bitwiseXorExpression(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 238 | |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 239 | dsl::DSLExpression bitwiseAndExpression(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 240 | |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 241 | dsl::DSLExpression equalityExpression(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 242 | |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 243 | dsl::DSLExpression relationalExpression(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 244 | |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 245 | dsl::DSLExpression shiftExpression(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 246 | |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 247 | dsl::DSLExpression additiveExpression(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 248 | |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 249 | dsl::DSLExpression multiplicativeExpression(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 250 | |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 251 | dsl::DSLExpression unaryExpression(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 252 | |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 253 | dsl::DSLExpression postfixExpression(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 254 | |
Brian Osman | cc91452 | 2021-09-24 18:58:37 +0000 | [diff] [blame] | 255 | dsl::DSLExpression swizzle(int offset, dsl::DSLExpression base, skstd::string_view swizzleMask); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 256 | |
Brian Osman | cc91452 | 2021-09-24 18:58:37 +0000 | [diff] [blame] | 257 | dsl::DSLExpression call(int offset, dsl::DSLExpression base, ExpressionArray args); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 258 | |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 259 | dsl::DSLExpression suffix(dsl::DSLExpression base); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 260 | |
Ethan Nicholas | b7cb38f | 2021-09-15 08:29:14 -0400 | [diff] [blame] | 261 | dsl::DSLExpression term(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 262 | |
| 263 | bool intLiteral(SKSL_INT* dest); |
| 264 | |
| 265 | bool floatLiteral(SKSL_FLOAT* dest); |
| 266 | |
| 267 | bool boolLiteral(bool* dest); |
| 268 | |
| 269 | bool identifier(skstd::string_view* dest); |
| 270 | |
Ethan Nicholas | 5547866 | 2021-08-10 17:14:26 -0400 | [diff] [blame] | 271 | class Checkpoint { |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 272 | public: |
| 273 | Checkpoint(DSLParser* p) : fParser(p) { |
| 274 | fPushbackCheckpoint = fParser->fPushback; |
| 275 | fLexerCheckpoint = fParser->fLexer.getCheckpoint(); |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 276 | fOldErrorReporter = &dsl::GetErrorReporter(); |
Ethan Nicholas | 5d97a96 | 2021-08-30 15:58:52 -0400 | [diff] [blame] | 277 | fOldEncounteredFatalError = fParser->fEncounteredFatalError; |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 278 | SkASSERT(fOldErrorReporter); |
| 279 | dsl::SetErrorReporter(&fErrorReporter); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 280 | } |
| 281 | |
Ethan Nicholas | 5547866 | 2021-08-10 17:14:26 -0400 | [diff] [blame] | 282 | ~Checkpoint() { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 283 | SkASSERTF(!fOldErrorReporter, |
Ethan Nicholas | 65e368b | 2021-08-05 14:53:02 -0400 | [diff] [blame] | 284 | "Checkpoint was not accepted or rewound before destruction"); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | void accept() { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 288 | this->restoreErrorReporter(); |
Ethan Nicholas | 65e368b | 2021-08-05 14:53:02 -0400 | [diff] [blame] | 289 | // Parser errors should have been fatal, but we can encounter other errors like type |
| 290 | // mismatches despite accepting the parse. Forward those messages to the actual error |
| 291 | // handler now. |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 292 | fErrorReporter.forwardErrors(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | void rewind() { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 296 | this->restoreErrorReporter(); |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 297 | fParser->fPushback = fPushbackCheckpoint; |
| 298 | fParser->fLexer.rewindToCheckpoint(fLexerCheckpoint); |
Ethan Nicholas | 5d97a96 | 2021-08-30 15:58:52 -0400 | [diff] [blame] | 299 | fParser->fEncounteredFatalError = fOldEncounteredFatalError; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 300 | } |
| 301 | |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 302 | private: |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 303 | class ForwardingErrorReporter : public ErrorReporter { |
Ethan Nicholas | 5547866 | 2021-08-10 17:14:26 -0400 | [diff] [blame] | 304 | public: |
Ethan Nicholas | 3272412 | 2021-09-07 13:49:07 -0400 | [diff] [blame] | 305 | void handleError(skstd::string_view msg, PositionInfo pos) override { |
Ethan Nicholas | 5547866 | 2021-08-10 17:14:26 -0400 | [diff] [blame] | 306 | fErrors.push_back({String(msg), pos}); |
| 307 | } |
| 308 | |
| 309 | void forwardErrors() { |
| 310 | for (Error& error : fErrors) { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 311 | dsl::GetErrorReporter().error(error.fMsg.c_str(), error.fPos); |
Ethan Nicholas | 5547866 | 2021-08-10 17:14:26 -0400 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
| 315 | private: |
| 316 | struct Error { |
| 317 | String fMsg; |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 318 | PositionInfo fPos; |
Ethan Nicholas | 5547866 | 2021-08-10 17:14:26 -0400 | [diff] [blame] | 319 | }; |
| 320 | |
| 321 | SkTArray<Error> fErrors; |
Ethan Nicholas | 65e368b | 2021-08-05 14:53:02 -0400 | [diff] [blame] | 322 | }; |
| 323 | |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 324 | void restoreErrorReporter() { |
| 325 | SkASSERT(fOldErrorReporter); |
Ethan Nicholas | 5c4463e | 2021-08-29 14:31:19 -0400 | [diff] [blame] | 326 | fErrorReporter.reportPendingErrors(PositionInfo()); |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 327 | dsl::SetErrorReporter(fOldErrorReporter); |
| 328 | fOldErrorReporter = nullptr; |
Ethan Nicholas | 65e368b | 2021-08-05 14:53:02 -0400 | [diff] [blame] | 329 | } |
| 330 | |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 331 | DSLParser* fParser; |
| 332 | Token fPushbackCheckpoint; |
Ethan Nicholas | 5fad2b8 | 2021-09-27 10:39:18 -0400 | [diff] [blame^] | 333 | SkSL::Lexer::Checkpoint fLexerCheckpoint; |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 334 | ForwardingErrorReporter fErrorReporter; |
| 335 | ErrorReporter* fOldErrorReporter; |
Ethan Nicholas | 5d97a96 | 2021-08-30 15:58:52 -0400 | [diff] [blame] | 336 | bool fOldEncounteredFatalError; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 337 | }; |
| 338 | |
| 339 | static std::unordered_map<skstd::string_view, LayoutToken>* layoutTokens; |
| 340 | |
| 341 | Compiler& fCompiler; |
| 342 | ProgramSettings fSettings; |
| 343 | ErrorReporter* fErrorReporter; |
Ethan Nicholas | 5d97a96 | 2021-08-30 15:58:52 -0400 | [diff] [blame] | 344 | bool fEncounteredFatalError; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 345 | ProgramKind fKind; |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 346 | std::unique_ptr<String> fText; |
Ethan Nicholas | dd2fdea | 2021-07-20 15:23:04 -0400 | [diff] [blame] | 347 | Lexer fLexer; |
| 348 | // current parse depth, used to enforce a recursion limit to try to keep us from overflowing the |
| 349 | // stack on pathological inputs |
| 350 | int fDepth = 0; |
| 351 | Token fPushback; |
| 352 | |
| 353 | friend class AutoDSLDepth; |
| 354 | friend class HCodeGenerator; |
| 355 | }; |
| 356 | |
| 357 | } // namespace SkSL |
| 358 | |
| 359 | #endif // SKSL_DSL_PARSER |
| 360 | |
| 361 | #endif |