Skip to content

Commit fad4ab7

Browse files
committed
Replace suitable for loops with Array methods (in /lib)
See #441.
1 parent 3e8bcbe commit fad4ab7

File tree

2 files changed

+17
-39
lines changed

2 files changed

+17
-39
lines changed

lib/compiler/passes/generate-js.js

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,8 @@ function generateJS(ast, options) {
179179
paramsLengthCode = 'bc[ip + ' + (baseLength - 1) + ']';
180180

181181
return [
182-
'params = bc.slice(ip + ' + baseLength + ', ip + ' + baseLength + ' + ' + paramsLengthCode + ');',
183-
'for (i = 0; i < ' + paramsLengthCode + '; i++) {',
184-
' params[i] = stack[stack.length - 1 - params[i]];',
185-
'}',
182+
'params = bc.slice(ip + ' + baseLength + ', ip + ' + baseLength + ' + ' + paramsLengthCode + ')',
183+
' .map(function(p) { return stack[stack.length - 1 - p]; });',
186184
'',
187185
'stack.splice(',
188186
' stack.length - bc[ip + 2],',
@@ -197,13 +195,7 @@ function generateJS(ast, options) {
197195

198196
parts.push([
199197
'function peg$decode(s) {',
200-
' var bc = new Array(s.length), i;',
201-
'',
202-
' for (i = 0; i < s.length; i++) {',
203-
' bc[i] = s.charCodeAt(i) - 32;',
204-
' }',
205-
'',
206-
' return bc;',
198+
' return s.split("").map(function(ch) { return ch.charCodeAt(0) - 32; });',
207199
'}',
208200
'',
209201
'function peg$parseRule(index) {'
@@ -218,7 +210,7 @@ function generateJS(ast, options) {
218210
' ends = [],',
219211
' stack = [],',
220212
' startPos = peg$currPos,',
221-
' params, i;'
213+
' params;'
222214
].join('\n'));
223215
} else {
224216
parts.push([
@@ -228,7 +220,7 @@ function generateJS(ast, options) {
228220
' end = bc.length,',
229221
' ends = [],',
230222
' stack = [],',
231-
' params, i;'
223+
' params;'
232224
].join('\n'));
233225
}
234226

@@ -786,14 +778,11 @@ function generateJS(ast, options) {
786778
' },',
787779
'',
788780
' "class": function(expectation) {',
789-
' var escapedParts = "",',
790-
' i;',
791-
'',
792-
' for (i = 0; i < expectation.parts.length; i++) {',
793-
' escapedParts += Array.isArray(expectation.parts[i])',
794-
' ? classEscape(expectation.parts[i][0]) + "-" + classEscape(expectation.parts[i][1])',
795-
' : classEscape(expectation.parts[i]);',
796-
' }',
781+
' var escapedParts = expectation.parts.map(function(part) {',
782+
' return Array.isArray(part)',
783+
' ? classEscape(part[0]) + "-" + classEscape(part[1])',
784+
' : classEscape(part);',
785+
' });',
797786
'',
798787
' return "[" + (expectation.inverted ? "^" : "") + escapedParts + "]";',
799788
' },',
@@ -846,13 +835,9 @@ function generateJS(ast, options) {
846835
' }',
847836
'',
848837
' function describeExpected(expected) {',
849-
' var descriptions = new Array(expected.length),',
838+
' var descriptions = expected.map(describeExpectation),',
850839
' i, j;',
851840
'',
852-
' for (i = 0; i < expected.length; i++) {',
853-
' descriptions[i] = describeExpectation(expected[i]);',
854-
' }',
855-
'',
856841
' descriptions.sort();',
857842
'',
858843
' if (descriptions.length > 0) {',

lib/parser.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,11 @@ peg$SyntaxError.buildMessage = function(expected, found) {
3636
},
3737

3838
"class": function(expectation) {
39-
var escapedParts = "",
40-
i;
41-
42-
for (i = 0; i < expectation.parts.length; i++) {
43-
escapedParts += Array.isArray(expectation.parts[i])
44-
? classEscape(expectation.parts[i][0]) + "-" + classEscape(expectation.parts[i][1])
45-
: classEscape(expectation.parts[i]);
46-
}
39+
var escapedParts = expectation.parts.map(function(part) {
40+
return Array.isArray(part)
41+
? classEscape(part[0]) + "-" + classEscape(part[1])
42+
: classEscape(part);
43+
});
4744

4845
return "[" + (expectation.inverted ? "^" : "") + escapedParts + "]";
4946
},
@@ -96,13 +93,9 @@ peg$SyntaxError.buildMessage = function(expected, found) {
9693
}
9794

9895
function describeExpected(expected) {
99-
var descriptions = new Array(expected.length),
96+
var descriptions = expected.map(describeExpectation),
10097
i, j;
10198

102-
for (i = 0; i < expected.length; i++) {
103-
descriptions[i] = describeExpectation(expected[i]);
104-
}
105-
10699
descriptions.sort();
107100

108101
if (descriptions.length > 0) {

0 commit comments

Comments
 (0)