blob: 5391f6c2ee566b41a71519ec5923ebea04810b03 [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 Nicholasc576e932017-09-07 15:44:01 -040017#include "SkSLLexer.h"
18#include "SkSLLayoutLexer.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070019
20struct yy_buffer_state;
21#define YY_TYPEDEF_YY_BUFFER_STATE
22typedef struct yy_buffer_state *YY_BUFFER_STATE;
23
24namespace SkSL {
25
26struct ASTBlock;
27struct ASTBreakStatement;
28struct ASTContinueStatement;
29struct ASTDeclaration;
30struct ASTDiscardStatement;
31struct ASTDoStatement;
32struct ASTExpression;
33struct ASTExpressionStatement;
34struct ASTForStatement;
35struct ASTIfStatement;
36struct ASTInterfaceBlock;
ethannicholasb3058bd2016-07-01 08:22:01 -070037struct ASTParameter;
ethannicholas5961bc92016-10-12 06:39:56 -070038struct ASTPrecision;
ethannicholasb3058bd2016-07-01 08:22:01 -070039struct ASTReturnStatement;
40struct ASTStatement;
41struct ASTSuffix;
Ethan Nicholasaf197692017-02-27 13:26:45 -050042struct ASTSwitchCase;
43struct ASTSwitchStatement;
ethannicholasb3058bd2016-07-01 08:22:01 -070044struct ASTType;
45struct ASTWhileStatement;
ethannicholas14fe8cc2016-09-07 13:37:16 -070046struct ASTVarDeclarations;
Ethan Nicholas11d53972016-11-28 11:23:23 -050047struct Modifiers;
ethannicholasb3058bd2016-07-01 08:22:01 -070048class SymbolTable;
49
50/**
51 * Consumes .sksl text and produces an abstract syntax tree describing the contents.
52 */
53class Parser {
54public:
Ethan Nicholasc576e932017-09-07 15:44:01 -040055 Parser(const char* text, size_t length, SymbolTable& types, ErrorReporter& errors);
ethannicholasb3058bd2016-07-01 08:22:01 -070056
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
Ethan Nicholasc576e932017-09-07 15:44:01 -040064 StringFragment text(Token token);
65
66 Position position(Token token);
67
ethannicholasb3058bd2016-07-01 08:22:01 -070068private:
69 /**
Ethan Nicholasc576e932017-09-07 15:44:01 -040070 * Return the next token, including whitespace tokens, from the parse stream.
Ethan Nicholas762466e2017-06-29 10:03:38 -040071 */
Ethan Nicholasc576e932017-09-07 15:44:01 -040072 Token nextRawToken();
Ethan Nicholas762466e2017-06-29 10:03:38 -040073
74 /**
75 * Return the next non-whitespace token from the parse stream.
ethannicholasb3058bd2016-07-01 08:22:01 -070076 */
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 /**
Ethan Nicholas762466e2017-06-29 10:03:38 -040087 * Returns the next non-whitespace token without consuming it from the stream.
ethannicholasb3058bd2016-07-01 08:22:01 -070088 */
89 Token peek();
90
91 /**
Ethan Nicholas0c9d13b2017-05-08 16:18:19 -040092 * 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 /**
Ethan Nicholas762466e2017-06-29 10:03:38 -040098 * 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:
ethannicholasb3058bd2016-07-01 08:22:01 -0700100 *
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 */
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500106 bool expect(Token::Kind kind, const char* expected, Token* result = nullptr);
Ethan Nicholasc576e932017-09-07 15:44:01 -0400107 bool expect(Token::Kind kind, String expected, Token* result = nullptr);
ethannicholasb3058bd2016-07-01 08:22:01 -0700108
Ethan Nicholasc576e932017-09-07 15:44:01 -0400109 void error(Token token, String msg);
110 void error(int offset, String msg);
ethannicholasb3058bd2016-07-01 08:22:01 -0700111 /**
112 * Returns true if the 'name' identifier refers to a type name. For instance, isType("int") will
113 * always return true.
114 */
Ethan Nicholasc576e932017-09-07 15:44:01 -0400115 bool isType(StringFragment name);
ethannicholasb3058bd2016-07-01 08:22:01 -0700116
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
ethannicholas5961bc92016-10-12 06:39:56 -0700121 std::unique_ptr<ASTDeclaration> precision();
ethannicholasb3058bd2016-07-01 08:22:01 -0700122
123 std::unique_ptr<ASTDeclaration> directive();
124
Ethan Nicholas762466e2017-06-29 10:03:38 -0400125 std::unique_ptr<ASTDeclaration> section();
126
ethannicholasb3058bd2016-07-01 08:22:01 -0700127 std::unique_ptr<ASTDeclaration> declaration();
128
ethannicholas14fe8cc2016-09-07 13:37:16 -0700129 std::unique_ptr<ASTVarDeclarations> varDeclarations();
ethannicholasb3058bd2016-07-01 08:22:01 -0700130
131 std::unique_ptr<ASTType> structDeclaration();
132
Ethan Nicholas11d53972016-11-28 11:23:23 -0500133 std::unique_ptr<ASTVarDeclarations> structVarDeclaration(Modifiers modifiers);
ethannicholasb3058bd2016-07-01 08:22:01 -0700134
Ethan Nicholas11d53972016-11-28 11:23:23 -0500135 std::unique_ptr<ASTVarDeclarations> varDeclarationEnd(Modifiers modifiers,
136 std::unique_ptr<ASTType> type,
Ethan Nicholasc576e932017-09-07 15:44:01 -0400137 StringFragment name);
ethannicholasb3058bd2016-07-01 08:22:01 -0700138
139 std::unique_ptr<ASTParameter> parameter();
140
141 int layoutInt();
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400142
Ethan Nicholas762466e2017-06-29 10:03:38 -0400143 String layoutCode();
144
145 Layout::Key layoutKey();
146
Ethan Nicholas11d53972016-11-28 11:23:23 -0500147 Layout layout();
ethannicholasb3058bd2016-07-01 08:22:01 -0700148
Ethan Nicholas11d53972016-11-28 11:23:23 -0500149 Modifiers modifiers();
ethannicholasb3058bd2016-07-01 08:22:01 -0700150
Ethan Nicholas11d53972016-11-28 11:23:23 -0500151 Modifiers modifiersWithDefaults(int defaultFlags);
ethannicholasb3058bd2016-07-01 08:22:01 -0700152
153 std::unique_ptr<ASTStatement> statement();
154
155 std::unique_ptr<ASTType> type();
156
Ethan Nicholas11d53972016-11-28 11:23:23 -0500157 std::unique_ptr<ASTDeclaration> interfaceBlock(Modifiers mods);
ethannicholasb3058bd2016-07-01 08:22:01 -0700158
159 std::unique_ptr<ASTIfStatement> ifStatement();
160
161 std::unique_ptr<ASTDoStatement> doStatement();
162
163 std::unique_ptr<ASTWhileStatement> whileStatement();
164
165 std::unique_ptr<ASTForStatement> forStatement();
166
Ethan Nicholasaf197692017-02-27 13:26:45 -0500167 std::unique_ptr<ASTSwitchCase> switchCase();
168
169 std::unique_ptr<ASTStatement> switchStatement();
170
ethannicholasb3058bd2016-07-01 08:22:01 -0700171 std::unique_ptr<ASTReturnStatement> returnStatement();
172
173 std::unique_ptr<ASTBreakStatement> breakStatement();
174
175 std::unique_ptr<ASTContinueStatement> continueStatement();
176
177 std::unique_ptr<ASTDiscardStatement> discardStatement();
178
179 std::unique_ptr<ASTBlock> block();
180
181 std::unique_ptr<ASTExpressionStatement> expressionStatement();
182
183 std::unique_ptr<ASTExpression> expression();
184
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400185 std::unique_ptr<ASTExpression> commaExpression();
186
ethannicholasb3058bd2016-07-01 08:22:01 -0700187 std::unique_ptr<ASTExpression> assignmentExpression();
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400188
ethannicholasb3058bd2016-07-01 08:22:01 -0700189 std::unique_ptr<ASTExpression> ternaryExpression();
190
191 std::unique_ptr<ASTExpression> logicalOrExpression();
192
193 std::unique_ptr<ASTExpression> logicalXorExpression();
194
195 std::unique_ptr<ASTExpression> logicalAndExpression();
196
197 std::unique_ptr<ASTExpression> bitwiseOrExpression();
198
199 std::unique_ptr<ASTExpression> bitwiseXorExpression();
200
201 std::unique_ptr<ASTExpression> bitwiseAndExpression();
202
203 std::unique_ptr<ASTExpression> equalityExpression();
204
205 std::unique_ptr<ASTExpression> relationalExpression();
206
207 std::unique_ptr<ASTExpression> shiftExpression();
208
209 std::unique_ptr<ASTExpression> additiveExpression();
210
211 std::unique_ptr<ASTExpression> multiplicativeExpression();
212
213 std::unique_ptr<ASTExpression> unaryExpression();
214
215 std::unique_ptr<ASTExpression> postfixExpression();
216
217 std::unique_ptr<ASTSuffix> suffix();
218
219 std::unique_ptr<ASTExpression> term();
220
221 bool intLiteral(int64_t* dest);
222
223 bool floatLiteral(double* dest);
224
225 bool boolLiteral(bool* dest);
226
Ethan Nicholasc576e932017-09-07 15:44:01 -0400227 bool identifier(StringFragment* dest);
ethannicholasb3058bd2016-07-01 08:22:01 -0700228
Ethan Nicholasc576e932017-09-07 15:44:01 -0400229 const char* fText;
230 Lexer fLexer;
231 LayoutLexer fLayoutLexer;
ethannicholasb3058bd2016-07-01 08:22:01 -0700232 YY_BUFFER_STATE fBuffer;
ethannicholascad64162016-10-27 10:54:02 -0700233 // current parse depth, used to enforce a recursion limit to try to keep us from overflowing the
234 // stack on pathological inputs
235 int fDepth = 0;
ethannicholasb3058bd2016-07-01 08:22:01 -0700236 Token fPushback;
237 SymbolTable& fTypes;
238 ErrorReporter& fErrors;
ethannicholascad64162016-10-27 10:54:02 -0700239
240 friend class AutoDepth;
ethannicholasb3058bd2016-07-01 08:22:01 -0700241};
242
243} // namespace
244
245#endif