-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
343 lines (290 loc) · 10.2 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
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
/**
* The input to a given parse function
*/
export type ParseInput = {
/**
* Entire original code string
*/
code: string,
/**
* Current index within the code string
*/
index: number
}
/**
* Source location in the original code string; useful for error output
*/
export type ParseSource = {
/**
* Entire original code string
*/
code: string,
/**
* Index in the code string where this entity starts
*/
start: number,
/**
* Index in the code string where this entity ends
*/
end: number
}
/**
* The outcome of a parse attempt. `undefined` means the expected thing wasn't
* found, but nothing was necessarily malformed
*/
export type ParseResult<TParsed, TError = never> =
| {
kind: 'success',
/**
* The remaining input after having parsed this
*/
input: ParseInput,
/**
* The span of the source string occupied by this parsed value; useful when outputting AST nodes
*/
src: ParseSource,
parsed: TParsed,
}
| {
kind: 'error',
/**
* The remaining input after having parsed this (for errors, should be the same as original input)
*/
input: ParseInput,
error: TError
}
| undefined
/**
* A function that takes an input and attempts to parse from the front of it
*/
export type Parser<TParsed, TError = never> = (input: ParseInput) => ParseResult<TParsed, TError>
/**
* Given an array of Parsers, get the array of their TParsed types
*/
export type ParsedOf<TParsers extends Parser<unknown, unknown>[]> = {
[Index in keyof TParsers]: TParsers[Index] extends Parser<infer TParsed, unknown> ? TParsed : never;
}
/**
* Given an array of Parsers, get the array of their TError types
*/
export type ErrorsOf<TParsers extends Parser<unknown, unknown>[]> = {
[Index in keyof TParsers]: TParsers[Index] extends Parser<unknown, infer TError> ? TError : never;
}
/**
* Create an initial `ParseInput` from just a code string
*/
export const input = (code: string): ParseInput => ({ code, index: 0 })
/**
* Don't progress input, return a successful parse of `undefined`
*/
export const nothing: Parser<undefined, never> = input => ({ kind: 'success', parsed: undefined, input, src: { code: input.code, start: input.index, end: input.index } })
/**
* Parse an exact string
*/
export const exact = <T extends string>(str: T): Parser<T, never> => input => {
if (input.code.substring(input.index).startsWith(str)) {
const end = input.index + str.length
return { kind: 'success', parsed: str, input: { ...input, index: end }, src: { code: input.code, start: input.index, end } }
} else {
return undefined
}
}
/**
* If parsed value doesn't match `pred`, convert it to a none-result
*/
export const filter = <TParsed, TError>(
parser: Parser<TParsed, TError>,
pred: (res: TParsed) => boolean
): Parser<TParsed, TError> => input => {
const res = parser(input)
if (res?.kind === 'success' && !pred(res.parsed)) {
return undefined
} else {
return res
}
}
/**
* If parsed value, transform it using `fn`
*/
export const map = <TParsed, TError, TMapped>(
parser: Parser<TParsed, TError>,
fn: (res: TParsed, src: ParseSource) => TMapped
): Parser<TMapped, TError> => input => {
const res = parser(input)
if (res?.kind === 'success') {
return { ...res, parsed: fn(res.parsed, res.src) }
} else {
return res
}
}
/**
* Require that the parser finds something and not nothing, erroring if nothing
* is parsed. `error` callback is passed to generate error messages.
*/
export const required = <TParsed, TError1, TError2>(
parser: Parser<TParsed, TError1>,
error: (input: ParseInput) => TError2
): Parser<TParsed, TError1 | TError2> => input => {
const res = parser(input)
if (res == null) {
return { kind: 'error', input, error: error(input) }
} else {
return res
}
}
// --- Characters ---
/**
* Parse a single (any) character, if the input is non-empty
*/
export const char: Parser<string, never> = input => (
input.index >= 0 && input.index < input.code.length
? {
kind: 'success',
parsed: input.code[input.index],
input: { ...input, index: input.index + 1 },
src: {
code: input.code,
start: input.index,
end: input.index + 1
}
}
: undefined
)
/**
* Parse a single whitespace character
*/
export const whitespaceChar: Parser<string, never> = filter(char, ch => ch.match(whitespaceRegex) != null)
export const whitespaceRegex = /[\s]/
/**
* Parse a single numeric character
*/
export const numericChar: Parser<string, never> = filter(char, ch => ch.match(numericRegex) != null)
export const numericRegex = /[0-9]/
/**
* Parse a single alphabetic character
*/
export const alphaChar: Parser<string, never> = filter(char, ch => ch.match(alphaRegex) != null)
export const alphaRegex = /[A-Za-z]/
// --- Combinators ---
/**
* If `parser` doesn't find anything, succeed anyway with an `undefined` value
*/
export const optional = <TParsed, TError>(parser: Parser<TParsed, TError>): Parser<TParsed | undefined, TError> => input =>
parser(input) ?? nothing(input)
/**
* Try each parser in sequence and return the first successful or erroneous result
*/
export const oneOf = <TParsers extends Parser<unknown, unknown>[]>(...possibilities: TParsers): TParsers[number] => input => {
for (const possibility of possibilities) {
const res = possibility(input)
if (res != null) {
return res
}
}
}
/**
* Parse a sequence of things, one after the other
*/
export const tuple = <
TParsers extends Parser<unknown, unknown>[]
>(
...pieces: TParsers
): Parser<ParsedOf<TParsers>, ErrorsOf<TParsers>[number]> => input => {
let nextInput = input
const items: unknown[] = []
for (const piece of pieces) {
const pieceResult = piece(nextInput)
if (pieceResult?.kind === 'success') {
nextInput = pieceResult.input
items.push(pieceResult.parsed)
} else {
return pieceResult as ParseResult<ParsedOf<TParsers>, ErrorsOf<TParsers>[number]>
}
}
return { kind: 'success', parsed: items as ParsedOf<TParsers>, input: nextInput, src: { code: input.code, start: input.index, end: nextInput.index } }
}
/**
* Parse 0 or more instances of `item`, separated by `sep` (if provided)
* - Only `item`s are actually returned, not `sep`s
* - Succeeds even if nothing is found
*/
export const manySep0 = <TParsed, TError1, TError2>(item: Parser<TParsed, TError1>, sep: Parser<unknown, TError2> | undefined): Parser<TParsed[], TError1 | TError2> => input => {
let nextInput = input
const items: TParsed[] = []
while (true) {
if (items.length > 0 && sep != null) {
const sepResult = sep(nextInput)
if (sepResult == null) {
return { kind: 'success', parsed: items, input: nextInput, src: { code: input.code, start: input.index, end: nextInput.index } }
} else if (sepResult.kind === 'error') {
return sepResult
} else {
nextInput = sepResult.input
// do nothing, move on to item
}
}
const itemResult = item(nextInput)
if (itemResult == null) {
return { kind: 'success', parsed: items, input: nextInput, src: { code: input.code, start: input.index, end: nextInput.index } }
} else if (itemResult.kind === 'error') {
return itemResult
} else {
nextInput = itemResult.input
items.push(itemResult.parsed)
}
}
}
/**
* Parse 1 or more instances of `item`, separated by `sep` (if provided)
* - Only `item`s are actually returned, not `sep`s
* - Returns `undefined` if no `item`s are found
*/
export const manySep1 = <TParsed, TError>(item: Parser<TParsed, TError>, sep: Parser<unknown, TError> | undefined): Parser<TParsed[], TError> => filter(manySep0(item, sep), parsed => parsed.length > 0)
/**
* Parse 0 or more instances of `item`. Succeeds even if nothing is found.
*/
export const many0 = <TParsed, TError>(item: Parser<TParsed, TError>): Parser<TParsed[], TError> => manySep0(item, undefined)
/**
* Parse 1 or more instances of `item`. Returns `undefined` if no `item`s are found.
*/
export const many1 = <TParsed, TError>(item: Parser<TParsed, TError>): Parser<TParsed[], TError> => manySep1(item, undefined)
/**
* Parse 0 or more characters, where `chParser` is a 1-character parser, returned as a single string
*/
export const take0 = <TError>(charParser: Parser<string, TError>): Parser<string, TError> => input => {
let nextInput = input
let res = ''
while (true) {
const charResult = charParser(nextInput)
if (charResult == null) {
return { kind: 'success', parsed: res, input: nextInput, src: { code: input.code, start: input.index, end: nextInput.index } }
} else if (charResult.kind === 'error') {
return charResult
} else {
nextInput = charResult.input
res += charResult.parsed
}
}
}
/**
* Parse 1 or more characters, where `chParser` is a 1-character parser, returned as a single string
*/
export const take1 = <TError>(chParser: Parser<string, TError>): Parser<string, TError> => filter(take0(chParser), s => s.length > 0)
/**
* Given a series of precedence levels (parsers), parse them in order starting
* at whichever one is passed back in as `startingFrom`. Useful for parsing
* expressions with multiple precedence levels, where each level wants to parse
* nested expressions only from levels beneath itself.
*/
export const precedence = <TParsers extends Parser<unknown, unknown>[]>(
...levels: TParsers
) => (
startingFrom?: TParsers[number]
): TParsers[number] =>
startingFrom == null
? oneOf(...levels)
: oneOf(...levels.slice(levels.indexOf(startingFrom) + 1))
/**
* Any amount of whitespace (or none)
*/
export const whitespace: Parser<undefined> = map(take0(whitespaceChar), () => undefined)