Skip to content

Commit f0b1281

Browse files
committed
avoid reallocation
1 parent 3b3ae4b commit f0b1281

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/parser/parser_lib.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class SourceMemo<T> {
1212
set(key: Source, value: T): void {
1313
if (this.#source !== key.source) {
1414
this.#source = key.source;
15-
this.#map = new Map();
15+
this.clear();
1616
}
1717
this.#map.set(key.position, value);
1818
}

src/parser/test.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
// This code is Deno only
22

3-
import { assertArrayIncludes } from "@std/assert/array-includes";
43
import { assertEquals } from "@std/assert/equals";
54
import { assertLess } from "@std/assert/less";
65
import { assertNotEquals } from "@std/assert/not-equals";
76
import { assertThrows } from "@std/assert/throws";
87
import { EXAMPLE_SENTENCES, MALFORMED_SENTENCES } from "../examples.ts";
98
import { parse } from "./parser.ts";
10-
import { end, match, matchString, sequence } from "./parser_lib.ts";
9+
import { all, end, many, match, matchString, sequence } from "./parser_lib.ts";
1110
import { KU_LILI, KU_SULI, PU } from "./ucsur.ts";
1211

1312
Deno.test("AST all distinct", () => {
@@ -60,5 +59,19 @@ Deno.test("small parser", () => {
6059
match(/a/, '"a"').skip(end),
6160
)
6261
.generateParser();
63-
assertArrayIncludes(parser("toki pona a").unwrap(), [["toki", "pona", "a"]]);
62+
assertEquals(parser("toki pona a").unwrap(), [["toki", "pona", "a"]]);
63+
});
64+
65+
Deno.test("many", () => {
66+
const space = match(/\s*/, "space");
67+
const parser = many(matchString("a").skip(space)).skip(end)
68+
.generateParser();
69+
assertEquals(parser("a a a").unwrap(), [["a", "a", "a"]]);
70+
});
71+
72+
Deno.test("all", () => {
73+
const space = match(/\s*/, "space");
74+
const parser = all(matchString("a").skip(space)).skip(end)
75+
.generateParser();
76+
assertEquals(parser("a a a").unwrap(), [["a", "a", "a"]]);
6477
});

0 commit comments

Comments
 (0)