Skip to content

Commit 6e1d25e

Browse files
committed
improve error messages
1 parent e58de37 commit 6e1d25e

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

src/parser/parser_lib.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -297,21 +297,18 @@ export function checkedAsWhole<T>(parser: Parser<T>): CheckedParser<T> {
297297
export function choiceWithCheck<T>(
298298
...choices: ReadonlyArray<CheckedParser<T>>
299299
): Parser<T> {
300-
return choices.reduceRight(
301-
(right: Parser<T>, { check, parser }) =>
302-
new Parser((position) => {
303-
const arrayResult = check.rawParser(position);
304-
if (arrayResult.isError()) {
305-
return ArrayResult.concat(
306-
arrayResult as ArrayResult<never>,
307-
right.rawParser(position),
308-
);
309-
} else {
310-
return parser.rawParser(position);
311-
}
312-
}),
313-
empty,
314-
);
300+
return new Parser((position) => {
301+
const errors: Array<ArrayResultError> = [];
302+
for (const { check, parser } of choices) {
303+
const result = check.rawParser(position);
304+
if (result.isError()) {
305+
errors.push(...result.errors);
306+
} else {
307+
return parser.rawParser(position);
308+
}
309+
}
310+
return ArrayResult.errors(errors);
311+
});
315312
}
316313
export function optionalWithCheck<T>(
317314
parser: CheckedParser<T>,

0 commit comments

Comments
 (0)