-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.ts
More file actions
324 lines (283 loc) · 8.58 KB
/
index.ts
File metadata and controls
324 lines (283 loc) · 8.58 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
export type ParsedType<A> = A extends { Parser: () => Generator }
? ParsedTypeForClass<A>
: A extends (...args: unknown[]) => unknown ? ParsedTypeForFunction<A>
: never;
type ParsedTypeForFunction<F extends (...args: unknown[]) => unknown> =
ReturnType<F> extends Generator<unknown, infer Y> ? Y : never;
type ParsedTypeForClass<C extends { Parser: () => Generator }> = ReturnType<
C["Parser"]
> extends Generator<unknown, infer Y> ? Y
: never;
export type ParseItem<Result = unknown> =
| string
| RegExp
| Iterable<ParseItem>
| (() => Generator<ParseItem, Result, unknown>);
export type ParseYieldable<Result = unknown> = ParseItem<Result>;
export interface ParseError {
iterationCount: number;
yielded: ParseItem | Error;
nested?: Array<ParseError>;
}
export type ParseResult<Result> =
| {
success: false;
remaining: string;
failedOn: ParseError;
}
| {
success: true;
remaining: string;
result: Result;
};
export type ParseYieldedValue<Input extends ParseItem> = Input extends RegExp
? RegExpMatchArray
: string;
export type ParseGenerator<Result = unknown> =
| Generator<ParseItem<unknown>, Result, string | RegExpMatchArray>
| Generator<ParseItem<unknown>, Result, unknown>
| Generator<unknown, Result, undefined>
| Iterable<ParseItem>;
export function parse<Result = void>(
input: string,
iterable: ParseGenerator<Result>,
): ParseResult<Result> {
let lastResult: ParseYieldedValue<ParseItem> | undefined;
let iterationCount = -1;
const iterator = iterable[Symbol.iterator]();
main: while (true) {
const nestedErrors: Array<ParseError> = [];
iterationCount += 1;
const next = iterator.next(lastResult as any);
if (next.done) {
if (next.value instanceof Error) {
return {
success: false,
remaining: input,
failedOn: {
iterationCount,
yielded: next.value,
},
};
}
return {
success: true,
remaining: input,
result: next.value,
};
}
const yielded = next.value as ParseItem;
const choices =
typeof yielded !== "string" && (yielded as any)[Symbol.iterator]
? (yielded as Iterable<ParseItem>)
: [yielded];
for (const choice of choices) {
if (typeof choice === "string") {
let found = false;
const newInput = input.replace(choice, (_1, offset: number) => {
found = offset === 0;
return "";
});
if (found) {
input = newInput;
lastResult = choice;
continue main;
}
} else if (choice instanceof RegExp) {
if (["^", "$"].includes(choice.source[0]) === false) {
throw new Error(`Regex must be from start: ${choice}`);
}
const match = input.match(choice);
if (match) {
lastResult = match;
// input = input.replace(item, '');
input = input.slice(match[0].length);
continue main;
}
} else if (choice instanceof Function) {
const choiceResult = parse(input, choice());
if (choiceResult.success) {
lastResult = choiceResult.result as any;
input = choiceResult.remaining;
continue main;
} else if (choiceResult.failedOn) {
nestedErrors.push(choiceResult.failedOn);
// if (choiceResult.failedOn.iterationCount > 0) {
// return {
// success: false,
// remaining: input,
// failedOn: {
// iterationCount,
// yielded: choice,
// nested: nestedErrors.length === 0 ? undefined : nestedErrors,
// },
// };
// }
}
}
}
return {
success: false,
remaining: input,
failedOn: {
iterationCount,
yielded,
nested: nestedErrors.length === 0 ? undefined : nestedErrors,
},
};
}
}
export function* mustEnd() {
yield /^$/;
}
export function* isEnd() {
const { index }: { index: number } = yield /$/;
return index === 0;
}
export function* hasMore() {
const { index }: { index: number } = yield /$/;
return index > 0;
// return !(yield isEnd);
}
export function has(prefix: ParseYieldable): () => ParseGenerator<boolean> {
return function* () {
return (yield [prefix, ""]) !== "";
};
}
export function optional(
...potentials: Array<ParseYieldable | any>
): () => ParseGenerator<any> {
return function* () {
const result = yield [...potentials, ""];
return result === "" ? undefined : result;
};
}
export function lookAhead(
regex: RegExp,
): () => Generator<RegExp, RegExpMatchArray, RegExpMatchArray> {
const lookAheadRegex = new RegExp(`^(?=${regex.source})`);
return function* () {
return yield lookAheadRegex;
};
}
////////
export function invert<Result = void>(
needle: {},
iterable: ParseGenerator<Result>,
): string | null {
const result = invertInner(needle, iterable);
if (result !== null && result.type === "done") {
return result.components.join("");
}
return null;
}
function invertInner<Result = void>(
needle: Record<string, string>,
iterable: ParseGenerator<Result>,
): { type: "done" | "prefix"; components: ReadonlyArray<string> } | null {
let reply: unknown | undefined;
const expectedKeys = Object.keys(needle);
if (expectedKeys.length === 0) {
throw new Error("Expected object must have keys.");
}
const iterator = iterable[Symbol.iterator]();
const components: Array<string> = [];
const regexpMap = new Map<Symbol, { regexp: RegExp; index: number }>();
while (true) {
const next = iterator.next(reply as any);
if (next.done) {
if (next.value instanceof Error) {
return null;
}
const result = next.value;
if (result == null) {
return { type: "prefix", components: Object.freeze(components) };
}
const resultKeys = new Set(Object.keys(result));
if (
expectedKeys.length === resultKeys.size &&
expectedKeys.every((key) => {
if (!resultKeys.has(key)) {
return false;
}
if (typeof result[key] === "symbol") {
const entry = regexpMap.get(result[key]);
if (entry !== undefined) {
if (
entry.regexp.test(needle[key])
) {
components[entry.index] = needle[key];
return true;
}
}
}
return result[key] === needle[key];
})
) {
return { type: "done", components: Object.freeze(components) };
} else {
return null;
}
}
const yielded = next.value;
const choices =
typeof yielded !== "string" && (yielded as any)[Symbol.iterator]
? (yielded as Iterable<unknown>)
: [yielded];
for (const choice of choices) {
reply = undefined;
if (typeof choice === "string") {
components.push(choice);
reply = choice;
break; // Assume first string is the canonical version.
} else if (choice instanceof RegExp) {
const index = components.length;
components.push(""); // This will be replaced later using the index.
// components.push('???'); // This will be replaced later using the index.
const s = Symbol();
regexpMap.set(s, { regexp: choice, index });
reply = [s];
} else if (choice instanceof Function) {
const result = invertInner(needle, choice());
if (result != null) {
if (result.type === "done") {
return {
type: "done",
components: Object.freeze(components.concat(result.components)),
};
} else {
components.push(...result.components);
}
}
}
}
}
}
// type CustomFunc<T> = (p: Parser) => T;
// interface MatcherFunc {
// (s: string): string;
// (r: RegExp): [string];
// <T>(c: CustomFunc<T>): T;
// }
// type Parser = MatcherFunc & {
// peek: MatcherFunc;
// error(description: string): void;
// };
// function Digit(this: Parser): number {
// const [digits] = this(/^\d+$/);
// const value = parseInt(digits, 10);
// if (value < 0 || value > 255) {
// this.error(`value must be between 0 and 255, was ${value}`);
// }
// return value;
// }
// function IPAddress(this: Parser): [number, number, number, number] {
// const first = this(Digit);
// this(".");
// const second = this(Digit);
// this(".");
// const third = this(Digit);
// this(".");
// const fourth = this(Digit);
// return [first, second, third, fourth];
// }