diff --git a/lib/compiler/passes/generate-js.js b/lib/compiler/passes/generate-js.js index 090c856..76a3a18 100644 --- a/lib/compiler/passes/generate-js.js +++ b/lib/compiler/passes/generate-js.js @@ -1056,7 +1056,7 @@ function generateJS(ast, options) { '', ' function peg$buildException(message, expected, location) {', ' function cleanupExpected(expected) {', - ' var i = 1;', + ' var i, j;', '', ' expected.sort(function(a, b) {', ' if (a.description < b.description) {', @@ -1073,12 +1073,14 @@ function generateJS(ast, options) { * expectation object exists only once, so it's enough to use |===| instead * of deeper structural comparison. */ - ' while (i < expected.length) {', - ' if (expected[i - 1] === expected[i]) {', - ' expected.splice(i, 1);', - ' } else {', - ' i++;', + ' if (expected.length > 0) {', + ' for (i = 1, j = 1; i < expected.length; i++) {', + ' if (expected[i - 1] !== expected[i]) {', + ' expected[j] = expected[i];', + ' j++;', + ' }', ' }', + ' expected.length = j;', ' }', ' }', '', diff --git a/lib/parser.js b/lib/parser.js index 9e026b8..630783c 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -492,7 +492,7 @@ module.exports = (function() { function peg$buildException(message, expected, location) { function cleanupExpected(expected) { - var i = 1; + var i, j; expected.sort(function(a, b) { if (a.description < b.description) { @@ -504,12 +504,14 @@ module.exports = (function() { } }); - while (i < expected.length) { - if (expected[i - 1] === expected[i]) { - expected.splice(i, 1); - } else { - i++; + if (expected.length > 0) { + for (i = 1, j = 1; i < expected.length; i++) { + if (expected[i - 1] !== expected[i]) { + expected[j] = expected[i]; + j++; + } } + expected.length = j; } }