File tree 2 files changed +23
-1
lines changed
2 files changed +23
-1
lines changed Original file line number Diff line number Diff line change 1
1
import * as combinators from './combinators.js'
2
2
import * as constructors from './constructors.js'
3
+ import * as parser from './parser.js'
3
4
4
- const parsing = { ...combinators , ...constructors }
5
+ const parsing = { ...combinators , ...constructors , ... parser }
5
6
export default parsing
6
7
7
8
export * from './combinators.js'
Original file line number Diff line number Diff line change 1
1
import type { Either , Right } from '@matt.kantor/either'
2
+ import * as either from '@matt.kantor/either'
2
3
3
4
export type InvalidInputError = {
4
5
readonly input : string
@@ -17,3 +18,23 @@ export type Success<Output> = {
17
18
readonly remainingInput : string
18
19
readonly output : Output
19
20
}
21
+
22
+ /**
23
+ * Apply `parser` to the given `input`, requiring it to consume the entire input
24
+ * (all the way to the end of the string).
25
+ *
26
+ * Unlike `Parser`s, in the return value `Output` is not wrapped in `Success`
27
+ * (there will never be any `remainingInput`).
28
+ */
29
+ export const parse = < Output > (
30
+ parser : Parser < Output > ,
31
+ input : string ,
32
+ ) : Either < InvalidInputError , Output > =>
33
+ either . flatMap ( parser ( input ) , ( { remainingInput, output } ) =>
34
+ remainingInput . length !== 0
35
+ ? either . makeLeft ( {
36
+ input,
37
+ message : 'excess content followed valid input' ,
38
+ } )
39
+ : either . makeRight ( output ) ,
40
+ )
You can’t perform that action at this time.
0 commit comments