diff --git a/lib/compiler/passes/generate-js.js b/lib/compiler/passes/generate-js.js index 82d1ea4..ed6aff6 100644 --- a/lib/compiler/passes/generate-js.js +++ b/lib/compiler/passes/generate-js.js @@ -179,10 +179,8 @@ function generateJS(ast, options) { paramsLengthCode = 'bc[ip + ' + (baseLength - 1) + ']'; return [ - 'params = bc.slice(ip + ' + baseLength + ', ip + ' + baseLength + ' + ' + paramsLengthCode + ');', - 'for (i = 0; i < ' + paramsLengthCode + '; i++) {', - ' params[i] = stack[stack.length - 1 - params[i]];', - '}', + 'params = bc.slice(ip + ' + baseLength + ', ip + ' + baseLength + ' + ' + paramsLengthCode + ')', + ' .map(function(p) { return stack[stack.length - 1 - p]; });', '', 'stack.splice(', ' stack.length - bc[ip + 2],', @@ -197,13 +195,7 @@ function generateJS(ast, options) { parts.push([ 'function peg$decode(s) {', - ' var bc = new Array(s.length), i;', - '', - ' for (i = 0; i < s.length; i++) {', - ' bc[i] = s.charCodeAt(i) - 32;', - ' }', - '', - ' return bc;', + ' return s.split("").map(function(ch) { return ch.charCodeAt(0) - 32; });', '}', '', 'function peg$parseRule(index) {' @@ -218,7 +210,7 @@ function generateJS(ast, options) { ' ends = [],', ' stack = [],', ' startPos = peg$currPos,', - ' params, i;' + ' params;' ].join('\n')); } else { parts.push([ @@ -228,7 +220,7 @@ function generateJS(ast, options) { ' end = bc.length,', ' ends = [],', ' stack = [],', - ' params, i;' + ' params;' ].join('\n')); } @@ -786,14 +778,11 @@ function generateJS(ast, options) { ' },', '', ' "class": function(expectation) {', - ' var escapedParts = "",', - ' i;', - '', - ' for (i = 0; i < expectation.parts.length; i++) {', - ' escapedParts += Array.isArray(expectation.parts[i])', - ' ? classEscape(expectation.parts[i][0]) + "-" + classEscape(expectation.parts[i][1])', - ' : classEscape(expectation.parts[i]);', - ' }', + ' var escapedParts = expectation.parts.map(function(part) {', + ' return Array.isArray(part)', + ' ? classEscape(part[0]) + "-" + classEscape(part[1])', + ' : classEscape(part);', + ' });', '', ' return "[" + (expectation.inverted ? "^" : "") + escapedParts + "]";', ' },', @@ -846,13 +835,9 @@ function generateJS(ast, options) { ' }', '', ' function describeExpected(expected) {', - ' var descriptions = new Array(expected.length),', + ' var descriptions = expected.map(describeExpectation),', ' i, j;', '', - ' for (i = 0; i < expected.length; i++) {', - ' descriptions[i] = describeExpectation(expected[i]);', - ' }', - '', ' descriptions.sort();', '', ' if (descriptions.length > 0) {', diff --git a/lib/parser.js b/lib/parser.js index 873f854..3dfcadd 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -36,14 +36,11 @@ peg$SyntaxError.buildMessage = function(expected, found) { }, "class": function(expectation) { - var escapedParts = "", - i; - - for (i = 0; i < expectation.parts.length; i++) { - escapedParts += Array.isArray(expectation.parts[i]) - ? classEscape(expectation.parts[i][0]) + "-" + classEscape(expectation.parts[i][1]) - : classEscape(expectation.parts[i]); - } + var escapedParts = expectation.parts.map(function(part) { + return Array.isArray(part) + ? classEscape(part[0]) + "-" + classEscape(part[1]) + : classEscape(part); + }); return "[" + (expectation.inverted ? "^" : "") + escapedParts + "]"; }, @@ -96,13 +93,9 @@ peg$SyntaxError.buildMessage = function(expected, found) { } function describeExpected(expected) { - var descriptions = new Array(expected.length), + var descriptions = expected.map(describeExpectation), i, j; - for (i = 0; i < expected.length; i++) { - descriptions[i] = describeExpectation(expected[i]); - } - descriptions.sort(); if (descriptions.length > 0) {