@ -54,7 +54,7 @@ PEG.grammarParser = (function(){
var result0 = result1 !== null
? (function() {
var result = {};
for (var i = 0; i < (arguments[1]).length; i++) { result[(arguments[1])[i].getName()] = (arguments[1])[i]; }
PEG.ArrayUtils.each((arguments[1]), function(rule) { result[rule.getName()] = rule; });
return result;
}).apply(this, result1)
: null;
@ -1,6 +1,6 @@
grammar: __ rule+ {
for (var i = 0; i < $2.length; i++) { result[$2[i].getName()] = $2[i]; }
PEG.ArrayUtils.each($2, function(rule) { result[rule.getName()] = rule; });
}
@ -13,6 +13,13 @@ PEG = {};
/* Array manipulation utility functions. */
PEG.ArrayUtils = {
each: function(array, callback) {
var length = array.length;
for (var i = 0; i < length; i++) {
callback(array[i]);
},
map: function(array, callback) {
var result = [];
@ -4,6 +4,19 @@
module("PEG.ArrayUtils");
test("each", function() {
var sum;
function increment(x) { sum += x; }
sum = 0;
PEG.ArrayUtils.each([], increment);
strictEqual(sum, 0);
PEG.ArrayUtils.each([1, 2, 3], increment);
strictEqual(sum, 6);
});
test("map", function() {
function square(x) { return x * x; }