WIP hack combinator to try and improve error messages.
This commit is contained in:
parent
61d967b6f1
commit
b287b1d9b6
1 changed files with 23 additions and 0 deletions
23
peg.js
23
peg.js
|
@ -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);
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue