blob: e82048459143e1cac5261fed5bd966fd29dfbc26 [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 Nicholasc0709392017-06-27 11:20:22 -040016#include "ir/SkSLLayout.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070017#include "SkSLToken.h"
18
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 Nicholas0df1b042017-03-31 13:56:23 -040054 Parser(String text, SymbolTable& types, ErrorReporter& errors);
ethannicholasb3058bd2016-07-01 08:22:01 -070055
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
65private:
66 /**
Ethan Nicholasc0709392017-06-27 11:20:22 -040067 * 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.
ethannicholasb3058bd2016-07-01 08:22:01 -070073 */
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 /**
Ethan Nicholasc0709392017-06-27 11:20:22 -040084 * Returns the next non-whitespace token without consuming it from the stream.
ethannicholasb3058bd2016-07-01 08:22:01 -070085 */
86 Token peek();
87
88 /**
Ethan Nicholas0c9d13b2017-05-08 16:18:19 -040089 * 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 /**
Ethan Nicholasc0709392017-06-27 11:20:22 -040095 * 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:
ethannicholasb3058bd2016-07-01 08:22:01 -070097 *
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 */
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500103 bool expect(Token::Kind kind, const char* expected, Token* result = nullptr);
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400104 bool expect(Token::Kind kind, String expected, Token* result = nullptr);
ethannicholasb3058bd2016-07-01 08:22:01 -0700105
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500106 void error(Position p, const char* msg);
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400107 void error(Position p, String msg);
108
ethannicholasb3058bd2016-07-01 08:22:01 -0700109 /**
110 * Returns true if the 'name' identifier refers to a type name. For instance, isType("int") will
111 * always return true.
112 */
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400113 bool isType(String name);
ethannicholasb3058bd2016-07-01 08:22:01 -0700114
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
ethannicholas5961bc92016-10-12 06:39:56 -0700119 std::unique_ptr<ASTDeclaration> precision();
ethannicholasb3058bd2016-07-01 08:22:01 -0700120
121 std::unique_ptr<ASTDeclaration> directive();
122
Ethan Nicholasc0709392017-06-27 11:20:22 -0400123 std::unique_ptr<ASTDeclaration> section();
124
ethannicholasb3058bd2016-07-01 08:22:01 -0700125 std::unique_ptr<ASTDeclaration> declaration();
126
ethannicholas14fe8cc2016-09-07 13:37:16 -0700127 std::unique_ptr<ASTVarDeclarations> varDeclarations();
ethannicholasb3058bd2016-07-01 08:22:01 -0700128
129 std::unique_ptr<ASTType> structDeclaration();
130
Ethan Nicholas11d53972016-11-28 11:23:23 -0500131 std::unique_ptr<ASTVarDeclarations> structVarDeclaration(Modifiers modifiers);
ethannicholasb3058bd2016-07-01 08:22:01 -0700132
Ethan Nicholas11d53972016-11-28 11:23:23 -0500133 std::unique_ptr<ASTVarDeclarations> varDeclarationEnd(Modifiers modifiers,
134 std::unique_ptr<ASTType> type,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400135 String name);
ethannicholasb3058bd2016-07-01 08:22:01 -0700136
137 std::unique_ptr<ASTParameter> parameter();
138
139 int layoutInt();
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400140
Ethan Nicholasc0709392017-06-27 11:20:22 -0400141 String layoutCode();
142
143 Layout::Key layoutKey();
144
Ethan Nicholas11d53972016-11-28 11:23:23 -0500145 Layout layout();
ethannicholasb3058bd2016-07-01 08:22:01 -0700146
Ethan Nicholas11d53972016-11-28 11:23:23 -0500147 Modifiers modifiers();
ethannicholasb3058bd2016-07-01 08:22:01 -0700148
Ethan Nicholas11d53972016-11-28 11:23:23 -0500149 Modifiers modifiersWithDefaults(int defaultFlags);
ethannicholasb3058bd2016-07-01 08:22:01 -0700150
151 std::unique_ptr<ASTStatement> statement();
152
153 std::unique_ptr<ASTType> type();
154
Ethan Nicholas11d53972016-11-28 11:23:23 -0500155 std::unique_ptr<ASTDeclaration> interfaceBlock(Modifiers mods);
ethannicholasb3058bd2016-07-01 08:22:01 -0700156
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
Ethan Nicholasaf197692017-02-27 13:26:45 -0500165 std::unique_ptr<ASTSwitchCase> switchCase();
166
167 std::unique_ptr<ASTStatement> switchStatement();
168
ethannicholasb3058bd2016-07-01 08:22:01 -0700169 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
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400183 std::unique_ptr<ASTExpression> commaExpression();
184
ethannicholasb3058bd2016-07-01 08:22:01 -0700185 std::unique_ptr<ASTExpression> assignmentExpression();
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400186
ethannicholasb3058bd2016-07-01 08:22:01 -0700187 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
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400225 bool identifier(String* dest);
ethannicholasb3058bd2016-07-01 08:22:01 -0700226
ethannicholasb3058bd2016-07-01 08:22:01 -0700227 void* fScanner;
Ethan Nicholasbfe15f62017-03-01 11:46:51 -0500228 void* fLayoutScanner;
ethannicholasb3058bd2016-07-01 08:22:01 -0700229 YY_BUFFER_STATE fBuffer;
ethannicholascad64162016-10-27 10:54:02 -0700230 // 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;
ethannicholasb3058bd2016-07-01 08:22:01 -0700233 Token fPushback;
234 SymbolTable& fTypes;
235 ErrorReporter& fErrors;
ethannicholascad64162016-10-27 10:54:02 -0700236
237 friend class AutoDepth;
ethannicholasb3058bd2016-07-01 08:22:01 -0700238};
239
240} // namespace
241
242#endif