Skip to content

Commit 45567e3

Browse files
committed
reorganize code
1 parent 0846800 commit 45567e3

12 files changed

Lines changed: 36 additions & 28 deletions

File tree

src/main.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
import { closestString } from "@std/text/closest-string";
44
import BrowserDetector from "browser-dtector";
5-
import { dictionary } from "./dictionary/dictionary.ts";
6-
import { parseDictionary } from "./dictionary/parser.ts";
7-
import { Dictionary } from "./dictionary/type.ts";
85
import PROJECT_DATA from "../project_data.json" with { type: "json" };
96
import { extractResultError, Result, ResultError } from "./compound.ts";
10-
import { loadCustomDictionary } from "./dictionary/dictionary.ts";
7+
import { dictionary, loadCustomDictionary } from "./dictionary/dictionary.ts";
8+
import { parseDictionary } from "./dictionary/parser.ts";
9+
import { Dictionary } from "./dictionary/type.ts";
1110
import {
1211
assertQuotaExceededError,
1312
checkLocalStorage,

src/parser/parser_lib.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,33 +35,22 @@ export class Parser<T> {
3535
}
3636
return this[rawParser](0).map(({ value }) => value);
3737
}
38-
#rawMap<U>(mapper: (value: T) => U): Parser<U> {
39-
return new Parser((input) =>
40-
this[rawParser](input)
41-
.map(({ value, length }): ValueLength<U> => ({
42-
value: mapper(value),
43-
length,
44-
}))
45-
);
46-
}
38+
4739
map<U>(mapper: (value: T) => U): Parser<U> {
4840
return withPosition(this)
4941
.#rawMap((value) =>
5042
withPositionedError(() => mapper(value.value), value)
5143
);
5244
}
53-
#rawFilter(mapper: (value: T) => boolean): Parser<T> {
54-
return new Parser((input) =>
55-
this[rawParser](input).filter(({ value }) => mapper(value))
56-
);
57-
}
45+
5846
filter(mapper: (value: T) => boolean): Parser<T> {
5947
return withPosition(this)
6048
.#rawFilter((value) =>
6149
withPositionedError(() => mapper(value.value), value)
6250
)
6351
.map(({ value }) => value);
6452
}
53+
6554
then<U>(mapper: (value: T) => Parser<U>): Parser<U> {
6655
return new Parser((position) =>
6756
this[rawParser](position)
@@ -75,21 +64,41 @@ export class Parser<T> {
7564
)
7665
);
7766
}
67+
7868
sort(comparer: (left: T, right: T) => number): Parser<T> {
7969
return new Parser((input) =>
8070
this[rawParser](input)
8171
.sort((left, right) => comparer(left.value, right.value))
8272
);
8373
}
74+
8475
sortBy(mapper: (value: T) => number): Parser<T> {
8576
return this.sort((left, right) => mapper(left) - mapper(right));
8677
}
78+
8779
with<U>(parser: Parser<U>): Parser<U> {
8880
return sequence(this, parser).map(([_, arrayResult]) => arrayResult);
8981
}
82+
9083
skip<U>(parser: Parser<U>): Parser<T> {
9184
return sequence(this, parser).map(([arrayResult]) => arrayResult);
9285
}
86+
87+
#rawMap<U>(mapper: (value: T) => U): Parser<U> {
88+
return new Parser((input) =>
89+
this[rawParser](input)
90+
.map(({ value, length }): ValueLength<U> => ({
91+
value: mapper(value),
92+
length,
93+
}))
94+
);
95+
}
96+
97+
#rawFilter(mapper: (value: T) => boolean): Parser<T> {
98+
return new Parser((input) =>
99+
this[rawParser](input).filter(({ value }) => mapper(value))
100+
);
101+
}
93102
}
94103
export type Position = Readonly<{ position: number; length: number }>;
95104
export class PositionedError extends ResultError {

src/translator/adjective.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as Dictionary from "../dictionary/type.ts";
21
import { IterableResult } from "../compound.ts";
2+
import * as Dictionary from "../dictionary/type.ts";
33
import { mapNullable, nullableAsArray } from "../misc/misc.ts";
44
import * as TokiPona from "../parser/ast.ts";
55
import { UntranslatableError } from "../translator2/error.ts";

src/translator/as_string.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as Dictionary from "../dictionary/type.ts";
21
import { IterableResult } from "../compound.ts";
2+
import * as Dictionary from "../dictionary/type.ts";
33
import { adjective, compoundAdjective } from "./adjective.ts";
44
import * as EnglishComposer from "./composer.ts";
55
import { noun, simpleNounForms } from "./noun.ts";

src/translator/determiner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { zip } from "@std/collections/zip";
2-
import * as Dictionary from "../dictionary/type.ts";
32
import { IterableResult } from "../compound.ts";
3+
import * as Dictionary from "../dictionary/type.ts";
44
import { FilteredError } from "../translator2/error.ts";
55
import { word } from "../translator2/word.ts";
66
import * as English from "./ast.ts";

src/translator/noun.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as Dictionary from "../dictionary/type.ts";
21
import { IterableResult } from "../compound.ts";
2+
import * as Dictionary from "../dictionary/type.ts";
33
import { mapNullable, nullableAsArray } from "../misc/misc.ts";
44
import { settings } from "../settings.ts";
55
import { condense } from "../translator2/misc.ts";

src/translator/pronoun.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as Dictionary from "../dictionary/type.ts";
21
import { IterableResult } from "../compound.ts";
2+
import * as Dictionary from "../dictionary/type.ts";
33
import { word } from "../translator2/word.ts";
44
import * as English from "./ast.ts";
55
import { fromNounForms, PartialNoun } from "./noun.ts";

src/translator/verb.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as Dictionary from "../dictionary/type.ts";
21
import { IterableResult } from "../compound.ts";
2+
import * as Dictionary from "../dictionary/type.ts";
33
import { nullableAsArray } from "../misc/misc.ts";
44
import { settings } from "../settings.ts";
55
import { FilteredError } from "../translator2/error.ts";

src/translator/word_unit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Definition } from "../dictionary/type.ts";
21
import { IterableResult } from "../compound.ts";
32
import { dictionary } from "../dictionary/dictionary.ts";
3+
import { Definition } from "../dictionary/type.ts";
44
import * as TokiPona from "../parser/ast.ts";
55
import { TranslationTodoError } from "../translator2/error.ts";
66
import { number, numberAsText } from "../translator2/number.ts";

src/translator2/adjective.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as Dictionary from "../dictionary/type.ts";
21
import { IterableResult } from "../compound.ts";
2+
import * as Dictionary from "../dictionary/type.ts";
33
import { nullableAsArray } from "../misc/misc.ts";
44
import * as TokiPona from "../parser/ast.ts";
55
import * as English from "./ast.ts";

0 commit comments

Comments
 (0)