blob: 75f304bc8c5ccf7605b9967cb64318ceb3d1cc1c [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;
ethannicholas14fe8cc2016-09-07 13:37:16 -070043struct ASTVarDeclarations;
ethannicholasb3058bd2016-07-01 08:22:01 -070044class 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
fmalitad214d6a2016-09-30 08:05:24 -0700103 void precision();
ethannicholasb3058bd2016-07-01 08:22:01 -0700104
105 std::unique_ptr<ASTDeclaration> directive();
106
107 std::unique_ptr<ASTDeclaration> declaration();
108
ethannicholas14fe8cc2016-09-07 13:37:16 -0700109 std::unique_ptr<ASTVarDeclarations> varDeclarations();
ethannicholasb3058bd2016-07-01 08:22:01 -0700110
111 std::unique_ptr<ASTType> structDeclaration();
112
ethannicholas14fe8cc2016-09-07 13:37:16 -0700113 std::unique_ptr<ASTVarDeclarations> structVarDeclaration(ASTModifiers modifiers);
ethannicholasb3058bd2016-07-01 08:22:01 -0700114
ethannicholas14fe8cc2016-09-07 13:37:16 -0700115 std::unique_ptr<ASTVarDeclarations> varDeclarationEnd(ASTModifiers modifiers,
116 std::unique_ptr<ASTType> type,
117 std::string name);
ethannicholasb3058bd2016-07-01 08:22:01 -0700118
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