Skip to content

Commit 124e4cb

Browse files
committed
fix
1 parent 6e023a2 commit 124e4cb

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/parser/parser_lib.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,18 +223,20 @@ export function matchString(
223223
description = `"${match}"`,
224224
): Parser<string> {
225225
return new Parser(({ source, position }) => {
226-
const sourceString = source.slice(position);
227226
if (
228227
source.length - position >= match.length &&
229-
sourceString.slice(0, match.length)
228+
source.slice(position, position + match.length) === match
230229
) {
231230
return new ArrayResult([{
232231
rest: { source, position: position + match.length },
233232
value: match,
234233
}]);
235234
} else {
236235
return new ArrayResult(
237-
new UnexpectedError(describeSource(sourceString), description),
236+
new UnexpectedError(
237+
describeSource(source.slice(position)),
238+
description,
239+
),
238240
);
239241
}
240242
});

src/parser/test.ts

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

3+
import { assertArrayIncludes } from "@std/assert/array-includes";
34
import { assertEquals } from "@std/assert/equals";
45
import { assertLess } from "@std/assert/less";
56
import { assertNotEquals } from "@std/assert/not-equals";
67
import { assertThrows } from "@std/assert/throws";
78
import { EXAMPLE_SENTENCES, MALFORMED_SENTENCES } from "../examples.ts";
89
import { parse } from "./parser.ts";
10+
import { end, match, matchString, sequence } from "./parser_lib.ts";
911
import { KU_LILI, KU_SULI, PU } from "./ucsur.ts";
1012

1113
Deno.test("AST all distinct", () => {
@@ -49,3 +51,14 @@ function uniquePairs<T>(
4951
): ReadonlyArray<readonly [T, T]> {
5052
return array.flatMap((a, i) => array.slice(i + 1).map((b) => [a, b]));
5153
}
54+
55+
Deno.test("parser", () => {
56+
const space = match(/\s*/, "space");
57+
const parser = sequence(
58+
match(/toki/, '"toki"').skip(space),
59+
matchString("pona").skip(space),
60+
match(/a/, '"a"').skip(end),
61+
)
62+
.generateParser();
63+
assertArrayIncludes(parser("toki pona a").unwrap(), [["toki", "pona", "a"]]);
64+
});

0 commit comments

Comments
 (0)