forked from gubaojian/esprima-cpp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
esprima.h
517 lines (455 loc) · 17.7 KB
/
esprima.h
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
#ifndef __ESPRIMA_H__
#define __ESPRIMA_H__
#include <assert.h>
#include <string>
#include <vector>
// API from https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API
namespace esprima {
struct Pool;
struct Poolable {
Poolable *poolable;
Poolable(Pool &pool);
virtual ~Poolable() {}
};
struct Pool {
Poolable *first;
Pool() : first() {}
~Pool() {
while (first) {
Poolable *poolable = first;
first = first->poolable;
delete poolable;
}
}
};
struct Position : Poolable {
int line;
int column;
Position(Pool &pool) : Poolable(pool), line(), column() {}
};
struct SourceLocation : Poolable{
Position *start;
Position *end;
SourceLocation(Pool &pool) : Poolable(pool), start(), end() {}
};
struct Program;
struct Identifier;
struct BlockStatement;
struct EmptyStatement;
struct ExpressionStatement;
struct IfStatement;
struct LabeledStatement;
struct BreakStatement;
struct ContinueStatement;
struct WithStatement;
struct SwitchCase;
struct SwitchStatement;
struct ReturnStatement;
struct ThrowStatement;
struct CatchClause;
struct TryStatement;
struct WhileStatement;
struct DoWhileStatement;
struct ForStatement;
struct ForInStatement;
struct DebuggerStatement;
struct FunctionDeclaration;
struct VariableDeclarator;
struct VariableDeclaration;
struct ThisExpression;
struct ArrayExpression;
struct Property;
struct ObjectExpression;
struct FunctionExpression;
struct SequenceExpression;
struct UnaryExpression;
struct BinaryExpression;
struct AssignmentExpression;
struct UpdateExpression;
struct LogicalExpression;
struct ConditionalExpression;
struct NewExpression;
struct CallExpression;
struct MemberExpression;
struct NullLiteral;
struct RegExpLiteral;
struct StringLiteral;
struct NumericLiteral;
struct BooleanLiteral;
struct Visitor {
virtual void visit(Program *node) = 0;
virtual void visit(Identifier *node) = 0;
virtual void visit(BlockStatement *node) = 0;
virtual void visit(EmptyStatement *node) = 0;
virtual void visit(ExpressionStatement *node) = 0;
virtual void visit(IfStatement *node) = 0;
virtual void visit(LabeledStatement *node) = 0;
virtual void visit(BreakStatement *node) = 0;
virtual void visit(ContinueStatement *node) = 0;
virtual void visit(WithStatement *node) = 0;
virtual void visit(SwitchCase *node) = 0;
virtual void visit(SwitchStatement *node) = 0;
virtual void visit(ReturnStatement *node) = 0;
virtual void visit(ThrowStatement *node) = 0;
virtual void visit(CatchClause *node) = 0;
virtual void visit(TryStatement *node) = 0;
virtual void visit(WhileStatement *node) = 0;
virtual void visit(DoWhileStatement *node) = 0;
virtual void visit(ForStatement *node) = 0;
virtual void visit(ForInStatement *node) = 0;
virtual void visit(DebuggerStatement *node) = 0;
virtual void visit(FunctionDeclaration *node) = 0;
virtual void visit(VariableDeclarator *node) = 0;
virtual void visit(VariableDeclaration *node) = 0;
virtual void visit(ThisExpression *node) = 0;
virtual void visit(ArrayExpression *node) = 0;
virtual void visit(Property *node) = 0;
virtual void visit(ObjectExpression *node) = 0;
virtual void visit(FunctionExpression *node) = 0;
virtual void visit(SequenceExpression *node) = 0;
virtual void visit(UnaryExpression *node) = 0;
virtual void visit(BinaryExpression *node) = 0;
virtual void visit(AssignmentExpression *node) = 0;
virtual void visit(UpdateExpression *node) = 0;
virtual void visit(LogicalExpression *node) = 0;
virtual void visit(ConditionalExpression *node) = 0;
virtual void visit(NewExpression *node) = 0;
virtual void visit(CallExpression *node) = 0;
virtual void visit(MemberExpression *node) = 0;
virtual void visit(NullLiteral *node) = 0;
virtual void visit(RegExpLiteral *node) = 0;
virtual void visit(StringLiteral *node) = 0;
virtual void visit(NumericLiteral *node) = 0;
virtual void visit(BooleanLiteral *node) = 0;
void visitChildren(Program *node);
void visitChildren(Identifier *node);
void visitChildren(BlockStatement *node);
void visitChildren(EmptyStatement *node);
void visitChildren(ExpressionStatement *node);
void visitChildren(IfStatement *node);
void visitChildren(LabeledStatement *node);
void visitChildren(BreakStatement *node);
void visitChildren(ContinueStatement *node);
void visitChildren(WithStatement *node);
void visitChildren(SwitchCase *node);
void visitChildren(SwitchStatement *node);
void visitChildren(ReturnStatement *node);
void visitChildren(ThrowStatement *node);
void visitChildren(CatchClause *node);
void visitChildren(TryStatement *node);
void visitChildren(WhileStatement *node);
void visitChildren(DoWhileStatement *node);
void visitChildren(ForStatement *node);
void visitChildren(ForInStatement *node);
void visitChildren(DebuggerStatement *node);
void visitChildren(FunctionDeclaration *node);
void visitChildren(VariableDeclarator *node);
void visitChildren(VariableDeclaration *node);
void visitChildren(ThisExpression *node);
void visitChildren(ArrayExpression *node);
void visitChildren(Property *node);
void visitChildren(ObjectExpression *node);
void visitChildren(FunctionExpression *node);
void visitChildren(SequenceExpression *node);
void visitChildren(UnaryExpression *node);
void visitChildren(BinaryExpression *node);
void visitChildren(AssignmentExpression *node);
void visitChildren(UpdateExpression *node);
void visitChildren(LogicalExpression *node);
void visitChildren(ConditionalExpression *node);
void visitChildren(NewExpression *node);
void visitChildren(CallExpression *node);
void visitChildren(MemberExpression *node);
void visitChildren(NullLiteral *node);
void visitChildren(RegExpLiteral *node);
void visitChildren(StringLiteral *node);
void visitChildren(NumericLiteral *node);
void visitChildren(BooleanLiteral *node);
};
struct Node : Poolable {
SourceLocation *loc;
SourceLocation *groupLoc;
int range[2];
int groupRange[2];
Node(Pool &pool) : Poolable(pool), loc(), groupLoc(NULL) { range[0] = range[1] = groupRange[0] = groupRange[1] = 0; }
virtual ~Node() {}
virtual void accept(Visitor *visitor) = 0;
template <class T> bool is() {
return dynamic_cast<T *>(this) != NULL;
}
template <class T> T *as() {
T *t = dynamic_cast<T *>(this);
assert(t != NULL);
return t;
}
};
struct Program : Node {
std::vector<Node *> body;
Program(Pool &pool) : Node(pool) {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct Identifier : Node {
std::string name;
Identifier(Pool &pool) : Node(pool) {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct BlockStatement : Node {
std::vector<Node *> body;
BlockStatement(Pool &pool) : Node(pool) {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
// This is a mixin, not a base class, and so isn't poolable
struct Function {
Identifier *id;
std::vector<Identifier *> params;
BlockStatement *body;
Function() : id(), body() {}
};
struct EmptyStatement : Node {
EmptyStatement(Pool &pool) : Node(pool) {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct ExpressionStatement : Node {
Node *expression;
ExpressionStatement(Pool &pool) : Node(pool), expression() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct IfStatement : Node {
Node *test;
Node *consequent;
Node *alternate;
IfStatement(Pool &pool) : Node(pool), test(), consequent(), alternate() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct LabeledStatement : Node {
Identifier *label;
Node *body;
LabeledStatement(Pool &pool) : Node(pool), label(), body() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct BreakStatement : Node {
Identifier *label;
BreakStatement(Pool &pool) : Node(pool), label() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct ContinueStatement : Node {
Identifier *label;
ContinueStatement(Pool &pool) : Node(pool), label() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct WithStatement : Node {
Node *object;
Node *body;
WithStatement(Pool &pool) : Node(pool), object(), body() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct SwitchCase : Node {
Node *test;
std::vector<Node *> consequent;
SwitchCase(Pool &pool) : Node(pool), test() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct SwitchStatement : Node {
Node *discriminant;
std::vector<SwitchCase *> cases;
SwitchStatement(Pool &pool) : Node(pool), discriminant() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct ReturnStatement : Node {
Node *argument;
ReturnStatement(Pool &pool) : Node(pool), argument() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct ThrowStatement : Node {
Node *argument;
ThrowStatement(Pool &pool) : Node(pool), argument() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct CatchClause : Node {
Node *param;
BlockStatement *body;
CatchClause(Pool &pool) : Node(pool), param(), body() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct TryStatement : Node {
BlockStatement *block;
CatchClause *handler;
BlockStatement *finalizer;
TryStatement(Pool &pool) : Node(pool), block(), handler(), finalizer() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct WhileStatement : Node {
Node *test;
Node *body;
WhileStatement(Pool &pool) : Node(pool), test(), body() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct DoWhileStatement : Node {
Node *body;
Node *test;
DoWhileStatement(Pool &pool) : Node(pool), body(), test() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct ForStatement : Node {
Node *init; // Either a VariableDeclaration or an Node
Node *test;
Node *update;
Node *body;
ForStatement(Pool &pool) : Node(pool), init(), test(), update(), body() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct ForInStatement : Node {
Node *left; // Either a VariableDeclaration or an Node
Node *right;
Node *body;
ForInStatement(Pool &pool) : Node(pool), left(), right(), body() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct DebuggerStatement : Node {
DebuggerStatement(Pool &pool) : Node(pool) {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct FunctionDeclaration : Node, Function {
FunctionDeclaration(Pool &pool) : Node(pool) {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct VariableDeclarator : Node {
Identifier *id;
Node *init;
VariableDeclarator(Pool &pool) : Node(pool), id(), init() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct VariableDeclaration : Node {
std::vector<VariableDeclarator *> declarations;
std::string kind;
VariableDeclaration(Pool &pool) : Node(pool) {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct ThisExpression : Node {
ThisExpression(Pool &pool) : Node(pool) {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct ArrayExpression : Node {
std::vector<Node *> elements;
ArrayExpression(Pool &pool) : Node(pool) {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct Property : Node {
std::string kind;
Node *key; // Either a Literal or an Identifier
Node *value;
Property(Pool &pool) : Node(pool), key(), value() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct ObjectExpression : Node {
std::vector<Property *> properties;
ObjectExpression(Pool &pool) : Node(pool) {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct FunctionExpression : Node, Function {
FunctionExpression(Pool &pool) : Node(pool) {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct SequenceExpression : Node {
std::vector<Node *> expressions;
SequenceExpression(Pool &pool) : Node(pool) {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct UnaryExpression : Node {
std::string operator_;
bool prefix;
Node *argument;
UnaryExpression(Pool &pool) : Node(pool), prefix(), argument() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct BinaryExpression : Node {
std::string operator_;
Node *left;
Node *right;
BinaryExpression(Pool &pool) : Node(pool), left(), right() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct AssignmentExpression : Node {
std::string operator_;
Node *left;
Node *right;
AssignmentExpression(Pool &pool) : Node(pool), left(), right() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct UpdateExpression : Node {
std::string operator_;
Node *argument;
bool prefix;
UpdateExpression(Pool &pool) : Node(pool), argument(), prefix() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct LogicalExpression : Node {
std::string operator_;
Node *left;
Node *right;
LogicalExpression(Pool &pool) : Node(pool), left(), right() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct ConditionalExpression : Node {
Node *test;
Node *alternate;
Node *consequent;
ConditionalExpression(Pool &pool) : Node(pool), test(), alternate(), consequent() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct NewExpression : Node {
Node *callee;
std::vector<Node *> arguments;
NewExpression(Pool &pool) : Node(pool), callee() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct CallExpression : Node {
Node *callee;
std::vector<Node *> arguments;
CallExpression(Pool &pool) : Node(pool), callee() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct MemberExpression : Node {
Node *object;
Node *property; // Identifier if computed == false
bool computed;
MemberExpression(Pool &pool) : Node(pool), object(), property(), computed() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct Literal : Node {
Literal(Pool &pool) : Node(pool) {}
};
struct NullLiteral : Literal {
NullLiteral(Pool &pool) : Literal(pool) {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct RegExpLiteral : Literal {
std::string pattern;
std::string flags;
RegExpLiteral(Pool &pool) : Literal(pool) {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct StringLiteral : Literal {
std::string value;
StringLiteral(Pool &pool) : Literal(pool) {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct NumericLiteral : Literal {
double value;
NumericLiteral(Pool &pool) : Literal(pool), value() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct BooleanLiteral : Literal {
bool value;
BooleanLiteral(Pool &pool) : Literal(pool), value() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct ParseError {
std::string description;
int index;
int lineNumber;
int column;
};
Program *parse(Pool &pool, const std::string &code);
}
#endif