Skip to content

Commit

Permalink
Extract |buildNodeVisitor|
Browse files Browse the repository at this point in the history
  • Loading branch information
dmajda committed Aug 17, 2010
1 parent 1279e87 commit 4d50a37
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
14 changes: 4 additions & 10 deletions src/checks.js
Expand Up @@ -16,7 +16,7 @@ PEG.compiler.checks = [
return function(node) { each(node[propertyName], check); }; return function(node) { each(node[propertyName], check); };
} }


var checkFunctions = { var check = buildNodeVisitor({
grammar: grammar:
function(node) { function(node) {
for (var name in node.rules) { for (var name in node.rules) {
Expand Down Expand Up @@ -49,9 +49,7 @@ PEG.compiler.checks = [
literal: nop, literal: nop,
any: nop, any: nop,
"class": nop "class": nop
}; });

function check(node) { checkFunctions[node.type](node); }


check(ast); check(ast);
}, },
Expand All @@ -64,7 +62,7 @@ PEG.compiler.checks = [
check(node.expression, appliedRules); check(node.expression, appliedRules);
} }


var checkFunctions = { var check = buildNodeVisitor({
grammar: grammar:
function(node, appliedRules) { function(node, appliedRules) {
for (var name in node.rules) { for (var name in node.rules) {
Expand Down Expand Up @@ -114,11 +112,7 @@ PEG.compiler.checks = [
literal: nop, literal: nop,
any: nop, any: nop,
"class": nop "class": nop
}; });

function check(node, appliedRules) {
checkFunctions[node.type](node, appliedRules);
}


check(ast, []); check(ast, []);
} }
Expand Down
8 changes: 2 additions & 6 deletions src/emitter.js
Expand Up @@ -90,7 +90,7 @@ PEG.compiler.emitter = function(ast) {
} }
}; };


var emitFunctions = { var emit = buildNodeVisitor({
grammar: function(node) { grammar: function(node) {
var initializerCode = node.initializer !== null var initializerCode = node.initializer !== null
? emit(node.initializer) ? emit(node.initializer)
Expand Down Expand Up @@ -710,11 +710,7 @@ PEG.compiler.emitter = function(ast) {
} }
); );
} }
}; });

function emit(node, resultVar) {
return emitFunctions[node.type](node, resultVar);
}


return emit(ast); return emit(ast);
}; };
8 changes: 2 additions & 6 deletions src/passes.js
Expand Up @@ -28,7 +28,7 @@ PEG.compiler.passes = [
}; };
} }


var replaceFunctions = { var replace = buildNodeVisitor({
grammar: grammar:
function(node, from, to) { function(node, from, to) {
for (var name in node.rules) { for (var name in node.rules) {
Expand Down Expand Up @@ -59,11 +59,7 @@ PEG.compiler.passes = [
literal: nop, literal: nop,
any: nop, any: nop,
"class": nop "class": nop
}; });

function replace(node, from, to) {
replaceFunctions[node.type](node, from, to);
}


replace(ast, from, to); replace(ast, from, to);
} }
Expand Down
12 changes: 12 additions & 0 deletions src/utils.js
Expand Up @@ -73,3 +73,15 @@ function quoteForRegexpClass(s) {
.replace(/\u2029/g, '\\u2029') // paragraph separator .replace(/\u2029/g, '\\u2029') // paragraph separator
.replace(/\n/g, '\\n') // line feed .replace(/\n/g, '\\n') // line feed
} }

/*
* Builds a node visitor -- a function which takes a node and any number of
* other parameters, calls an appropriate function according to the node type,
* passes it all its parameters and returns its value. The functions for various
* node types are passed in a parameter to |buildNodeVisitor| as a hash.
*/
function buildNodeVisitor(functions) {
return function(node) {
functions[node.type].apply(null, arguments));
}
}

0 comments on commit 4d50a37

Please sign in to comment.