-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser-6.ts
356 lines (303 loc) · 7.72 KB
/
parser-6.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// A text parser based on results and coroutines.
export type Ok<T> = {
readonly ok: true
readonly data: T
readonly error?: undefined
readonly index: number
readonly source: string
}
export type Error = {
readonly ok: false
readonly data?: undefined
readonly error: string
readonly index: number
readonly source: string
}
export type PreviousState = {
readonly index: number
readonly source: string
readonly ok?: true
}
export type Result<T> = Ok<T> | Error
export type Parser<T> = (state: PreviousState) => Result<T>
export type Infer<T extends Parser<any>> = T extends Parser<infer U> ? U : never
export function ok<T>(source: string, index: number, data: T): Result<T> {
return {
ok: true,
data,
index,
source,
}
}
export function error<T>(
source: string,
index: number,
error: string,
): Result<T> {
return {
ok: false,
error,
index,
source,
}
}
/** Creates an initial state. */
export function initial(source: string): PreviousState {
return { index: 0, source }
}
/** Create a function that actually parses something. */
export function createParseFn<T>(
parser: Parser<T>,
): (source: string) => Result<T> {
return (source) => parser(initial(source))
}
/** Matches a specific chunk of text. */
export function text<T extends string>(text: T): Parser<T> {
const { length } = text
return ({ source, index }) => {
if (source.slice(index, index + length) == text) {
return ok(source, index + length, text)
} else {
return error(
source,
index,
`Expected '${text}'; found '${source.slice(index, index + 20)}${
source.length > index + 20 ? "..." : ""
}'.`,
)
}
}
}
/** Matches a specific chunk of text. */
export function regex(regex: RegExp): Parser<RegExpMatchArray> {
if (regex.global) {
throw new Error(
"The regular expression passed to 'regex' must not be global.",
)
}
if (regex.sticky) {
throw new Error(
"The regular expression passed to 'regex' must not be sticky.",
)
}
if (regex.multiline) {
throw new Error(
"The regular expression passed to 'regex' must not be multiline.",
)
}
if (!regex.source.startsWith("^")) {
throw new Error(
"The regular expression passed to 'regex' must start with a begin assertion (^).",
)
}
return ({ source, index }) => {
const match = source.slice(index).match(regex)
if (!match) {
return error(
source,
index,
`Expected match for ${regex}; found '${source.slice(
index,
index + 20,
)}${source.length > index + 20 ? "..." : ""}'.`,
)
}
return ok(source, index + match[0]!.length, match)
}
}
/** The type of function passed to {@link coroutine} */
export type CoroutineFunction<T> = (
run: <U>(parser: Parser<U>) => U,
resetIndex: () => void,
) => T
/** Runs a function that may include matchers. */
export function coroutine<T>(fn: CoroutineFunction<T>): Parser<T> {
return ({ source, index: startIndex }) => {
let index = startIndex
try {
const data = fn(
(parser) => {
const nextState = parser({ source, index })
if (!nextState.ok) {
throw new Error(nextState.error)
}
index = nextState.index
return nextState.data
},
() => {
index = startIndex
},
)
return ok(source, index, data)
} catch (err) {
return error(
source,
index,
err instanceof Error ? err.message : String(err),
)
}
}
}
/** Matches the first successful parser. */
export function choice<T extends readonly Parser<any>[]>(
...parsers: T
): Parser<Infer<T[number]>> {
return coroutine((run, resetIndex) => {
for (const parser of parsers) {
resetIndex()
try {
return run(parser)
} catch {}
}
throw new Error("Expected to match at least one parser.")
})
}
/** Matches all parsers as a sequence. */
export function seq<T extends readonly Parser<any>[]>(
...parsers: T
): Parser<{ -readonly [K in keyof T]: Infer<T[K]> }> {
return coroutine((run) => {
const output: { -readonly [K in keyof T]: Infer<T[K]> } = [] as any
for (const parser of parsers) {
output.push(run(parser))
}
return output
})
}
/** Matches a parser but returns to the initial index. */
export function lookahead<T>(parser: Parser<T>): Parser<T> {
return coroutine((run, resetIndex) => {
const value: T = run(parser)
resetIndex()
return value
})
}
/** Matches if the passed parser fails, otherwise succeeds. */
export function not<T>(parser: Parser<T>): Parser<void> {
return coroutine((run, resetIndex) => {
try {
run(parser)
} catch {
resetIndex()
return
}
throw new Error("The parser passed to 'not' succeeded.")
})
}
/** Creates a parser with a locally scoped setup. */
export function withSetup<T>(setup: () => CoroutineFunction<T>): Parser<T> {
return coroutine(setup())
}
/**
* Maps the value of a parser using a function. If the mapper throws, the parser
* returns a failed state.
*/
export function map<T, U>(parser: Parser<T>, fn: (value: T) => U): Parser<U> {
return coroutine((run, resetIndex) => {
const value = run(parser)
try {
return fn(value)
} catch (err) {
resetIndex()
throw err
}
})
}
/** Changes the data of a parser to `undefined`. */
export function voidify(parser: Parser<unknown>): Parser<void> {
return coroutine((run) => {
run(parser)
})
}
/**
* Refines a parser by forcing its data to match a condition. If the refiner
* throws, the parser returns a failed state.
*/
export function refine<T>(
parser: Parser<T>,
fn: (value: T) => boolean,
): Parser<T> {
return coroutine((run, resetIndex) => {
const value = run(parser)
try {
if (fn(value)) {
return value
} else {
throw new Error("Refinement failed.")
}
} catch (err) {
resetIndex()
throw err
}
})
}
/** Always succeeds, matching nothing. */
export const succeed = coroutine(() => {})
/** Makes a parser optional. */
export function optional<T>(parser: Parser<T>): Parser<T | undefined> {
return coroutine((run, resetIndex) => {
try {
return run(parser)
} catch {
resetIndex()
}
})
}
/** If the parser succeeds, returns its value. Otherwise, returns the default. */
export function withDefault<T, U>(
parser: Parser<T>,
defaultValue: U,
): Parser<T | U> {
return coroutine((run, resetIndex) => {
try {
return run(parser)
} catch {
resetIndex()
return defaultValue
}
})
}
/**
* Creates a parser that only figures out what it's parsing when needed. If the
* function throws, parsing fails.
*/
export function lazy<T>(fn: () => Parser<T>): Parser<T> {
let cached: Parser<T> | undefined
return coroutine((run) => {
if (!cached) {
cached = fn()
}
return run(cached)
})
}
/** Only succeeds when at the end of a file. */
export const eof: Parser<void> = voidify(regex(/^$/))
/** Matches zero or more occurrences of a parser. */
export function many<T>(parser: Parser<T>): Parser<T[]> {
return coroutine((run) => {
const output: T[] = []
while (true) {
try {
output.push(run(parser))
} catch {
return output
}
}
})
}
/** Matches one or more occurrences of a parser. */
export function many1<T>(parser: Parser<T>): Parser<T[]> {
return coroutine((run) => {
const output: T[] = []
while (true) {
try {
output.push(run(parser))
} catch {
if (output.length == 0) {
throw new Error("Expected at least one match; found zero.")
}
return output
}
}
})
}