/
ExpressionParser.ts
181 lines (137 loc) · 4.76 KB
/
ExpressionParser.ts
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
import { PathList } from "./types";
const viewModelParseWatchCache: {[key: string]: PathList[] } = {};
export function parsePath(f: any, parseThis?: boolean): PathList[] {
let str: string = f.toString().trim();
str = str.split("\n").filter((s) => !/^\/\//.test(s.trim())).join("\n");
const key: string = (parseThis === undefined ? "un:" : (parseThis ? "_this:" : "_noThis:") ) + str;
const px1: PathList[] = viewModelParseWatchCache[key];
if (px1) {
return px1;
}
if (str.endsWith("}")) {
str = str.substr(0, str.length - 1);
}
if (str.startsWith("function (")) {
str = str.substr("function (".length);
}
if (str.startsWith("function(")) {
str = str.substr("function(".length);
}
str = str.trim();
let index: number = str.indexOf(")");
const commaIndex = str.indexOf(",");
if (commaIndex !== -1 && commaIndex < index) {
index = commaIndex;
}
const isThis: boolean = parseThis === undefined ? (index === 0 || parseThis) : parseThis;
const p: string = (isThis ? "\\_this|this" : (str.substr(0, index) || "")).trim();
/**
* This is the case when there is no parameter to check and there `parseThis` is false
*/
if (p.length === 0) {
const empty = [];
viewModelParseWatchCache[key] = empty;
return empty;
}
str = str.substr(index + 1);
const regExp: string = `(?:(${p})(?:(\\.[a-zA-Z_][a-zA-Z_0-9]*)+)\\s?(?:(\\(|\\=\\=\\=|\\=\\=|\\=)?))`;
const re: RegExp = new RegExp(regExp, "gi");
let path: string[] = [];
const ms: any = str.replace(re, (m) => {
// console.log(`m: ${m}`);
let px: string = m;
if (px.startsWith("this.")) {
if (parseThis !== true) {
px = px.substr(5);
}
} else if (px.startsWith("_this.")) {
if (parseThis !== true) {
px = px.substr(6);
} else {
// need to convert _this to this
px = px.substr(1);
}
} else {
px = px.substr(p.length + 1);
}
// console.log(px);
if (!path.find((y) => y === px)) {
path.push(px);
}
path = path.filter( (f1) => f1.endsWith("==") || !(f1.endsWith("(") || f1.endsWith("=") ));
path = path.map(
(px2) => px2.endsWith("===") ? px2.substr(0, px2.length - 3) :
( px2.endsWith("==") ? px2.substr(0, px2.length - 2) : px2 ) )
.map((px2) => px2.trim());
return m;
});
path = path.sort( (a, b) => b.localeCompare(a) );
const dups = path;
path = [];
for (const iterator of dups) {
if (path.find( (px2) => px2 === iterator )) {
continue;
}
path.push(iterator);
}
const rp: string[] = [];
for (const rpitem of path) {
if (rp.find( (x) => x.startsWith(rpitem) )) {
continue;
}
rp.push(rpitem);
}
// tslint:disable-next-line: no-console
// console.log(`Watching: ${path.join(", ")}`);
const pl = path.map( (p1) => p1.split("."));
viewModelParseWatchCache[key] = pl;
return pl;
}
interface IPathLists {
thisPath?: PathList[];
pathList?: PathList[];
combined?: PathList[];
}
const viewModelParseWatchCache2: {[key: string]: IPathLists } = {};
export function parsePathLists(f: any): IPathLists {
const str = f.toString().trim();
const key: string = str;
const px1 = viewModelParseWatchCache2[key];
if (px1) {
return px1;
}
// str = str.split("\n").filter((s) => !/^\/\//.test(s.trim())).join("\n");
// if (str.endsWith("}")) {
// str = str.substr(0, str.length - 1);
// }
// if (str.startsWith("function (")) {
// str = str.substr("function (".length);
// }
// if (str.startsWith("function(")) {
// str = str.substr("function(".length);
// }
// str = str.trim();
const pl = {
pathList: parsePath(str, false),
thisPath: parsePath(str, true),
combined: []
};
if (pl.thisPath.length && pl.pathList.length) {
// we need to combine this
// pl.combinedPathList =
pl.combined = pl.thisPath
.map((x) => {
x[0] = "t";
x.splice(0, 0, "this");
return x;
})
.concat(pl.pathList.map((x) => {
x.splice(0, 0, "this", "x");
return x;
}));
pl.thisPath = [];
pl.pathList = [];
}
viewModelParseWatchCache2[key] = pl;
return pl;
}