Skip to content

Commit 77ccb7a

Browse files
committed
add assertions to combinators
1 parent f5e82ec commit 77ccb7a

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/parser/parser_lib.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { assert } from "@std/assert/assert";
12
import { memoize } from "@std/cache/memoize";
23
import { ArrayResult, ArrayResultError } from "../array_result.ts";
34
import { Clearable, ClearableCacheSet, Lazy } from "../cache.ts";
@@ -120,13 +121,18 @@ export function lazy<T>(parser: () => Parser<T>): Parser<T> {
120121
}
121122
}
122123
export function choice<T>(...choices: ReadonlyArray<Parser<T>>): Parser<T> {
124+
assert(choices.length > 1, "`choice` called with less than 2 arguments");
123125
return new Parser((src) =>
124126
new ArrayResult(choices).flatMap((parser) => parser.rawParser(src))
125127
);
126128
}
127129
export function choiceOnlyOne<T>(
128130
...choices: ReadonlyArray<Parser<T>>
129131
): Parser<T> {
132+
assert(
133+
choices.length > 1,
134+
"`choiceOnlyOne` called with less than 2 arguments",
135+
);
130136
return choices.reduceRight(
131137
(right, left) =>
132138
new Parser((src) => {
@@ -151,6 +157,7 @@ export function sequence<T extends ReadonlyArray<unknown>>(
151157
& Readonly<{ [I in keyof T]: Parser<T[I]> }>
152158
& Readonly<{ length: T["length"] }>
153159
): Parser<T> {
160+
assert(sequence.length > 1, "`sequence` called with less than 2 arguments");
154161
// We resorted to using `any` types here, make sure it works properly
155162
return sequence.reduceRight(
156163
(right: Parser<any>, left) =>

0 commit comments

Comments
 (0)