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