/
index.ts
304 lines (247 loc) · 6.12 KB
/
index.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
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
import type {
Parser,
AnyNode,
PathNode,
ConcatenatedNode,
ValueNode,
QueryNode,
ExpressionNode,
} from '../ast';
import {
toValue,
toPath,
toConcatenatedNode,
toQuery,
toExpression,
} from '../ast';
const SEGMENT_SEPARATOR = '.';
const OPEN_CURL = '{';
const CLOSE_CURL = '}';
const OPEN_BRACKET = '[';
const CLOSE_BRACKET = ']';
const EQUALS = '=';
const SINGLE_QUOTE = "'";
const DOUBLE_QUOTE = '"';
const BACK_TICK = '`';
// const IDENTIFIER_REGEX = /[\w\-@]+/;
/** A _faster_ way to match chars instead of a regex (/[\w\-@]+/) */
const isIdentifierChar = (char?: string): boolean => {
if (!char) {
return false;
}
const charCode = char.charCodeAt(0);
return (
(charCode >= 48 && charCode <= 57) || // 0 - 9
(charCode >= 65 && charCode <= 90) || // A-Z
(charCode >= 97 && charCode <= 122) || // a-z
charCode === 95 || // _
charCode === 45 || // -
charCode === 64 // @
);
};
/** Parse out a binding AST from a path */
export const parse: Parser = (path) => {
let index = 1;
let ch = path.charAt(0);
/** get the next char in the string */
const next = (expected?: string) => {
if (expected && ch !== expected) {
throw new Error(`Expected char: ${expected} but got: ${ch}`);
}
ch = path.charAt(index);
index += 1;
// console.log(`Index: ${index} Char: ${ch}`);
return ch;
};
/** gobble all whitespace */
const whitespace = () => {
/* eslint-disable no-unmodified-loop-condition */
while (ch === ' ') {
next();
}
};
/** get an identifier if you can */
const identifier = (): ValueNode | undefined => {
if (!isIdentifierChar(ch)) {
return;
}
let value = ch;
while (next()) {
if (!isIdentifierChar(ch)) {
break;
}
value += ch;
}
if (value) {
return toValue(value);
}
};
/** get an expression node if you can */
const expression = (): ExpressionNode | undefined => {
if (ch === BACK_TICK) {
next(BACK_TICK);
let exp = ch;
while (next()) {
if (ch === BACK_TICK) {
break;
}
exp += ch;
}
next(BACK_TICK);
if (exp) {
return toExpression(exp);
}
}
};
/** Grab a value using a regex */
const regex = (match: RegExp): ValueNode | undefined => {
if (!ch?.match(match)) {
return;
}
let value = ch;
while (next()) {
if (!ch?.match(match)) {
break;
}
value += ch;
}
if (value) {
return toValue(value);
}
};
/** parse out a nestedPath if you can */
const nestedPath = (): PathNode | undefined => {
if (ch === OPEN_CURL) {
next(OPEN_CURL);
if (ch === OPEN_CURL) {
next(OPEN_CURL);
/* eslint-disable-next-line @typescript-eslint/no-use-before-define */
const modelRef = parsePath();
next(CLOSE_CURL);
next(CLOSE_CURL);
return modelRef;
}
}
};
/** get a simple segment node */
const simpleSegment = () => nestedPath() ?? expression() ?? identifier();
/** Parse a segment */
const segment = ():
| ConcatenatedNode
| PathNode
| ValueNode
| ExpressionNode
| undefined => {
// Either a string, modelRef, or concatenated version (both)
const segments: Array<ValueNode | PathNode | ExpressionNode> = [];
let nextSegment = simpleSegment();
while (nextSegment !== undefined) {
segments.push(nextSegment);
nextSegment = simpleSegment();
}
if (segments.length === 0) {
return undefined;
}
return toConcatenatedNode(segments);
};
/** get an optionally quoted block */
const optionallyQuotedSegment = ():
| ValueNode
| PathNode
| ExpressionNode
| undefined => {
whitespace();
// see if we have a quote
if (ch === SINGLE_QUOTE || ch === DOUBLE_QUOTE) {
const singleQuote = ch === SINGLE_QUOTE;
next(singleQuote ? SINGLE_QUOTE : DOUBLE_QUOTE);
const id = regex(/[^'"]+/);
next(singleQuote ? SINGLE_QUOTE : DOUBLE_QUOTE);
return id;
}
return simpleSegment();
};
/** eat equals signs */
const equals = (): boolean => {
if (ch !== EQUALS) {
return false;
}
while (ch === EQUALS) {
next();
}
return true;
};
/** Parse out a bracket */
const parseBracket = ():
| ValueNode
| QueryNode
| PathNode
| ExpressionNode
| undefined => {
if (ch === OPEN_BRACKET) {
next(OPEN_BRACKET);
whitespace();
let value: ValueNode | QueryNode | PathNode | ExpressionNode | undefined =
optionallyQuotedSegment();
if (value) {
whitespace();
if (equals()) {
whitespace();
const second = optionallyQuotedSegment();
value = toQuery(value, second);
whitespace();
}
} else {
throw new Error(`Expected identifier`);
}
if (value) {
next(CLOSE_BRACKET);
}
return value;
}
};
/** Parse a segment and any number of brackets following it */
const parseSegmentAndBrackets = (): Array<AnyNode> => {
// try to parse a segment first
const parsed = [];
const firstSegment = segment();
if (firstSegment) {
parsed.push(firstSegment);
let bracketSegment = parseBracket();
while (bracketSegment !== undefined) {
parsed.push(bracketSegment);
bracketSegment = parseBracket();
}
}
return parsed;
};
/** Parse out a path segment */
const parsePath = (): PathNode => {
const parts: AnyNode[] = [];
let nextSegment = parseSegmentAndBrackets();
while (nextSegment !== undefined) {
parts.push(...nextSegment);
if (!ch || ch === CLOSE_CURL) {
break;
}
if (nextSegment.length === 0 && ch) {
throw new Error(`Unexpected character: ${ch}`);
}
next(SEGMENT_SEPARATOR);
nextSegment = parseSegmentAndBrackets();
}
return toPath(parts);
};
try {
const result = parsePath();
return {
status: true,
path: result,
};
} catch (e: any) {
return {
status: false,
error: e.message,
};
}
};