| 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> | 
| Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/sksl/SkSLErrorReporter.h" | 
|  | 16 | #include "src/sksl/SkSLLexer.h" | 
|  | 17 | #include "src/sksl/ir/SkSLLayout.h" | 
| ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 18 |  | 
|  | 19 | struct yy_buffer_state; | 
|  | 20 | #define YY_TYPEDEF_YY_BUFFER_STATE | 
|  | 21 | typedef struct yy_buffer_state *YY_BUFFER_STATE; | 
|  | 22 |  | 
|  | 23 | namespace SkSL { | 
|  | 24 |  | 
|  | 25 | struct ASTBlock; | 
|  | 26 | struct ASTBreakStatement; | 
|  | 27 | struct ASTContinueStatement; | 
|  | 28 | struct ASTDeclaration; | 
|  | 29 | struct ASTDiscardStatement; | 
|  | 30 | struct ASTDoStatement; | 
|  | 31 | struct ASTExpression; | 
|  | 32 | struct ASTExpressionStatement; | 
|  | 33 | struct ASTForStatement; | 
|  | 34 | struct ASTIfStatement; | 
|  | 35 | struct ASTInterfaceBlock; | 
| ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 36 | struct ASTParameter; | 
| ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 37 | struct ASTPrecision; | 
| ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 38 | struct ASTReturnStatement; | 
|  | 39 | struct ASTStatement; | 
|  | 40 | struct ASTSuffix; | 
| Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 41 | struct ASTSwitchCase; | 
|  | 42 | struct ASTSwitchStatement; | 
| ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 43 | struct ASTType; | 
|  | 44 | struct ASTWhileStatement; | 
| ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 45 | struct ASTVarDeclarations; | 
| Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 46 | struct Modifiers; | 
| ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 47 | class SymbolTable; | 
|  | 48 |  | 
|  | 49 | /** | 
|  | 50 | * Consumes .sksl text and produces an abstract syntax tree describing the contents. | 
|  | 51 | */ | 
|  | 52 | class Parser { | 
|  | 53 | public: | 
| Ethan Nicholas | b93af7e | 2018-07-24 11:28:52 -0400 | [diff] [blame] | 54 | enum class LayoutToken { | 
|  | 55 | LOCATION, | 
|  | 56 | OFFSET, | 
|  | 57 | BINDING, | 
|  | 58 | INDEX, | 
|  | 59 | SET, | 
|  | 60 | BUILTIN, | 
|  | 61 | INPUT_ATTACHMENT_INDEX, | 
|  | 62 | ORIGIN_UPPER_LEFT, | 
|  | 63 | OVERRIDE_COVERAGE, | 
|  | 64 | BLEND_SUPPORT_ALL_EQUATIONS, | 
|  | 65 | BLEND_SUPPORT_MULTIPLY, | 
|  | 66 | BLEND_SUPPORT_SCREEN, | 
|  | 67 | BLEND_SUPPORT_OVERLAY, | 
|  | 68 | BLEND_SUPPORT_DARKEN, | 
|  | 69 | BLEND_SUPPORT_LIGHTEN, | 
|  | 70 | BLEND_SUPPORT_COLORDODGE, | 
|  | 71 | BLEND_SUPPORT_COLORBURN, | 
|  | 72 | BLEND_SUPPORT_HARDLIGHT, | 
|  | 73 | BLEND_SUPPORT_SOFTLIGHT, | 
|  | 74 | BLEND_SUPPORT_DIFFERENCE, | 
|  | 75 | BLEND_SUPPORT_EXCLUSION, | 
|  | 76 | BLEND_SUPPORT_HSL_HUE, | 
|  | 77 | BLEND_SUPPORT_HSL_SATURATION, | 
|  | 78 | BLEND_SUPPORT_HSL_COLOR, | 
|  | 79 | BLEND_SUPPORT_HSL_LUMINOSITY, | 
|  | 80 | PUSH_CONSTANT, | 
|  | 81 | POINTS, | 
|  | 82 | LINES, | 
|  | 83 | LINE_STRIP, | 
|  | 84 | LINES_ADJACENCY, | 
|  | 85 | TRIANGLES, | 
|  | 86 | TRIANGLE_STRIP, | 
|  | 87 | TRIANGLES_ADJACENCY, | 
|  | 88 | MAX_VERTICES, | 
|  | 89 | INVOCATIONS, | 
|  | 90 | WHEN, | 
|  | 91 | KEY, | 
| Ethan Nicholas | 78aceb2 | 2018-08-31 16:13:58 -0400 | [diff] [blame] | 92 | TRACKED, | 
| Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 93 | CTYPE, | 
| Brian Osman | f28e55d | 2018-10-03 16:35:54 -0400 | [diff] [blame] | 94 | SKPMCOLOR4F, | 
| Brian Salomon | eca66b3 | 2019-06-01 11:18:15 -0400 | [diff] [blame^] | 95 | SKVECTOR4, | 
| Ethan Nicholas | 78aceb2 | 2018-08-31 16:13:58 -0400 | [diff] [blame] | 96 | SKRECT, | 
|  | 97 | SKIRECT, | 
|  | 98 | SKPMCOLOR, | 
| Ethan Nicholas | 65e49ba | 2019-05-30 14:50:08 -0400 | [diff] [blame] | 99 | SKMATRIX44, | 
| Ethan Nicholas | c1c686b | 2019-04-02 17:30:23 -0400 | [diff] [blame] | 100 | BOOL, | 
|  | 101 | INT, | 
|  | 102 | FLOAT, | 
| Ethan Nicholas | b93af7e | 2018-07-24 11:28:52 -0400 | [diff] [blame] | 103 | }; | 
|  | 104 |  | 
| Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 105 | Parser(const char* text, size_t length, SymbolTable& types, ErrorReporter& errors); | 
| ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 106 |  | 
|  | 107 | /** | 
|  | 108 | * Consumes a complete .sksl file and produces a list of declarations. Errors are reported via | 
|  | 109 | * the ErrorReporter; the return value may contain some declarations even when errors have | 
|  | 110 | * occurred. | 
|  | 111 | */ | 
|  | 112 | std::vector<std::unique_ptr<ASTDeclaration>> file(); | 
|  | 113 |  | 
| Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 114 | StringFragment text(Token token); | 
|  | 115 |  | 
|  | 116 | Position position(Token token); | 
|  | 117 |  | 
| ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 118 | private: | 
| Ethan Nicholas | b93af7e | 2018-07-24 11:28:52 -0400 | [diff] [blame] | 119 | static void InitLayoutMap(); | 
|  | 120 |  | 
| ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 121 | /** | 
| Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 122 | * Return the next token, including whitespace tokens, from the parse stream. | 
| Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 123 | */ | 
| Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 124 | Token nextRawToken(); | 
| Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 125 |  | 
|  | 126 | /** | 
|  | 127 | * Return the next non-whitespace token from the parse stream. | 
| ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 128 | */ | 
|  | 129 | Token nextToken(); | 
|  | 130 |  | 
|  | 131 | /** | 
|  | 132 | * Push a token back onto the parse stream, so that it is the next one read. Only a single level | 
|  | 133 | * of pushback is supported (that is, it is an error to call pushback() twice in a row without | 
|  | 134 | * an intervening nextToken()). | 
|  | 135 | */ | 
|  | 136 | void pushback(Token t); | 
|  | 137 |  | 
|  | 138 | /** | 
| Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 139 | * Returns the next non-whitespace token without consuming it from the stream. | 
| ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 140 | */ | 
|  | 141 | Token peek(); | 
|  | 142 |  | 
|  | 143 | /** | 
| Ethan Nicholas | 0c9d13b | 2017-05-08 16:18:19 -0400 | [diff] [blame] | 144 | * Checks to see if the next token is of the specified type. If so, stores it in result (if | 
|  | 145 | * result is non-null) and returns true. Otherwise, pushes it back and returns false. | 
|  | 146 | */ | 
|  | 147 | bool checkNext(Token::Kind kind, Token* result = nullptr); | 
|  | 148 |  | 
|  | 149 | /** | 
| Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 150 | * Reads the next non-whitespace token and generates an error if it is not the expected type. | 
|  | 151 | * The 'expected' string is part of the error message, which reads: | 
| ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 152 | * | 
|  | 153 | * "expected <expected>, but found '<actual text>'" | 
|  | 154 | * | 
|  | 155 | * If 'result' is non-null, it is set to point to the token that was read. | 
|  | 156 | * Returns true if the read token was as expected, false otherwise. | 
|  | 157 | */ | 
| Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 158 | bool expect(Token::Kind kind, const char* expected, Token* result = nullptr); | 
| Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 159 | bool expect(Token::Kind kind, String expected, Token* result = nullptr); | 
| ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 160 |  | 
| Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 161 | void error(Token token, String msg); | 
|  | 162 | void error(int offset, String msg); | 
| ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 163 | /** | 
|  | 164 | * Returns true if the 'name' identifier refers to a type name. For instance, isType("int") will | 
|  | 165 | * always return true. | 
|  | 166 | */ | 
| Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 167 | bool isType(StringFragment name); | 
| ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 168 |  | 
|  | 169 | // these functions parse individual grammar rules from the current parse position; you probably | 
|  | 170 | // don't need to call any of these outside of the parser. The function declarations in the .cpp | 
|  | 171 | // file have comments describing the grammar rules. | 
|  | 172 |  | 
| ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 173 | std::unique_ptr<ASTDeclaration> precision(); | 
| ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 174 |  | 
|  | 175 | std::unique_ptr<ASTDeclaration> directive(); | 
|  | 176 |  | 
| Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 177 | std::unique_ptr<ASTDeclaration> section(); | 
|  | 178 |  | 
| Ethan Nicholas | aae47c8 | 2017-11-10 15:34:03 -0500 | [diff] [blame] | 179 | std::unique_ptr<ASTDeclaration> enumDeclaration(); | 
|  | 180 |  | 
| ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 181 | std::unique_ptr<ASTDeclaration> declaration(); | 
|  | 182 |  | 
| ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 183 | std::unique_ptr<ASTVarDeclarations> varDeclarations(); | 
| ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 184 |  | 
|  | 185 | std::unique_ptr<ASTType> structDeclaration(); | 
|  | 186 |  | 
| Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 187 | std::unique_ptr<ASTVarDeclarations> structVarDeclaration(Modifiers modifiers); | 
| ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 188 |  | 
| Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 189 | std::unique_ptr<ASTVarDeclarations> varDeclarationEnd(Modifiers modifiers, | 
|  | 190 | std::unique_ptr<ASTType> type, | 
| Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 191 | StringFragment name); | 
| ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 192 |  | 
|  | 193 | std::unique_ptr<ASTParameter> parameter(); | 
|  | 194 |  | 
|  | 195 | int layoutInt(); | 
| Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 196 |  | 
| Ethan Nicholas | d608c09 | 2017-10-26 09:30:08 -0400 | [diff] [blame] | 197 | StringFragment layoutIdentifier(); | 
|  | 198 |  | 
| Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 199 | String layoutCode(); | 
|  | 200 |  | 
|  | 201 | Layout::Key layoutKey(); | 
|  | 202 |  | 
| Ethan Nicholas | 78aceb2 | 2018-08-31 16:13:58 -0400 | [diff] [blame] | 203 | Layout::CType layoutCType(); | 
|  | 204 |  | 
| Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 205 | Layout layout(); | 
| ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 206 |  | 
| Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 207 | Modifiers modifiers(); | 
| ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 208 |  | 
| Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 209 | Modifiers modifiersWithDefaults(int defaultFlags); | 
| ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 210 |  | 
|  | 211 | std::unique_ptr<ASTStatement> statement(); | 
|  | 212 |  | 
|  | 213 | std::unique_ptr<ASTType> type(); | 
|  | 214 |  | 
| Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 215 | std::unique_ptr<ASTDeclaration> interfaceBlock(Modifiers mods); | 
| ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 216 |  | 
|  | 217 | std::unique_ptr<ASTIfStatement> ifStatement(); | 
|  | 218 |  | 
|  | 219 | std::unique_ptr<ASTDoStatement> doStatement(); | 
|  | 220 |  | 
|  | 221 | std::unique_ptr<ASTWhileStatement> whileStatement(); | 
|  | 222 |  | 
|  | 223 | std::unique_ptr<ASTForStatement> forStatement(); | 
|  | 224 |  | 
| Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 225 | std::unique_ptr<ASTSwitchCase> switchCase(); | 
|  | 226 |  | 
|  | 227 | std::unique_ptr<ASTStatement> switchStatement(); | 
|  | 228 |  | 
| ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 229 | std::unique_ptr<ASTReturnStatement> returnStatement(); | 
|  | 230 |  | 
|  | 231 | std::unique_ptr<ASTBreakStatement> breakStatement(); | 
|  | 232 |  | 
|  | 233 | std::unique_ptr<ASTContinueStatement> continueStatement(); | 
|  | 234 |  | 
|  | 235 | std::unique_ptr<ASTDiscardStatement> discardStatement(); | 
|  | 236 |  | 
|  | 237 | std::unique_ptr<ASTBlock> block(); | 
|  | 238 |  | 
|  | 239 | std::unique_ptr<ASTExpressionStatement> expressionStatement(); | 
|  | 240 |  | 
|  | 241 | std::unique_ptr<ASTExpression> expression(); | 
|  | 242 |  | 
| Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 243 | std::unique_ptr<ASTExpression> commaExpression(); | 
|  | 244 |  | 
| ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 245 | std::unique_ptr<ASTExpression> assignmentExpression(); | 
| Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 246 |  | 
| ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 247 | std::unique_ptr<ASTExpression> ternaryExpression(); | 
|  | 248 |  | 
|  | 249 | std::unique_ptr<ASTExpression> logicalOrExpression(); | 
|  | 250 |  | 
|  | 251 | std::unique_ptr<ASTExpression> logicalXorExpression(); | 
|  | 252 |  | 
|  | 253 | std::unique_ptr<ASTExpression> logicalAndExpression(); | 
|  | 254 |  | 
|  | 255 | std::unique_ptr<ASTExpression> bitwiseOrExpression(); | 
|  | 256 |  | 
|  | 257 | std::unique_ptr<ASTExpression> bitwiseXorExpression(); | 
|  | 258 |  | 
|  | 259 | std::unique_ptr<ASTExpression> bitwiseAndExpression(); | 
|  | 260 |  | 
|  | 261 | std::unique_ptr<ASTExpression> equalityExpression(); | 
|  | 262 |  | 
|  | 263 | std::unique_ptr<ASTExpression> relationalExpression(); | 
|  | 264 |  | 
|  | 265 | std::unique_ptr<ASTExpression> shiftExpression(); | 
|  | 266 |  | 
|  | 267 | std::unique_ptr<ASTExpression> additiveExpression(); | 
|  | 268 |  | 
|  | 269 | std::unique_ptr<ASTExpression> multiplicativeExpression(); | 
|  | 270 |  | 
|  | 271 | std::unique_ptr<ASTExpression> unaryExpression(); | 
|  | 272 |  | 
|  | 273 | std::unique_ptr<ASTExpression> postfixExpression(); | 
|  | 274 |  | 
|  | 275 | std::unique_ptr<ASTSuffix> suffix(); | 
|  | 276 |  | 
|  | 277 | std::unique_ptr<ASTExpression> term(); | 
|  | 278 |  | 
|  | 279 | bool intLiteral(int64_t* dest); | 
|  | 280 |  | 
|  | 281 | bool floatLiteral(double* dest); | 
|  | 282 |  | 
|  | 283 | bool boolLiteral(bool* dest); | 
|  | 284 |  | 
| Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 285 | bool identifier(StringFragment* dest); | 
| ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 286 |  | 
| Brian Salomon | 140f3da | 2018-08-23 13:51:27 +0000 | [diff] [blame] | 287 | static std::unordered_map<String, LayoutToken>* layoutTokens; | 
| Ethan Nicholas | b93af7e | 2018-07-24 11:28:52 -0400 | [diff] [blame] | 288 |  | 
| Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 289 | const char* fText; | 
|  | 290 | Lexer fLexer; | 
| ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 291 | YY_BUFFER_STATE fBuffer; | 
| ethannicholas | cad6416 | 2016-10-27 10:54:02 -0700 | [diff] [blame] | 292 | // current parse depth, used to enforce a recursion limit to try to keep us from overflowing the | 
|  | 293 | // stack on pathological inputs | 
|  | 294 | int fDepth = 0; | 
| ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 295 | Token fPushback; | 
|  | 296 | SymbolTable& fTypes; | 
|  | 297 | ErrorReporter& fErrors; | 
| ethannicholas | cad6416 | 2016-10-27 10:54:02 -0700 | [diff] [blame] | 298 |  | 
|  | 299 | friend class AutoDepth; | 
| Ethan Nicholas | 130fb3f | 2018-02-01 12:14:34 -0500 | [diff] [blame] | 300 | friend class HCodeGenerator; | 
| ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 301 | }; | 
|  | 302 |  | 
|  | 303 | } // namespace | 
|  | 304 |  | 
|  | 305 | #endif |