Skip to content

Commit ed1b5e9

Browse files
committed
rename
1 parent ab2d995 commit ed1b5e9

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

src/parser/parser_lib.ts

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { assertGreaterOrEqual } from "@std/assert/greater-or-equal";
33
import { MemoizationCacheResult, memoize } from "@std/cache/memoize";
44
import { ArrayResult, ArrayResultError } from "../array_result.ts";
55

6-
type Source = Readonly<{ source: string; position: number }>;
6+
type Input = Readonly<{ source: string; position: number }>;
77
type ParserResult<T> = ArrayResult<Readonly<{ value: T; length: number }>>;
8-
type InnerParser<T> = (input: Source) => ParserResult<T>;
8+
type InnerParser<T> = (input: Input) => ParserResult<T>;
99

1010
let source = "";
1111
const allMemo: Set<WeakRef<SourceMemo<unknown>>> = new Set();
@@ -25,24 +25,24 @@ class SourceMemo<T> {
2525
constructor() {
2626
allMemo.add(new WeakRef(this));
2727
}
28-
set(key: Source, value: T): void {
28+
set(key: Input, value: T): void {
2929
if (source !== key.source) {
3030
source = key.source;
3131
clearCache();
3232
}
3333
this.#map.set(key.position, value);
3434
}
35-
get(key: Source): undefined | T {
35+
get(key: Input): undefined | T {
3636
if (source === key.source) {
3737
return this.#map.get(key.position);
3838
} else {
3939
return undefined;
4040
}
4141
}
42-
has(key: Source): boolean {
42+
has(key: Input): boolean {
4343
return source === key.source && this.#map.has(key.position);
4444
}
45-
delete(key: Source): void {
45+
delete(key: Input): void {
4646
if (source === key.source) {
4747
this.#map.delete(key.position);
4848
}
@@ -56,41 +56,41 @@ export class Parser<T> {
5656
constructor(parser: InnerParser<T>) {
5757
this.rawParser = memoize<
5858
InnerParser<T>,
59-
Source,
59+
Input,
6060
SourceMemo<MemoizationCacheResult<ParserResult<T>>>
6161
>(
62-
(source) => {
63-
assertGreaterOrEqual(source.source.length, source.position);
64-
return parser(source);
62+
(input) => {
63+
assertGreaterOrEqual(input.source.length, input.position);
64+
return parser(input);
6565
},
6666
{ cache: new SourceMemo() },
6767
);
6868
}
6969
generateParser(): (source: string) => ArrayResult<T> {
70-
return (source) =>
71-
this.rawParser({ source, position: 0 })
70+
return (input) =>
71+
this.rawParser({ source: input, position: 0 })
7272
.map(({ value }) => value);
7373
}
7474
map<U>(mapper: (value: T) => U): Parser<U> {
75-
return new Parser((source) =>
76-
this.rawParser(source)
75+
return new Parser((input) =>
76+
this.rawParser(input)
7777
.map(({ value, length }) => ({ value: mapper(value), length }))
7878
);
7979
}
8080
filter(mapper: (value: T) => boolean): Parser<T> {
81-
return new Parser((source) =>
82-
this.rawParser(source).filter(({ value }) => mapper(value))
81+
return new Parser((input) =>
82+
this.rawParser(input).filter(({ value }) => mapper(value))
8383
);
8484
}
8585
then<U>(mapper: (value: T) => Parser<U>): Parser<U> {
86-
return new Parser((source) =>
86+
return new Parser((input) =>
8787
this
88-
.rawParser(source)
88+
.rawParser(input)
8989
.flatMap(({ value, length }) =>
9090
mapper(value)
9191
.rawParser({
92-
source: source.source,
93-
position: source.position + length,
92+
source: input.source,
93+
position: input.position + length,
9494
})
9595
.map(({ value, length: addedLength }) => ({
9696
value,
@@ -100,8 +100,8 @@ export class Parser<T> {
100100
);
101101
}
102102
sort(comparer: (left: T, right: T) => number): Parser<T> {
103-
return new Parser((source) =>
104-
this.rawParser(source)
103+
return new Parser((input) =>
104+
this.rawParser(input)
105105
.sort((left, right) => comparer(left.value, right.value))
106106
);
107107
}
@@ -136,22 +136,22 @@ export const nothing = new Parser(() =>
136136
);
137137
export const emptyArray = nothing.map(() => []);
138138
export function lookAhead<T>(parser: Parser<T>): Parser<T> {
139-
return new Parser((source) =>
140-
parser.rawParser(source)
139+
return new Parser((input) =>
140+
parser.rawParser(input)
141141
.map(({ value }) => ({ value, length: 0 }))
142142
);
143143
}
144144
export function lazy<T>(parser: () => Parser<T>): Parser<T> {
145-
return new Parser((source) => parser().rawParser(source));
145+
return new Parser((input) => parser().rawParser(input));
146146
}
147147
export function choice<T>(...choices: ReadonlyArray<Parser<T>>): Parser<T> {
148148
assertGreater(
149149
choices.length,
150150
1,
151151
"`choice` called with less than 2 arguments",
152152
);
153-
return new Parser((source) =>
154-
new ArrayResult(choices).flatMap((parser) => parser.rawParser(source))
153+
return new Parser((input) =>
154+
new ArrayResult(choices).flatMap((parser) => parser.rawParser(input))
155155
);
156156
}
157157
export function choiceOnlyOne<T>(
@@ -164,10 +164,10 @@ export function choiceOnlyOne<T>(
164164
);
165165
return choices.reduceRight(
166166
(right, left) =>
167-
new Parser((source) => {
168-
const arrayResult = left.rawParser(source);
167+
new Parser((input) => {
168+
const arrayResult = left.rawParser(input);
169169
if (arrayResult.isError()) {
170-
return ArrayResult.concat(arrayResult, right.rawParser(source));
170+
return ArrayResult.concat(arrayResult, right.rawParser(input));
171171
} else {
172172
return arrayResult;
173173
}
@@ -291,24 +291,24 @@ export const everything = new Parser(({ source, position }) =>
291291
}])
292292
);
293293
export const character = match(/./us, "character");
294-
export const end = new Parser((source) =>
295-
source.position === source.source.length
294+
export const end = new Parser((input) =>
295+
input.position === input.source.length
296296
? new ArrayResult([{ value: null, length: 0 }])
297297
: new ArrayResult(
298298
new UnexpectedError(
299-
describeSource(source.source.slice(source.position)),
299+
describeSource(input.source.slice(input.position)),
300300
"end of text",
301301
),
302302
)
303303
);
304304
export function withSource<T>(
305305
parser: Parser<T>,
306306
): Parser<readonly [value: T, source: string]> {
307-
return new Parser((source) =>
308-
parser.rawParser(source).map(({ value, length }) => ({
307+
return new Parser((input) =>
308+
parser.rawParser(input).map(({ value, length }) => ({
309309
value: [
310310
value,
311-
source.source.slice(source.position, source.position + length),
311+
input.source.slice(input.position, input.position + length),
312312
] as const,
313313
length,
314314
}))

0 commit comments

Comments
 (0)