Improved error reporting for predicates a bit.

redux
David Majda 14 years ago
parent 69906e9730
commit 452243d450

@ -436,12 +436,16 @@ PEG.Grammar.ZeroOrMore.prototype.compile = function(resultVar) {
};
PEG.Grammar.NotPredicate.prototype.compile = function(resultVar) {
var savedPosVar = PEG.Compiler.generateUniqueIdentifier("savedPos");
var expressionResultVar = PEG.Compiler.generateUniqueIdentifier("result");
var savedPosVar = PEG.Compiler.generateUniqueIdentifier("savedPos");
var savedReportMatchFailuresVar = PEG.Compiler.generateUniqueIdentifier("savedReportMatchFailuresVar");
var expressionResultVar = PEG.Compiler.generateUniqueIdentifier("result");
return PEG.Compiler.formatCode(
"var ${savedPosVar} = this._pos;",
"var ${savedReportMatchFailuresVar} = context.reportMatchFailures;",
"context.reportMatchFailures = false;",
"${expressionCode}",
"context.reportMatchFailures = ${savedReportMatchFailuresVar};",
"if (${expressionResultVar} === null) {",
" var ${resultVar} = '';",
"} else {",
@ -449,10 +453,11 @@ PEG.Grammar.NotPredicate.prototype.compile = function(resultVar) {
" this._pos = ${savedPosVar};",
"}",
{
expressionCode: this._expression.compile(expressionResultVar),
expressionResultVar: expressionResultVar,
savedPosVar: savedPosVar,
resultVar: resultVar
expressionCode: this._expression.compile(expressionResultVar),
expressionResultVar: expressionResultVar,
savedPosVar: savedPosVar,
savedReportMatchFailuresVar: savedReportMatchFailuresVar,
resultVar: resultVar
}
);
};

@ -379,6 +379,20 @@ test("error messages", function() {
'Expected "c" but "d" found.'
);
var notPredicateParser = PEG.buildParser('start: !"a" "b"');
doesNotParseWithMessage(
notPredicateParser,
"c",
'Expected "b" but "c" found.'
);
var andPredicateParser = PEG.buildParser('start: &"a" [a-b]');
doesNotParseWithMessage(
andPredicateParser,
"c",
'Expected end of input but "c" found.'
);
var emptyParser = PEG.buildParser('start: ');
doesNotParseWithMessage(
emptyParser,

Loading…
Cancel
Save