Skip to content

Commit afee1fe

Browse files
committed
refactor
1 parent cefafa2 commit afee1fe

File tree

6 files changed

+59
-67
lines changed

6 files changed

+59
-67
lines changed

dictionary/parser.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ const checkedNoun = new CheckedParser(
257257
),
258258
noun,
259259
);
260-
function simpleDefinitionWith<T>(
260+
function checkedSimpleUnitWith<T>(
261261
tag: Parser<unknown>,
262262
after: Parser<T>,
263263
): CheckedParser<readonly [string, T]> {
@@ -266,30 +266,33 @@ function simpleDefinitionWith<T>(
266266
closeParenthesis.with(after),
267267
);
268268
}
269-
function simpleDefinition(tag: Parser<unknown>): CheckedParser<string> {
270-
return simpleDefinitionWith(tag, nothing).map(([word]) => word);
269+
function checkedSimpleUnit(tag: Parser<unknown>): CheckedParser<string> {
270+
return checkedSimpleUnitWith(tag, nothing).map(([word]) => word);
271271
}
272-
function simpleDefinitionWithTemplate(
272+
function checkedSimpleUnitWithTemplate(
273273
tag: Parser<unknown>,
274274
templateInside: Parser<unknown>,
275275
): CheckedParser<string> {
276-
return simpleDefinitionWith(tag, template(templateInside))
276+
return checkedSimpleUnitWith(tag, template(templateInside))
277277
.map(([word]) => word);
278278
}
279-
const interjectionDefinition = simpleDefinition(keyword("i"))
279+
const interjectionDefinition = checkedSimpleUnit(keyword("i"))
280280
.map((interjection) => ({ type: "interjection", interjection }) as const);
281-
const particleDefinition = simpleDefinition(
282-
sequence(keyword("particle"), keyword("def")),
281+
const particleDefinition = checkedSequence(
282+
word.skip(openParenthesis).skip(keyword("particle")),
283+
sequence(keyword("def"), closeParenthesis),
283284
)
284-
.map((definition) => ({ type: "particle definition", definition }) as const);
285-
const adverbDefinition = simpleDefinition(keyword("adv"))
285+
.map(([definition]) =>
286+
({ type: "particle definition", definition }) as const
287+
);
288+
const adverbDefinition = checkedSimpleUnit(keyword("adv"))
286289
.map((adverb) => ({ type: "adverb", adverb }) as const);
287-
const prepositionDefinition = simpleDefinitionWithTemplate(
290+
const prepositionDefinition = checkedSimpleUnitWithTemplate(
288291
keyword("prep"),
289292
sequence(keyword("indirect"), keyword("object")),
290293
)
291294
.map((preposition) => ({ type: "preposition", preposition }) as const);
292-
const numeralDefinition = simpleDefinition(keyword("num"))
295+
const numeralDefinition = checkedSimpleUnit(keyword("num"))
293296
.mapWithPositionedError((num) => {
294297
const numeral = Number.parseInt(num);
295298
if (Number.isNaN(numeral)) {
@@ -394,7 +397,7 @@ const nounDefinition = new CheckedParser(
394397
sequence(
395398
noun,
396399
optionalWithCheck(
397-
simpleDefinitionWithTemplate(keyword("prep"), keyword("headword")),
400+
checkedSimpleUnitWithTemplate(keyword("prep"), keyword("headword")),
398401
),
399402
),
400403
)
@@ -430,7 +433,7 @@ const verbDefinition = checkedSequence(
430433
sequence(closeParenthesis, openBracket, keyword("object")),
431434
closeBracket
432435
.with(optionalWithCheck(
433-
simpleDefinitionWith(keyword("prep"), noun)
436+
checkedSimpleUnitWith(keyword("prep"), noun)
434437
.map(([preposition, object]) => ({ preposition, object }) as const),
435438
))
436439
.map(nullableAsArray),
@@ -474,7 +477,7 @@ const verbDefinition = checkedSequence(
474477
optionalWithCheck(checkedNoun),
475478
),
476479
optionalWithCheck(
477-
simpleDefinitionWith(
480+
checkedSimpleUnitWith(
478481
keyword("prep"),
479482
choiceWithCheck<"template" | Noun>(
480483
checkedSequence(

src/dictionary.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ function update(): void {
3737
}
3838
redefineSet(
3939
contentWordSet,
40-
({ type }) =>
41-
type !== "filler" &&
42-
type !== "particle definition",
40+
({ type }) => !["filler", "particle definition"].includes(type),
4341
);
4442
redefineSetWithType(prepositionSet, "preposition");
4543
redefineSet(

src/parser/test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ Deno.test("no ali", () => {
4040
assertNotEquals(word, "ali");
4141
}
4242
});
43-
function uniquePairs<T>(
44-
array: ReadonlyArray<T>,
45-
): ReadonlyArray<readonly [T, T]> {
46-
return array.flatMap((a, i) => array.slice(i + 1).map((b) => [a, b]));
47-
}
4843
Deno.test("small parser", () => {
4944
const space = match(/\s*/, "space");
5045
const parser = sequence(
@@ -64,3 +59,8 @@ Deno.test("all", () => {
6459
const parser = all(matchString("a").skip(space)).skip(end);
6560
assertEquals(parser.parse("a a a").unwrap(), [["a", "a", "a"]]);
6661
});
62+
function uniquePairs<T>(
63+
array: ReadonlyArray<T>,
64+
): ReadonlyArray<readonly [T, T]> {
65+
return array.flatMap((a, i) => array.slice(i + 1).map((b) => [a, b]));
66+
}

src/translator/composer.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,16 @@ export function verb(phrase: English.VerbPhrase, depth: number): string {
109109
]
110110
.join(" ");
111111
}
112-
function defaultClause(clause: English.Clause & { type: "default" }): string {
113-
const subject = !clause.hideSubject ? [noun(clause.subject, 0)] : [];
114-
return [
115-
...subject,
116-
verb(clause.verb, 0),
117-
]
118-
.join(" ");
119-
}
120112
function clause(ast: English.Clause): string {
121113
switch (ast.type) {
122-
case "default":
123-
return defaultClause(ast);
114+
case "default": {
115+
const subject = !ast.hideSubject ? [noun(ast.subject, 0)] : [];
116+
return [
117+
...subject,
118+
verb(ast.verb, 0),
119+
]
120+
.join(" ");
121+
}
124122
case "interjection":
125123
return word(ast.interjection);
126124
case "subject phrase":

src/translator/test.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,30 @@ Deno.test("adjective with adverb", () => {
1212
const translations = translate("pona ike").unwrap();
1313
assertArrayIncludes(translations, ["Badly good"]);
1414
});
15-
const NUMBER_TESTS = new Map(Object.entries({
16-
"tu tu tu wan": 7,
17-
"luka tu": 7,
18-
"mute mute mute luka luka luka tu wan": 78,
19-
"wan": 1,
20-
"tu": 2,
21-
"tu wan": 3,
22-
"tu tu": 4,
23-
"luka": 5,
24-
"tu tu wan": 5,
25-
"luka wan": 6,
26-
"mute": 20,
27-
"luka luka luka luka": 20,
28-
"mute luka luka luka wan": 36,
29-
"ale": 100,
30-
"mute mute mute mute mute": 100,
31-
"ale ale ale": 300,
32-
"wan ale": 100,
33-
"tu wan ale": 300,
34-
"luka luka ale": 1000,
35-
"wan ale ale": 10000,
36-
"mute ale mute tu tu": 2024,
37-
}));
3815
Deno.test("numeral translation", () => {
16+
const NUMBER_TESTS = new Map(Object.entries({
17+
"tu tu tu wan": 7,
18+
"luka tu": 7,
19+
"mute mute mute luka luka luka tu wan": 78,
20+
"wan": 1,
21+
"tu": 2,
22+
"tu wan": 3,
23+
"tu tu": 4,
24+
"luka": 5,
25+
"tu tu wan": 5,
26+
"luka wan": 6,
27+
"mute": 20,
28+
"luka luka luka luka": 20,
29+
"mute luka luka luka wan": 36,
30+
"ale": 100,
31+
"mute mute mute mute mute": 100,
32+
"ale ale ale": 300,
33+
"wan ale": 100,
34+
"tu wan ale": 300,
35+
"luka luka ale": 1000,
36+
"wan ale ale": 10000,
37+
"mute ale mute tu tu": 2024,
38+
}));
3939
for (const [tokiPona, expected] of NUMBER_TESTS) {
4040
const numbers = number(tokiPona.trim().split(" ")).unwrap();
4141
assertArrayIncludes(numbers, [expected], `Error at "${tokiPona}"`);

telo_misikeke/update.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ async function buildCode(
2929
destination: URL,
3030
exportItems: ReadonlyArray<string>,
3131
): Promise<void> {
32-
const response = assertOk(await retry(() => fetch(source)));
32+
const response = await retry(() => fetch(source));
33+
if (!response.ok) {
34+
throw new Error(`unable to fetch ${response.url} (${response.statusText})`);
35+
}
3336
const rawCode = await response.text();
3437
const withoutCjs = rawCode.replaceAll(COMMONJS_EXPORT, "");
3538
if (withoutCjs.includes("module.exports")) {
@@ -60,13 +63,3 @@ if (import.meta.main) {
6063
// deno-lint-ignore no-console
6164
console.log("Updated telo misikeke.");
6265
}
63-
function assertOk(response: Response): Response {
64-
if (!response.ok) {
65-
const { url, status, statusText } = response;
66-
throw new Error(
67-
`unable to fetch ${url} (${status} ${statusText})`,
68-
);
69-
} else {
70-
return response;
71-
}
72-
}

0 commit comments

Comments
 (0)