Fix reported error position when part of the input is not consumed

Closes GH-48.
redux
David Majda 13 years ago
parent c90e7f369b
commit 8acea01525

@ -376,7 +376,7 @@ PEG.compiler.emitter = function(ast) {
' var column = 1;', ' var column = 1;',
' var seenCR = false;', ' var seenCR = false;',
' ', ' ',
' for (var i = 0; i < rightmostFailuresPos; i++) {', ' for (var i = 0; i < Math.max(pos, rightmostFailuresPos); i++) {',
' var ch = input.charAt(i);', ' var ch = input.charAt(i);',
' if (ch === "\\n") {', ' if (ch === "\\n") {',
' if (!seenCR) { line++; }', ' if (!seenCR) { line++; }',

@ -444,23 +444,30 @@ test("error messages", function() {
}); });
test("error positions", function() { test("error positions", function() {
var parser = PEG.buildParser([ var simpleParser = PEG.buildParser('start = "a"');
/* Regular match failure */
doesNotParseWithPos(simpleParser, "b", 1, 1);
/* Trailing input */
doesNotParseWithPos(simpleParser, "ab", 1, 2);
var digitsParser = PEG.buildParser([
'start = line (("\\r" / "\\n" / "\\u2028" / "\\u2029")+ line)*', 'start = line (("\\r" / "\\n" / "\\u2028" / "\\u2029")+ line)*',
'line = digits (" "+ digits)*', 'line = digits (" "+ digits)*',
'digits = digits:[0-9]+ { return digits.join(""); }' 'digits = digits:[0-9]+ { return digits.join(""); }'
].join("\n")); ].join("\n"));
doesNotParseWithPos(parser, "a", 1, 1); doesNotParseWithPos(digitsParser, "1\n2\n\n3\n\n\n4 5 x", 7, 5);
doesNotParseWithPos(parser, "1\n2\n\n3\n\n\n4 5 x", 7, 5);
/* Non-Unix newlines */ /* Non-Unix newlines */
doesNotParseWithPos(parser, "1\rx", 2, 1); // Old Mac doesNotParseWithPos(digitsParser, "1\rx", 2, 1); // Old Mac
doesNotParseWithPos(parser, "1\r\nx", 2, 1); // Windows doesNotParseWithPos(digitsParser, "1\r\nx", 2, 1); // Windows
doesNotParseWithPos(parser, "1\n\rx", 3, 1); // mismatched doesNotParseWithPos(digitsParser, "1\n\rx", 3, 1); // mismatched
/* Strange newlines */ /* Strange newlines */
doesNotParseWithPos(parser, "1\u2028x", 2, 1); // line separator doesNotParseWithPos(digitsParser, "1\u2028x", 2, 1); // line separator
doesNotParseWithPos(parser, "1\u2029x", 2, 1); // paragraph separator doesNotParseWithPos(digitsParser, "1\u2029x", 2, 1); // paragraph separator
}); });
test("start rule", function() { test("start rule", function() {

Loading…
Cancel
Save