aboutsummaryrefslogtreecommitdiff
path: root/lib/ast.js
blob: b8b6b0720b78aad45a396e208df12697b0f782a3 (about) (plain)
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
function DEFNODE(type, props, methods, base) {
    if (arguments.length < 4) base = AST_Node;
    if (!props) props = [];
    else props = props.split(/\s+/);
    if (base && base.PROPS)
        props = props.concat(base.PROPS);
    var code = "return function AST_" + type + "(props){ if (props) { ";
    for (var i = props.length; --i >= 0;) {
        code += "this." + props[i] + " = props." + props[i] + ";";
    }
    if (methods && methods.initialize)
        code += "this.initialize();"
    code += " } }";
    var ctor = new Function(code)();
    if (base) {
        ctor.prototype = new base;
    }
    ctor.prototype.CTOR = ctor;
    ctor.PROPS = props || null;
    if (type) {
        ctor.prototype.TYPE = ctor.TYPE = type;
    }
    if (methods) for (i in methods) if (HOP(methods, i)) {
        ctor.prototype[i] = methods[i];
    }
    return ctor;
};

var AST_Token = DEFNODE("Token", "type value line col pos endpos nlb comments_before", {

}, null);

var AST_Node = DEFNODE("Node", "start end", {
    clone: function() {
        return new this.CTOR(this);
    },
    // XXX: what was this for?
    // renew: function(args) {
    //     var ctor = this.CTOR, props = ctor.props;
    //     for (var i in props) if (!HOP(args, i)) args[i] = this[i];
    //     return new ctor(args);
    // },
    walk: function(w) {
        w._visit(this);
    }
}, null);

var AST_Directive = DEFNODE("Directive", "value", {
    print: function(output) {
        output.string(this.value);
    }
});

var AST_Debugger = DEFNODE("Debugger", null, {
    print: function(output) {
        output.print("debugger");
    }
});

var AST_Parenthesized = DEFNODE("Parenthesized", "expression", {
    $documentation: "Represents an expression which is always parenthesized.  Used for the \
conditions in IF/WHILE/DO and expression in SWITCH/WITH.",
    walk: function(w) {
        w._visit(this, function(){
            this.expression.walk(w);
        });
    }
});

var AST_Bracketed = DEFNODE("Bracketed", "body", {
    $documentation: "Represents a block of statements that are always included in brackets. \
Used for bodies of FUNCTION/TRY/CATCH/THROW/SWITCH.",
    walk: function(w) {
        w._visit(this, function(){
            this.body.forEach(function(stat){
                stat.walk(w);
            });
        });
    }
});

/* -----[ loops ]----- */

var AST_Statement = DEFNODE("Statement", "label body", {
    walk: function(w) {
        w._visit(this, function(){
            if (this.label) this.label.walk(w);
            if (this.body) {
                if (this.body instanceof AST_Node)
                    this.body.walk(w);
                else
                    this.walk_array(w);
            }
        });
    },
    walk_array: AST_Bracketed.prototype.walk
});

var AST_SimpleStatement = DEFNODE("SimpleStatement", null, {

}, AST_Statement);

var AST_BlockStatement = DEFNODE("BlockStatement", null, {

}, AST_Statement);

var AST_EmptyStatement = DEFNODE("EmptyStatement", null, {

}, AST_Statement);

var AST_Do = DEFNODE("Do", "condition", {
    walk: function(w) {
        w._visit(this, function(){
            this.condition.walk(w);
            AST_Statement.prototype.walk.call(this, w);
        });
    }
}, AST_Statement);

var AST_While = DEFNODE("While", "condition", {
    walk: function(w) {
        w._visit(this, function(){
            this.condition.walk(w);
            AST_Statement.prototype.walk.call(this, w);
        });
    }
}, AST_Statement);

var AST_For = DEFNODE("For", "init condition step", {
    walk: function(w) {
        w._visit(this, function(){
            if (this.init) this.init.walk(w);
            if (this.condition) this.condition.walk(w);
            if (this.step) this.step.walk(w);
            AST_Statement.prototype.walk.call(this, w);
        });
    }
}, AST_Statement);

var AST_ForIn = DEFNODE("ForIn", "init name object", {
    walk: function(w) {
        w._visit(this, function(){
            if (this.init) this.init.walk(w);
            this.object.walk(w);
            AST_Statement.prototype.walk.call(this, w);
        });
    }
}, AST_Statement);

var AST_With = DEFNODE("With", "expression", {
    walk: function(w) {
        w._visit(this, function(){
            this.expression.walk(w);
            AST_Statement.prototype.walk.call(this, w);
        });
    }
}, AST_Statement);

/* -----[ functions ]----- */

var AST_Scope = DEFNODE("Scope", "identifiers", {
    walk: function(w) {
        w._visit(this, function(){
            if (this.identifiers) this.identifiers.forEach(function(el){
                el.walk(w);
            });
            AST_Statement.prototype.walk.call(this, w);
        });
    }
}, AST_Statement);

var AST_Toplevel = DEFNODE("Toplevel", null, {

}, AST_Scope);

var AST_Lambda = DEFNODE("Lambda", "name argnames", {
    walk: function(w) {
        w._visit(this, function(){
            if (this.name) this.name.walk(w);
            this.argnames.forEach(function(el){
                el.walk(w);
            });
            AST_Scope.prototype.walk.call(this, w);
        });
    }
}, AST_Scope);

var AST_Function = DEFNODE("Function", null, {

}, AST_Lambda);

var AST_Defun = DEFNODE("Defun", null, {

}, AST_Function);

/* -----[ JUMPS ]----- */

var AST_Jump = DEFNODE("Jump", null, {

});

var AST_Exit = DEFNODE("Exit", "value", {
    walk: function(w) {
        w._visit(this, function(){
            if (this.value) this.value.walk(w);
        });
    }
}, AST_Jump);

var AST_Return = DEFNODE("Return", null, {

}, AST_Exit);

var AST_Throw = DEFNODE("Throw", null, {

}, AST_Exit);

var AST_LoopControl = DEFNODE("LoopControl", "label", {
    walk: function(w) {
        w._visit(this, function(){
            if (this.label) this.label.walk(w);
        });
    }
}, AST_Jump);

var AST_Break = DEFNODE("Break", null, {

}, AST_LoopControl);

var AST_Continue = DEFNODE("Continue", null, {

}, AST_LoopControl);

/* -----[ IF ]----- */

var AST_If = DEFNODE("If", "condition consequent alternative", {
    walk: function(w) {
        w._visit(this, function(){
            this.condition.walk(w);
            this.consequent.walk(w);
            if (this.alternative) this.alternative.walk(w);
        });
    }
});

/* -----[ SWITCH ]----- */

var AST_Switch = DEFNODE("Switch", "expression", {
    walk: function(w) {
        w._visit(this, function(){
            this.expression.walk(w);
            AST_Statement.prototype.walk.call(this, w);
        });
    }
}, AST_Statement);

var AST_SwitchBlock = DEFNODE("SwitchBlock", null, {
    walk       : AST_Statement.prototype.walk,
    walk_array : AST_Bracketed.prototype.walk
}, AST_Bracketed);

var AST_SwitchBranch = DEFNODE("SwitchBranch", "body", {
    walk       : AST_Statement.prototype.walk,
    walk_array : AST_Bracketed.prototype.walk
});

var AST_Default = DEFNODE("Default", null, {

}, AST_SwitchBranch);

var AST_Case = DEFNODE("Case", "expression", {
    walk: function(w) {
        w._visit(this, function(){
            this.expression.walk(w);
            AST_Statement.prototype.walk.call(this, w);
        });
    }
}, AST_SwitchBranch);

/* -----[ EXCEPTIONS ]----- */

var AST_Try = DEFNODE("Try", "btry bcatch bfinally", {
    walk: function(w) {
        w._visit(this, function(){
            this.btry.walk(w);
            if (this.bcatch) this.bcatch.walk(w);
            if (this.bfinally) this.bfinally.walk(w);
        });
    }
});

var AST_Catch = DEFNODE("Catch", "argname body", {
    walk: function(w) {
        w._visit(this, function(){
            this.argname.walk(w);
            this.body.walk(w);
        });
    }
});

var AST_Finally = DEFNODE("Finally", null, {

}, AST_Bracketed);

/* -----[ VAR/CONST ]----- */

var AST_Definitions = DEFNODE("Definitions", "definitions", {
    walk: function(w) {
        w._visit(this, function(){
            this.definitions.forEach(function(el){
                el.walk(w);
            });
        });
    }
});

var AST_Var = DEFNODE("Var", null, {

}, AST_Definitions);

var AST_Const = DEFNODE("Const", null, {

}, AST_Definitions);

var AST_VarDef = DEFNODE("VarDef", "name value", {
    walk: function(w) {
        w._visit(this, function(){
            this.name.walk(w);
            if (this.value) this.value.walk(w);
        });
    }
});

/* -----[ OTHER ]----- */

var AST_Call = DEFNODE("Call", "expression args", {
    walk: function(w) {
        w._visit(this, function(){
            this.expression.walk(w);
            this.args.forEach(function(el){
                el.walk(w);
            });
        });
    }
});

var AST_New = DEFNODE("New", null, {

}, AST_Call);

var AST_Seq = DEFNODE("Seq", "first second", {
    walk: function(w) {
        w._visit(this, function(){
            this.first.walk(w);
            this.second.walk(w);
        });
    }
});

var AST_PropAccess = DEFNODE("PropAccess", "expression property", {

});

var AST_Dot = DEFNODE("Dot", null, {
    walk: function(w) {
        w._visit(this, function(){
            this.expression.walk(w);
        });
    }
}, AST_PropAccess);

var AST_Sub = DEFNODE("Sub", null, {
    walk: function(w) {
        w._visit(this, function(){
            this.expression.walk(w);
            this.property.walk(w);
        });
    }
}, AST_PropAccess);

var AST_Unary = DEFNODE("Unary", "operator expression", {
    walk: function(w) {
        w._visit(this, function(){
            this.expression.walk(w);
        });
    }
});

var AST_UnaryPrefix = DEFNODE("UnaryPrefix", null, {

}, AST_Unary);

var AST_UnaryPostfix = DEFNODE("UnaryPostfix", null, {

}, AST_Unary);

var AST_Binary = DEFNODE("Binary", "left operator right", {
    walk: function(w) {
        w._visit(this, function(){
            this.left.walk(w);
            this.right.walk(w);
        });
    }
});

var AST_Conditional = DEFNODE("Conditional", "condition consequent alternative", {
    walk: function(w) {
        w._visit(this, function(){
            this.condition.walk(w);
            this.consequent.walk(w);
            this.alternative.walk(w);
        });
    }
});

var AST_Assign = DEFNODE("Assign", null, {

}, AST_Binary);

/* -----[ LITERALS ]----- */

var AST_Array = DEFNODE("Array", "elements", {
    walk: function(w) {
        w._visit(this, function(){
            this.elements.forEach(function(el){
                el.walk(w);
            });
        });
    }
});

var AST_Object = DEFNODE("Object", "properties", {
    walk: function(w) {
        w._visit(this, function(){
            this.properties.forEach(function(prop){
                prop.walk(w);
            });
        });
    }
});

var AST_ObjectProperty = DEFNODE("ObjectProperty");

var AST_ObjectKeyVal = DEFNODE("ObjectKeyval", "key value", {
    walk: function(w) {
        w._visit(this, function(){
            this.value.walk(w);
        });
    }
}, AST_ObjectProperty);

var AST_ObjectSetter = DEFNODE("ObjectSetter", "name func", {
    walk: function(w) {
        w._visit(this, function(){
            this.func.walk(w);
        });
    }
}, AST_ObjectProperty);

var AST_ObjectGetter = DEFNODE("ObjectGetter", "name func", {
    walk: function(w) {
        w._visit(this, function(){
            this.func.walk(w);
        });
    }
}, AST_ObjectProperty);

var AST_Symbol = DEFNODE("Symbol", "name", {
});

var AST_This = DEFNODE("This", null, {

}, AST_Symbol);

var AST_SymbolRef = DEFNODE("SymbolRef", "scope symbol", {

}, AST_Symbol);

var AST_Label = DEFNODE("Label", null, {

}, AST_SymbolRef);

var AST_Constant = DEFNODE("Constant", null, {
    getValue: function() {
        return this.value;
    }
});

var AST_String = DEFNODE("String", "value", {

}, AST_Constant);

var AST_Number = DEFNODE("Number", "value", {

}, AST_Constant);

var AST_RegExp = DEFNODE("Regexp", "pattern mods", {
    initialize: function() {
        this.value = new RegExp(this.pattern, this.mods);
    }
}, AST_Constant);

var AST_Atom = DEFNODE("Atom", null, {

}, AST_Constant);

var AST_Null = DEFNODE("Null", null, {
    value: null
}, AST_Atom);

var AST_Undefined = DEFNODE("Undefined", null, {
    value: (function(){}())
}, AST_Atom);

var AST_False = DEFNODE("False", null, {
    value: false
}, AST_Atom);

var AST_True = DEFNODE("True", null, {
    value: true
}, AST_Atom);

/* -----[ Walker ]----- */

function TreeWalker(visitor) {
    this.stack = [];
    if (visitor) this.visit = visitor;
};

TreeWalker.prototype = {
    visit: function(node){},
    parent: function(n) {
        if (n == null) n = 1;
        return this.stack[this.stack.length - n];
    },
    find_parent: function(type) {
        for (var a = this.stack, i = a.length; --i >= 0;)
            if (a[i] instanceof type) return a[i];
        return null;
    },
    _visit: function(node, descend) {
        this.visit(node);
        if (descend) {
            this.stack.push(node);
            descend.call(node);
            this.stack.pop();
        }
    }
};