diff --git a/lib/compiler.js b/lib/compiler.js index 22d3156..12ee525 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -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 } ); }; diff --git a/test/compiler-test.js b/test/compiler-test.js index 9d3f711..9c3b536 100644 --- a/test/compiler-test.js +++ b/test/compiler-test.js @@ -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,