Attempt to merge applicable multiple expecteds at the same spot.

Avoid counting a successful capture as an error at the current locaiton.
This commit is contained in:
Tangent Wantwight 2023-08-04 20:28:26 -04:00
parent 0c90b45d3a
commit 1c04d8dece

8
peg.js
View file

@ -79,7 +79,7 @@ Peg.Regex = function (regex) {
regex.lastIndex = index;
const matches = regex.exec(source);
return matches
? [[matches, regex.lastIndex], regex.lastIndex, pattern.expectLabel]
? [[matches, regex.lastIndex], -1, pattern.expectLabel]
: [null, index, pattern.expectLabel];
}).expects(regex.source);
return pattern;
@ -105,6 +105,8 @@ Peg.Choose = function (...patterns) {
} else if (furthest > furthestFound) {
furthestFound = furthest;
furthestExpected = expected;
} else if (furthest == furthestFound) {
furthestExpected = furthestExpected + " | " + expected;
}
}
return [null, furthestFound, furthestExpected];
@ -128,9 +130,11 @@ Peg.Sequence = function (...patterns) {
let furthestExpected = genericExpected;
for (const pattern of patterns) {
const [value, furthest, expected] = pattern(source, index);
if (furthest >= furthestFound) {
if (furthest > furthestFound) {
furthestFound = furthest;
furthestExpected = expected;
} else if (furthest == furthestFound) {
furthestExpected = furthestExpected + " | " + expected;
}
if (value == null) {
return [null, furthestFound, furthestExpected];