-
Notifications
You must be signed in to change notification settings - Fork 28
/
esprima.h
528 lines (464 loc) · 18.1 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
518
519
520
521
522
523
524
525
526
527
528
#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 {
Position *start;
Position *end;
SourceLocation(Pool &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 Statement : Node {
Statement(Pool &pool) : Node(pool) {}
};
struct Expression : Node {
Expression(Pool &pool) : Node(pool) {}
};
struct Program : Node {
std::vector<Statement *> body;
Program(Pool &pool) : Node(pool) {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct Identifier : Expression {
std::string name;
Identifier(Pool &pool) : Expression(pool) {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct BlockStatement : Statement {
std::vector<Statement *> body;
BlockStatement(Pool &pool) : Statement(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 : Statement {
EmptyStatement(Pool &pool) : Statement(pool) {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct ExpressionStatement : Statement {
Expression *expression;
ExpressionStatement(Pool &pool) : Statement(pool), expression() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct IfStatement : Statement {
Expression *test;
Statement *consequent;
Statement *alternate;
IfStatement(Pool &pool) : Statement(pool), test(), consequent(), alternate() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct LabeledStatement : Statement {
Identifier *label;
Statement *body;
LabeledStatement(Pool &pool) : Statement(pool), label(), body() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct BreakStatement : Statement {
Identifier *label;
BreakStatement(Pool &pool) : Statement(pool), label() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct ContinueStatement : Statement {
Identifier *label;
ContinueStatement(Pool &pool) : Statement(pool), label() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct WithStatement : Statement {
Expression *object;
Statement *body;
WithStatement(Pool &pool) : Statement(pool), object(), body() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct SwitchCase : Node {
Expression *test;
std::vector<Statement *> consequent;
SwitchCase(Pool &pool) : Node(pool), test() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct SwitchStatement : Statement {
Expression *discriminant;
std::vector<SwitchCase *> cases;
SwitchStatement(Pool &pool) : Statement(pool), discriminant() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct ReturnStatement : Statement {
Expression *argument;
ReturnStatement(Pool &pool) : Statement(pool), argument() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct ThrowStatement : Statement {
Expression *argument;
ThrowStatement(Pool &pool) : Statement(pool), argument() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct CatchClause : Node {
Expression *param;
BlockStatement *body;
CatchClause(Pool &pool) : Node(pool), param(), body() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct TryStatement : Statement {
BlockStatement *block;
CatchClause *handler;
BlockStatement *finalizer;
TryStatement(Pool &pool) : Statement(pool), block(), handler(), finalizer() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct WhileStatement : Statement {
Expression *test;
Statement *body;
WhileStatement(Pool &pool) : Statement(pool), test(), body() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct DoWhileStatement : Statement {
Statement *body;
Expression *test;
DoWhileStatement(Pool &pool) : Statement(pool), body(), test() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct ForStatement : Statement {
Node *init; // Either a VariableDeclaration or an Expression
Expression *test;
Expression *update;
Statement *body;
ForStatement(Pool &pool) : Statement(pool), init(), test(), update(), body() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct ForInStatement : Statement {
Node *left; // Either a VariableDeclaration or an Expression
Expression *right;
Statement *body;
ForInStatement(Pool &pool) : Statement(pool), left(), right(), body() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct DebuggerStatement : Statement {
DebuggerStatement(Pool &pool) : Statement(pool) {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct Declaration : Statement {
Declaration(Pool &pool) : Statement(pool) {}
};
struct FunctionDeclaration : Declaration, Function {
FunctionDeclaration(Pool &pool) : Declaration(pool) {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct VariableDeclarator : Node {
Identifier *id;
Expression *init;
VariableDeclarator(Pool &pool) : Node(pool), id(), init() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct VariableDeclaration : Declaration {
std::vector<VariableDeclarator *> declarations;
std::string kind;
VariableDeclaration(Pool &pool) : Declaration(pool) {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct ThisExpression : Expression {
ThisExpression(Pool &pool) : Expression(pool) {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct ArrayExpression : Expression {
std::vector<Expression *> elements;
ArrayExpression(Pool &pool) : Expression(pool) {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct Property : Node {
std::string kind;
Expression *key; // Either a Literal or an Identifier
Expression *value;
Property(Pool &pool) : Node(pool), key(), value() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct ObjectExpression : Expression {
std::vector<Property *> properties;
ObjectExpression(Pool &pool) : Expression(pool) {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct FunctionExpression : Expression, Function {
FunctionExpression(Pool &pool) : Expression(pool) {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct SequenceExpression : Expression {
std::vector<Expression *> expressions;
SequenceExpression(Pool &pool) : Expression(pool) {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct UnaryExpression : Expression {
std::string operator_;
bool prefix;
Expression *argument;
UnaryExpression(Pool &pool) : Expression(pool), prefix(), argument() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct BinaryExpression : Expression {
std::string operator_;
Expression *left;
Expression *right;
BinaryExpression(Pool &pool) : Expression(pool), left(), right() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct AssignmentExpression : Expression {
std::string operator_;
Expression *left;
Expression *right;
AssignmentExpression(Pool &pool) : Expression(pool), left(), right() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct UpdateExpression : Expression {
std::string operator_;
Expression *argument;
bool prefix;
UpdateExpression(Pool &pool) : Expression(pool), argument(), prefix() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct LogicalExpression : Expression {
std::string operator_;
Expression *left;
Expression *right;
LogicalExpression(Pool &pool) : Expression(pool), left(), right() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct ConditionalExpression : Expression {
Expression *test;
Expression *alternate;
Expression *consequent;
ConditionalExpression(Pool &pool) : Expression(pool), test(), alternate(), consequent() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct NewExpression : Expression {
Expression *callee;
std::vector<Expression *> arguments;
NewExpression(Pool &pool) : Expression(pool), callee() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct CallExpression : Expression {
Expression *callee;
std::vector<Expression *> arguments;
CallExpression(Pool &pool) : Expression(pool), callee() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct MemberExpression : Expression {
Expression *object;
Expression *property; // Identifier if computed == false
bool computed;
MemberExpression(Pool &pool) : Expression(pool), object(), property(), computed() {}
void accept(Visitor *visitor) { visitor->visit(this); }
};
struct Literal : Expression {
Literal(Pool &pool) : Expression(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