This repository was archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathwatch_parser.js
288 lines (229 loc) · 8.12 KB
/
watch_parser.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
import {AST, ContextReferenceAST, CollectionAST, MethodAST,
FieldReadAST, PureFunctionAST, ConstantAST} from 'watchtower';
import {CollectionChangeRecord} from 'watchtower';
import {Parser} from './parser';
import {Expression,ArrayOfExpression,Chain,Filter,Assign,
Conditional, AccessScope, AccessMember, AccessKeyed,
CallScope, CallFunction, CallMember, PrefixNot,
Binary, LiteralPrimitive, LiteralArray, LiteralObject, LiteralString, Literal} from './ast';
var scopeContextRef = new ContextReferenceAST();
export class WatchParser {
constructor(parser:Parser){
this._parser = parser;
this._id = 0;
this._visitor = new WatchVisitor();
}
parse(exp:string, filters, collection=false, context=null){
var contextRef = this._visitor.contextRef,
ast;
try {
this._visitor.filters = filters;
if (context != null) {
this._visitor.contextRef = new ConstantAST(context, `#${this._id++}`);
}
ast = this._parser.parse(exp);
return collection ? this._visitor.visitCollection(ast) : this._visitor.visit(ast);
} finally {
this._visitor.contextRef = contextRef;
this._visitor.filters = null;
}
}
}
class WatchVisitor {
constructor(){
this.contextRef = scopeContextRef;
}
visit(exp:Expression):AST{
exp.accept(this);
assert(this.ast != null);
try {
return this.ast;
} finally {
this.ast = null;
}
}
visitCollection(exp:Expression):AST{
return new CollectionAST(this.visit(exp));
}
visitCallScope(exp:CallScope) {
this.ast = new MethodAST(this.contextRef, exp.name, this._toAst(exp.args));
}
visitCallMember(exp:CallMember) {
this.ast = new MethodAST(this.visit(exp.object), exp.name, this._toAst(exp.args));
}
visitAccessScope(exp:AccessScope) {
this.ast = new FieldReadAST(this.contextRef, exp.name);
}
visitAccessMember(exp:AccessMember) {
this.ast = new FieldReadAST(this.visit(exp.object), exp.name);
}
visitBinary(exp:Binary) {
this.ast = new PureFunctionAST(
exp.operation,
operationToFunction(exp.operation),
[this.visit(exp.left), this.visit(exp.right)]
);
}
visitPrefix(exp:PrefixNot) {
this.ast = new PureFunctionAST(
exp.operation,
operationToFunction(exp.operation),
[this.visit(exp.expression)]
);
}
visitConditional(exp:Conditional) {
this.ast = new PureFunctionAST(
'?:',
operation_ternary,
[this.visit(exp.condition), this.visit(exp.yes), this.visit(exp.no)]
);
}
visitAccessKeyed(exp:AccessKeyed) {
this.ast = new PureFunctionAST(
'[]',
operation_bracket,
[this.visit(exp.object), this.visit(exp.key)]
);
}
visitLiteralPrimitive(exp:LiteralPrimitive) {
this.ast = new ConstantAST(exp.value);
}
visitLiteralString(exp:LiteralString) {
this.ast = new ConstantAST(exp.value);
}
visitLiteralArray(exp:LiteralArray) {
var items = this._toAst(exp.elements);
this.ast = new PureFunctionAST(`[${items.join(', ')}]`, arrayFn, items);
}
visitLiteralObject(exp:LiteralObject) {
var keys = exp.keys;
var values = this._toAst(exp.values),
kv = [],
i, length;
assert(keys.length == values.length);
for (i = 0, length = keys.length; i < length; i++) {
kv.push(`${keys[i]}: ${values[i]}`);
}
this.ast = new PureFunctionAST(`{${kv.join(', ')}}`, mapFn(keys), values);
}
visitFilter(exp:Filter) {
var filterFunction = this.filters(exp.name);
var args = [this.visitCollection(exp.expression)];
args.push(...this._toAst(exp.args).map((ast) => new CollectionAST(ast)));
this.ast = new PureFunctionAST(
`|${exp.name}`,
filterWrapper(filterFunction, args.length),
args
);
}
// TODO(misko): this is a corner case. Choosing not to implement for now.
visitCallFunction(exp:CallFunction) {
this._notSupported("function's returning functions");
}
visitAssign(exp:Assign) {
this._notSupported('assignement');
}
visitLiteral(exp:Literal) {
this._notSupported('literal');
}
visitExpression(exp:Expression) {
this._notSupported('?');
}
visitChain(exp:Chain) {
this._notSupported(';');
}
_notSupported(name:string) {
throw new Error(`Can not watch expression containing '${name}'.`);
}
_toAst(expressions:ArrayOfExpression){
return expressions.map((exp) => this.visit(exp));
}
}
//ALL CODE BELOW HAS OVERLAP WITH EXPRESSIONS
function operationToFunction(operation:string) {
switch(operation) {
case '!' : return function(value) { return !value; };
case '+' : return function(left, right) { return autoConvertAdd(left, right); };
case '-' : return function(left, right) { return (left != null && right != null) ? left - right : (left != null ? left : (right != null ? 0 - right : 0)); };
case '*' : return function(left, right) { return (left == null || right == null) ? null : left * right; };
case '/' : return function(left, right) { return (left == null || right == null) ? null : left / right; };
case '~/' : return function(left, right) { return (left == null || right == null) ? null : Math.floor(left / right); };
case '%' : return function(left, right) { return (left == null || right == null) ? null : left % right; };
case '==' : return function(left, right) { return left == right; };
case '!=' : return function(left, right) { return left != right; };
case '<' : return function(left, right) { return (left == null || right == null) ? null : left < right; };
case '>' : return function(left, right) { return (left == null || right == null) ? null : left > right; };
case '<=' : return function(left, right) { return (left == null || right == null) ? null : left <= right; };
case '>=' : return function(left, right) { return (left == null || right == null) ? null : left >= right; };
case '^' : return function(left, right) { return (left == null || right == null) ? null : left ^ right; };
case '&' : return function(left, right) { return (left == null || right == null) ? null : left & right; };
case '&&' : return function(left, right) { return !!left && !!right; };
case '||' : return function(left, right) { return !!left || !!right; };
default: throw new Error(operation);
}
}
function operation_ternary(condition, yes, no) { return !!condition ? yes : no; }
function operation_bracket(obj, key) { return obj == null ? null : obj[key]; }
/// Add the two arguments with automatic type conversion.
function autoConvertAdd(a, b) {
if (a != null && b != null) {
// TODO(deboer): Support others.
if (typeof a == 'string' && typeof b != 'string') {
return a + b.toString();
}
if (typeof a != 'string' && typeof b == 'string') {
return a.toString() + b;
}
return a + b;
}
if (a != null) {
return a;
}
if (b != null) {
return b;
}
return 0;
}
// TODO(misko): figure out why do we need to make a copy?
function arrayFn(...existing){
return existing;
}
// TODO(misko): figure out why do we need to make a copy instead of reusing instance?
function mapFn(keys){
return function(...values){
assert(values.length == keys.length);
var instance = {},
length = keys.length,
i;
for(i = 0; i < length; i++){
instance[keys[i]] = values[i];
}
return instance;
}
}
function filterWrapper(filterFn, length) {
var args = [],
argsWatches = [];
return function(...values){
for (var i = 0, length = values.length; i < length; i++) {
var value = values[i];
var lastValue = args[i];
if (value !== lastValue) {
if (value instanceof CollectionChangeRecord) {
args[i] = value.iterable;
} else {
args[i] = value;
}
}
}
var value = filterFn(...args);
//TODO: Is this an optimization we can make?
//if (value is Iterable) {
// Since filters are pure we can guarantee that this well never change.
// By wrapping in UnmodifiableListView we can hint to the dirty checker
// and short circuit the iterator.
// value = new UnmodifiableListView(value);
//}
return value;
}
}