Skip to content

Commit 430d812

Browse files
committed
memoize
1 parent c9ed54a commit 430d812

File tree

1 file changed

+12
-23
lines changed

1 file changed

+12
-23
lines changed

src/parser/lexer.ts

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
matchString,
1515
nothing,
1616
optionalAll,
17-
Parser,
1817
sequence,
1918
sourceOnly,
2019
UnexpectedError,
@@ -51,12 +50,13 @@ const latinWord = match(/[a-z][a-zA-Z]*/, "word").skip(spaces);
5150
const variationSelector = match(/[\uFE00-\uFE0F]/, "variation selector");
5251
const ucsur = match(UCSUR_CHARACTER_REGEX, "UCSUR glyph")
5352
.map((ucsur) => UCSUR_TO_LATIN.get(ucsur)!);
54-
function specificSpecialUcsur(specialUcsur: string): Parser<string> {
55-
return matchString(
53+
54+
const specificSpecialUcsur = memoize((specialUcsur: string) =>
55+
matchString(
5656
specialUcsur,
5757
SPECIAL_UCSUR_DESCRIPTIONS.get(specialUcsur)!,
58-
);
59-
}
58+
)
59+
);
6060
const singleUcsurWord = ucsur.skip(optionalAll(variationSelector)).skip(spaces);
6161
const joiner = choiceOnlyOne(
6262
matchString("\u200D", "zero width joiner"),
@@ -71,12 +71,12 @@ const properWords = allAtLeastOnce(
7171
)
7272
.map((array) => array.join(" "))
7373
.map((words) => ({ type: "proper word", words, kind: "latin" }) as const);
74-
function specificWord(thatWord: string): Parser<string> {
75-
return word.filter((thisWord) =>
74+
const specificWord = memoize((thatWord: string) =>
75+
word.filter((thisWord) =>
7676
thatWord === thisWord ||
7777
throwError(new UnexpectedError(`"${thisWord}"`, `"${thatWord}"`))
78-
);
79-
}
78+
)
79+
);
8080
const multipleA = specificWord("a")
8181
.with(count(allAtLeastOnce(specificWord("a"))))
8282
.map((count) => ({ type: "multiple a", count: count + 1 }) as const);
@@ -151,20 +151,9 @@ const cartouches = allAtLeastOnce(cartouche)
151151
kind: "cartouche",
152152
}) as const
153153
);
154-
function longContainer<T>(
155-
left: string,
156-
right: string,
157-
inside: Parser<T>,
158-
): Parser<T> {
159-
return specificSpecialUcsur(left)
160-
.with(inside)
161-
.skip(specificSpecialUcsur(right));
162-
}
163-
const longSpaceContainer = longContainer(
164-
START_OF_LONG_GLYPH,
165-
END_OF_LONG_GLYPH,
166-
count(spacesWithoutNewline).filter((length) => length > 0),
167-
)
154+
const longSpaceContainer = specificSpecialUcsur(START_OF_LONG_GLYPH)
155+
.with(count(spacesWithoutNewline).filter((length) => length > 0))
156+
.skip(specificSpecialUcsur(END_OF_LONG_GLYPH))
168157
.skip(spaces);
169158
const longGlyphHead = choiceOnlyOne(
170159
combinedGlyphs,

0 commit comments

Comments
 (0)