Skip to content

Commit 77be855

Browse files
committed
reorganize
1 parent 20befb7 commit 77be855

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

dictionary/parser.ts

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,26 @@ import { Definition, Noun, PartialVerb } from "./type.ts";
2727

2828
const RESERVED_SYMBOLS = "#()*+/:;<=>@[\\]^`{|}~";
2929

30-
function lex<T>(parser: Parser<T>): Parser<T> {
31-
return parser.skip(ignore);
32-
}
30+
const hashSign = matchString("#", "hash sign");
31+
const backtick = matchString("`", "backtick");
32+
const colon = matchString(":", "colon");
33+
34+
const tokiPonaWord = lex(match(/[a-z][a-zA-Z]*/, "word"));
35+
const openParenthesis = lex(matchString("(", "open parenthesis"));
36+
const closeParenthesis = lex(matchString(")", "close parenthesis"));
37+
const openBracket = lex(matchString("[", "open bracket"));
38+
const closeBracket = lex(matchString("]", "close bracket"));
39+
const comma = lex(matchString(",", "comma"));
40+
const semicolon = lex(matchString(";", "semicolon"));
41+
const slash = lex(matchString("/", "slash"));
42+
43+
const character = match(/./u, "character");
44+
const wordCharacter = match(
45+
new RegExp(`[^${escapeRegex(RESERVED_SYMBOLS)}]`),
46+
"word character",
47+
);
3348
const comment = checkedSequence(
34-
matchString("#", "hash sign"),
49+
hashSign,
3550
match(/[^\n]*?(?=\r?\n|$)/, "comment content"),
3651
);
3752
const spaces = checkedSequence(
@@ -40,26 +55,13 @@ const spaces = checkedSequence(
4055
);
4156
const ignore = allWithCheck(
4257
new CheckedParser(
43-
choiceOnlyOne(comment.check, spaces.check),
58+
choiceOnlyOne(hashSign, spaces.check),
4459
choiceWithCheck(spaces, comment),
4560
),
4661
);
47-
const backtick = matchString("`", "backtick");
48-
const colon = matchString(":", "colon");
49-
const character = match(/./u, "character");
50-
const wordCharacter = match(
51-
new RegExp(`[^${escapeRegex(RESERVED_SYMBOLS)}]`),
52-
"word character",
53-
);
54-
const tokiPonaWord = lex(match(/[a-z][a-zA-Z]*/, "word"));
55-
const openParenthesis = lex(matchString("(", "open parenthesis"));
56-
const closeParenthesis = lex(matchString(")", "close parenthesis"));
57-
const openBracket = lex(matchString("[", "open bracket"));
58-
const closeBracket = lex(matchString("]", "close bracket"));
59-
const comma = lex(matchString(",", "comma"));
60-
const semicolon = lex(matchString(";", "semicolon"));
61-
const slash = lex(matchString("/", "slash"));
62-
62+
function lex<T>(parser: Parser<T>): Parser<T> {
63+
return parser.skip(ignore);
64+
}
6365
const keyword = memoize(<T extends string>(keyword: T) =>
6466
lex(withPosition(match(/[a-z\-]+/, `"${keyword}"`)))
6567
.map((positioned) =>
@@ -79,7 +81,7 @@ const unescapedWord = sequence(
7981
choiceWithCheck(checkedCharacter, escape),
8082
allWithCheck(
8183
new CheckedParser(
82-
choiceOnlyOne(wordCharacter, backtick, comment.check),
84+
choiceOnlyOne(wordCharacter, backtick, hashSign),
8385
choiceWithCheck(checkedCharacter, escape, comment.map(() => "")),
8486
),
8587
),

0 commit comments

Comments
 (0)