-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser-api.js
510 lines (440 loc) · 22.3 KB
/
parser-api.js
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
/* jshint -W053, strict:true, eqeqeq:true, quotmark:single, undef:true, unused:true, trailing:true */
/* global exports, module, define */
(function(root, mod) {
'use strict';
if(typeof exports === 'object' && typeof module === 'object') {
// CommonJS
return mod(exports);
} else if (typeof define === 'function' && define.amd) {
// AMD
return define(['exports'], mod);
} else {
// Plain browser env
mod(root.ParserAPI || (root.ParserAPI = {}));
}
})(this, function(exports) {
'use strict';
// ## Token types
// The assignment of fine-grained, information-carrying type objects
// allows the tokenizer to store the information it has about a
// token in a way that is very cheap for the parser to look up.
// All token type variables start with an underscore, to make them
// easy to recognize.
// These are the general types. The `type` property is only used to
// make them recognizeable when debugging.
var types = {
PROGRAM: new String('Program'),
EMPTY_STATEMENT: new String('EmptyStatement'),
BLOCK_STATEMENT: new String('BlockStatement'),
EXPRESSION_STATEMENT: new String('ExpressionStatement'),
IF_STATEMENT: new String('IfStatement'),
LABELED_STATEMENT: new String('LabeledStatement'),
BREAK_STATEMENT: new String('BreakStatement'),
WITH_STATEMENT: new String('WithStatement'),
SWITCH_STATEMENT: new String('SwitchStatement'),
RETURN_STATEMENT: new String('ReturnStatement'),
THROW_STATEMENT: new String('ThrowStatement'),
TRY_STATEMENT: new String('TryStatement'),
WHILE_STATEMENT: new String('WhileStatement'),
DOWHILE_STATEMENT: new String('DoWhileStatement'),
FOR_STATEMENT: new String('ForStatement'),
FORIN_STATEMENT: new String('ForInStatement'),
FOROF_STATEMENT: new String('ForOfStatement'),
DEBUGGER_STATEMENT: new String('DebuggerStatement'),
FUNCTION_DECLARATION: new String('FunctionDeclaration'),
VARIABLE_DECLARATION: new String('VariableDeclaration'),
VARIABLE_DECLARATOR: new String('VariableDeclarator'),
THIS_EXPRESSION: new String('ThisExpression'),
ARRAY_EXPRESSION: new String('ArrayExpression'),
OBJECT_EXPRESSION: new String('ObjectExpression'),
FUNCTION_EXPRESSION: new String('FunctionExpression'),
SEQUENCE_EXPRESSION: new String('SequenceExpression'),
UNARY_EXPRESSION: new String('UnaryExpression'),
BINARY_EXPRESSION: new String('BinaryExpression'),
ASSIGNMENT_EXPRESSION: new String('AssignmentExpression'),
UPDATE_EXPRESSION: new String('UpdateExpression'),
LOGICAL_EXPRESSION: new String('LogicalExpression'),
CONDITIONAL_EXPRESSION: new String('ConditionalExpression'),
NEW_EXPRESSION: new String('NewExpression'),
CALL_EXPRESSION: new String('CallExpression'),
MEMBER_EXPRESSION: new String('MemberExpression'),
SWITCH_CASE: new String('SwitchCase'),
CATCH_CLAUSE: new String('CatchClause'),
IDENTIFIER: new String('Identifier'),
LITERAL: new String('Literal')
};
// The type field is a string representing the AST variant type. Each subtype of Node is documented below with the specific string of its type field. You can use this field to determine which interface a node implements.
var nodes = {
// ### Programs
'Program': function Program() {
this.type = types.PROGRAM;
this.body = []; // [ Statement ]
this.loc = null; // SourceLocation | null
},
// A complete program source tree.
'SourceLocation': function SourceLocation() {
this.start = null; // Position
this.end = null; // Position
this.source = null; // string | null
},
// The loc field represents the source location information of the node. If the parser produced no information about the node's source location, the field is null; otherwise it is an object consisting of a start position (the position of the first character of the parsed source region) and an end position (the position of the first character after the parsed source region):
// Each Position object consists of a line number (1-indexed) and a column number (0-indexed):
'Position': function Position() {
this.line = 1; // number >= 1
this.column = 0; // number >= 0
},
// ### Statements
'EmptyStatement': function EmptyStatement() {
this.type = types.EMPTY_STATEMENT;
this.loc = null; // SourceLocation | null
},
// An empty statement, i.e., a solitary semicolon.
'BlockStatement': function BlockStatement() {
this.type = types.BLOCK_STATEMENT;
this.body = []; // [ Statement ]
this.loc = null; // SourceLocation | null
},
// A block statement, i.e., a sequence of statements surrounded by braces.
'ExpressionStatement': function ExpressionStatement() {
this.type = types.EXPRESSION_STATEMENT;
this.expression = null; // Expression
this.loc = null; // SourceLocation | null
},
// An expression statement, i.e., a statement consisting of a single expression.
'IfStatement': function IfStatement() {
this.type = types.IF_STATEMENT;
this.test = null; // Expression
this.consequent = null; // Statement
this.alternate = null; // Statement | null
this.loc = null; // SourceLocation | null
},
// An if statement.
'LabeledStatement': function LabeledStatement() {
this.type = types.LABELED_STATEMENT;
this.label = null; // Identifier
this.body = null; // Statement
this.loc = null; // SourceLocation | null
},
// A labeled statement, i.e., a statement prefixed by a break/continue label.
'BreakStatement': function BreakStatement() {
this.type = types.BREAK_STATEMENT;
this.label = null; // Identifier | null
this.loc = null; // SourceLocation | null
},
// A break statement.
'ContinueStatement': function ContinueStatement() {
this.type = types.CONTINUE_STATEMENT;
this.label = null; // Identifier | null
this.loc = null; // SourceLocation | null
},
// A continue statement.
'WithStatement': function WithStatement() {
this.type = types.WITH_STATEMENT;
this.object = null; // Expression
this.body = null; // Statement
this.loc = null; // SourceLocation | null
},
// A with statement.
'SwitchStatement': function SwitchStatement() {
this.type = types.SWITCH_STATEMENT;
this.discriminant = null; // Expression
this.cases = []; // [ SwitchCase ]
this.lexical = false; // boolean
this.loc = null; // SourceLocation | null
},
// A switch statement. The lexical flag is metadata indicating whether the switch statement contains any unnested let declarations (and therefore introduces a new lexical scope).
'ReturnStatement': function ReturnStatement() {
this.type = types.RETURN_STATEMENT;
this.argument = null; // Expression | null
this.loc = null; // SourceLocation | null
},
// A return statement.
'ThrowStatement': function ThrowStatement() {
this.type = types.THROW_STATEMENT;
this.argument = null; // Expression
this.loc = null; // SourceLocation | null
},
// A throw statement.
'TryStatement': function TryStatement() {
this.type = types.TRY_STATEMENT;
this.block = null; // BlockStatement
this.handler = null; // CatchClause | null
this.finalizer = null; // BlockStatement | null
this.loc = null; // SourceLocation | null
},
// A try statement.
'WhileStatement': function WhileStatement() {
this.type = types.WHILE_STATEMENT;
this.body = null; // Statement
this.test = null; // Expression
this.loc = null; // SourceLocation | null
},
// A while statement
'DoWhileStatement': function DoWhileStatement() {
this.type = types.DOWHILE_STATEMENT;
this.body = null; // Statement
this.test = null; // Expression
this.loc = null; // SourceLocation | null
},
// A do/while statement.
'ForStatement': function ForStatement() {
this.type = types.FOR_STATEMENT;
this.init = null; // VariableDeclaration | Expression | null
this.test = null; // Expression | null
this.update = null; // Expression | null
this.body = null; // Statement
this.loc = null; // SourceLocation | null
},
// A for statement.
'ForInStatement': function ForInStatement() {
this.type = types.FORIN_STATEMENT;
this.left = null; // VariableDeclaration | Expression
this.right = null; // Expression
this.body = null; // Statement
this.loc = null; // SourceLocation | null
},
// A for/in statement, or, if each is true, a for each/in statement.
'ForOfStatement': function ForOfStatement() {
this.type = types.FOROF_STATEMENT;
this.left = null; // VariableDeclaration | Expression
this.right = null; // Expression
this.body = null; // Statement
this.loc = null; // SourceLocation | null
},
// A for/of statement.
'DebuggerStatement': function DebuggerStatement() {
this.type = types.DEBUGGER_STATEMENT;
this.loc = null; // SourceLocation | null
},
// A debugger statement.
// ### Declarations
'FunctionDeclaration': function FunctionDeclaration() {
this.type = types.FUNCTION_DECLARATION;
this.id = null; // Identifier
this.params = []; // [ Pattern ]
this.defaults = []; // [ Expression ]
this.rest = null; // Identifier | null
this.body = null; // BlockStatement | Expression
this.expression = false; // boolean
this.loc = null; // SourceLocation | null
},
// A function declaration.
// Note: The id field cannot be null.
'VariableDeclaration': function VariableDeclaration(state) {
this.type = types.VARIABLE_DECLARATION;
this.declarations = []; // [ VariableDeclarator ]
this.kind = state.tokType; // "var" | "let" | "const"
this.loc = null; // SourceLocation | null
},
// A variable declaration, via one of var, let, or const.
'VariableDeclarator': function VariableDeclarator() {
this.type = types.VARIABLE_DECLARATOR;
this.id = null; // Pattern
this.init = null; // Expression | null
this.loc = null; // SourceLocation | null
},
// A variable declarator.
// Note: The id field cannot be null.
// ### Expressions
'ThisExpression': function ThisExpression() {
this.type = types.THIS_EXPRESSION;
this.loc = null; // SourceLocation | null
},
// A this expression.
'ArrayExpression': function ArrayExpression() {
this.type = types.ARRAY_EXPRESSION;
this.elements = []; // [ Expression | null ]
this.loc = null; // SourceLocation | null
},
// An array expression.
'ObjectExpression': function ObjectExpression() {
this.type = types.OBJECT_EXPRESSION;
this.properties = []; // [ ObjectExpressionProp ]
this.loc = null; // SourceLocation | null
},
// An object expression. A literal property in an object expression can have either a string or number as its value. Ordinary property initializers have a kind value "init"; getters and setters have the kind values "get" and "set", respectively.
'FunctionExpression': function FunctionExpression() {
this.type = types.FUNCTION_EXPRESSION;
this.id = null; // Identifier | null
this.params = []; // [ Pattern ]
this.defaults = []; // [ Expression ]
this.rest = null; // Identifier | null
this.body = null; // BlockStatement | Expression
this.expression = false; // boolean
this.loc = null; // SourceLocation | null
},
// A function expression.
'SequenceExpression': function SequenceExpression() {
this.type = types.SEQUENCE_EXPRESSION;
this.expressions = []; // [ Expression ]
this.loc = null; // SourceLocation | null
},
// A sequence expression, i.e., a comma-separated sequence of expressions.
'UnaryExpression': function UnaryExpression() {
this.type = types.UNARY_EXPRESSION;
this.operator = null; // state.UnaryOperator
this.argument = null; // Expression
this.prefix = true; // boolean
this.loc = null; // SourceLocation | null
},
// A unary operator expression.
'BinaryExpression': function BinaryExpression() {
this.type = types.BINARY_EXPRESSION;
this.operator = null; // state.BinaryOperator
this.left = null; // Expression
this.right = null; // Expression
this.loc = null; // SourceLocation | null
},
// A binary operator expression.
'AssignmentExpression': function AssignmentExpression() {
this.type = types.ASSIGNMENT_EXPRESSION;
this.operator = null; // state.AssignmentOperator
this.left = null; // Expression
this.right = null; // Expression
this.loc = null; // SourceLocation | null
},
// An assignment operator expression.
'UpdateExpression': function UpdateExpression() {
this.type = types.UPDATE_EXPRESSION;
this.operator = null; // state.UpdateOperator
this.argument = null; // Expression
this.prefix = true; // boolean
this.loc = null; // SourceLocation | null
},
// An update (increment or decrement) operator expression.
'LogicalExpression': function LogicalExpression() {
this.type = types.LOGICAL_EXPRESSION;
this.operator = null; // state.LogicalOperator
this.left = null; // Expression
this.right = null; // Expression
this.loc = null; // SourceLocation | null
},
// A logical operator expression.
'ConditionalExpression': function ConditionalExpression() {
this.type = types.CONDITIONAL_EXPRESSION;
this.test = null; // Expression
this.consequent = null; // Expression
this.alternate = null; // Expression
this.loc = null; // SourceLocation | null
},
// A conditional expression, i.e., a ternary ?/: expression.
'NewExpression': function NewExpression() {
this.type = types.NEW_EXPRESSION;
this.callee = null; // Expression
this.arguments = []; // [ Expression | null ]
this.loc = null; // SourceLocation | null
},
// A new expression.
'CallExpression': function CallExpression(callee) {
this.type = types.CALL_EXPRESSION;
this.callee = callee; // Expression
this.arguments = []; // [ Expression | null ]
this.loc = null; // SourceLocation | null
},
// A function or method call expression.
'MemberExpression': function MemberExpression() {
this.type = types.MEMBER_EXPRESSION;
this.object = null; // Expression
this.property = null; // Identifier | Expression
this.computed = false; // boolean
this.loc = null; // SourceLocation | null
},
// A member expression. If computed === true, the node corresponds to a computed e1[e2] expression and property is an Expression. If computed === false, the node corresponds to a static e1.x expression and property is an Identifier.
// ### Clauses
'SwitchCase': function SwitchCase() {
this.type = types.SWITCH_CASE;
this.test = null; // Expression | null
this.consequent = []; // [ Statement ]
this.loc = null; // SourceLocation | null
},
// A case (if test is an Expression) or default (if test === null) clause in the body of a switch statement.
'CatchClause': function CatchClause() {
this.type = types.CATCH_CLAUSE;
this.param = null; // Pattern
this.body = null; // BlockStatement
this.loc = null; // SourceLocation | null
},
// A catch clause following a try block. The optional guard property corresponds to the optional expression guard on the bound variable.
// ### Miscellaneous
'Identifier': function Identifier() {
this.type = types.IDENTIFIER;
this.name = ''; // string
this.loc = null; // SourceLocation | null
},
// An identifier. Note that an identifier may be an expression or a destructuring pattern.
'Literal': function Literal() {
this.type = types.LITERAL;
this.value = null; // number
this.loc = null; // SourceLocation | null
},
// A literal token. Note that a literal can be an expression.
};
var operators = {
// Assignment operator tokens.
'AssignmentOperator': {
'eq': new String('='),
'plus': new String('+='),
'minus': new String('-='),
'mult': new String('*='),
'div': new String('/='),
'modulo': new String('%='),
'left_shift': new String('<<='),
'right_shift': new String('>>='),
'zero_fill_right_shift': new String('>>>='),
'OR': new String('|='),
'XOR': new String('^='),
'AND': new String('&=')
},
// Binary operator tokens.
'BinaryOperator': {
'eq_eq': new String('=='),
'ex_eq': new String('!='),
'eq_eq_eq': new String('==='),
'ex_eq_eq': new String('!=='),
'lt': new String('<'),
'lt_eq': new String('<='),
'gt': new String('>'),
'gt_eq': new String('>='),
'left_shift': new String('<<'),
'right_shift': new String('>>'),
'zero_fill_right_shift': new String('>>>'),
'plus': new String('+'),
'minus': new String('-'),
'mult': new String('*'),
'div': new String('/'),
'modulo': new String('%'),
'OR': new String('|'),
'AND': new String('&'),
'XOR': new String('^'),
'in': new String('in'),
'instanceof': new String('instanceof')
},
// Logical operator tokens.
'LogicalOperator': {
'OR': new String('||'),
'AND': new String('&&')
},
// Update operator tokens.
'UpdateOperator': {
'increment': new String('++'),
'decrement': new String('--')
},
// Unary operator tokens.
'UnaryOperator': {
'minus': new String('-'),
'plus': new String('+'),
'ex': new String('!'),
'BITWISE_NOT': new String('~'),
'typeof': new String('typeof'),
'void': new String('void'),
'delete': new String('delete')
}
};
exports.type = types;
exports.node = nodes;
exports.AssignmentOperator = operators.AssignmentOperator;
exports.BinaryOperator = operators.BinaryOperator;
exports.LogicalOperator = operators.LogicalOperator;
exports.UpdateOperator = operators.UpdateOperator;
exports.UnaryOperator = operators.UnaryOperator;
});