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 30
/
Copy pathast.js
132 lines (105 loc) · 2.65 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
import {
_Handler,
_ConstantHandler,
_InvokeHandler,
_FieldHandler,
_EvalWatchRecord
} from './watch_record';
function _argsList(list) {
if (!list) list = [];
if (!Array.isArray(list)) return '';
return list.join(', ');
}
export class AST {
constructor(expression) {
if (typeof expression !== "string") {
throw "expression must be a string";
}
this.expression = expression.indexOf('#.') === 0
? expression.substr(2)
: expression;
}
setupWatch(group) {
throw "setupWatch() not implemented";
}
toString() {
return this.expression;
}
}
AST._CONTEXT = '#';
export class ContextReferenceAST extends AST {
constructor() {
super(AST._CONTEXT);
}
setupWatch(group) {
return new _ConstantWatchRecord(group, this.expression, group.context);
}
}
export class ConstantAST extends AST {
constructor(constant, expression) {
if (arguments.length < 2) {
expression = null;
}
super(expression === null
? (typeof constant === "string" ? `"${constant}"` : `${constant}`)
: expression);
this.constant = constant;
}
setupWatch(group) {
return new _ConstantWatchRecord(group, this.expression, this.constant);
}
}
export class FieldReadAST extends AST {
constructor(lhs, name) {
this.lhs = lhs;
this.name = name;
super(`${lhs}.${name}`);
}
setupWatch(group) {
return group.addFieldWatch(this.lhs, this.name, this.expression);
}
}
export class PureFunctionAST extends AST {
constructor(name, fn, argsAST) {
this.fn = fn;
this.argsAST = argsAST;
this.name = name;
super(`${name}(${_argsList(argsAST)})`);
}
setupWatch(group) {
return group.addFunctionWatch(this.fn, this.argsAST, this.expression);
}
}
export class MethodAST extends AST {
constructor(lhsAST, name, argsAST) {
this.lhsAST = lhsAST;
this.name = name;
this.argsAST = argsAST;
super(`${lhsAST}.${name}(${_argsList(argsAST)})`);
}
setupWatch(group) {
return group.addMethodWatch(this.lhsAST, this.name, this.argsAST, this.expression);
}
}
export class CollectionAST extends AST {
constructor(valueAST) {
this.valueAST = valueAST;
super(`#collection(${valueAST})`);
}
setupWatch(group) {
return group.addCollectionWatch(this.valueAST);
}
}
class _ConstantWatchRecord {
constructor(watchGroup, expression, currentValue) {
this.currentValue = currentValue;
this.handler = new _ConstantHandler(watchGroup, expression, currentValue);
this.field = this.previousValue = this.object = this.nextChange = null;
}
check() {
return false;
}
remove() {
return null;
}
}