1- import { assert } from "@std/assert/assert" ;
1+ import { assertGreater } from "@std/assert/greater" ;
2+ import { assertGreaterOrEqual } from "@std/assert/greater-or-equal" ;
23import { MemoizationCacheResult , memoize } from "@std/cache/memoize" ;
34import { ArrayResult , ArrayResultError } from "../array_result.ts" ;
45
@@ -55,7 +56,7 @@ export class Parser<T> {
5556 constructor ( parser : InnerParser < T > ) {
5657 // TODO: remove assertion
5758 const ensureParser : InnerParser < T > = ( source ) => {
58- assert ( source . source . length >= source . position ) ;
59+ assertGreaterOrEqual ( source . source . length , source . position ) ;
5960 return parser ( source ) ;
6061 } ;
6162 this . rawParser = memoize <
@@ -146,16 +147,21 @@ export function lazy<T>(parser: () => Parser<T>): Parser<T> {
146147 return new Parser ( ( source ) => parser ( ) . rawParser ( source ) ) ;
147148}
148149export 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+ ) ;
150155 return new Parser ( ( source ) =>
151156 new ArrayResult ( choices ) . flatMap ( ( parser ) => parser . rawParser ( source ) )
152157 ) ;
153158}
154159export function choiceOnlyOne < T > (
155160 ...choices : ReadonlyArray < Parser < T > >
156161) : Parser < T > {
157- assert (
158- choices . length > 1 ,
162+ assertGreater (
163+ choices . length ,
164+ 1 ,
159165 "`choiceOnlyOne` called with less than 2 arguments" ,
160166 ) ;
161167 return choices . reduceRight (
@@ -182,7 +188,11 @@ export function sequence<T extends ReadonlyArray<unknown>>(
182188 & Readonly < { [ I in keyof T ] : Parser < T [ I ] > } >
183189 & Readonly < { length : T [ "length" ] } >
184190) : 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+ ) ;
186196 // We resorted to using `any` types here, make sure it works properly
187197 return sequence . reduceRight (
188198 ( right : Parser < any > , left ) =>
0 commit comments