Skip to content

Commit

Permalink
Fix error messages in certain cases with trailing input
Browse files Browse the repository at this point in the history
In case the generated parser parsed successfully part of input and left
some input unparsed (trailing input), the error message produced was
sometimes wrong. The code worked correctly only if there were no match
failures in the successfully parsed part (highly unlikely).

This commit fixes things by explicitly triggering a match failure with the
following expectation at the end of the successfully parsed part of the
input:

  peg$fail({ type: "end", description: "end of input" });

This change also made it possible to simplify the |buildMessage|
function, which can now ignore the case of no expectations.

Fixes #119.
  • Loading branch information
dmajda committed Dec 8, 2013
1 parent 44e0318 commit a56d3ac
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 49 deletions.
38 changes: 14 additions & 24 deletions lib/compiler/passes/generate-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -902,28 +902,18 @@ module.exports = function(ast, options) {
' .replace(/[\\u1080-\\uFFFF]/g, function(ch) { return \'\\\\u\' + hex(ch); });',
' }',
'',
' var expectedDescs, expectedDesc, foundDesc, i;',
' var expectedDescs = new Array(expected.length),',
' expectedDesc, foundDesc, i;',
'',
' switch (expected.length) {',
' case 0:',
' expectedDesc = "end of input";',
' break;',
'',
' case 1:',
' expectedDesc = expected[0].description;',
' break;',
'',
' default:',
' expectedDescs = new Array(expected.length);',
'',
' for (i = 0; i < expected.length; i++) {',
' expectedDescs[i] = expected[i].description;',
' }',
' for (i = 0; i < expected.length; i++) {',
' expectedDescs[i] = expected[i].description;',
' }',
'',
' expectedDesc = expectedDescs.slice(0, -1).join(", ")',
' expectedDesc = expected.length > 1',
' ? expectedDescs.slice(0, -1).join(", ")',
' + " or "',
' + expectedDescs[expected.length - 1];',
' }',
' + expectedDescs[expected.length - 1]',
' : expectedDescs[0];',
'',
' foundDesc = found ? "\\"" + stringEscape(found) + "\\"" : "end of input";',
'',
Expand Down Expand Up @@ -975,11 +965,11 @@ module.exports = function(ast, options) {
' if (peg$result !== peg$FAILED && peg$currPos === input.length) {',
' return peg$result;',
' } else {',
' throw peg$buildException(',
' null,',
' peg$maxFailExpected,',
' Math.max(peg$currPos, peg$maxFailPos)',
' );',
' if (peg$result !== peg$FAILED && peg$currPos < input.length) {',
' peg$fail({ type: "end", description: "end of input" });',
' }',
'',
' throw peg$buildException(null, peg$maxFailExpected, peg$maxFailPos);',
' }',
' }',
'',
Expand Down
38 changes: 14 additions & 24 deletions lib/parser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion spec/generated-parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,9 @@ describe("generated parser", function() {
it("reports expectations correctly with no alternative", function() {
var parser = PEG.buildParser('start = ', options);

expect(parser).toFailToParse("a", { expected: [] });
expect(parser).toFailToParse("a", {
expected: [{ type: "end", description: "end of input" }]
});
});

it("reports expectations correctly with one alternative", function() {
Expand Down

0 comments on commit a56d3ac

Please sign in to comment.