1
- import { assert } from "@std/assert/assert" ;
1
+ import { assertGreater } from "@std/assert/greater" ;
2
+ import { assertGreaterOrEqual } from "@std/assert/greater-or-equal" ;
2
3
import { MemoizationCacheResult , memoize } from "@std/cache/memoize" ;
3
4
import { ArrayResult , ArrayResultError } from "../array_result.ts" ;
4
5
@@ -55,7 +56,7 @@ export class Parser<T> {
55
56
constructor ( parser : InnerParser < T > ) {
56
57
// TODO: remove assertion
57
58
const ensureParser : InnerParser < T > = ( source ) => {
58
- assert ( source . source . length >= source . position ) ;
59
+ assertGreaterOrEqual ( source . source . length , source . position ) ;
59
60
return parser ( source ) ;
60
61
} ;
61
62
this . rawParser = memoize <
@@ -146,16 +147,21 @@ export function lazy<T>(parser: () => Parser<T>): Parser<T> {
146
147
return new Parser ( ( source ) => parser ( ) . rawParser ( source ) ) ;
147
148
}
148
149
export function choice < T > ( ...choices : ReadonlyArray < Parser < T > > ) : Parser < T > {
149
- assert ( choices . length > 1 , "`choice` called with less than 2 arguments" ) ;
150
+ assertGreater (
151
+ choices . length ,
152
+ 1 ,
153
+ "`choice` called with less than 2 arguments" ,
154
+ ) ;
150
155
return new Parser ( ( source ) =>
151
156
new ArrayResult ( choices ) . flatMap ( ( parser ) => parser . rawParser ( source ) )
152
157
) ;
153
158
}
154
159
export function choiceOnlyOne < T > (
155
160
...choices : ReadonlyArray < Parser < T > >
156
161
) : Parser < T > {
157
- assert (
158
- choices . length > 1 ,
162
+ assertGreater (
163
+ choices . length ,
164
+ 1 ,
159
165
"`choiceOnlyOne` called with less than 2 arguments" ,
160
166
) ;
161
167
return choices . reduceRight (
@@ -182,7 +188,11 @@ export function sequence<T extends ReadonlyArray<unknown>>(
182
188
& Readonly < { [ I in keyof T ] : Parser < T [ I ] > } >
183
189
& Readonly < { length : T [ "length" ] } >
184
190
) : Parser < T > {
185
- assert ( sequence . length > 1 , "`sequence` called with less than 2 arguments" ) ;
191
+ assertGreater (
192
+ sequence . length ,
193
+ 1 ,
194
+ "`sequence` called with less than 2 arguments" ,
195
+ ) ;
186
196
// We resorted to using `any` types here, make sure it works properly
187
197
return sequence . reduceRight (
188
198
( right : Parser < any > , left ) =>
0 commit comments