Choose() returns the expected message from the alternative that made the furthest progress, if any did.

This commit is contained in:
Tangent Wantwight 2023-08-04 02:18:07 -04:00
parent 3bb125dd8c
commit 8214f05fe3

4
peg.js
View file

@ -90,15 +90,17 @@ Peg.Choose = function (...patterns) {
const expected = patterns.map((pattern) => pattern.expectLabel).join(" | ");
return Peg.WrapPattern(function (source, index) {
let furthest = index;
let furthestExpected = expected;
for (const pattern of patterns) {
const match = pattern(source, index);
if (match[0]) {
return match;
} else if (match[1] > furthest) {
furthest = match[1];
furthestExpected = match[2];
}
}
return [false, furthest, expected];
return [false, furthest, furthestExpected];
}).expects(expected);
};