Skip to content

Commit cc2b530

Browse files
committed
more memoization
1 parent af9b211 commit cc2b530

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/parser/parser_lib.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { assertGreater } from "@std/assert/greater";
22
import { MemoizationCacheResult, memoize } from "@std/cache/memoize";
3+
import { lazy as lazyEval } from "../../misc/misc.ts";
34
import { ArrayResult, ArrayResultError } from "../array_result.ts";
45

56
type ParserResult<T> = ArrayResult<Readonly<{ value: T; length: number }>>;
@@ -188,7 +189,7 @@ export function sequence<T extends ReadonlyArray<unknown>>(
188189
}
189190
export const many = memoize(<T>(parser: Parser<T>): Parser<ReadonlyArray<T>> =>
190191
choice(
191-
sequence(parser, lazy(() => many(parser)))
192+
sequence(parser, lazy(lazyEval(() => many(parser))))
192193
.map(([first, rest]) => [first, ...rest]),
193194
emptyArray,
194195
)
@@ -201,7 +202,7 @@ export function manyAtLeastOnce<T>(
201202
}
202203
export const all = memoize(<T>(parser: Parser<T>): Parser<ReadonlyArray<T>> =>
203204
choiceOnlyOne(
204-
sequence(parser, lazy(() => all(parser)))
205+
sequence(parser, lazy(lazyEval(() => all(parser))))
205206
.map(([first, rest]) => [first, ...rest]),
206207
emptyArray,
207208
)
@@ -382,7 +383,7 @@ export const allWithCheck = memoize(<T>(
382383
choiceWithCheck(
383384
new CheckedParser(
384385
parser.check,
385-
sequence(parser.parser, lazy(() => allWithCheck(parser)))
386+
sequence(parser.parser, lazy(lazyEval(() => allWithCheck(parser))))
386387
.map(([first, rest]) => [first, ...rest]),
387388
),
388389
checkedAsWhole(emptyArray),

0 commit comments

Comments
 (0)