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