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 #include "SkSLLayoutLexer.h"
19 
20 struct yy_buffer_state;
21 #define YY_TYPEDEF_YY_BUFFER_STATE
22 typedef struct yy_buffer_state *YY_BUFFER_STATE;
23 
24 namespace SkSL {
25 
26 struct ASTBlock;
27 struct ASTBreakStatement;
28 struct ASTContinueStatement;
29 struct ASTDeclaration;
30 struct ASTDiscardStatement;
31 struct ASTDoStatement;
32 struct ASTExpression;
33 struct ASTExpressionStatement;
34 struct ASTForStatement;
35 struct ASTIfStatement;
36 struct ASTInterfaceBlock;
37 struct ASTParameter;
38 struct ASTPrecision;
39 struct ASTReturnStatement;
40 struct ASTStatement;
41 struct ASTSuffix;
42 struct ASTSwitchCase;
43 struct ASTSwitchStatement;
44 struct ASTType;
45 struct ASTWhileStatement;
46 struct ASTVarDeclarations;
47 struct Modifiers;
48 class SymbolTable;
49 
50 /**
51  * Consumes .sksl text and produces an abstract syntax tree describing the contents.
52  */
53 class Parser {
54 public:
55     Parser(const char* text, size_t length, SymbolTable& types, ErrorReporter& errors);
56 
57     /**
58      * Consumes a complete .sksl file and produces a list of declarations. Errors are reported via
59      * the ErrorReporter; the return value may contain some declarations even when errors have
60      * occurred.
61      */
62     std::vector<std::unique_ptr<ASTDeclaration>> file();
63 
64     StringFragment text(Token token);
65 
66     Position position(Token token);
67 
68 private:
69     /**
70      * Return the next token, including whitespace tokens, from the parse stream.
71      */
72     Token nextRawToken();
73 
74     /**
75      * Return the next non-whitespace token from the parse stream.
76      */
77     Token nextToken();
78 
79     /**
80      * Push a token back onto the parse stream, so that it is the next one read. Only a single level
81      * of pushback is supported (that is, it is an error to call pushback() twice in a row without
82      * an intervening nextToken()).
83      */
84     void pushback(Token t);
85 
86     /**
87      * Returns the next non-whitespace token without consuming it from the stream.
88      */
89     Token peek();
90 
91     /**
92      * Checks to see if the next token is of the specified type. If so, stores it in result (if
93      * result is non-null) and returns true. Otherwise, pushes it back and returns false.
94      */
95     bool checkNext(Token::Kind kind, Token* result = nullptr);
96 
97     /**
98      * Reads the next non-whitespace token and generates an error if it is not the expected type.
99      * The 'expected' string is part of the error message, which reads:
100      *
101      * "expected <expected>, but found '<actual text>'"
102      *
103      * If 'result' is non-null, it is set to point to the token that was read.
104      * Returns true if the read token was as expected, false otherwise.
105      */
106     bool expect(Token::Kind kind, const char* expected, Token* result = nullptr);
107     bool expect(Token::Kind kind, String expected, Token* result = nullptr);
108 
109     void error(Token token, String msg);
110     void error(int offset, String msg);
111     /**
112      * Returns true if the 'name' identifier refers to a type name. For instance, isType("int") will
113      * always return true.
114      */
115     bool isType(StringFragment name);
116 
117     // these functions parse individual grammar rules from the current parse position; you probably
118     // don't need to call any of these outside of the parser. The function declarations in the .cpp
119     // file have comments describing the grammar rules.
120 
121     std::unique_ptr<ASTDeclaration> precision();
122 
123     std::unique_ptr<ASTDeclaration> directive();
124 
125     std::unique_ptr<ASTDeclaration> section();
126 
127     std::unique_ptr<ASTDeclaration> enumDeclaration();
128 
129     std::unique_ptr<ASTDeclaration> declaration();
130 
131     std::unique_ptr<ASTVarDeclarations> varDeclarations();
132 
133     std::unique_ptr<ASTType> structDeclaration();
134 
135     std::unique_ptr<ASTVarDeclarations> structVarDeclaration(Modifiers modifiers);
136 
137     std::unique_ptr<ASTVarDeclarations> varDeclarationEnd(Modifiers modifiers,
138                                                           std::unique_ptr<ASTType> type,
139                                                           StringFragment name);
140 
141     std::unique_ptr<ASTParameter> parameter();
142 
143     int layoutInt();
144 
145     StringFragment layoutIdentifier();
146 
147     String layoutCode();
148 
149     Layout::Key layoutKey();
150 
151     Layout layout();
152 
153     Modifiers modifiers();
154 
155     Modifiers modifiersWithDefaults(int defaultFlags);
156 
157     std::unique_ptr<ASTStatement> statement();
158 
159     std::unique_ptr<ASTType> type();
160 
161     std::unique_ptr<ASTDeclaration> interfaceBlock(Modifiers mods);
162 
163     std::unique_ptr<ASTIfStatement> ifStatement();
164 
165     std::unique_ptr<ASTDoStatement> doStatement();
166 
167     std::unique_ptr<ASTWhileStatement> whileStatement();
168 
169     std::unique_ptr<ASTForStatement> forStatement();
170 
171     std::unique_ptr<ASTSwitchCase> switchCase();
172 
173     std::unique_ptr<ASTStatement> switchStatement();
174 
175     std::unique_ptr<ASTReturnStatement> returnStatement();
176 
177     std::unique_ptr<ASTBreakStatement> breakStatement();
178 
179     std::unique_ptr<ASTContinueStatement> continueStatement();
180 
181     std::unique_ptr<ASTDiscardStatement> discardStatement();
182 
183     std::unique_ptr<ASTBlock> block();
184 
185     std::unique_ptr<ASTExpressionStatement> expressionStatement();
186 
187     std::unique_ptr<ASTExpression> expression();
188 
189     std::unique_ptr<ASTExpression> commaExpression();
190 
191     std::unique_ptr<ASTExpression> assignmentExpression();
192 
193     std::unique_ptr<ASTExpression> ternaryExpression();
194 
195     std::unique_ptr<ASTExpression> logicalOrExpression();
196 
197     std::unique_ptr<ASTExpression> logicalXorExpression();
198 
199     std::unique_ptr<ASTExpression> logicalAndExpression();
200 
201     std::unique_ptr<ASTExpression> bitwiseOrExpression();
202 
203     std::unique_ptr<ASTExpression> bitwiseXorExpression();
204 
205     std::unique_ptr<ASTExpression> bitwiseAndExpression();
206 
207     std::unique_ptr<ASTExpression> equalityExpression();
208 
209     std::unique_ptr<ASTExpression> relationalExpression();
210 
211     std::unique_ptr<ASTExpression> shiftExpression();
212 
213     std::unique_ptr<ASTExpression> additiveExpression();
214 
215     std::unique_ptr<ASTExpression> multiplicativeExpression();
216 
217     std::unique_ptr<ASTExpression> unaryExpression();
218 
219     std::unique_ptr<ASTExpression> postfixExpression();
220 
221     std::unique_ptr<ASTSuffix> suffix();
222 
223     std::unique_ptr<ASTExpression> term();
224 
225     bool intLiteral(int64_t* dest);
226 
227     bool floatLiteral(double* dest);
228 
229     bool boolLiteral(bool* dest);
230 
231     bool identifier(StringFragment* dest);
232 
233     const char* fText;
234     Lexer fLexer;
235     LayoutLexer fLayoutLexer;
236     YY_BUFFER_STATE fBuffer;
237     // current parse depth, used to enforce a recursion limit to try to keep us from overflowing the
238     // stack on pathological inputs
239     int fDepth = 0;
240     Token fPushback;
241     SymbolTable& fTypes;
242     ErrorReporter& fErrors;
243 
244     friend class AutoDepth;
245     friend class HCodeGenerator;
246 };
247 
248 } // namespace
249 
250 #endif
251