Skip to content

Commit 42a8e9b

Browse files
committed
refactor
1 parent 316b696 commit 42a8e9b

File tree

1 file changed

+22
-28
lines changed

1 file changed

+22
-28
lines changed

dictionary/parser.ts

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,30 @@ import {
3636
const RESERVED_SYMBOLS = "#()*+/:;<=>@[\\]^`{|}~";
3737
const WORDS = new RegExp(`[^${escapeRegex(RESERVED_SYMBOLS)}]`);
3838

39+
function lex<T>(parser: Parser<T>): Parser<T> {
40+
return parser.skip(spaces);
41+
}
3942
const comment = match(/#[^\n\r]*/, "comment");
4043
const spaces = sourceOnly(all(choiceOnlyOne(match(/\s/, "space"), comment)));
41-
const tokiPonaWord = match(/[a-z][a-zA-Z]*/, "word");
4244
const backtick = matchString("`", "backtick");
43-
const openParenthesis = matchString("(", "open parenthesis");
44-
const closeParenthesis = matchString(")", "close parenthesis");
45-
const openBracket = matchString("[", "open bracket");
46-
const closeBracket = matchString("]", "close bracket");
47-
const comma = matchString(",", "comma");
48-
const colon = matchString(":", "colon");
45+
46+
const tokiPonaWord = lex(match(/[a-z][a-zA-Z]*/, "word"));
47+
const openParenthesis = lex(matchString("(", "open parenthesis"));
48+
const closeParenthesis = lex(matchString(")", "close parenthesis"));
49+
const openBracket = lex(matchString("[", "open bracket"));
50+
const closeBracket = lex(matchString("]", "close bracket"));
51+
const comma = lex(matchString(",", "comma"));
52+
const colon = lex(matchString(":", "colon"));
4953
const semicolon = lex(matchString(";", "semicolon"));
54+
const slash = lex(matchString("/", "slash"));
5055

51-
function lex<T>(parser: Parser<T>): Parser<T> {
52-
return parser.skip(spaces);
53-
}
56+
const keyword = memoize(<T extends string>(keyword: T): Parser<T> =>
57+
lex(match(/[a-z\-]+/, keyword))
58+
.filter((that) =>
59+
keyword === that ||
60+
throwError(new UnexpectedError(`"${that}"`, `"${keyword}"`))
61+
) as Parser<T>
62+
);
5463
const unescapedWord = allAtLeastOnce(
5564
choiceOnlyOne(
5665
match(WORDS, "word"),
@@ -65,17 +74,8 @@ const unescapedWord = allAtLeastOnce(
6574
word !== "" || throwError(new ArrayResultError("missing word"))
6675
);
6776
const word = unescapedWord.map(escapeHtml);
68-
const slash = lex(matchString("/", "slash"));
6977
const forms = sequence(word, all(slash.with(word)))
7078
.map(([first, rest]) => [first, ...rest]);
71-
72-
const keyword = memoize(<T extends string>(keyword: T): Parser<T> =>
73-
lex(match(/[a-z\-]+/, keyword))
74-
.filter((that) =>
75-
keyword === that ||
76-
throwError(new UnexpectedError(`"${that}"`, `"${keyword}"`))
77-
) as Parser<T>
78-
);
7979
const number = choiceOnlyOne(keyword("singular"), keyword("plural"));
8080
const optionalNumber = optionalAll(number);
8181
const perspective = choiceOnlyOne(
@@ -84,12 +84,10 @@ const perspective = choiceOnlyOne(
8484
keyword("third"),
8585
);
8686
function tag<T>(parser: Parser<T>): Parser<T> {
87-
return lex(openParenthesis)
88-
.with(parser)
89-
.skip(lex(closeParenthesis));
87+
return openParenthesis.with(parser).skip(closeParenthesis);
9088
}
9189
function template<T>(parser: Parser<T>): Parser<T> {
92-
return lex(openBracket).with(parser).skip(lex(closeBracket));
90+
return openBracket.with(parser).skip(closeBracket);
9391
}
9492
const simpleUnit = memoize((kind: string) => word.skip(tag(keyword(kind))));
9593
function detectRepetition(
@@ -498,11 +496,7 @@ const definition = choiceOnlyOne<Definition>(
498496
type: "filler",
499497
})),
500498
);
501-
const singleWord = lex(tokiPonaWord);
502-
const head = sequence(
503-
all(singleWord.skip(lex(comma))),
504-
singleWord,
505-
)
499+
const head = sequence(all(tokiPonaWord.skip(comma)), tokiPonaWord)
506500
.skip(colon)
507501
.map(([init, last]) => [...init, last]);
508502
const entry = withSource(spaces.with(all(definition)))

0 commit comments

Comments
 (0)