Choose() returns the expected message from the alternative that made the furthest progress, if any did.
This commit is contained in:
parent
3bb125dd8c
commit
8214f05fe3
1 changed files with 3 additions and 1 deletions
4
peg.js
4
peg.js
|
@ -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);
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue