Skip to content

Commit 30e351d

Browse files
committed
rename
1 parent 34f9667 commit 30e351d

File tree

7 files changed

+71
-60
lines changed

7 files changed

+71
-60
lines changed

dictionary/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ const head = sequence(all(tokiPonaWord.skip(comma)), tokiPonaWord)
480480
.skip(colon)
481481
.map(([init, last]) => [...init, last]);
482482
const entry = withSource(spaces.with(all(definition)))
483-
.map(([definitions, src]) => ({ definitions, src: src.trimEnd() }));
483+
.map(([definitions, source]) => ({ definitions, source: source.trimEnd() }));
484484
const dictionaryParser = spaces
485485
.with(all(sequence(head, entry)))
486486
.skip(end)

dictionary/test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { assertMatch } from "@std/assert/match";
44
import { dictionary } from "./dictionary.ts";
55

66
Deno.test("definition source has leading space", () => {
7-
for (const [word, { src }] of dictionary) {
8-
assertMatch(src, /^\s/, `Error at ${word}`);
7+
for (const [word, { source }] of dictionary) {
8+
assertMatch(source, /^\s/, `Error at ${word}`);
99
}
1010
});

dictionary/type.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,6 @@ export type Definition =
9494
| Readonly<{ type: "interjection"; interjection: string }>;
9595
export type Entry = Readonly<{
9696
definitions: ReadonlyArray<Definition>;
97-
src: string;
97+
source: string;
9898
}>;
9999
export type Dictionary = Map<string, Entry>;

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ function main(): void {
253253
function importWord(): void {
254254
const word = importWordTextBox.value.trim();
255255
if (/^[a-z][a-zA-Z]*$/.test(word)) {
256-
const definitions = dictionary.get(word)?.src;
256+
const definitions = dictionary.get(word)?.source;
257257
if (definitions != null) {
258258
displayToCustomDictionary(`${word}:${definitions}`);
259259
} else {

src/parser/parser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -709,8 +709,8 @@ const sentence = choice<Sentence>(
709709
.filter(filter(SENTENCE_RULE));
710710
export const parse = spaces
711711
.with(
712-
lookAhead(everything.filter((src) =>
713-
src.trimEnd().length <= 500 ||
712+
lookAhead(everything.filter((source) =>
713+
source.trimEnd().length <= 500 ||
714714
throwError(new UnrecognizedError("long text"))
715715
)),
716716
)

src/parser/parser_lib.ts

Lines changed: 61 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ export type ValueRest<T> = Readonly<{ rest: string; value: T }>;
77
export type ParserResult<T> = ArrayResult<ValueRest<T>>;
88

99
export class Parser<T> {
10-
readonly nonMemoizedParser: (src: string) => ParserResult<T>;
11-
readonly rawParser: (src: string) => ParserResult<T>;
10+
readonly nonMemoizedParser: (source: string) => ParserResult<T>;
11+
readonly rawParser: (source: string) => ParserResult<T>;
1212
static cache: null | ClearableCacheSet = null;
13-
constructor(parser: (src: string) => ParserResult<T>) {
13+
constructor(parser: (source: string) => ParserResult<T>) {
1414
this.nonMemoizedParser = parser;
1515
if (Parser.cache != null) {
1616
const cache: Map<string, MemoizationCacheResult<ParserResult<T>>> =
@@ -21,35 +21,35 @@ export class Parser<T> {
2121
this.rawParser = this.nonMemoizedParser;
2222
}
2323
}
24-
parser(): (src: string) => ArrayResult<T> {
24+
parser(): (source: string) => ArrayResult<T> {
2525
const { rawParser } = this;
26-
return (src) => rawParser(src).map(({ value }) => value);
26+
return (source) => rawParser(source).map(({ value }) => value);
2727
}
2828
map<U>(mapper: (value: T) => U): Parser<U> {
2929
const { nonMemoizedParser: unmemoizedParser } = this;
30-
return new Parser((src) =>
31-
unmemoizedParser(src)
30+
return new Parser((source) =>
31+
unmemoizedParser(source)
3232
.map(({ value, rest }) => ({ value: mapper(value), rest }))
3333
);
3434
}
3535
filter(mapper: (value: T) => boolean): Parser<T> {
3636
const { nonMemoizedParser: unmemoizedParser } = this;
37-
return new Parser((src) =>
38-
unmemoizedParser(src).filter(({ value }) => mapper(value))
37+
return new Parser((source) =>
38+
unmemoizedParser(source).filter(({ value }) => mapper(value))
3939
);
4040
}
4141
then<U>(mapper: (value: T) => Parser<U>): Parser<U> {
4242
const { cache } = Parser;
4343
const { nonMemoizedParser: unmemoizedParser } = this;
44-
return new Parser((src) => {
45-
const parser = Parser.inContext(() => unmemoizedParser(src), cache);
44+
return new Parser((source) => {
45+
const parser = Parser.inContext(() => unmemoizedParser(source), cache);
4646
return parser.flatMap(({ value, rest }) => mapper(value).rawParser(rest));
4747
});
4848
}
4949
sort(comparer: (left: T, right: T) => number): Parser<T> {
5050
const { nonMemoizedParser: unmemoizedParser } = this;
51-
return new Parser((src) =>
52-
unmemoizedParser(src).sort((left, right) =>
51+
return new Parser((source) =>
52+
unmemoizedParser(source).sort((left, right) =>
5353
comparer(left.value, right.value)
5454
)
5555
);
@@ -99,31 +99,36 @@ export function error(error: ArrayResultError): Parser<never> {
9999
return new Parser(() => new ArrayResult(error));
100100
}
101101
export const empty = new Parser<never>(() => new ArrayResult());
102-
export const nothing = new Parser((src) =>
103-
new ArrayResult([{ value: null, rest: src }])
102+
export const nothing = new Parser((source) =>
103+
new ArrayResult([{ value: null, rest: source }])
104104
);
105105
export const emptyArray = nothing.map(() => []);
106106
export function lookAhead<T>(parser: Parser<T>): Parser<T> {
107-
return new Parser((src) =>
108-
parser.nonMemoizedParser(src).map(({ value }) => ({ value, rest: src }))
107+
return new Parser((source) =>
108+
parser.nonMemoizedParser(source).map(({ value }) => ({
109+
value,
110+
rest: source,
111+
}))
109112
);
110113
}
111114
export function lazy<T>(parser: () => Parser<T>): Parser<T> {
112115
const { cache } = Parser;
113116
if (Parser.cache != null) {
114117
const cachedParser = new Lazy(() => Parser.inContext(parser, cache));
115118
Parser.addToCache(cachedParser);
116-
return new Parser((src) => cachedParser.getValue().nonMemoizedParser(src));
119+
return new Parser((source) =>
120+
cachedParser.getValue().nonMemoizedParser(source)
121+
);
117122
} else {
118-
return new Parser((src) =>
119-
Parser.inContext(parser, cache).nonMemoizedParser(src)
123+
return new Parser((source) =>
124+
Parser.inContext(parser, cache).nonMemoizedParser(source)
120125
);
121126
}
122127
}
123128
export function choice<T>(...choices: ReadonlyArray<Parser<T>>): Parser<T> {
124129
assert(choices.length > 1, "`choice` called with less than 2 arguments");
125-
return new Parser((src) =>
126-
new ArrayResult(choices).flatMap((parser) => parser.rawParser(src))
130+
return new Parser((source) =>
131+
new ArrayResult(choices).flatMap((parser) => parser.rawParser(source))
127132
);
128133
}
129134
export function choiceOnlyOne<T>(
@@ -135,10 +140,10 @@ export function choiceOnlyOne<T>(
135140
);
136141
return choices.reduceRight(
137142
(right, left) =>
138-
new Parser((src) => {
139-
const arrayResult = left.rawParser(src);
143+
new Parser((source) => {
144+
const arrayResult = left.rawParser(source);
140145
if (arrayResult.isError()) {
141-
return ArrayResult.concat(arrayResult, right.rawParser(src));
146+
return ArrayResult.concat(arrayResult, right.rawParser(source));
142147
} else {
143148
return arrayResult;
144149
}
@@ -192,13 +197,13 @@ export function allAtLeastOnce<T>(parser: Parser<T>): Parser<ReadonlyArray<T>> {
192197
export function count(parser: Parser<{ length: number }>): Parser<number> {
193198
return parser.map(({ length }) => length);
194199
}
195-
function describeSource(src: string): string {
196-
if (src === "") {
200+
function describeSource(source: string): string {
201+
if (source === "") {
197202
return "end of text";
198203
} else {
199-
const [token] = src.match(/\S*/)!;
204+
const [token] = source.match(/\S*/)!;
200205
if (token === "") {
201-
if (/^\r?\n/.test(src)) {
206+
if (/^\r?\n/.test(source)) {
202207
return "newline";
203208
} else {
204209
return "space";
@@ -213,16 +218,16 @@ export function matchCapture(
213218
description: string,
214219
): Parser<RegExpMatchArray> {
215220
const newRegex = new RegExp(`^${regex.source}`, regex.flags);
216-
return new Parser((src) => {
217-
const match = src.match(newRegex);
221+
return new Parser((source) => {
222+
const match = source.match(newRegex);
218223
if (match != null) {
219224
return new ArrayResult([{
220225
value: match,
221-
rest: src.slice(match[0].length),
226+
rest: source.slice(match[0].length),
222227
}]);
223228
} else {
224229
return new ArrayResult(
225-
new UnexpectedError(describeSource(src), description),
230+
new UnexpectedError(describeSource(source), description),
226231
);
227232
}
228233
});
@@ -231,43 +236,49 @@ export function match(regex: RegExp, description: string): Parser<string> {
231236
return matchCapture(regex, description).map(([matched]) => matched);
232237
}
233238
export function slice(length: number, description: string): Parser<string> {
234-
return new Parser((src) =>
235-
src.length >= length
239+
return new Parser((source) =>
240+
source.length >= length
236241
? new ArrayResult([{
237-
rest: src.slice(length),
238-
value: src.slice(0, length),
242+
rest: source.slice(length),
243+
value: source.slice(0, length),
239244
}])
240-
: new ArrayResult(new UnexpectedError(describeSource(src), description))
245+
: new ArrayResult(
246+
new UnexpectedError(describeSource(source), description),
247+
)
241248
);
242249
}
243250
export function matchString(
244251
match: string,
245252
description = `"${match}"`,
246253
): Parser<string> {
247-
return new Parser((src) =>
248-
src.length >= match.length && src.slice(0, match.length) === match
254+
return new Parser((source) =>
255+
source.length >= match.length && source.slice(0, match.length) === match
249256
? new ArrayResult([{
250-
rest: src.slice(match.length),
257+
rest: source.slice(match.length),
251258
value: match,
252259
}])
253-
: new ArrayResult(new UnexpectedError(describeSource(src), description))
260+
: new ArrayResult(
261+
new UnexpectedError(describeSource(source), description),
262+
)
254263
);
255264
}
256-
export const everything = new Parser((src) =>
257-
new ArrayResult([{ value: src, rest: "" }])
265+
export const everything = new Parser((source) =>
266+
new ArrayResult([{ value: source, rest: "" }])
258267
);
259268
export const character = match(/./us, "character");
260-
export const end = new Parser((src) =>
261-
src === ""
269+
export const end = new Parser((source) =>
270+
source === ""
262271
? new ArrayResult([{ value: null, rest: "" }])
263-
: new ArrayResult(new UnexpectedError(describeSource(src), "end of text"))
272+
: new ArrayResult(
273+
new UnexpectedError(describeSource(source), "end of text"),
274+
)
264275
);
265276
export function withSource<T>(
266277
parser: Parser<T>,
267278
): Parser<readonly [value: T, source: string]> {
268-
return new Parser((src) =>
269-
parser.nonMemoizedParser(src).map(({ value, rest }) => ({
270-
value: [value, src.slice(0, src.length - rest.length)],
279+
return new Parser((source) =>
280+
parser.nonMemoizedParser(source).map(({ value, rest }) => ({
281+
value: [value, source.slice(0, source.length - rest.length)],
271282
rest,
272283
}))
273284
);

src/settings_frontend.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ const UPDATERS: Readonly<{ [K in keyof Settings]: Updater<Settings[K]> }> = {
5454
};
5555
const KEYS = Object.keys(UPDATERS) as ReadonlyArray<keyof Settings>;
5656
function loadOneFromLocalStorage<T extends keyof Settings>(key: T): void {
57-
const src = localStorage.getItem(key);
58-
if (src != null) {
59-
settings[key] = UPDATERS[key].parse(src) ?? defaultSettings[key];
57+
const source = localStorage.getItem(key);
58+
if (source != null) {
59+
settings[key] = UPDATERS[key].parse(source) ?? defaultSettings[key];
6060
} else {
6161
settings[key] = defaultSettings[key];
6262
}

0 commit comments

Comments
 (0)