Browse Source

Implemented and used PEG.ArrayUtils.each.

redux
David Majda 12 years ago
parent
commit
6bbd88088b
  1. 2
      lib/metagrammar.js
  2. 2
      lib/metagrammar.pegjs
  3. 7
      lib/runtime.js
  4. 13
      test/runtime-test.js

2
lib/metagrammar.js

@ -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;

2
lib/metagrammar.pegjs

@ -1,6 +1,6 @@
grammar: __ rule+ {
var result = {};
for (var i = 0; i < $2.length; i++) { result[$2[i].getName()] = $2[i]; }
PEG.ArrayUtils.each($2, function(rule) { result[rule.getName()] = rule; });
return result;
}

7
lib/runtime.js

@ -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 = [];
var length = array.length;

13
test/runtime-test.js

@ -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);
sum = 0;
PEG.ArrayUtils.each([1, 2, 3], increment);
strictEqual(sum, 6);
});
test("map", function() {
function square(x) { return x * x; }

Loading…
Cancel
Save