1
+ import { assert } from "@std/assert/assert" ;
1
2
import { memoize } from "@std/cache/memoize" ;
2
3
import { ArrayResult , ArrayResultError } from "../array_result.ts" ;
3
4
import { Clearable , ClearableCacheSet , Lazy } from "../cache.ts" ;
@@ -120,13 +121,18 @@ export function lazy<T>(parser: () => Parser<T>): Parser<T> {
120
121
}
121
122
}
122
123
export function choice < T > ( ...choices : ReadonlyArray < Parser < T > > ) : Parser < T > {
124
+ assert ( choices . length > 1 , "`choice` called with less than 2 arguments" ) ;
123
125
return new Parser ( ( src ) =>
124
126
new ArrayResult ( choices ) . flatMap ( ( parser ) => parser . rawParser ( src ) )
125
127
) ;
126
128
}
127
129
export function choiceOnlyOne < T > (
128
130
...choices : ReadonlyArray < Parser < T > >
129
131
) : Parser < T > {
132
+ assert (
133
+ choices . length > 1 ,
134
+ "`choiceOnlyOne` called with less than 2 arguments" ,
135
+ ) ;
130
136
return choices . reduceRight (
131
137
( right , left ) =>
132
138
new Parser ( ( src ) => {
@@ -151,6 +157,7 @@ export function sequence<T extends ReadonlyArray<unknown>>(
151
157
& Readonly < { [ I in keyof T ] : Parser < T [ I ] > } >
152
158
& Readonly < { length : T [ "length" ] } >
153
159
) : Parser < T > {
160
+ assert ( sequence . length > 1 , "`sequence` called with less than 2 arguments" ) ;
154
161
// We resorted to using `any` types here, make sure it works properly
155
162
return sequence . reduceRight (
156
163
( right : Parser < any > , left ) =>
0 commit comments