blob: 8d8702b2d1a6588b5818379bfa994959dea83eed [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
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 Nicholas11d53972016-11-28 11:23:23 -05007
ethannicholasb3058bd2016-07-01 08:22:01 -07008#ifndef SKSL_PARSER
9#define SKSL_PARSER
10
ethannicholasb3058bd2016-07-01 08:22:01 -070011#include <vector>
12#include <memory>
Ethan Nicholas3614d9a2017-02-15 12:33:30 -050013#include <unordered_map>
ethannicholasb3058bd2016-07-01 08:22:01 -070014#include <unordered_set>
15#include "SkSLErrorReporter.h"
Ethan Nicholas762466e2017-06-29 10:03:38 -040016#include "ir/SkSLLayout.h"
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070017#include "SkSLLexer.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070018
19struct yy_buffer_state;
20#define YY_TYPEDEF_YY_BUFFER_STATE
21typedef struct yy_buffer_state *YY_BUFFER_STATE;
22
23namespace SkSL {
24
25struct ASTBlock;
26struct ASTBreakStatement;
27struct ASTContinueStatement;
28struct ASTDeclaration;
29struct ASTDiscardStatement;
30struct ASTDoStatement;
31struct ASTExpression;
32struct ASTExpressionStatement;
33struct ASTForStatement;
34struct ASTIfStatement;
35struct ASTInterfaceBlock;
ethannicholasb3058bd2016-07-01 08:22:01 -070036struct ASTParameter;
ethannicholas5961bc92016-10-12 06:39:56 -070037struct ASTPrecision;
ethannicholasb3058bd2016-07-01 08:22:01 -070038struct ASTReturnStatement;
39struct ASTStatement;
40struct ASTSuffix;
Ethan Nicholasaf197692017-02-27 13:26:45 -050041struct ASTSwitchCase;
42struct ASTSwitchStatement;
ethannicholasb3058bd2016-07-01 08:22:01 -070043struct ASTType;
44struct ASTWhileStatement;
ethannicholas14fe8cc2016-09-07 13:37:16 -070045struct ASTVarDeclarations;
Ethan Nicholas11d53972016-11-28 11:23:23 -050046struct Modifiers;
ethannicholasb3058bd2016-07-01 08:22:01 -070047class SymbolTable;
48
49/**
50 * Consumes .sksl text and produces an abstract syntax tree describing the contents.
51 */
52class Parser {
53public:
Ethan Nicholasb93af7e2018-07-24 11:28:52 -040054 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,
Michael Ludwiga4275592018-08-31 10:52:47 -040092 CTYPE,
93 TRACKED
Ethan Nicholasb93af7e2018-07-24 11:28:52 -040094 };
95
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070096 Parser(const char* text, size_t length, SymbolTable& types, ErrorReporter& errors);
ethannicholasb3058bd2016-07-01 08:22:01 -070097
98 /**
99 * Consumes a complete .sksl file and produces a list of declarations. Errors are reported via
100 * the ErrorReporter; the return value may contain some declarations even when errors have
101 * occurred.
102 */
103 std::vector<std::unique_ptr<ASTDeclaration>> file();
104
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700105 StringFragment text(Token token);
106
107 Position position(Token token);
108
ethannicholasb3058bd2016-07-01 08:22:01 -0700109private:
Ethan Nicholasb93af7e2018-07-24 11:28:52 -0400110 static void InitLayoutMap();
111
ethannicholasb3058bd2016-07-01 08:22:01 -0700112 /**
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700113 * Return the next token, including whitespace tokens, from the parse stream.
Ethan Nicholas762466e2017-06-29 10:03:38 -0400114 */
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700115 Token nextRawToken();
Ethan Nicholas762466e2017-06-29 10:03:38 -0400116
117 /**
118 * Return the next non-whitespace token from the parse stream.
ethannicholasb3058bd2016-07-01 08:22:01 -0700119 */
120 Token nextToken();
121
122 /**
123 * Push a token back onto the parse stream, so that it is the next one read. Only a single level
124 * of pushback is supported (that is, it is an error to call pushback() twice in a row without
125 * an intervening nextToken()).
126 */
127 void pushback(Token t);
128
129 /**
Ethan Nicholas762466e2017-06-29 10:03:38 -0400130 * Returns the next non-whitespace token without consuming it from the stream.
ethannicholasb3058bd2016-07-01 08:22:01 -0700131 */
132 Token peek();
133
134 /**
Ethan Nicholas0c9d13b2017-05-08 16:18:19 -0400135 * Checks to see if the next token is of the specified type. If so, stores it in result (if
136 * result is non-null) and returns true. Otherwise, pushes it back and returns false.
137 */
138 bool checkNext(Token::Kind kind, Token* result = nullptr);
139
140 /**
Ethan Nicholas762466e2017-06-29 10:03:38 -0400141 * Reads the next non-whitespace token and generates an error if it is not the expected type.
142 * The 'expected' string is part of the error message, which reads:
ethannicholasb3058bd2016-07-01 08:22:01 -0700143 *
144 * "expected <expected>, but found '<actual text>'"
145 *
146 * If 'result' is non-null, it is set to point to the token that was read.
147 * Returns true if the read token was as expected, false otherwise.
148 */
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500149 bool expect(Token::Kind kind, const char* expected, Token* result = nullptr);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700150 bool expect(Token::Kind kind, String expected, Token* result = nullptr);
ethannicholasb3058bd2016-07-01 08:22:01 -0700151
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700152 void error(Token token, String msg);
153 void error(int offset, String msg);
ethannicholasb3058bd2016-07-01 08:22:01 -0700154 /**
155 * Returns true if the 'name' identifier refers to a type name. For instance, isType("int") will
156 * always return true.
157 */
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700158 bool isType(StringFragment name);
ethannicholasb3058bd2016-07-01 08:22:01 -0700159
160 // these functions parse individual grammar rules from the current parse position; you probably
161 // don't need to call any of these outside of the parser. The function declarations in the .cpp
162 // file have comments describing the grammar rules.
163
ethannicholas5961bc92016-10-12 06:39:56 -0700164 std::unique_ptr<ASTDeclaration> precision();
ethannicholasb3058bd2016-07-01 08:22:01 -0700165
166 std::unique_ptr<ASTDeclaration> directive();
167
Ethan Nicholas762466e2017-06-29 10:03:38 -0400168 std::unique_ptr<ASTDeclaration> section();
169
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500170 std::unique_ptr<ASTDeclaration> enumDeclaration();
171
ethannicholasb3058bd2016-07-01 08:22:01 -0700172 std::unique_ptr<ASTDeclaration> declaration();
173
ethannicholas14fe8cc2016-09-07 13:37:16 -0700174 std::unique_ptr<ASTVarDeclarations> varDeclarations();
ethannicholasb3058bd2016-07-01 08:22:01 -0700175
176 std::unique_ptr<ASTType> structDeclaration();
177
Ethan Nicholas11d53972016-11-28 11:23:23 -0500178 std::unique_ptr<ASTVarDeclarations> structVarDeclaration(Modifiers modifiers);
ethannicholasb3058bd2016-07-01 08:22:01 -0700179
Ethan Nicholas11d53972016-11-28 11:23:23 -0500180 std::unique_ptr<ASTVarDeclarations> varDeclarationEnd(Modifiers modifiers,
181 std::unique_ptr<ASTType> type,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700182 StringFragment name);
ethannicholasb3058bd2016-07-01 08:22:01 -0700183
184 std::unique_ptr<ASTParameter> parameter();
185
186 int layoutInt();
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400187
Ethan Nicholasd608c092017-10-26 09:30:08 -0400188 StringFragment layoutIdentifier();
189
Ethan Nicholas762466e2017-06-29 10:03:38 -0400190 String layoutCode();
191
192 Layout::Key layoutKey();
193
Ethan Nicholas11d53972016-11-28 11:23:23 -0500194 Layout layout();
ethannicholasb3058bd2016-07-01 08:22:01 -0700195
Ethan Nicholas11d53972016-11-28 11:23:23 -0500196 Modifiers modifiers();
ethannicholasb3058bd2016-07-01 08:22:01 -0700197
Ethan Nicholas11d53972016-11-28 11:23:23 -0500198 Modifiers modifiersWithDefaults(int defaultFlags);
ethannicholasb3058bd2016-07-01 08:22:01 -0700199
200 std::unique_ptr<ASTStatement> statement();
201
202 std::unique_ptr<ASTType> type();
203
Ethan Nicholas11d53972016-11-28 11:23:23 -0500204 std::unique_ptr<ASTDeclaration> interfaceBlock(Modifiers mods);
ethannicholasb3058bd2016-07-01 08:22:01 -0700205
206 std::unique_ptr<ASTIfStatement> ifStatement();
207
208 std::unique_ptr<ASTDoStatement> doStatement();
209
210 std::unique_ptr<ASTWhileStatement> whileStatement();
211
212 std::unique_ptr<ASTForStatement> forStatement();
213
Ethan Nicholasaf197692017-02-27 13:26:45 -0500214 std::unique_ptr<ASTSwitchCase> switchCase();
215
216 std::unique_ptr<ASTStatement> switchStatement();
217
ethannicholasb3058bd2016-07-01 08:22:01 -0700218 std::unique_ptr<ASTReturnStatement> returnStatement();
219
220 std::unique_ptr<ASTBreakStatement> breakStatement();
221
222 std::unique_ptr<ASTContinueStatement> continueStatement();
223
224 std::unique_ptr<ASTDiscardStatement> discardStatement();
225
226 std::unique_ptr<ASTBlock> block();
227
228 std::unique_ptr<ASTExpressionStatement> expressionStatement();
229
230 std::unique_ptr<ASTExpression> expression();
231
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400232 std::unique_ptr<ASTExpression> commaExpression();
233
ethannicholasb3058bd2016-07-01 08:22:01 -0700234 std::unique_ptr<ASTExpression> assignmentExpression();
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400235
ethannicholasb3058bd2016-07-01 08:22:01 -0700236 std::unique_ptr<ASTExpression> ternaryExpression();
237
238 std::unique_ptr<ASTExpression> logicalOrExpression();
239
240 std::unique_ptr<ASTExpression> logicalXorExpression();
241
242 std::unique_ptr<ASTExpression> logicalAndExpression();
243
244 std::unique_ptr<ASTExpression> bitwiseOrExpression();
245
246 std::unique_ptr<ASTExpression> bitwiseXorExpression();
247
248 std::unique_ptr<ASTExpression> bitwiseAndExpression();
249
250 std::unique_ptr<ASTExpression> equalityExpression();
251
252 std::unique_ptr<ASTExpression> relationalExpression();
253
254 std::unique_ptr<ASTExpression> shiftExpression();
255
256 std::unique_ptr<ASTExpression> additiveExpression();
257
258 std::unique_ptr<ASTExpression> multiplicativeExpression();
259
260 std::unique_ptr<ASTExpression> unaryExpression();
261
262 std::unique_ptr<ASTExpression> postfixExpression();
263
264 std::unique_ptr<ASTSuffix> suffix();
265
266 std::unique_ptr<ASTExpression> term();
267
268 bool intLiteral(int64_t* dest);
269
270 bool floatLiteral(double* dest);
271
272 bool boolLiteral(bool* dest);
273
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700274 bool identifier(StringFragment* dest);
ethannicholasb3058bd2016-07-01 08:22:01 -0700275
Brian Salomon140f3da2018-08-23 13:51:27 +0000276 static std::unordered_map<String, LayoutToken>* layoutTokens;
Ethan Nicholasb93af7e2018-07-24 11:28:52 -0400277
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700278 const char* fText;
279 Lexer fLexer;
ethannicholasb3058bd2016-07-01 08:22:01 -0700280 YY_BUFFER_STATE fBuffer;
ethannicholascad64162016-10-27 10:54:02 -0700281 // current parse depth, used to enforce a recursion limit to try to keep us from overflowing the
282 // stack on pathological inputs
283 int fDepth = 0;
ethannicholasb3058bd2016-07-01 08:22:01 -0700284 Token fPushback;
285 SymbolTable& fTypes;
286 ErrorReporter& fErrors;
ethannicholascad64162016-10-27 10:54:02 -0700287
288 friend class AutoDepth;
Ethan Nicholas130fb3f2018-02-01 12:14:34 -0500289 friend class HCodeGenerator;
ethannicholasb3058bd2016-07-01 08:22:01 -0700290};
291
292} // namespace
293
294#endif