Repository navigation
Expand file tree
/
Copy pathast.js
More file actions
484 lines (411 loc) · 10.1 KB
/
Copy pathast.js
File metadata and controls
484 lines (411 loc) · 10.1 KB
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
import {FIELD, autoConvertAdd, isBlank, isPresent, FunctionWrapper, BaseException} from "angular2/src/facade/lang";
import {List, Map, ListWrapper, StringMapWrapper} from "angular2/src/facade/collection";
import {ContextWithVariableBindings} from "./context_with_variable_bindings";
export class AST {
eval(context) {
throw new BaseException("Not supported");
}
get isAssignable() {
return false;
}
assign(context, value) {
throw new BaseException("Not supported");
}
visit(visitor) {
}
toString():string {
return "AST";
}
}
export class EmptyExpr extends AST {
eval(context) {
return null;
}
visit(visitor) {
//do nothing
}
}
export class Structural extends AST {
value:AST;
constructor(value:AST) {
super();
this.value = value;
}
eval(context) {
return value.eval(context);
}
visit(visitor) {
return visitor.visitStructural(this);
}
}
export class ImplicitReceiver extends AST {
eval(context) {
return context;
}
visit(visitor) {
return visitor.visitImplicitReceiver(this);
}
}
/**
* Multiple expressions separated by a semicolon.
*/
export class Chain extends AST {
expressions:List;
constructor(expressions:List) {
super();
this.expressions = expressions;
}
eval(context) {
var result;
for (var i = 0; i < this.expressions.length; i++) {
var last = this.expressions[i].eval(context);
if (isPresent(last)) result = last;
}
return result;
}
visit(visitor) {
return visitor.visitChain(this);
}
}
export class Conditional extends AST {
condition:AST;
trueExp:AST;
falseExp:AST;
constructor(condition:AST, trueExp:AST, falseExp:AST){
super();
this.condition = condition;
this.trueExp = trueExp;
this.falseExp = falseExp;
}
eval(context) {
if(this.condition.eval(context)) {
return this.trueExp.eval(context);
} else {
return this.falseExp.eval(context);
}
}
visit(visitor) {
return visitor.visitConditional(this);
}
}
export class AccessMember extends AST {
receiver:AST;
name:string;
getter:Function;
setter:Function;
constructor(receiver:AST, name:string, getter:Function, setter:Function) {
super();
this.receiver = receiver;
this.name = name;
this.getter = getter;
this.setter = setter;
}
eval(context) {
var evaluatedContext = this.receiver.eval(context);
while (evaluatedContext instanceof ContextWithVariableBindings) {
if (evaluatedContext.hasBinding(this.name)) {
return evaluatedContext.get(this.name);
}
evaluatedContext = evaluatedContext.parent;
}
return this.getter(evaluatedContext);
}
get isAssignable() {
return true;
}
assign(context, value) {
var evaluatedContext = this.receiver.eval(context);
while (evaluatedContext instanceof ContextWithVariableBindings) {
if (evaluatedContext.hasBinding(this.name)) {
throw new BaseException(`Cannot reassign a variable binding ${this.name}`)
}
evaluatedContext = evaluatedContext.parent;
}
return this.setter(evaluatedContext, value);
}
visit(visitor) {
return visitor.visitAccessMember(this);
}
}
export class KeyedAccess extends AST {
obj:AST;
key:AST;
constructor(obj:AST, key:AST) {
super();
this.obj = obj;
this.key = key;
}
eval(context) {
var obj = this.obj.eval(context);
var key = this.key.eval(context);
return obj[key];
}
get isAssignable() {
return true;
}
assign(context, value) {
var obj = this.obj.eval(context);
var key = this.key.eval(context);
obj[key] = value;
return value;
}
visit(visitor) {
return visitor.visitKeyedAccess(this);
}
}
export class Formatter extends AST {
exp:AST;
name:string;
args:List<AST>;
allArgs:List<AST>;
constructor(exp:AST, name:string, args:List) {
super();
this.exp = exp;
this.name = name;
this.args = args;
this.allArgs = ListWrapper.concat([exp], args);
}
visit(visitor) {
return visitor.visitFormatter(this);
}
}
export class LiteralPrimitive extends AST {
value;
constructor(value) {
super();
this.value = value;
}
eval(context) {
return this.value;
}
visit(visitor) {
return visitor.visitLiteralPrimitive(this);
}
}
export class LiteralArray extends AST {
expressions:List;
constructor(expressions:List) {
super();
this.expressions = expressions;
}
eval(context) {
return ListWrapper.map(this.expressions, (e) => e.eval(context));
}
visit(visitor) {
return visitor.visitLiteralArray(this);
}
}
export class LiteralMap extends AST {
keys:List;
values:List;
constructor(keys:List, values:List) {
super();
this.keys = keys;
this.values = values;
}
eval(context) {
var res = StringMapWrapper.create();
for(var i = 0; i < this.keys.length; ++i) {
StringMapWrapper.set(res, this.keys[i], this.values[i].eval(context));
}
return res;
}
visit(visitor) {
return visitor.visitLiteralMap(this);
}
}
export class Interpolation extends AST {
strings:List;
expressions:List;
constructor(strings:List, expressions:List) {
super();
this.strings = strings;
this.expressions = expressions;
}
eval(context) {
throw new BaseException("evaluating an Interpolation is not supported");
}
visit(visitor) {
visitor.visitInterpolation(this);
}
}
export class Binary extends AST {
operation:string;
left:AST;
right:AST;
constructor(operation:string, left:AST, right:AST) {
super();
this.operation = operation;
this.left = left;
this.right = right;
}
eval(context) {
var left = this.left.eval(context);
switch (this.operation) {
case '&&': return left && this.right.eval(context);
case '||': return left || this.right.eval(context);
}
var right = this.right.eval(context);
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;
}
throw 'Internal error [$operation] not handled';
}
visit(visitor) {
return visitor.visitBinary(this);
}
}
export class PrefixNot extends AST {
expression:AST;
constructor(expression:AST) {
super();
this.expression = expression;
}
eval(context) {
return !this.expression.eval(context);
}
visit(visitor) {
return visitor.visitPrefixNot(this);
}
}
export class Assignment extends AST {
target:AST;
value:AST;
constructor(target:AST, value:AST) {
super();
this.target = target;
this.value = value;
}
eval(context) {
return this.target.assign(context, this.value.eval(context));
}
visit(visitor) {
return visitor.visitAssignment(this);
}
}
export class MethodCall extends AST {
receiver:AST;
fn:Function;
args:List;
name:string;
constructor(receiver:AST, name:string, fn:Function, args:List) {
super();
this.receiver = receiver;
this.fn = fn;
this.args = args;
this.name = name;
}
eval(context) {
var evaluatedContext = this.receiver.eval(context);
var evaluatedArgs = evalList(context, this.args);
while (evaluatedContext instanceof ContextWithVariableBindings) {
if (evaluatedContext.hasBinding(this.name)) {
var fn = evaluatedContext.get(this.name);
return FunctionWrapper.apply(fn, evaluatedArgs);
}
evaluatedContext = evaluatedContext.parent;
}
return this.fn(evaluatedContext, evaluatedArgs);
}
visit(visitor) {
return visitor.visitMethodCall(this);
}
}
export class FunctionCall extends AST {
target:AST;
args:List;
constructor(target:AST, args:List) {
super();
this.target = target;
this.args = args;
}
eval(context) {
var obj = this.target.eval(context);
if (! (obj instanceof Function)) {
throw new BaseException(`${obj} is not a function`);
}
return FunctionWrapper.apply(obj, evalList(context, this.args));
}
visit(visitor) {
return visitor.visitFunctionCall(this);
}
}
export class ASTWithSource extends AST {
ast:AST;
source:string;
location:string;
constructor(ast:AST, source:string, location:string) {
super();
this.source = source;
this.location = location;
this.ast = ast;
}
eval(context) {
return this.ast.eval(context);
}
get isAssignable() {
return this.ast.isAssignable;
}
assign(context, value) {
return this.ast.assign(context, value);
}
visit(visitor) {
return this.ast.visit(visitor);
}
toString():string {
return `${this.source} in ${this.location}`;
}
}
export class TemplateBinding {
key:string;
keyIsVar:boolean;
name:string;
expression:ASTWithSource;
constructor(key:string, keyIsVar:boolean, name:string, expression:ASTWithSource) {
super();
this.key = key;
this.keyIsVar = keyIsVar;
// only either name or expression will be filled.
this.name = name;
this.expression = expression;
}
}
//INTERFACE
export class AstVisitor {
visitAccessMember(ast:AccessMember) {}
visitAssignment(ast:Assignment) {}
visitBinary(ast:Binary) {}
visitChain(ast:Chain){}
visitStructural(ast:Structural) {}
visitConditional(ast:Conditional) {}
visitFormatter(ast:Formatter) {}
visitFunctionCall(ast:FunctionCall) {}
visitImplicitReceiver(ast:ImplicitReceiver) {}
visitKeyedAccess(ast:KeyedAccess) {}
visitLiteralArray(ast:LiteralArray) {}
visitLiteralMap(ast:LiteralMap) {}
visitLiteralPrimitive(ast:LiteralPrimitive) {}
visitMethodCall(ast:MethodCall) {}
visitPrefixNot(ast:PrefixNot) {}
}
var _evalListCache = [[],[0],[0,0],[0,0,0],[0,0,0,0],[0,0,0,0,0]];
function evalList(context, exps:List){
var length = exps.length;
var result = _evalListCache[length];
for (var i = 0; i < length; i++) {
result[i] = exps[i].eval(context);
}
return result;
}