/
ast.js
511 lines (511 loc) · 19.8 KB
/
ast.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
511
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};
var lang_1 = require("angular2/src/facade/lang");
var collection_1 = require("angular2/src/facade/collection");
var AST = (function () {
function AST() {
}
AST.prototype.eval = function (context, locals) { throw new lang_1.BaseException("Not supported"); };
Object.defineProperty(AST.prototype, "isAssignable", {
get: function () { return false; },
enumerable: true,
configurable: true
});
AST.prototype.assign = function (context, locals, value) { throw new lang_1.BaseException("Not supported"); };
AST.prototype.visit = function (visitor) { return null; };
AST.prototype.toString = function () { return "AST"; };
return AST;
})();
exports.AST = AST;
var EmptyExpr = (function (_super) {
__extends(EmptyExpr, _super);
function EmptyExpr() {
_super.apply(this, arguments);
}
EmptyExpr.prototype.eval = function (context, locals) { return null; };
EmptyExpr.prototype.visit = function (visitor) {
// do nothing
};
return EmptyExpr;
})(AST);
exports.EmptyExpr = EmptyExpr;
var ImplicitReceiver = (function (_super) {
__extends(ImplicitReceiver, _super);
function ImplicitReceiver() {
_super.apply(this, arguments);
}
ImplicitReceiver.prototype.eval = function (context, locals) { return context; };
ImplicitReceiver.prototype.visit = function (visitor) { return visitor.visitImplicitReceiver(this); };
return ImplicitReceiver;
})(AST);
exports.ImplicitReceiver = ImplicitReceiver;
/**
* Multiple expressions separated by a semicolon.
*/
var Chain = (function (_super) {
__extends(Chain, _super);
function Chain(expressions) {
_super.call(this);
this.expressions = expressions;
}
Chain.prototype.eval = function (context, locals) {
var result;
for (var i = 0; i < this.expressions.length; i++) {
var last = this.expressions[i].eval(context, locals);
if (lang_1.isPresent(last))
result = last;
}
return result;
};
Chain.prototype.visit = function (visitor) { return visitor.visitChain(this); };
return Chain;
})(AST);
exports.Chain = Chain;
var Conditional = (function (_super) {
__extends(Conditional, _super);
function Conditional(condition, trueExp, falseExp) {
_super.call(this);
this.condition = condition;
this.trueExp = trueExp;
this.falseExp = falseExp;
}
Conditional.prototype.eval = function (context, locals) {
if (this.condition.eval(context, locals)) {
return this.trueExp.eval(context, locals);
}
else {
return this.falseExp.eval(context, locals);
}
};
Conditional.prototype.visit = function (visitor) { return visitor.visitConditional(this); };
return Conditional;
})(AST);
exports.Conditional = Conditional;
var If = (function (_super) {
__extends(If, _super);
function If(condition, trueExp, falseExp) {
_super.call(this);
this.condition = condition;
this.trueExp = trueExp;
this.falseExp = falseExp;
}
If.prototype.eval = function (context, locals) {
if (this.condition.eval(context, locals)) {
this.trueExp.eval(context, locals);
}
else if (lang_1.isPresent(this.falseExp)) {
this.falseExp.eval(context, locals);
}
};
If.prototype.visit = function (visitor) { return visitor.visitIf(this); };
return If;
})(AST);
exports.If = If;
var AccessMember = (function (_super) {
__extends(AccessMember, _super);
function AccessMember(receiver, name, getter, setter) {
_super.call(this);
this.receiver = receiver;
this.name = name;
this.getter = getter;
this.setter = setter;
}
AccessMember.prototype.eval = function (context, locals) {
if (this.receiver instanceof ImplicitReceiver && lang_1.isPresent(locals) &&
locals.contains(this.name)) {
return locals.get(this.name);
}
else {
var evaluatedReceiver = this.receiver.eval(context, locals);
return this.getter(evaluatedReceiver);
}
};
Object.defineProperty(AccessMember.prototype, "isAssignable", {
get: function () { return true; },
enumerable: true,
configurable: true
});
AccessMember.prototype.assign = function (context, locals, value) {
var evaluatedContext = this.receiver.eval(context, locals);
if (this.receiver instanceof ImplicitReceiver && lang_1.isPresent(locals) &&
locals.contains(this.name)) {
throw new lang_1.BaseException("Cannot reassign a variable binding " + this.name);
}
else {
return this.setter(evaluatedContext, value);
}
};
AccessMember.prototype.visit = function (visitor) { return visitor.visitAccessMember(this); };
return AccessMember;
})(AST);
exports.AccessMember = AccessMember;
var SafeAccessMember = (function (_super) {
__extends(SafeAccessMember, _super);
function SafeAccessMember(receiver, name, getter, setter) {
_super.call(this);
this.receiver = receiver;
this.name = name;
this.getter = getter;
this.setter = setter;
}
SafeAccessMember.prototype.eval = function (context, locals) {
var evaluatedReceiver = this.receiver.eval(context, locals);
return lang_1.isBlank(evaluatedReceiver) ? null : this.getter(evaluatedReceiver);
};
SafeAccessMember.prototype.visit = function (visitor) { return visitor.visitSafeAccessMember(this); };
return SafeAccessMember;
})(AST);
exports.SafeAccessMember = SafeAccessMember;
var KeyedAccess = (function (_super) {
__extends(KeyedAccess, _super);
function KeyedAccess(obj, key) {
_super.call(this);
this.obj = obj;
this.key = key;
}
KeyedAccess.prototype.eval = function (context, locals) {
var obj = this.obj.eval(context, locals);
var key = this.key.eval(context, locals);
return obj[key];
};
Object.defineProperty(KeyedAccess.prototype, "isAssignable", {
get: function () { return true; },
enumerable: true,
configurable: true
});
KeyedAccess.prototype.assign = function (context, locals, value) {
var obj = this.obj.eval(context, locals);
var key = this.key.eval(context, locals);
obj[key] = value;
return value;
};
KeyedAccess.prototype.visit = function (visitor) { return visitor.visitKeyedAccess(this); };
return KeyedAccess;
})(AST);
exports.KeyedAccess = KeyedAccess;
var Pipe = (function (_super) {
__extends(Pipe, _super);
function Pipe(exp, name, args, inBinding) {
_super.call(this);
this.exp = exp;
this.name = name;
this.args = args;
this.inBinding = inBinding;
}
Pipe.prototype.visit = function (visitor) { return visitor.visitPipe(this); };
return Pipe;
})(AST);
exports.Pipe = Pipe;
var LiteralPrimitive = (function (_super) {
__extends(LiteralPrimitive, _super);
function LiteralPrimitive(value) {
_super.call(this);
this.value = value;
}
LiteralPrimitive.prototype.eval = function (context, locals) { return this.value; };
LiteralPrimitive.prototype.visit = function (visitor) { return visitor.visitLiteralPrimitive(this); };
return LiteralPrimitive;
})(AST);
exports.LiteralPrimitive = LiteralPrimitive;
var LiteralArray = (function (_super) {
__extends(LiteralArray, _super);
function LiteralArray(expressions) {
_super.call(this);
this.expressions = expressions;
}
LiteralArray.prototype.eval = function (context, locals) {
return collection_1.ListWrapper.map(this.expressions, function (e) { return e.eval(context, locals); });
};
LiteralArray.prototype.visit = function (visitor) { return visitor.visitLiteralArray(this); };
return LiteralArray;
})(AST);
exports.LiteralArray = LiteralArray;
var LiteralMap = (function (_super) {
__extends(LiteralMap, _super);
function LiteralMap(keys, values) {
_super.call(this);
this.keys = keys;
this.values = values;
}
LiteralMap.prototype.eval = function (context, locals) {
var res = collection_1.StringMapWrapper.create();
for (var i = 0; i < this.keys.length; ++i) {
collection_1.StringMapWrapper.set(res, this.keys[i], this.values[i].eval(context, locals));
}
return res;
};
LiteralMap.prototype.visit = function (visitor) { return visitor.visitLiteralMap(this); };
return LiteralMap;
})(AST);
exports.LiteralMap = LiteralMap;
var Interpolation = (function (_super) {
__extends(Interpolation, _super);
function Interpolation(strings, expressions) {
_super.call(this);
this.strings = strings;
this.expressions = expressions;
}
Interpolation.prototype.eval = function (context, locals) { throw new lang_1.BaseException("evaluating an Interpolation is not supported"); };
Interpolation.prototype.visit = function (visitor) { visitor.visitInterpolation(this); };
return Interpolation;
})(AST);
exports.Interpolation = Interpolation;
var Binary = (function (_super) {
__extends(Binary, _super);
function Binary(operation, left, right) {
_super.call(this);
this.operation = operation;
this.left = left;
this.right = right;
}
Binary.prototype.eval = function (context, locals) {
var left = this.left.eval(context, locals);
switch (this.operation) {
case '&&':
return left && this.right.eval(context, locals);
case '||':
return left || this.right.eval(context, locals);
}
var right = this.right.eval(context, locals);
switch (this.operation) {
case '+':
return left + right;
case '-':
return left - right;
case '*':
return left * right;
case '/':
return left / right;
case '%':
return left % right;
case '==':
return left == right;
case '!=':
return left != right;
case '===':
return left === right;
case '!==':
return left !== right;
case '<':
return left < right;
case '>':
return left > right;
case '<=':
return left <= right;
case '>=':
return left >= right;
case '^':
return left ^ right;
case '&':
return left & right;
}
throw 'Internal error [$operation] not handled';
};
Binary.prototype.visit = function (visitor) { return visitor.visitBinary(this); };
return Binary;
})(AST);
exports.Binary = Binary;
var PrefixNot = (function (_super) {
__extends(PrefixNot, _super);
function PrefixNot(expression) {
_super.call(this);
this.expression = expression;
}
PrefixNot.prototype.eval = function (context, locals) { return !this.expression.eval(context, locals); };
PrefixNot.prototype.visit = function (visitor) { return visitor.visitPrefixNot(this); };
return PrefixNot;
})(AST);
exports.PrefixNot = PrefixNot;
var Assignment = (function (_super) {
__extends(Assignment, _super);
function Assignment(target, value) {
_super.call(this);
this.target = target;
this.value = value;
}
Assignment.prototype.eval = function (context, locals) {
return this.target.assign(context, locals, this.value.eval(context, locals));
};
Assignment.prototype.visit = function (visitor) { return visitor.visitAssignment(this); };
return Assignment;
})(AST);
exports.Assignment = Assignment;
var MethodCall = (function (_super) {
__extends(MethodCall, _super);
function MethodCall(receiver, name, fn, args) {
_super.call(this);
this.receiver = receiver;
this.name = name;
this.fn = fn;
this.args = args;
}
MethodCall.prototype.eval = function (context, locals) {
var evaluatedArgs = evalList(context, locals, this.args);
if (this.receiver instanceof ImplicitReceiver && lang_1.isPresent(locals) &&
locals.contains(this.name)) {
var fn = locals.get(this.name);
return lang_1.FunctionWrapper.apply(fn, evaluatedArgs);
}
else {
var evaluatedReceiver = this.receiver.eval(context, locals);
return this.fn(evaluatedReceiver, evaluatedArgs);
}
};
MethodCall.prototype.visit = function (visitor) { return visitor.visitMethodCall(this); };
return MethodCall;
})(AST);
exports.MethodCall = MethodCall;
var SafeMethodCall = (function (_super) {
__extends(SafeMethodCall, _super);
function SafeMethodCall(receiver, name, fn, args) {
_super.call(this);
this.receiver = receiver;
this.name = name;
this.fn = fn;
this.args = args;
}
SafeMethodCall.prototype.eval = function (context, locals) {
var evaluatedReceiver = this.receiver.eval(context, locals);
if (lang_1.isBlank(evaluatedReceiver))
return null;
var evaluatedArgs = evalList(context, locals, this.args);
return this.fn(evaluatedReceiver, evaluatedArgs);
};
SafeMethodCall.prototype.visit = function (visitor) { return visitor.visitSafeMethodCall(this); };
return SafeMethodCall;
})(AST);
exports.SafeMethodCall = SafeMethodCall;
var FunctionCall = (function (_super) {
__extends(FunctionCall, _super);
function FunctionCall(target, args) {
_super.call(this);
this.target = target;
this.args = args;
}
FunctionCall.prototype.eval = function (context, locals) {
var obj = this.target.eval(context, locals);
if (!(obj instanceof Function)) {
throw new lang_1.BaseException(obj + " is not a function");
}
return lang_1.FunctionWrapper.apply(obj, evalList(context, locals, this.args));
};
FunctionCall.prototype.visit = function (visitor) { return visitor.visitFunctionCall(this); };
return FunctionCall;
})(AST);
exports.FunctionCall = FunctionCall;
var ASTWithSource = (function (_super) {
__extends(ASTWithSource, _super);
function ASTWithSource(ast, source, location) {
_super.call(this);
this.ast = ast;
this.source = source;
this.location = location;
}
ASTWithSource.prototype.eval = function (context, locals) { return this.ast.eval(context, locals); };
Object.defineProperty(ASTWithSource.prototype, "isAssignable", {
get: function () { return this.ast.isAssignable; },
enumerable: true,
configurable: true
});
ASTWithSource.prototype.assign = function (context, locals, value) { return this.ast.assign(context, locals, value); };
ASTWithSource.prototype.visit = function (visitor) { return this.ast.visit(visitor); };
ASTWithSource.prototype.toString = function () { return this.source + " in " + this.location; };
return ASTWithSource;
})(AST);
exports.ASTWithSource = ASTWithSource;
var TemplateBinding = (function () {
function TemplateBinding(key, keyIsVar, name, expression) {
this.key = key;
this.keyIsVar = keyIsVar;
this.name = name;
this.expression = expression;
}
return TemplateBinding;
})();
exports.TemplateBinding = TemplateBinding;
var AstTransformer = (function () {
function AstTransformer() {
}
AstTransformer.prototype.visitImplicitReceiver = function (ast) { return ast; };
AstTransformer.prototype.visitInterpolation = function (ast) {
return new Interpolation(ast.strings, this.visitAll(ast.expressions));
};
AstTransformer.prototype.visitLiteralPrimitive = function (ast) { return new LiteralPrimitive(ast.value); };
AstTransformer.prototype.visitAccessMember = function (ast) {
return new AccessMember(ast.receiver.visit(this), ast.name, ast.getter, ast.setter);
};
AstTransformer.prototype.visitSafeAccessMember = function (ast) {
return new SafeAccessMember(ast.receiver.visit(this), ast.name, ast.getter, ast.setter);
};
AstTransformer.prototype.visitMethodCall = function (ast) {
return new MethodCall(ast.receiver.visit(this), ast.name, ast.fn, this.visitAll(ast.args));
};
AstTransformer.prototype.visitSafeMethodCall = function (ast) {
return new SafeMethodCall(ast.receiver.visit(this), ast.name, ast.fn, this.visitAll(ast.args));
};
AstTransformer.prototype.visitFunctionCall = function (ast) {
return new FunctionCall(ast.target.visit(this), this.visitAll(ast.args));
};
AstTransformer.prototype.visitLiteralArray = function (ast) { return new LiteralArray(this.visitAll(ast.expressions)); };
AstTransformer.prototype.visitLiteralMap = function (ast) { return new LiteralMap(ast.keys, this.visitAll(ast.values)); };
AstTransformer.prototype.visitBinary = function (ast) {
return new Binary(ast.operation, ast.left.visit(this), ast.right.visit(this));
};
AstTransformer.prototype.visitPrefixNot = function (ast) { return new PrefixNot(ast.expression.visit(this)); };
AstTransformer.prototype.visitConditional = function (ast) {
return new Conditional(ast.condition.visit(this), ast.trueExp.visit(this), ast.falseExp.visit(this));
};
AstTransformer.prototype.visitPipe = function (ast) {
return new Pipe(ast.exp.visit(this), ast.name, this.visitAll(ast.args), ast.inBinding);
};
AstTransformer.prototype.visitKeyedAccess = function (ast) {
return new KeyedAccess(ast.obj.visit(this), ast.key.visit(this));
};
AstTransformer.prototype.visitAll = function (asts) {
var res = collection_1.ListWrapper.createFixedSize(asts.length);
for (var i = 0; i < asts.length; ++i) {
res[i] = asts[i].visit(this);
}
return res;
};
AstTransformer.prototype.visitChain = function (ast) { return new Chain(this.visitAll(ast.expressions)); };
AstTransformer.prototype.visitAssignment = function (ast) {
return new Assignment(ast.target.visit(this), ast.value.visit(this));
};
AstTransformer.prototype.visitIf = function (ast) {
var falseExp = lang_1.isPresent(ast.falseExp) ? ast.falseExp.visit(this) : null;
return new If(ast.condition.visit(this), ast.trueExp.visit(this), falseExp);
};
return AstTransformer;
})();
exports.AstTransformer = AstTransformer;
var _evalListCache = [
[],
[0],
[0, 0],
[0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
];
function evalList(context, locals, exps) {
var length = exps.length;
if (length > 10) {
throw new lang_1.BaseException("Cannot have more than 10 argument");
}
var result = _evalListCache[length];
for (var i = 0; i < length; i++) {
result[i] = exps[i].eval(context, locals);
}
return result;
}
exports.__esModule = true;
//# sourceMappingURL=ast.js.map