1
2
3
4
5
6 #ifndef SNGCPP_AST_STATEMENT_INCLUDED
7 #define SNGCPP_AST_STATEMENT_INCLUDED
8 #include <sngcpp/ast/Node.hpp>
9 #include <string>
10
11 namespace sngcpp { namespace ast {
12
13 class LabeledStatementNode : public UnaryNode
14 {
15 public:
16 LabeledStatementNode();
17 LabeledStatementNode(const Span& span_, const std::u32string& label_, Node* statement_);
18 void Accept(Visitor& visitor) override;
19 void Write(Writer& writer) override;
20 void Read(Reader& reader) override;
21 const std::u32string& Label() const { return label; }
22 private:
23 std::u32string label;
24 };
25
26 class CaseStatementNode : public UnaryNode
27 {
28 public:
29 CaseStatementNode();
30 CaseStatementNode(const Span& span_, Node* caseExpr_, Node* statement_);
31 void Accept(Visitor& visitor) override;
32 void Write(Writer& writer) override;
33 void Read(Reader& reader) override;
34 Node* CaseExpr() { return caseExpr.get(); }
35 private:
36 std::unique_ptr<Node> caseExpr;
37 };
38
39 class DefaultStatementNode : public UnaryNode
40 {
41 public:
42 DefaultStatementNode();
43 DefaultStatementNode(const Span& span_, Node* statement_);
44 void Accept(Visitor& visitor) override;
45 };
46
47 class ExpressionStatementNode : public UnaryNode
48 {
49 public:
50 ExpressionStatementNode();
51 ExpressionStatementNode(const Span& span_, Node* expression_);
52 void Accept(Visitor& visitor) override;
53 };
54
55 class CompoundStatementNode : public UnaryNode
56 {
57 public:
58 CompoundStatementNode();
59 CompoundStatementNode(const Span& span_);
60 void Accept(Visitor& visitor) override;
61 void Write(Writer& writer) override;
62 void Read(Reader& reader) override;
63 void AddStatement(const Span& span_, Node* statement);
64 void SetEndBraceSpan(const Span& endBraceSpan_) { endBraceSpan = endBraceSpan_; }
65 const Span& EndBraceSpan() const { return endBraceSpan; }
66 private:
67 Span endBraceSpan;
68 };
69
70 class StatementSequenceNode : public BinaryNode
71 {
72 public:
73 StatementSequenceNode();
74 StatementSequenceNode(const Span& span_, Node* leftStatement_, Node* rightStatement_);
75 void Accept(Visitor& visitor) override;
76 };
77
78 class IfStatementNode : public Node
79 {
80 public:
81 IfStatementNode();
82 IfStatementNode(const Span& span_, Node* condition_, Node* thenS_, Node* elseS_, const Span& elseSpan_);
83 void Accept(Visitor& visitor) override;
84 void Write(Writer& writer) override;
85 void Read(Reader& reader) override;
86 Node* Condition() { return condition.get(); }
87 Node* ThenS() { return thenS.get(); }
88 Node* ElseS() { return elseS.get(); }
89 const Span& ElseSpan() const { return elseSpan; }
90 private:
91 std::unique_ptr<Node> condition;
92 std::unique_ptr<Node> thenS;
93 std::unique_ptr<Node> elseS;
94 Span elseSpan;
95 };
96
97 class SwitchStatementNode : public Node
98 {
99 public:
100 SwitchStatementNode();
101 SwitchStatementNode(const Span& span_, Node* condition_, Node* statement_);
102 void Accept(Visitor& visitor) override;
103 void Write(Writer& writer) override;
104 void Read(Reader& reader) override;
105 Node* Condition() { return condition.get(); }
106 Node* Statement() { return statement.get(); }
107 private:
108 std::unique_ptr<Node> condition;
109 std::unique_ptr<Node> statement;
110 };
111
112 class WhileStatementNode : public Node
113 {
114 public:
115 WhileStatementNode();
116 WhileStatementNode(const Span& span_, Node* condition_, Node* statement_);
117 void Accept(Visitor& visitor) override;
118 void Write(Writer& writer) override;
119 void Read(Reader& reader) override;
120 Node* Condition() { return condition.get(); }
121 Node* Statement() { return statement.get(); }
122 private:
123 std::unique_ptr<Node> condition;
124 std::unique_ptr<Node> statement;
125 };
126
127 class DoStatementNode : public Node
128 {
129 public:
130 DoStatementNode();
131 DoStatementNode(const Span& span_, Node* statement_, Node* condition_, const Span& whileSpan_);
132 void Accept(Visitor& visitor) override;
133 void Write(Writer& writer) override;
134 void Read(Reader& reader) override;
135 Node* Statement() { return statement.get(); }
136 Node* Condition() { return condition.get(); }
137 const Span& WhileSpan() const { return whileSpan; }
138 private:
139 std::unique_ptr<Node> statement;
140 std::unique_ptr<Node> condition;
141 Span whileSpan;
142 };
143
144 class RangeForStatementNode : public Node
145 {
146 public:
147 RangeForStatementNode();
148 RangeForStatementNode(const Span& span_, Node* forRangeDeclaration_, Node* forRangeInitializer_, Node* statement_);
149 void Accept(Visitor& visitor) override;
150 void Write(Writer& writer) override;
151 void Read(Reader& reader) override;
152 Node* ForRangeDeclaration() { return forRangeDeclaration.get(); }
153 Node* ForRangeInitializer() { return forRangeInitializer.get(); }
154 Node* Statement() { return statement.get(); }
155 private:
156 std::unique_ptr<Node> forRangeDeclaration;
157 std::unique_ptr<Node> forRangeInitializer;
158 std::unique_ptr<Node> statement;
159 };
160
161 class ForStatementNode : public Node
162 {
163 public:
164 ForStatementNode();
165 ForStatementNode(const Span& span_, Node* initS_, Node* condition_, Node* loopExpr_, Node* actionS_);
166 void Accept(Visitor& visitor) override;
167 void Write(Writer& writer) override;
168 void Read(Reader& reader) override;
169 Node* InitS() { return initS.get(); }
170 Node* Condition() { return condition.get(); }
171 Node* LoopExpr() { return loopExpr.get(); }
172 Node* ActionS() { return actionS.get(); }
173 private:
174 std::unique_ptr<Node> initS;
175 std::unique_ptr<Node> condition;
176 std::unique_ptr<Node> loopExpr;
177 std::unique_ptr<Node> actionS;
178 };
179
180 class BreakStatementNode : public Node
181 {
182 public:
183 BreakStatementNode();
184 BreakStatementNode(const Span& span_);
185 void Accept(Visitor& visitor) override;
186 };
187
188 class ContinueStatementNode : public Node
189 {
190 public:
191 ContinueStatementNode();
192 ContinueStatementNode(const Span& span_);
193 void Accept(Visitor& visitor) override;
194 };
195
196 class ReturnStatementNode : public Node
197 {
198 public:
199 ReturnStatementNode();
200 ReturnStatementNode(const Span& span_, Node* returnExpr_);
201 void Accept(Visitor& visitor) override;
202 void Write(Writer& writer) override;
203 void Read(Reader& reader) override;
204 Node* ReturnExpr() { return returnExpr.get(); }
205 private:
206 std::unique_ptr<Node> returnExpr;
207 };
208
209 class GotoStatementNode : public Node
210 {
211 public:
212 GotoStatementNode();
213 GotoStatementNode(const Span& span_, const std::u32string& target_);
214 void Accept(Visitor& visitor) override;
215 void Write(Writer& writer) override;
216 void Read(Reader& reader) override;
217 const std::u32string& Target() const { return target; }
218 private:
219 std::u32string target;
220 };
221
222 class DeclarationStatementNode : public UnaryNode
223 {
224 public:
225 DeclarationStatementNode();
226 DeclarationStatementNode(const Span& span_, Node* declaration_);
227 void Accept(Visitor& visitor) override;
228 };
229
230 class TryStatementNode : public Node
231 {
232 public:
233 TryStatementNode();
234 TryStatementNode(const Span& span_, CompoundStatementNode* tryBlock_);
235 void Accept(Visitor& visitor) override;
236 void Write(Writer& writer) override;
237 void Read(Reader& reader) override;
238 void AddHandler(const Span& span, Node* handler);
239 CompoundStatementNode* TryBlock() { return tryBlock.get(); }
240 Node* Handlers() { return handlers.get(); }
241 private:
242 std::unique_ptr<CompoundStatementNode> tryBlock;
243 std::unique_ptr<Node> handlers;
244 };
245
246 class HandlerNode : public Node
247 {
248 public:
249 HandlerNode();
250 HandlerNode(const Span& span_, Node* exceptionDeclaration_, CompoundStatementNode* catchBlock_);
251 void Accept(Visitor& visitor) override;
252 void Write(Writer& writer) override;
253 void Read(Reader& reader) override;
254 Node* ExceptionDeclaration() { return exceptionDeclaration.get(); }
255 CompoundStatementNode* CatchBlock() { return catchBlock.get(); }
256 private:
257 std::unique_ptr<Node> exceptionDeclaration;
258 std::unique_ptr<CompoundStatementNode> catchBlock;
259 };
260
261 class HandlerSequenceNode : public BinaryNode
262 {
263 public:
264 HandlerSequenceNode();
265 HandlerSequenceNode(const Span& span_, Node* left_, Node* right_);
266 void Accept(Visitor& visitor) override;
267 };
268
269 class CatchAllNode : public Node
270 {
271 public:
272 CatchAllNode();
273 CatchAllNode(const Span& span_);
274 void Accept(Visitor& visitor) override;
275 };
276
277 } }
278
279 #endif // SNGCPP_AST_STATEMENT_INCLUDED