1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
#include "ASTPrinter.h"
#include "Expression.h"
#include <iostream>
#include <iosfwd>
std::ostream& operator<<(std::ostream& os, const TokenType& t)
{
switch (t)
{
case TokenType::LeftParen: os << "\"LeftParen\""; return os;
case TokenType::RightParen: os << "\"RightParen\""; return os;
case TokenType::LeftBrace: os << "\"LeftBrace\""; return os;
case TokenType::RightBrace: os << "\"RightBrace\""; return os;
case TokenType::Comma: os << "\"Comma\""; return os;
case TokenType::Dot: os << "\"Dot\""; return os;
case TokenType::Minus: os << "\"Minus\""; return os;
case TokenType::Plus: os << "\"Plus\""; return os;
case TokenType::Semicolon: os << "\"Semicolon\""; return os;
case TokenType::Star: os << "\"Star\""; return os;
case TokenType::Division: os << "\"Division\""; return os;
case TokenType::Modulo: os << "\"Modulo\""; return os;
case TokenType::BitwiseNot: os << "\"BitwiseNot\""; return os;
case TokenType::BitwiseAnd: os << "\"BitwiseAnd\""; return os;
case TokenType::BitwiseXor: os << "\"BitwiseXor\""; return os;
case TokenType::BitwiseOr: os << "\"BitwiseOr\""; return os;
case TokenType::QuestionMark: os << "\"QuestionMark\""; return os;
case TokenType::Colon: os << "\"Colon\""; return os;
case TokenType::LeftSquareBracket: os << "\"LeftSquareBracket\""; return os;
case TokenType::RightSquareBracket: os << "\"RightSquareBracket\""; return os;
case TokenType::Bang: os << "\"Bang\""; return os;
case TokenType::BangEqual: os << "\"BangEqual\""; return os;
case TokenType::Equal: os << "\"Equal\""; return os;
case TokenType::EqualEqual: os << "\"EqualEqual\""; return os;
case TokenType::StrictEqual: os << "\"StrictEqual\""; return os;
case TokenType::StrictNotEqual: os << "\"StrictNotEqual\""; return os;
case TokenType::Greater: os << "\"Greater\""; return os;
case TokenType::GreaterEqual: os << "\"GreaterEqual\""; return os;
case TokenType::Less: os << "\"Less\""; return os;
case TokenType::LessEqual: os << "\"LessEqual\""; return os;
case TokenType::MinusMinus: os << "\"MinusMinus\""; return os;
case TokenType::PlusPlus: os << "\"PlusPlus\""; return os;
case TokenType::LeftShift: os << "\"LeftShift\""; return os;
case TokenType::RightShift: os << "\"RightShift\""; return os;
case TokenType::LogicalAnd: os << "\"LogicalAnd\""; return os;
case TokenType::LogicalOr: os << "\"LogicalOr\""; return os;
case TokenType::StarEqual: os << "\"StarEqual\""; return os;
case TokenType::DivideEqual: os << "\"DivideEqual\""; return os;
case TokenType::ModuloEqual: os << "\"ModuloEqual\""; return os;
case TokenType::PlusEqual: os << "\"PlusEqual\""; return os;
case TokenType::MinusEqual: os << "\"MinusEqual\""; return os;
case TokenType::LeftShiftEqual: os << "\"LeftShiftEqual\""; return os;
case TokenType::RightShiftEqual: os << "\"RightShiftEqual\""; return os;
case TokenType::BitwiseAndEqual: os << "\"BitwiseAndEqual\""; return os;
case TokenType::BitwiseXorEqual: os << "\"BitwiseXorEqual\""; return os;
case TokenType::BitwiseOrEqual: os << "\"BitwiseOrEqual\""; return os;
case TokenType::Identifier: os << "\"Identifier\""; return os;
case TokenType::String: os << "\"String\""; return os;
case TokenType::Number: os << "\"Number\""; return os;
case TokenType::Break: os << "\"Break\""; return os;
case TokenType::Case: os << "\"Case\""; return os;
case TokenType::Catch: os << "\"Catch\""; return os;
case TokenType::Class: os << "\"Class\""; return os;
case TokenType::Const: os << "\"Const\""; return os;
case TokenType::Continue: os << "\"Continue\""; return os;
case TokenType::Debugger: os << "\"Debugger\""; return os;
case TokenType::Default: os << "\"Default\""; return os;
case TokenType::Delete: os << "\"Delete\""; return os;
case TokenType::Do: os << "\"Do\""; return os;
case TokenType::Else: os << "\"Else\""; return os;
case TokenType::Export: os << "\"Export\""; return os;
case TokenType::Extends: os << "\"Extends\""; return os;
case TokenType::Finally: os << "\"Finally\""; return os;
case TokenType::For: os << "\"For\""; return os;
case TokenType::Function: os << "\"Function\""; return os;
case TokenType::If: os << "\"If\""; return os;
case TokenType::Import: os << "\"Import\""; return os;
case TokenType::In: os << "\"In\""; return os;
case TokenType::Instanceof: os << "\"Instanceof\""; return os;
case TokenType::New: os << "\"New\""; return os;
case TokenType::Return: os << "\"Return\""; return os;
case TokenType::Super: os << "\"Super\""; return os;
case TokenType::Switch: os << "\"Switch\""; return os;
case TokenType::This: os << "\"This\""; return os;
case TokenType::Throw: os << "\"Throw\""; return os;
case TokenType::Try: os << "\"Try\""; return os;
case TokenType::Typeof: os << "\"Typeof\""; return os;
case TokenType::Var: os << "\"Var\""; return os;
case TokenType::Void: os << "\"Void\""; return os;
case TokenType::While: os << "\"While\""; return os;
case TokenType::With: os << "\"With\""; return os;
case TokenType::Yield: os << "\"Yield\""; return os;
case TokenType::Null: os << "\"Null\""; return os;
case TokenType::Undefined: os << "\"Undefined\""; return os;
case TokenType::True: os << "\"True\""; return os;
case TokenType::False: os << "\"False\""; return os;
case TokenType::Eof: os << "\"Eof\""; return os;
case TokenType::Invalid: os << "\"Invalid\""; return os;
default:
break;
}
return os;
}
class ASTToJsonPrinter : public ExpressionVisitor
{
public:
ASTToJsonPrinter(std::ostream& os);
virtual ~ASTToJsonPrinter() {}
virtual void Visit(LiteralNull* e) override;
virtual void Visit(LiteralUndefined* e) override;
virtual void Visit(LiteralString* e) override;
virtual void Visit(LiteralNumber* e) override;
virtual void Visit(LiteralBoolean* e) override;
virtual void Visit(LiteralField* e) override;
virtual void Visit(LiteralObject* e) override;
virtual void Visit(BinaryExpression* e) override;
virtual void Visit(UnaryExpression* e) override;
virtual void Visit(IdentifierExpression* e) override;
virtual void Visit(ListExpression* e) override;
virtual void Visit(VariableDefinitionExpression* e) override;
virtual void Visit(BlockStatement* e) override;
virtual void Visit(LabeledStatement* e) override;
virtual void Visit(IfStatement* e) override;
virtual void Visit(SwitchStatement* e) override;
virtual void Visit(CaseStatement *) override;
virtual void Visit(WhileStatement* e) override;
virtual void Visit(ForStatement* e) override;
virtual void Visit(FunctionDeclaration* e) override;
virtual void Visit(TopStatements* e) override;
virtual void Visit(EmptyExpression* e) override;
virtual void Visit(CallExpression* e) override;
void Print( const char* name, const ExpressionPtr& expr, bool isLast);
void Print( const char* name, const IPLVector<ExpressionPtr>& list, bool isLast);
void Print( const char* name, const IPLVector<IPLString>& list, bool isLast);
void Print( const char* name, const IPLString& member, bool isLast);
void Print( const char* name, bool member, bool isLast);
void Print( const char* name, const TokenType& member, bool isLast);
void Print( const char* name, const double& member, bool isLast);
inline void Enter();
inline void Exit();
inline void NoIndentNextAccept();
inline void IndentNextAccept();
inline void InsertIndent();
private:
std::ostream& m_Output;
unsigned m_Level;
bool m_IndentNext;
};
ASTToJsonPrinter::ASTToJsonPrinter(std::ostream& os)
: m_Output(os)
, m_Level(0)
, m_IndentNext(true)
{}
void PrepNextElement(std::ostream& os, bool isLast)
{
if (!isLast)
{
os << ",";
}
os << "\n";
}
void ASTToJsonPrinter::Print(const char* name, const ExpressionPtr& expr, bool isLast)
{
if (!expr)
{
return;
}
InsertIndent();
m_Output << "\"" << name << "\": ";
NoIndentNextAccept();
expr->Accept(*this);
PrepNextElement(m_Output, isLast);
}
void ASTToJsonPrinter::Print(const char* name, const IPLVector<ExpressionPtr>& list, bool isLast)
{
InsertIndent();
m_Output << "\"" << name << "\": [";
if (list.empty())
{
m_Output << "]";
PrepNextElement(m_Output, isLast);
return;
}
Enter();
for (auto i = 0u; i < list.size(); ++i)
{
IndentNextAccept();
list[i]->Accept(*this);
if (i + 1 != list.size())
{
m_Output << ",\n";
}
}
Exit();
m_Output << "\n";
InsertIndent();
m_Output << "]";
PrepNextElement(m_Output, isLast);
}
void ASTToJsonPrinter::Print(const char* name, const IPLVector<IPLString>& list, bool isLast)
{
InsertIndent();
m_Output << "\"" << name << "\": [";
if (list.empty())
{
m_Output << "]";
PrepNextElement(m_Output, isLast);
return;
}
Enter();
for (auto i = 0u; i < list.size(); ++i)
{
InsertIndent();
m_Output << "\"" << list[i] << "\"";
if (i + 1 != list.size())
{
m_Output << ",\n";
}
}
Exit();
m_Output << "\n";
InsertIndent();
m_Output << "]";
PrepNextElement(m_Output, isLast);
}
void ASTToJsonPrinter::Print(const char* name, bool member, bool isLast)
{
InsertIndent();
m_Output << "\"" << name << "\": " << member;
PrepNextElement(m_Output, isLast);
}
void ASTToJsonPrinter::Print(const char* name, const TokenType& member, bool isLast)
{
InsertIndent();
m_Output << "\"" << name << "\": " << member;
PrepNextElement(m_Output, isLast);
}
IPLString StripString(const IPLString& str)
{
if (str.front() == '\"')
{
return str.substr(1, str.length() - 2);
}
return str;
}
void ASTToJsonPrinter::Print(const char* name, const IPLString& member, bool isLast)
{
InsertIndent();
m_Output << "\"" << name << "\": \"" << StripString(member) << "\"";
PrepNextElement(m_Output, isLast);
}
void ASTToJsonPrinter::Print(const char* name, const double& member, bool isLast)
{
InsertIndent();
m_Output << "\"" << name << "\": " << member;
PrepNextElement(m_Output, isLast);
}
#define MEMBERS_COUNT(type, name, def) ++membersCount;
#define VISIT_MEMBER(type, name, def) ++currentVisitCount; Print(#name, e->Get##name(), currentVisitCount == membersCount);
#define GENERATE_AST_PRINTER_FUNCTION(ClassName, MEMBERS_ITERATOR) \
void ASTToJsonPrinter::Visit(ClassName* e) \
{ \
int membersCount = 0, currentVisitCount = 0; \
(void)currentVisitCount;(void)e; \
MEMBERS_ITERATOR(MEMBERS_COUNT); \
if (m_Level) \
{ \
InsertIndent(); \
} \
m_Output << "{"; \
Enter(); \
InsertIndent(); \
m_Output << "\"ExpressionType\": \"" << #ClassName "\""; \
if (membersCount) \
{ \
m_Output << ","; \
} \
m_Output << "\n"; \
MEMBERS_ITERATOR(VISIT_MEMBER); \
Exit(); \
InsertIndent(); \
m_Output << "}"; \
}
EXPRESSION_DEFINITION_ITERATOR(GENERATE_AST_PRINTER_FUNCTION);
void ASTToJsonPrinter::Enter()
{
m_Output << "\n";
++m_Level;
}
void ASTToJsonPrinter::Exit()
{
--m_Level;
}
void ASTToJsonPrinter::IndentNextAccept()
{
m_IndentNext = true;
}
void ASTToJsonPrinter::InsertIndent()
{
for (unsigned i = 0; i < m_Level; ++i)
{
m_Output << " ";
}
}
void ASTToJsonPrinter::NoIndentNextAccept()
{
m_IndentNext = false;
}
void PrintAST(const ExpressionPtr& ast, std::ostream& where)
{
ASTToJsonPrinter p(where);
ast->Accept(p);
}
|