Fix deduplication in |peg$cleanupExpected|

The deduplication skipped over an expected string right after the one
that was removed because the index variable was incorrectly incremented
in that case.

Based on a patch by @fresheneesz:

  https://github.com/dmajda/pegjs/pull/146
redux
David Majda 12 years ago
parent 851681d663
commit 8759d4899e

@ -880,13 +880,15 @@ module.exports = function(ast, options) {
' }', ' }',
'', '',
' function peg$cleanupExpected(expected) {', ' function peg$cleanupExpected(expected) {',
' var i;', ' var i = 0;',
'', '',
' expected.sort();', ' expected.sort();',
'', '',
' for (i = 1; i < expected.length; i++) {', ' while (i < expected.length) {',
' if (expected[i - 1] === expected[i]) {', ' if (expected[i - 1] === expected[i]) {',
' expected.splice(i, 1);', ' expected.splice(i, 1);',
' } else {',
' i++;',
' }', ' }',
' }', ' }',
' }', ' }',

@ -450,13 +450,15 @@ module.exports = (function() {
} }
function peg$cleanupExpected(expected) { function peg$cleanupExpected(expected) {
var i; var i = 0;
expected.sort(); expected.sort();
for (i = 1; i < expected.length; i++) { while (i < expected.length) {
if (expected[i - 1] === expected[i]) { if (expected[i - 1] === expected[i]) {
expected.splice(i, 1); expected.splice(i, 1);
} else {
i++;
} }
} }
} }

@ -833,7 +833,14 @@ describe("generated parser", function() {
}); });
it("removes duplicates from expected strings", function() { it("removes duplicates from expected strings", function() {
var parser = PEG.buildParser('start = "a" / "a"', options); /*
* There was a bug in the code that manifested only with three
* duplicates. This is why the following test uses three choices
* instead of seemingly sufficient two.
*
* See https://github.com/dmajda/pegjs/pull/146.
*/
var parser = PEG.buildParser('start = "a" / "a" / "a"', options);
expect(parser).toFailToParse("b", { expected: ['"a"'] }); expect(parser).toFailToParse("b", { expected: ['"a"'] });
}); });

Loading…
Cancel
Save