Replace suitable for loops with Array methods (in /lib)

See #441.
redux
David Majda 8 years ago
parent 3e8bcbea73
commit fad4ab74d1

@ -179,10 +179,8 @@ function generateJS(ast, options) {
paramsLengthCode = 'bc[ip + ' + (baseLength - 1) + ']'; paramsLengthCode = 'bc[ip + ' + (baseLength - 1) + ']';
return [ return [
'params = bc.slice(ip + ' + baseLength + ', ip + ' + baseLength + ' + ' + paramsLengthCode + ');', 'params = bc.slice(ip + ' + baseLength + ', ip + ' + baseLength + ' + ' + paramsLengthCode + ')',
'for (i = 0; i < ' + paramsLengthCode + '; i++) {', ' .map(function(p) { return stack[stack.length - 1 - p]; });',
' params[i] = stack[stack.length - 1 - params[i]];',
'}',
'', '',
'stack.splice(', 'stack.splice(',
' stack.length - bc[ip + 2],', ' stack.length - bc[ip + 2],',
@ -197,13 +195,7 @@ function generateJS(ast, options) {
parts.push([ parts.push([
'function peg$decode(s) {', 'function peg$decode(s) {',
' var bc = new Array(s.length), i;', ' return s.split("").map(function(ch) { return ch.charCodeAt(0) - 32; });',
'',
' for (i = 0; i < s.length; i++) {',
' bc[i] = s.charCodeAt(i) - 32;',
' }',
'',
' return bc;',
'}', '}',
'', '',
'function peg$parseRule(index) {' 'function peg$parseRule(index) {'
@ -218,7 +210,7 @@ function generateJS(ast, options) {
' ends = [],', ' ends = [],',
' stack = [],', ' stack = [],',
' startPos = peg$currPos,', ' startPos = peg$currPos,',
' params, i;' ' params;'
].join('\n')); ].join('\n'));
} else { } else {
parts.push([ parts.push([
@ -228,7 +220,7 @@ function generateJS(ast, options) {
' end = bc.length,', ' end = bc.length,',
' ends = [],', ' ends = [],',
' stack = [],', ' stack = [],',
' params, i;' ' params;'
].join('\n')); ].join('\n'));
} }
@ -786,14 +778,11 @@ function generateJS(ast, options) {
' },', ' },',
'', '',
' "class": function(expectation) {', ' "class": function(expectation) {',
' var escapedParts = "",', ' var escapedParts = expectation.parts.map(function(part) {',
' i;', ' return Array.isArray(part)',
'', ' ? classEscape(part[0]) + "-" + classEscape(part[1])',
' for (i = 0; i < expectation.parts.length; i++) {', ' : classEscape(part);',
' escapedParts += Array.isArray(expectation.parts[i])', ' });',
' ? classEscape(expectation.parts[i][0]) + "-" + classEscape(expectation.parts[i][1])',
' : classEscape(expectation.parts[i]);',
' }',
'', '',
' return "[" + (expectation.inverted ? "^" : "") + escapedParts + "]";', ' return "[" + (expectation.inverted ? "^" : "") + escapedParts + "]";',
' },', ' },',
@ -846,13 +835,9 @@ function generateJS(ast, options) {
' }', ' }',
'', '',
' function describeExpected(expected) {', ' function describeExpected(expected) {',
' var descriptions = new Array(expected.length),', ' var descriptions = expected.map(describeExpectation),',
' i, j;', ' i, j;',
'', '',
' for (i = 0; i < expected.length; i++) {',
' descriptions[i] = describeExpectation(expected[i]);',
' }',
'',
' descriptions.sort();', ' descriptions.sort();',
'', '',
' if (descriptions.length > 0) {', ' if (descriptions.length > 0) {',

@ -36,14 +36,11 @@ peg$SyntaxError.buildMessage = function(expected, found) {
}, },
"class": function(expectation) { "class": function(expectation) {
var escapedParts = "", var escapedParts = expectation.parts.map(function(part) {
i; return Array.isArray(part)
? classEscape(part[0]) + "-" + classEscape(part[1])
for (i = 0; i < expectation.parts.length; i++) { : classEscape(part);
escapedParts += Array.isArray(expectation.parts[i]) });
? classEscape(expectation.parts[i][0]) + "-" + classEscape(expectation.parts[i][1])
: classEscape(expectation.parts[i]);
}
return "[" + (expectation.inverted ? "^" : "") + escapedParts + "]"; return "[" + (expectation.inverted ? "^" : "") + escapedParts + "]";
}, },
@ -96,13 +93,9 @@ peg$SyntaxError.buildMessage = function(expected, found) {
} }
function describeExpected(expected) { function describeExpected(expected) {
var descriptions = new Array(expected.length), var descriptions = expected.map(describeExpectation),
i, j; i, j;
for (i = 0; i < expected.length; i++) {
descriptions[i] = describeExpectation(expected[i]);
}
descriptions.sort(); descriptions.sort();
if (descriptions.length > 0) { if (descriptions.length > 0) {

Loading…
Cancel
Save