• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
7 
8 #ifndef SKSL_PARSER
9 #define SKSL_PARSER
10 
11 #include <vector>
12 #include <memory>
13 #include <unordered_map>
14 #include <unordered_set>
15 #include "SkSLErrorReporter.h"
16 #include "ir/SkSLLayout.h"
17 #include "SkSLLexer.h"
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;
36 struct ASTParameter;
37 struct ASTPrecision;
38 struct ASTReturnStatement;
39 struct ASTStatement;
40 struct ASTSuffix;
41 struct ASTSwitchCase;
42 struct ASTSwitchStatement;
43 struct ASTType;
44 struct ASTWhileStatement;
45 struct ASTVarDeclarations;
46 struct Modifiers;
47 class SymbolTable;
48 
49 /**
50  * Consumes .sksl text and produces an abstract syntax tree describing the contents.
51  */
52 class Parser {
53 public:
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,
92         TRACKED,
93         CTYPE,
94         SKPMCOLOR4F,
95         SKRECT,
96         SKIRECT,
97         SKPMCOLOR,
98     };
99 
100     Parser(const char* text, size_t length, SymbolTable& types, ErrorReporter& errors);
101 
102     /**
103      * Consumes a complete .sksl file and produces a list of declarations. Errors are reported via
104      * the ErrorReporter; the return value may contain some declarations even when errors have
105      * occurred.
106      */
107     std::vector<std::unique_ptr<ASTDeclaration>> file();
108 
109     StringFragment text(Token token);
110 
111     Position position(Token token);
112 
113 private:
114     static void InitLayoutMap();
115 
116     /**
117      * Return the next token, including whitespace tokens, from the parse stream.
118      */
119     Token nextRawToken();
120 
121     /**
122      * Return the next non-whitespace token from the parse stream.
123      */
124     Token nextToken();
125 
126     /**
127      * Push a token back onto the parse stream, so that it is the next one read. Only a single level
128      * of pushback is supported (that is, it is an error to call pushback() twice in a row without
129      * an intervening nextToken()).
130      */
131     void pushback(Token t);
132 
133     /**
134      * Returns the next non-whitespace token without consuming it from the stream.
135      */
136     Token peek();
137 
138     /**
139      * Checks to see if the next token is of the specified type. If so, stores it in result (if
140      * result is non-null) and returns true. Otherwise, pushes it back and returns false.
141      */
142     bool checkNext(Token::Kind kind, Token* result = nullptr);
143 
144     /**
145      * Reads the next non-whitespace token and generates an error if it is not the expected type.
146      * The 'expected' string is part of the error message, which reads:
147      *
148      * "expected <expected>, but found '<actual text>'"
149      *
150      * If 'result' is non-null, it is set to point to the token that was read.
151      * Returns true if the read token was as expected, false otherwise.
152      */
153     bool expect(Token::Kind kind, const char* expected, Token* result = nullptr);
154     bool expect(Token::Kind kind, String expected, Token* result = nullptr);
155 
156     void error(Token token, String msg);
157     void error(int offset, String msg);
158     /**
159      * Returns true if the 'name' identifier refers to a type name. For instance, isType("int") will
160      * always return true.
161      */
162     bool isType(StringFragment name);
163 
164     // these functions parse individual grammar rules from the current parse position; you probably
165     // don't need to call any of these outside of the parser. The function declarations in the .cpp
166     // file have comments describing the grammar rules.
167 
168     std::unique_ptr<ASTDeclaration> precision();
169 
170     std::unique_ptr<ASTDeclaration> directive();
171 
172     std::unique_ptr<ASTDeclaration> section();
173 
174     std::unique_ptr<ASTDeclaration> enumDeclaration();
175 
176     std::unique_ptr<ASTDeclaration> declaration();
177 
178     std::unique_ptr<ASTVarDeclarations> varDeclarations();
179 
180     std::unique_ptr<ASTType> structDeclaration();
181 
182     std::unique_ptr<ASTVarDeclarations> structVarDeclaration(Modifiers modifiers);
183 
184     std::unique_ptr<ASTVarDeclarations> varDeclarationEnd(Modifiers modifiers,
185                                                           std::unique_ptr<ASTType> type,
186                                                           StringFragment name);
187 
188     std::unique_ptr<ASTParameter> parameter();
189 
190     int layoutInt();
191 
192     StringFragment layoutIdentifier();
193 
194     String layoutCode();
195 
196     Layout::Key layoutKey();
197 
198     Layout::CType layoutCType();
199 
200     Layout layout();
201 
202     Modifiers modifiers();
203 
204     Modifiers modifiersWithDefaults(int defaultFlags);
205 
206     std::unique_ptr<ASTStatement> statement();
207 
208     std::unique_ptr<ASTType> type();
209 
210     std::unique_ptr<ASTDeclaration> interfaceBlock(Modifiers mods);
211 
212     std::unique_ptr<ASTIfStatement> ifStatement();
213 
214     std::unique_ptr<ASTDoStatement> doStatement();
215 
216     std::unique_ptr<ASTWhileStatement> whileStatement();
217 
218     std::unique_ptr<ASTForStatement> forStatement();
219 
220     std::unique_ptr<ASTSwitchCase> switchCase();
221 
222     std::unique_ptr<ASTStatement> switchStatement();
223 
224     std::unique_ptr<ASTReturnStatement> returnStatement();
225 
226     std::unique_ptr<ASTBreakStatement> breakStatement();
227 
228     std::unique_ptr<ASTContinueStatement> continueStatement();
229 
230     std::unique_ptr<ASTDiscardStatement> discardStatement();
231 
232     std::unique_ptr<ASTBlock> block();
233 
234     std::unique_ptr<ASTExpressionStatement> expressionStatement();
235 
236     std::unique_ptr<ASTExpression> expression();
237 
238     std::unique_ptr<ASTExpression> commaExpression();
239 
240     std::unique_ptr<ASTExpression> assignmentExpression();
241 
242     std::unique_ptr<ASTExpression> ternaryExpression();
243 
244     std::unique_ptr<ASTExpression> logicalOrExpression();
245 
246     std::unique_ptr<ASTExpression> logicalXorExpression();
247 
248     std::unique_ptr<ASTExpression> logicalAndExpression();
249 
250     std::unique_ptr<ASTExpression> bitwiseOrExpression();
251 
252     std::unique_ptr<ASTExpression> bitwiseXorExpression();
253 
254     std::unique_ptr<ASTExpression> bitwiseAndExpression();
255 
256     std::unique_ptr<ASTExpression> equalityExpression();
257 
258     std::unique_ptr<ASTExpression> relationalExpression();
259 
260     std::unique_ptr<ASTExpression> shiftExpression();
261 
262     std::unique_ptr<ASTExpression> additiveExpression();
263 
264     std::unique_ptr<ASTExpression> multiplicativeExpression();
265 
266     std::unique_ptr<ASTExpression> unaryExpression();
267 
268     std::unique_ptr<ASTExpression> postfixExpression();
269 
270     std::unique_ptr<ASTSuffix> suffix();
271 
272     std::unique_ptr<ASTExpression> term();
273 
274     bool intLiteral(int64_t* dest);
275 
276     bool floatLiteral(double* dest);
277 
278     bool boolLiteral(bool* dest);
279 
280     bool identifier(StringFragment* dest);
281 
282     static std::unordered_map<String, LayoutToken>* layoutTokens;
283 
284     const char* fText;
285     Lexer fLexer;
286     YY_BUFFER_STATE fBuffer;
287     // current parse depth, used to enforce a recursion limit to try to keep us from overflowing the
288     // stack on pathological inputs
289     int fDepth = 0;
290     Token fPushback;
291     SymbolTable& fTypes;
292     ErrorReporter& fErrors;
293 
294     friend class AutoDepth;
295     friend class HCodeGenerator;
296 };
297 
298 } // namespace
299 
300 #endif
301