-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathparser.ts
349 lines (310 loc) · 9.08 KB
/
parser.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
import * as E from 'fp-ts/Either'
import * as O from 'fp-ts/Option'
import { flow, pipe } from 'fp-ts/function'
// internal state type
type ParserState<A> = {
remaining: string
value: A
}
// our errors can be of two types - string ones to make nice
// default built-in errors, or the user can use their own type
type ParserError<E> =
| { type: 'ParserError'; text: string }
| { type: 'CustomError'; value: E }
export const parserError = (text: string): ParserError<never> => ({
type: 'ParserError',
text,
})
export const customError = <E>(value: E): ParserError<E> => ({
type: 'CustomError',
value,
})
// this is a type for a Parser. The `A` generic shows the type that it will
// return if it succeeds
export type Parser<E, A> = {
type: 'Parser'
parse: (input: string) => E.Either<ParserError<E>, ParserState<A>>
}
// a constructor function for buiiding a Parser from it's function and wrapping
// it in a discriminated union to make TS happy
export const makeParser = <E, A>(
parse: (input: string) => E.Either<ParserError<E>, ParserState<A>>
): Parser<E, A> => ({
type: 'Parser',
parse,
})
// given a Parser and a string, run it, returning a value if it works
export const runParser = <E, A>(
parser: Parser<E, A>,
input: string
): E.Either<ParserError<E>, A> =>
pipe(
parser.parse(input),
E.chain(state =>
state.remaining.length == 0
? E.right(state.value)
: E.left(
parserError(
`Expected all of input to be used up, however "${state.remaining}" has not been consumed`
)
)
)
)
// a Parser that always fails with the given message
export const fail = (text: string): Parser<never, never> =>
makeParser(_ => E.left(parserError(text)))
// a Parser that always fails with the given custom value
export const failCustom = <E>(value: E): Parser<E, never> =>
makeParser(_ => E.left(customError(value)))
// helper function that takes the first X characters of string, returning null
// if X is over string length
const splitString = (
input: string,
length: number
): [string, E.Either<ParserError<never>, string>] => {
const match = input.slice(0, length)
const actualMatch =
match.length >= length
? E.right(match)
: E.left(
parserError(
`Tried to parse the next ${length} chars but only ${input.length} characters available.`
)
)
const rest = input.slice(length)
return [rest, actualMatch]
}
// map the item we've parsed into something else
export const map = <A, B>(f: (a: A) => B) => <E>(
parser: Parser<E, A>
): Parser<E, B> =>
makeParser(
flow(
parser.parse,
E.map(state => ({ ...state, value: f(state.value) }))
)
)
// try parser1 then parser2
export const alt = <E, A>(parser2: Parser<E, A>) => (
parser1: Parser<E, A>
): Parser<E, A> =>
makeParser(input => {
const resultA = parser1.parse(input)
if (E.isRight(resultA)) {
return resultA
}
return parser2.parse(input)
})
// provide a list of alternatives that will be tried in order
export const altMany = <E, A>(
parser1: Parser<E, A>,
...parsers: Parser<E, A>[]
): Parser<E, A> =>
parsers.reduce((total, parser) => alt<E, A>(total)(parser), parser1)
// try parserA, and if succeeds, pass the result to thenParserB
export const andThen = <E, A, B>(
thenParserB: (a: A) => Parser<E, B>
) => (parserA: Parser<E, A>): Parser<E, B> =>
makeParser(
flow(
parserA.parse,
E.chain(state =>
thenParserB(state.value).parse(state.remaining)
)
)
)
// take two parsers, and return a parser that parses two things in sequence
// and puts them in a tuple
export const pair = <E, A, B>(
parserA: Parser<E, A>,
parserB: Parser<E, B>
): Parser<E, [A, B]> =>
makeParser(input => {
const resultA = parserA.parse(input)
if (E.isLeft(resultA)) {
return resultA
}
const resultB = parserB.parse(resultA.right.remaining)
if (E.isLeft(resultB)) {
return resultB
}
return E.right({
...resultB.right,
value: [resultA.right.value, resultB.right.value],
})
})
// parse two things, then discard the second one
export const left = <E, A, B>(
parserA: Parser<E, A>,
parserB: Parser<E, B>
): Parser<E, A> =>
pipe(
pair(parserA, parserB),
map(([a, _]) => a)
)
// parse two things, then discard the first one
export const right = <E, A, B>(
parserA: Parser<E, A>,
parserB: Parser<E, B>
): Parser<E, B> =>
pipe(
pair(parserA, parserB),
map(([_, b]) => b)
)
// given a parser, return an array with one or more matches
export const oneOrMore = <E, A>(
parserA: Parser<E, A>
): Parser<E, A[]> =>
makeParser(input => {
const res = parserA.parse(input)
if (E.isLeft(res)) {
return res
}
const { remaining, value } = res.right
let values = [value]
let next = remaining
while (true) {
const parsed = parserA.parse(next)
if (E.isRight(parsed)) {
next = parsed.right.remaining
values.push(parsed.right.value)
} else {
break
}
}
return E.right({ remaining: next, value: values })
})
// given a parser, return an array with zero or more matches
export const zeroOrMore = <E, A>(
parserA: Parser<E, A>
): Parser<E, A[]> =>
makeParser(input => {
let next = input
let values = []
while (true) {
const parsed = parserA.parse(next)
if (E.isRight(parsed)) {
next = parsed.right.remaining
values.push(parsed.right.value)
} else {
break
}
}
return E.right({ remaining: next, value: values })
})
// a parser that matches any character
export const anyChar = makeParser(input => {
const [rest, optionMatch] = splitString(input, 1)
return pipe(
optionMatch,
E.map(match => ({ remaining: rest, value: match }))
)
})
// given a parser, and a predicate, return a new, fussier parser
export const pred = <E, A>(
parser: Parser<E, A>,
predicate: (a: A) => boolean
): Parser<E, A> =>
makeParser(
flow(
parser.parse,
E.chain(state =>
predicate(state.value)
? E.right(state)
: E.left(
parserError(
`Value ${state.value} did not satisfy predicate`
)
)
)
)
)
// parser that matches 'lit' exactly or fails
export const matchLiteral = <Lit extends string>(
lit: Lit
): Parser<never, Lit> =>
makeParser(input => {
// grab the next `X` chars
const [rest, optionMatch] = splitString(input, lit.length)
return pipe(
optionMatch,
// if we have a match, and it's what we expect, the Parser succeeds
E.chain(match =>
match === lit
? E.right({ remaining: rest, value: lit })
: E.left(
parserError(
`Could not match literal, expected ${lit} but got ${match}`
)
)
)
)
})
// predicate that checks whether a given char is numeric
const isNumber = (char: string): boolean => {
const code = char.charCodeAt(0)
return (
code > 47 && code < 58 // numeric (0-9)
)
}
// predicate that checks whether a given char is a letter
const isLetter = (char: string): boolean => {
const code = char.charCodeAt(0)
return (
(code > 64 && code < 91) || // upper alpha (A-Z)
(code > 96 && code < 123) // lower alpha (a-z)
)
}
// predicate that checks whether a given char is a letter or a number
const isAlphaNumeric = (char: string): boolean =>
isLetter(char) || isNumber(char)
// parse a single alphanumeric char
export const alphaNumeric = pred(anyChar, isAlphaNumeric)
// parse a single digit
export const numParser: Parser<never, number> = pipe(
pred(anyChar, isNumber),
map(a => Number(a))
)
// parse a string of alphanumeric chars
export const identifier = pipe(
oneOrMore(alphaNumeric),
map(as => as.join(''))
)
// parses a single piece of whitespace
export const whitespace = pred(anyChar, a => a.trim() === '')
// parser that consumes zero or more items of whitespace
export const space0 = zeroOrMore(whitespace)
// parser that consumes one or more items of whitespace
export const space1 = oneOrMore(whitespace)
// a Parser that looks for `match`, and if it finds it, returns `A`
export const mapLiteral = <A>(
match: string,
output: A
): Parser<never, A> =>
pipe(
matchLiteral(match),
map(_ => output)
)
type Op = { type: 'Add' } | { type: 'Subtract' }
type Expr =
| { type: 'Int'; value: number }
| { type: 'Op'; op: Op; value1: Expr; value2: Expr }
const parseOp = altMany<never, Op>(
mapLiteral('+', { type: 'Add' }),
mapLiteral('-', { type: 'Subtract' })
)
const intParser: Parser<never, Expr> = pipe(
numParser,
map(a => ({ type: 'Int', value: a }))
)
const parseOpExpr: Parser<never, Expr> = pipe(
pair(
pair(right(matchLiteral('('), intParser), parseOp),
left(intParser, matchLiteral(')'))
),
map(([[a, op], b]) => ({ type: 'Op', op, value1: a, value2: b }))
)
const parseExpr: Parser<never, Expr> = altMany(intParser, parseOpExpr)
console.log(runParser(parseExpr, '1'))
console.log(runParser(parseExpr, '(1+1)'))
console.log(runParser(parseExpr, '(1 + 1)')) // we haven't accounted for whitespace