WIP hack combinator to try and improve error messages.

This commit is contained in:
Tangent Wantwight 2023-08-04 17:43:11 -04:00
parent 61d967b6f1
commit b287b1d9b6

23
peg.js
View file

@ -183,3 +183,26 @@ Peg.End = () => {
}).expects("<eof>");
return end;
};
/**
* Creates a pattern that never succeeds, but reports how far its wrapped pattern could match before failing.
*
* This is a hack, meant to improve error messages after an AtLeast(). Maybe this can be removed by patterns returning both success and failure
*
* Never consumes input, and fails with zero length if the pattern succeeds.
* @param {Peg.Pattern<unknown>} pattern
* @return {Peg.Pattern<never>}
*/
Peg.Hint = function (pattern) {
return /** @type {Peg.Pattern<never>} */ (
Peg.WrapPattern(function (source, index) {
const match = pattern(source, index);
if (match[0]) {
console.log("oops match", match);
return [false, index, pattern.expectLabel];
} else {
return match;
}
})
).expects(pattern.expectLabel);
};