Skip to content

Commit 8b90cf5

Browse files
committed
add position and improve error message
1 parent 06ddab1 commit 8b90cf5

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

dictionary/parser.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
Parser,
2222
sequence,
2323
UnexpectedError,
24+
withPosition,
2425
withSource,
2526
} from "../src/parser/parser_lib.ts";
2627
import { Definition, Noun, PartialVerb } from "./type.ts";
@@ -61,10 +62,15 @@ const semicolon = lex(matchString(";", "semicolon"));
6162
const slash = lex(matchString("/", "slash"));
6263

6364
const keyword = memoize(<T extends string>(keyword: T) =>
64-
lex(match(/[a-z\-]+/, keyword))
65-
.filter((that) =>
66-
keyword === that ||
67-
throwError(new UnexpectedError(`"${that}"`, `"${keyword}"`))
65+
lex(withPosition(match(/[a-z\-]+/, keyword)))
66+
.map((positioned) =>
67+
positioned.value === keyword ? positioned.value : throwError(
68+
new UnexpectedError(
69+
`"${positioned.value}"`,
70+
`"${keyword}"`,
71+
positioned,
72+
),
73+
)
6874
) as Parser<T>
6975
);
7076
const unescapedWord = allAtLeastOnceWithCheck(

0 commit comments

Comments
 (0)