Skip to content

Commit b954d5f

Browse files
committed
fix
1 parent 26b95ec commit b954d5f

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

src/parser/parser_lib.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { assertGreater } from "@std/assert/greater";
2-
import { assertLessOrEqual } from "@std/assert/less-or-equal";
3-
import { MemoizationCacheResult, memoize } from "@std/cache/memoize";
2+
import { memoize } from "@std/cache/memoize";
43
import { ArrayResult, ArrayResultError } from "../array_result.ts";
54

65
type Input = Readonly<{ source: string; position: number }>;
@@ -54,18 +53,18 @@ class SourceMemo<T> {
5453
export class Parser<T> {
5554
readonly rawParser: InnerParser<T>;
5655
constructor(parser: InnerParser<T>) {
57-
this.rawParser = memoize<
58-
InnerParser<T>,
59-
Input,
60-
SourceMemo<MemoizationCacheResult<ParserResult<T>>>
61-
>(
62-
(input) => {
63-
// TODO: remove assertion
64-
assertLessOrEqual(input.position, input.source.length);
65-
return parser(input);
66-
},
67-
{ cache: new SourceMemo() },
68-
);
56+
// Turns out @std/[email protected] is buggy
57+
const cache: SourceMemo<ParserResult<T>> = new SourceMemo();
58+
allMemo.add(new WeakRef(cache));
59+
this.rawParser = (input) => {
60+
if (cache.has(input)) {
61+
return cache.get(input)!;
62+
} else {
63+
const result = parser(input);
64+
cache.set(input, result);
65+
return result;
66+
}
67+
};
6968
}
7069
generateParser(): (source: string) => ArrayResult<T> {
7170
return (input) => {

0 commit comments

Comments
 (0)