Skip to content

Commit 94c1636

Browse files
committed
remove unneeded ArrayResult.from calls
1 parent 1275c27 commit 94c1636

File tree

4 files changed

+70
-79
lines changed

4 files changed

+70
-79
lines changed

src/parser/parser_lib.ts

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { assert } from "@std/assert/assert";
22
import { MemoizationCacheResult, memoize } from "@std/cache/memoize";
33
import { ArrayResult, ArrayResultError } from "../array_result.ts";
44
import { Clearable, ClearableCacheSet, Lazy } from "../cache.ts";
5-
import { throwError } from "../../misc/misc.ts";
65

76
export type ValueRest<T> = Readonly<{ rest: string; value: T }>;
87
export type ParserResult<T> = ArrayResult<ValueRest<T>>;
@@ -213,60 +212,54 @@ export function matchCapture(
213212
description: string,
214213
): Parser<RegExpMatchArray> {
215214
const newRegex = new RegExp(`^${regex.source}`, regex.flags);
216-
return new Parser((src) =>
217-
ArrayResult.from(() => {
218-
const match = src.match(newRegex);
219-
if (match != null) {
220-
return new ArrayResult([{
221-
value: match,
222-
rest: src.slice(match[0].length),
223-
}]);
224-
} else {
225-
throw new UnexpectedError(describeSource(src), description);
226-
}
227-
})
228-
);
215+
return new Parser((src) => {
216+
const match = src.match(newRegex);
217+
if (match != null) {
218+
return new ArrayResult([{
219+
value: match,
220+
rest: src.slice(match[0].length),
221+
}]);
222+
} else {
223+
return new ArrayResult(
224+
new UnexpectedError(describeSource(src), description),
225+
);
226+
}
227+
});
229228
}
230229
export function match(regex: RegExp, description: string): Parser<string> {
231230
return matchCapture(regex, description).map(([matched]) => matched);
232231
}
233232
export function slice(length: number, description: string): Parser<string> {
234233
return new Parser((src) =>
235-
ArrayResult.from(() =>
236-
src.length >= length
237-
? new ArrayResult([{
238-
rest: src.slice(length),
239-
value: src.slice(0, length),
240-
}])
241-
: throwError(new UnexpectedError(describeSource(src), description))
242-
)
234+
src.length >= length
235+
? new ArrayResult([{
236+
rest: src.slice(length),
237+
value: src.slice(0, length),
238+
}])
239+
: new ArrayResult(new UnexpectedError(describeSource(src), description))
243240
);
244241
}
245242
export function matchString(
246243
match: string,
247244
description = `"${match}"`,
248245
): Parser<string> {
249246
return new Parser((src) =>
250-
ArrayResult.from(() =>
251-
src.length >= match.length && src.slice(0, match.length) === match
252-
? new ArrayResult([{
253-
rest: src.slice(match.length),
254-
value: match,
255-
}])
256-
: throwError(new UnexpectedError(describeSource(src), description))
257-
)
247+
src.length >= match.length && src.slice(0, match.length) === match
248+
? new ArrayResult([{
249+
rest: src.slice(match.length),
250+
value: match,
251+
}])
252+
: new ArrayResult(new UnexpectedError(describeSource(src), description))
258253
);
259254
}
260255
export const everything = new Parser((src) =>
261256
new ArrayResult([{ value: src, rest: "" }])
262257
);
263258
export const character = match(/./us, "character");
264259
export const end = new Parser((src) =>
265-
ArrayResult.from(() =>
266-
src === ""
267-
? new ArrayResult([{ value: null, rest: "" }])
268-
: throwError(new UnexpectedError(describeSource(src), "end of text"))
269-
)
260+
src === ""
261+
? new ArrayResult([{ value: null, rest: "" }])
262+
: new ArrayResult(new UnexpectedError(describeSource(src), "end of text"))
270263
);
271264
export function withSource<T>(
272265
parser: Parser<T>,

src/translator/adjective.ts

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as Dictionary from "../../dictionary/type.ts";
22
import { ArrayResult } from "../array_result.ts";
3-
import { nullableAsArray, throwError } from "../../misc/misc.ts";
3+
import { nullableAsArray } from "../../misc/misc.ts";
44
import * as TokiPona from "../parser/ast.ts";
55
import * as English from "./ast.ts";
66
import { UntranslatableError } from "./error.ts";
@@ -55,27 +55,24 @@ export function compoundAdjective(
5555
}>,
5656
): ArrayResult<English.AdjectivePhrase & { type: "compound" }> {
5757
const { adjectives, reduplicationCount, emphasis } = options;
58-
return ArrayResult.from(() =>
59-
reduplicationCount === 1
60-
? ArrayResult.combine(
61-
...adjectives
62-
.map((definition) =>
63-
adjective({ definition, reduplicationCount: 1, emphasis })
64-
),
65-
)
66-
.map((adjective) => ({
67-
type: "compound",
68-
conjunction: "and",
69-
adjective,
70-
emphasis: false,
71-
}))
72-
: throwError(
73-
new UntranslatableError(
74-
"reduplication",
75-
"compound adjective",
58+
if (reduplicationCount === 1) {
59+
return ArrayResult.combine(
60+
...adjectives
61+
.map((definition) =>
62+
adjective({ definition, reduplicationCount: 1, emphasis })
7663
),
77-
)
78-
);
64+
)
65+
.map((adjective) => ({
66+
type: "compound",
67+
conjunction: "and",
68+
adjective,
69+
emphasis: false,
70+
}));
71+
} else {
72+
return new ArrayResult(
73+
new UntranslatableError("reduplication", "compound adjective"),
74+
);
75+
}
7976
}
8077
export function rankAdjective(kind: Dictionary.AdjectiveType): number {
8178
return [

src/translator/number.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { sumOf } from "@std/collections/sum-of";
22
import { ArrayResult } from "../array_result.ts";
33
import { dictionary } from "../dictionary.ts";
4-
import { nullableAsArray, throwError } from "../../misc/misc.ts";
4+
import { nullableAsArray } from "../../misc/misc.ts";
55
import { FilteredError } from "./error.ts";
66

77
function singleNumber(word: string): ArrayResult<number> {
@@ -64,16 +64,16 @@ function nasinNanpaPona(number: ReadonlyArray<number>): null | number {
6464
}
6565
}
6666
function combineNumbers(numbers: ReadonlyArray<number>): ArrayResult<number> {
67-
return ArrayResult.from(() =>
68-
numbers.length === 1 || !numbers.includes(0)
69-
? ArrayResult.concat(
70-
ArrayResult.from(() =>
71-
new ArrayResult(nullableAsArray(nasinNanpaPona(numbers)))
72-
),
73-
ArrayResult.from(() => new ArrayResult([regularNumber(numbers)])),
74-
)
75-
: throwError(new FilteredError('"ala" along with other numeral'))
76-
);
67+
if (numbers.length === 1 || !numbers.includes(0)) {
68+
return ArrayResult.concat(
69+
ArrayResult.from(() =>
70+
new ArrayResult(nullableAsArray(nasinNanpaPona(numbers)))
71+
),
72+
ArrayResult.from(() => new ArrayResult([regularNumber(numbers)])),
73+
);
74+
} else {
75+
return new ArrayResult(new FilteredError('"ala" along with other numeral'));
76+
}
7777
}
7878
export function number(number: ReadonlyArray<string>): ArrayResult<number> {
7979
return ArrayResult.combine(...number.map(singleNumber))

src/translator/predicate.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ArrayResult } from "../array_result.ts";
2-
import { nullableAsArray, throwError } from "../../misc/misc.ts";
2+
import { nullableAsArray } from "../../misc/misc.ts";
33
import * as TokiPona from "../parser/ast.ts";
44
import { AdjectiveWithInWay } from "./adjective.ts";
55
import * as English from "./ast.ts";
@@ -127,17 +127,18 @@ function associatedPredicate(
127127
object: null | PhraseTranslation,
128128
preposition: ReadonlyArray<English.Preposition>,
129129
): ArrayResult<PartialCompoundVerb> {
130-
return ArrayResult.from(() => {
131-
const verbObject = object == null
132-
? new ArrayResult([phraseAsVerb(predicate)])
133-
: object.type === "noun"
134-
? predicateVerb(predicate, object.noun)
135-
: throwError(new UntranslatableError(object.type, "object"));
136-
return verbObject.map((verbObject) => ({
137-
...verbObject,
138-
preposition: [...verbObject.preposition, ...preposition],
139-
}));
140-
});
130+
let verbObject: ArrayResult<PartialCompoundVerb>;
131+
if (object == null) {
132+
verbObject = new ArrayResult([phraseAsVerb(predicate)]);
133+
} else if (object.type === "noun") {
134+
verbObject = predicateVerb(predicate, object.noun);
135+
} else {
136+
return new ArrayResult(new UntranslatableError(object.type, "object"));
137+
}
138+
return verbObject.map((verbObject) => ({
139+
...verbObject,
140+
preposition: [...verbObject.preposition, ...preposition],
141+
}));
141142
}
142143
export function predicate(
143144
tokiPonaPredicate: TokiPona.Predicate,

0 commit comments

Comments
 (0)