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

@ -379,6 +379,20 @@ test("error messages", function() {
'Expected "c" but "d" found.' '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: '); var emptyParser = PEG.buildParser('start: ');
doesNotParseWithMessage( doesNotParseWithMessage(
emptyParser, emptyParser,

Loading…
Cancel
Save