ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 7 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 8 | #ifndef SKSL_PARSER |
| 9 | #define SKSL_PARSER |
| 10 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 11 | #include <vector> |
| 12 | #include <memory> |
Ethan Nicholas | 3614d9a | 2017-02-15 12:33:30 -0500 | [diff] [blame] | 13 | #include <unordered_map> |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 14 | #include <unordered_set> |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 15 | #include "src/sksl/SkSLASTFile.h" |
Mike Klein | 4b432fa | 2019-06-06 11:44:05 -0500 | [diff] [blame] | 16 | #include "src/sksl/SkSLASTNode.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/sksl/SkSLErrorReporter.h" |
| 18 | #include "src/sksl/SkSLLexer.h" |
| 19 | #include "src/sksl/ir/SkSLLayout.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 20 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 21 | namespace SkSL { |
| 22 | |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 23 | struct Modifiers; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 24 | class SymbolTable; |
| 25 | |
| 26 | /** |
| 27 | * Consumes .sksl text and produces an abstract syntax tree describing the contents. |
| 28 | */ |
| 29 | class Parser { |
| 30 | public: |
Ethan Nicholas | b93af7e | 2018-07-24 11:28:52 -0400 | [diff] [blame] | 31 | enum class LayoutToken { |
| 32 | LOCATION, |
| 33 | OFFSET, |
| 34 | BINDING, |
| 35 | INDEX, |
| 36 | SET, |
| 37 | BUILTIN, |
| 38 | INPUT_ATTACHMENT_INDEX, |
| 39 | ORIGIN_UPPER_LEFT, |
| 40 | OVERRIDE_COVERAGE, |
| 41 | BLEND_SUPPORT_ALL_EQUATIONS, |
| 42 | BLEND_SUPPORT_MULTIPLY, |
| 43 | BLEND_SUPPORT_SCREEN, |
| 44 | BLEND_SUPPORT_OVERLAY, |
| 45 | BLEND_SUPPORT_DARKEN, |
| 46 | BLEND_SUPPORT_LIGHTEN, |
| 47 | BLEND_SUPPORT_COLORDODGE, |
| 48 | BLEND_SUPPORT_COLORBURN, |
| 49 | BLEND_SUPPORT_HARDLIGHT, |
| 50 | BLEND_SUPPORT_SOFTLIGHT, |
| 51 | BLEND_SUPPORT_DIFFERENCE, |
| 52 | BLEND_SUPPORT_EXCLUSION, |
| 53 | BLEND_SUPPORT_HSL_HUE, |
| 54 | BLEND_SUPPORT_HSL_SATURATION, |
| 55 | BLEND_SUPPORT_HSL_COLOR, |
| 56 | BLEND_SUPPORT_HSL_LUMINOSITY, |
| 57 | PUSH_CONSTANT, |
| 58 | POINTS, |
| 59 | LINES, |
| 60 | LINE_STRIP, |
| 61 | LINES_ADJACENCY, |
| 62 | TRIANGLES, |
| 63 | TRIANGLE_STRIP, |
| 64 | TRIANGLES_ADJACENCY, |
| 65 | MAX_VERTICES, |
| 66 | INVOCATIONS, |
Brian Osman | f59a961 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 67 | MARKER, |
Ethan Nicholas | b93af7e | 2018-07-24 11:28:52 -0400 | [diff] [blame] | 68 | WHEN, |
| 69 | KEY, |
Ethan Nicholas | 78aceb2 | 2018-08-31 16:13:58 -0400 | [diff] [blame] | 70 | TRACKED, |
Brian Osman | b32d66b | 2020-04-30 17:12:03 -0400 | [diff] [blame] | 71 | SRGB_UNPREMUL, |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 72 | CTYPE, |
Brian Osman | f28e55d | 2018-10-03 16:35:54 -0400 | [diff] [blame] | 73 | SKPMCOLOR4F, |
Mike Reed | b26b4e7 | 2020-01-22 14:31:21 -0500 | [diff] [blame] | 74 | SKV4, |
Ethan Nicholas | 78aceb2 | 2018-08-31 16:13:58 -0400 | [diff] [blame] | 75 | SKRECT, |
| 76 | SKIRECT, |
| 77 | SKPMCOLOR, |
Mike Reed | b26b4e7 | 2020-01-22 14:31:21 -0500 | [diff] [blame] | 78 | SKM44, |
Ethan Nicholas | c1c686b | 2019-04-02 17:30:23 -0400 | [diff] [blame] | 79 | BOOL, |
| 80 | INT, |
| 81 | FLOAT, |
Ethan Nicholas | b93af7e | 2018-07-24 11:28:52 -0400 | [diff] [blame] | 82 | }; |
| 83 | |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 84 | Parser(const char* text, size_t length, SymbolTable& symbols, ErrorReporter& errors); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 85 | |
| 86 | /** |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 87 | * Consumes a complete .sksl file and returns the parse tree. Errors are reported via the |
| 88 | * ErrorReporter; the return value may contain some declarations even when errors have occurred. |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 89 | */ |
Ethan Nicholas | ba9a04f | 2020-11-06 09:28:04 -0500 | [diff] [blame] | 90 | std::unique_ptr<ASTFile> compilationUnit(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 91 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 92 | StringFragment text(Token token); |
| 93 | |
| 94 | Position position(Token token); |
| 95 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 96 | private: |
Ethan Nicholas | b93af7e | 2018-07-24 11:28:52 -0400 | [diff] [blame] | 97 | static void InitLayoutMap(); |
| 98 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 99 | /** |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 100 | * Return the next token, including whitespace tokens, from the parse stream. |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 101 | */ |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 102 | Token nextRawToken(); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 103 | |
| 104 | /** |
| 105 | * Return the next non-whitespace token from the parse stream. |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 106 | */ |
| 107 | Token nextToken(); |
| 108 | |
| 109 | /** |
| 110 | * Push a token back onto the parse stream, so that it is the next one read. Only a single level |
| 111 | * of pushback is supported (that is, it is an error to call pushback() twice in a row without |
| 112 | * an intervening nextToken()). |
| 113 | */ |
| 114 | void pushback(Token t); |
| 115 | |
| 116 | /** |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 117 | * Returns the next non-whitespace token without consuming it from the stream. |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 118 | */ |
| 119 | Token peek(); |
| 120 | |
| 121 | /** |
Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 122 | * Checks to see if the next token is of the specified type. If so, stores it in result (if |
| 123 | * result is non-null) and returns true. Otherwise, pushes it back and returns false. |
| 124 | */ |
| 125 | bool checkNext(Token::Kind kind, Token* result = nullptr); |
| 126 | |
| 127 | /** |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 128 | * Reads the next non-whitespace token and generates an error if it is not the expected type. |
| 129 | * The 'expected' string is part of the error message, which reads: |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 130 | * |
| 131 | * "expected <expected>, but found '<actual text>'" |
| 132 | * |
| 133 | * If 'result' is non-null, it is set to point to the token that was read. |
| 134 | * Returns true if the read token was as expected, false otherwise. |
| 135 | */ |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 136 | bool expect(Token::Kind kind, const char* expected, Token* result = nullptr); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 137 | bool expect(Token::Kind kind, String expected, Token* result = nullptr); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 138 | |
John Stiles | 2630ea3 | 2020-12-04 10:51:21 -0500 | [diff] [blame] | 139 | /** |
| 140 | * Behaves like expect(TK_IDENTIFIER), but also verifies that identifier is not a type. |
| 141 | * If the token was actually a type, generates an error message of the form: |
| 142 | * |
| 143 | * "expected an identifier, but found type 'float2'" |
| 144 | */ |
| 145 | bool expectIdentifier(Token* result); |
| 146 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 147 | void error(Token token, String msg); |
| 148 | void error(int offset, String msg); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 149 | /** |
| 150 | * Returns true if the 'name' identifier refers to a type name. For instance, isType("int") will |
| 151 | * always return true. |
| 152 | */ |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 153 | bool isType(StringFragment name); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 154 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 155 | // The pointer to the node may be invalidated by modifying the fNodes vector |
| 156 | ASTNode& getNode(ASTNode::ID id) { |
Ethan Nicholas | 0c8582e | 2019-07-19 09:26:46 -0400 | [diff] [blame] | 157 | SkASSERT(id.fValue >= 0 && id.fValue < (int) fFile->fNodes.size()); |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 158 | return fFile->fNodes[id.fValue]; |
| 159 | } |
| 160 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 161 | // these functions parse individual grammar rules from the current parse position; you probably |
| 162 | // don't need to call any of these outside of the parser. The function declarations in the .cpp |
| 163 | // file have comments describing the grammar rules. |
| 164 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 165 | ASTNode::ID precision(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 166 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 167 | ASTNode::ID directive(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 168 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 169 | ASTNode::ID section(); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 170 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 171 | ASTNode::ID enumDeclaration(); |
Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 172 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 173 | ASTNode::ID declaration(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 174 | |
John Stiles | 76389b7 | 2021-01-25 13:49:05 -0500 | [diff] [blame] | 175 | ASTNode::ID varDeclarationsOrExpressionStatement(); |
| 176 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 177 | ASTNode::ID varDeclarations(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 178 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 179 | ASTNode::ID structDeclaration(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 180 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 181 | ASTNode::ID structVarDeclaration(Modifiers modifiers); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 182 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 183 | ASTNode::ID varDeclarationEnd(Modifiers modifiers, ASTNode::ID type, StringFragment name); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 184 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 185 | ASTNode::ID parameter(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 186 | |
| 187 | int layoutInt(); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 188 | |
Ethan Nicholas | d608c09 | 2017-10-26 09:30:08 -0400 | [diff] [blame] | 189 | StringFragment layoutIdentifier(); |
| 190 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 191 | StringFragment layoutCode(); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 192 | |
| 193 | Layout::Key layoutKey(); |
| 194 | |
Ethan Nicholas | 78aceb2 | 2018-08-31 16:13:58 -0400 | [diff] [blame] | 195 | Layout::CType layoutCType(); |
| 196 | |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 197 | Layout layout(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 198 | |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 199 | Modifiers modifiers(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 200 | |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 201 | Modifiers modifiersWithDefaults(int defaultFlags); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 202 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 203 | ASTNode::ID statement(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 204 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 205 | ASTNode::ID type(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 206 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 207 | ASTNode::ID interfaceBlock(Modifiers mods); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 208 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 209 | ASTNode::ID ifStatement(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 210 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 211 | ASTNode::ID doStatement(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 212 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 213 | ASTNode::ID whileStatement(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 214 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 215 | ASTNode::ID forStatement(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 216 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 217 | ASTNode::ID switchCase(); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 218 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 219 | ASTNode::ID switchStatement(); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 220 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 221 | ASTNode::ID returnStatement(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 222 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 223 | ASTNode::ID breakStatement(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 224 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 225 | ASTNode::ID continueStatement(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 226 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 227 | ASTNode::ID discardStatement(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 228 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 229 | ASTNode::ID block(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 230 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 231 | ASTNode::ID expressionStatement(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 232 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 233 | ASTNode::ID expression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 234 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 235 | ASTNode::ID assignmentExpression(); |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 236 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 237 | ASTNode::ID ternaryExpression(); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 238 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 239 | ASTNode::ID logicalOrExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 240 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 241 | ASTNode::ID logicalXorExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 242 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 243 | ASTNode::ID logicalAndExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 244 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 245 | ASTNode::ID bitwiseOrExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 246 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 247 | ASTNode::ID bitwiseXorExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 248 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 249 | ASTNode::ID bitwiseAndExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 250 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 251 | ASTNode::ID equalityExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 252 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 253 | ASTNode::ID relationalExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 254 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 255 | ASTNode::ID shiftExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 256 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 257 | ASTNode::ID additiveExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 258 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 259 | ASTNode::ID multiplicativeExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 260 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 261 | ASTNode::ID unaryExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 262 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 263 | ASTNode::ID postfixExpression(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 264 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 265 | ASTNode::ID suffix(ASTNode::ID base); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 266 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 267 | ASTNode::ID term(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 268 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 269 | bool intLiteral(SKSL_INT* dest); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 270 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 271 | bool floatLiteral(SKSL_FLOAT* dest); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 272 | |
| 273 | bool boolLiteral(bool* dest); |
| 274 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 275 | bool identifier(StringFragment* dest); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 276 | |
John Stiles | 3b20936 | 2020-11-16 17:03:10 -0500 | [diff] [blame] | 277 | template <typename... Args> ASTNode::ID createNode(Args&&... args); |
| 278 | |
| 279 | ASTNode::ID addChild(ASTNode::ID target, ASTNode::ID child); |
| 280 | |
| 281 | void createEmptyChild(ASTNode::ID target); |
| 282 | |
John Stiles | 8d3642e | 2021-01-22 09:50:04 -0500 | [diff] [blame] | 283 | class Checkpoint { |
| 284 | public: |
| 285 | Checkpoint(Parser* p) : fParser(p) { |
| 286 | fPushbackCheckpoint = fParser->fPushback; |
| 287 | fLexerCheckpoint = fParser->fLexer.getCheckpoint(); |
| 288 | fASTCheckpoint = fParser->fFile->fNodes.size(); |
| 289 | fErrorCount = fParser->fErrors.errorCount(); |
| 290 | } |
| 291 | |
| 292 | void rewind() { |
| 293 | fParser->fPushback = fPushbackCheckpoint; |
| 294 | fParser->fLexer.rewindToCheckpoint(fLexerCheckpoint); |
| 295 | fParser->fFile->fNodes.resize(fASTCheckpoint); |
| 296 | fParser->fErrors.setErrorCount(fErrorCount); |
| 297 | } |
| 298 | |
| 299 | private: |
| 300 | Parser* fParser; |
| 301 | Token fPushbackCheckpoint; |
| 302 | int32_t fLexerCheckpoint; |
| 303 | size_t fASTCheckpoint; |
| 304 | int fErrorCount; |
| 305 | }; |
| 306 | |
Brian Salomon | 140f3da | 2018-08-23 13:51:27 +0000 | [diff] [blame] | 307 | static std::unordered_map<String, LayoutToken>* layoutTokens; |
Ethan Nicholas | b93af7e | 2018-07-24 11:28:52 -0400 | [diff] [blame] | 308 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 309 | const char* fText; |
| 310 | Lexer fLexer; |
ethannicholas | cad6416 | 2016-10-27 10:54:02 -0700 | [diff] [blame] | 311 | // current parse depth, used to enforce a recursion limit to try to keep us from overflowing the |
| 312 | // stack on pathological inputs |
| 313 | int fDepth = 0; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 314 | Token fPushback; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 315 | SymbolTable& fSymbols; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 316 | ErrorReporter& fErrors; |
ethannicholas | cad6416 | 2016-10-27 10:54:02 -0700 | [diff] [blame] | 317 | |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 318 | std::unique_ptr<ASTFile> fFile; |
| 319 | |
ethannicholas | cad6416 | 2016-10-27 10:54:02 -0700 | [diff] [blame] | 320 | friend class AutoDepth; |
Ethan Nicholas | 130fb3f | 2018-02-01 12:14:34 -0500 | [diff] [blame] | 321 | friend class HCodeGenerator; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 322 | }; |
| 323 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 324 | } // namespace SkSL |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 325 | |
| 326 | #endif |