blob: 45629a370a61c27f77e035759506bd06092a6ecb [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 */
7
8#ifndef SKSL_PARSER
9#define SKSL_PARSER
10
11#include <string>
12#include <vector>
13#include <memory>
14#include <unordered_set>
15#include "SkSLErrorReporter.h"
16#include "SkSLToken.h"
17
18struct yy_buffer_state;
19#define YY_TYPEDEF_YY_BUFFER_STATE
20typedef struct yy_buffer_state *YY_BUFFER_STATE;
21
22namespace SkSL {
23
24struct ASTBlock;
25struct ASTBreakStatement;
26struct ASTContinueStatement;
27struct ASTDeclaration;
28struct ASTDiscardStatement;
29struct ASTDoStatement;
30struct ASTExpression;
31struct ASTExpressionStatement;
32struct ASTForStatement;
33struct ASTIfStatement;
34struct ASTInterfaceBlock;
35struct ASTLayout;
36struct ASTModifiers;
37struct ASTParameter;
38struct ASTReturnStatement;
39struct ASTStatement;
40struct ASTSuffix;
41struct ASTType;
42struct ASTWhileStatement;
43struct ASTVarDeclaration;
44class SymbolTable;
45
46/**
47 * Consumes .sksl text and produces an abstract syntax tree describing the contents.
48 */
49class Parser {
50public:
51 Parser(std::string text, SymbolTable& types, ErrorReporter& errors);
52
53 ~Parser();
54
55 /**
56 * Consumes a complete .sksl file and produces a list of declarations. Errors are reported via
57 * the ErrorReporter; the return value may contain some declarations even when errors have
58 * occurred.
59 */
60 std::vector<std::unique_ptr<ASTDeclaration>> file();
61
62private:
63 /**
64 * Return the next token from the parse stream.
65 */
66 Token nextToken();
67
68 /**
69 * Push a token back onto the parse stream, so that it is the next one read. Only a single level
70 * of pushback is supported (that is, it is an error to call pushback() twice in a row without
71 * an intervening nextToken()).
72 */
73 void pushback(Token t);
74
75 /**
76 * Returns the next token without consuming it from the stream.
77 */
78 Token peek();
79
80 /**
81 * Reads the next token and generates an error if it is not the expected type. The 'expected'
82 * string is part of the error message, which reads:
83 *
84 * "expected <expected>, but found '<actual text>'"
85 *
86 * If 'result' is non-null, it is set to point to the token that was read.
87 * Returns true if the read token was as expected, false otherwise.
88 */
89 bool expect(Token::Kind kind, std::string expected, Token* result = nullptr);
90
91 void error(Position p, std::string msg);
92
93 /**
94 * Returns true if the 'name' identifier refers to a type name. For instance, isType("int") will
95 * always return true.
96 */
97 bool isType(std::string name);
98
99 // these functions parse individual grammar rules from the current parse position; you probably
100 // don't need to call any of these outside of the parser. The function declarations in the .cpp
101 // file have comments describing the grammar rules.
102
103 void precision();
104
105 std::unique_ptr<ASTDeclaration> directive();
106
107 std::unique_ptr<ASTDeclaration> declaration();
108
109 std::unique_ptr<ASTVarDeclaration> varDeclaration();
110
111 std::unique_ptr<ASTType> structDeclaration();
112
113 std::unique_ptr<ASTVarDeclaration> structVarDeclaration(ASTModifiers modifiers);
114
115 std::unique_ptr<ASTVarDeclaration> varDeclarationEnd(ASTModifiers modifiers,
116 std::unique_ptr<ASTType> type,
117 std::string name);
118
119 std::unique_ptr<ASTParameter> parameter();
120
121 int layoutInt();
122
123 ASTLayout layout();
124
125 ASTModifiers modifiers();
126
127 ASTModifiers modifiersWithDefaults(int defaultFlags);
128
129 std::unique_ptr<ASTStatement> statement();
130
131 std::unique_ptr<ASTType> type();
132
133 std::unique_ptr<ASTDeclaration> interfaceBlock(ASTModifiers mods);
134
135 std::unique_ptr<ASTIfStatement> ifStatement();
136
137 std::unique_ptr<ASTDoStatement> doStatement();
138
139 std::unique_ptr<ASTWhileStatement> whileStatement();
140
141 std::unique_ptr<ASTForStatement> forStatement();
142
143 std::unique_ptr<ASTReturnStatement> returnStatement();
144
145 std::unique_ptr<ASTBreakStatement> breakStatement();
146
147 std::unique_ptr<ASTContinueStatement> continueStatement();
148
149 std::unique_ptr<ASTDiscardStatement> discardStatement();
150
151 std::unique_ptr<ASTBlock> block();
152
153 std::unique_ptr<ASTExpressionStatement> expressionStatement();
154
155 std::unique_ptr<ASTExpression> expression();
156
157 std::unique_ptr<ASTExpression> assignmentExpression();
158
159 std::unique_ptr<ASTExpression> ternaryExpression();
160
161 std::unique_ptr<ASTExpression> logicalOrExpression();
162
163 std::unique_ptr<ASTExpression> logicalXorExpression();
164
165 std::unique_ptr<ASTExpression> logicalAndExpression();
166
167 std::unique_ptr<ASTExpression> bitwiseOrExpression();
168
169 std::unique_ptr<ASTExpression> bitwiseXorExpression();
170
171 std::unique_ptr<ASTExpression> bitwiseAndExpression();
172
173 std::unique_ptr<ASTExpression> equalityExpression();
174
175 std::unique_ptr<ASTExpression> relationalExpression();
176
177 std::unique_ptr<ASTExpression> shiftExpression();
178
179 std::unique_ptr<ASTExpression> additiveExpression();
180
181 std::unique_ptr<ASTExpression> multiplicativeExpression();
182
183 std::unique_ptr<ASTExpression> unaryExpression();
184
185 std::unique_ptr<ASTExpression> postfixExpression();
186
187 std::unique_ptr<ASTSuffix> suffix();
188
189 std::unique_ptr<ASTExpression> term();
190
191 bool intLiteral(int64_t* dest);
192
193 bool floatLiteral(double* dest);
194
195 bool boolLiteral(bool* dest);
196
197 bool identifier(std::string* dest);
198
199
200 void* fScanner;
201 YY_BUFFER_STATE fBuffer;
202 Token fPushback;
203 SymbolTable& fTypes;
204 ErrorReporter& fErrors;
205};
206
207} // namespace
208
209#endif