Skip to content

Commit 3e5d1d1

Browse files
committed
simplify parser result
1 parent 124e4cb commit 3e5d1d1

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

src/parser/parser_lib.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { ArrayResult, ArrayResultError } from "../array_result.ts";
66
const CACHE_SIZE = 10;
77

88
type Source = Readonly<{ source: string; position: number }>;
9-
type ParserResult<T> = ArrayResult<Readonly<{ rest: Source; value: T }>>;
9+
type ParserResult<T> = ArrayResult<Readonly<{ value: T; size: number }>>;
1010
type InnerParser<T> = (input: Source) => ParserResult<T>;
1111

1212
class SourceMemo<T> {
@@ -47,7 +47,7 @@ export class Parser<T> {
4747
map<U>(mapper: (value: T) => U): Parser<U> {
4848
return new Parser((source) =>
4949
this.rawParser(source)
50-
.map(({ value, rest }) => ({ value: mapper(value), rest }))
50+
.map(({ value, size }) => ({ value: mapper(value), size }))
5151
);
5252
}
5353
filter(mapper: (value: T) => boolean): Parser<T> {
@@ -59,7 +59,12 @@ export class Parser<T> {
5959
return new Parser((source) =>
6060
this
6161
.rawParser(source)
62-
.flatMap(({ value, rest }) => mapper(value).rawParser(rest))
62+
.flatMap(({ value, size }) =>
63+
mapper(value).rawParser({
64+
source: source.source,
65+
position: source.position + size,
66+
})
67+
)
6368
);
6469
}
6570
sort(comparer: (left: T, right: T) => number): Parser<T> {
@@ -94,14 +99,14 @@ export function error(error: ArrayResultError): Parser<never> {
9499
return new Parser(() => new ArrayResult(error));
95100
}
96101
export const empty = new Parser<never>(() => new ArrayResult());
97-
export const nothing = new Parser((source) =>
98-
new ArrayResult([{ value: null, rest: source }])
102+
export const nothing = new Parser(() =>
103+
new ArrayResult([{ value: null, size: 0 }])
99104
);
100105
export const emptyArray = nothing.map(() => []);
101106
export function lookAhead<T>(parser: Parser<T>): Parser<T> {
102107
return new Parser((source) =>
103108
parser.rawParser(source)
104-
.map(({ value }) => ({ value, rest: source }))
109+
.map(({ value }) => ({ value, size: 0 }))
105110
);
106111
}
107112
export function lazy<T>(parser: () => Parser<T>): Parser<T> {
@@ -204,10 +209,7 @@ export function matchCapture(
204209
const sourceString = source.slice(position);
205210
const match = sourceString.match(newRegex);
206211
if (match != null) {
207-
return new ArrayResult([{
208-
value: match,
209-
rest: { source, position: position + match[0].length },
210-
}]);
212+
return new ArrayResult([{ value: match, size: match[0].length }]);
211213
} else {
212214
return new ArrayResult(
213215
new UnexpectedError(describeSource(sourceString), description),
@@ -228,8 +230,8 @@ export function matchString(
228230
source.slice(position, position + match.length) === match
229231
) {
230232
return new ArrayResult([{
231-
rest: { source, position: position + match.length },
232233
value: match,
234+
size: match.length,
233235
}]);
234236
} else {
235237
return new ArrayResult(
@@ -244,13 +246,13 @@ export function matchString(
244246
export const everything = new Parser(({ source, position }) =>
245247
new ArrayResult([{
246248
value: source.slice(position),
247-
rest: { source, position: source.length },
249+
size: source.length - position,
248250
}])
249251
);
250252
export const character = match(/./us, "character");
251253
export const end = new Parser((source) =>
252254
source.position === source.source.length
253-
? new ArrayResult([{ value: null, rest: source }])
255+
? new ArrayResult([{ value: null, size: 0 }])
254256
: new ArrayResult(
255257
new UnexpectedError(
256258
describeSource(source.source.slice(source.position)),
@@ -262,12 +264,12 @@ export function withSource<T>(
262264
parser: Parser<T>,
263265
): Parser<readonly [value: T, source: string]> {
264266
return new Parser((source) =>
265-
parser.rawParser(source).map(({ value, rest }) => ({
267+
parser.rawParser(source).map(({ value, size }) => ({
266268
value: [
267269
value,
268-
source.source.slice(source.position, rest.position),
270+
source.source.slice(source.position, source.position + size),
269271
] as const,
270-
rest,
272+
size,
271273
}))
272274
);
273275
}

0 commit comments

Comments
 (0)