Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added an utility that allows walking depth-first easily.

For example:

w.with_walkers({
    "function": function() {
        var node = w.dive(this); // apply other_walkers to subnodes
        var transformed_node = further_transform(node);
        return transformed_node;
    },
    ... // other_walkers here
}, ...);

(fixes #213)
  • Loading branch information...
commit d8233840e2e66a724bea702d9e2e3367a7cb7e36 1 parent d891e2d
Mihai Bazon authored
Showing with 26 additions and 0 deletions.
  1. +12 −0 lib/process.js
  2. +14 −0 tmp/test.js
12 lib/process.js
View
@@ -223,6 +223,17 @@ function ast_walker() {
}
};
+ function dive(ast) {
+ if (ast == null)
+ return null;
+ try {
+ stack.push(ast);
+ return walkers[ast[0]].apply(ast, ast.slice(1));
+ } finally {
+ stack.pop();
+ }
+ };
+
function with_walkers(walkers, cont){
var save = {}, i;
for (i in walkers) if (HOP(walkers, i)) {
@@ -239,6 +250,7 @@ function ast_walker() {
return {
walk: walk,
+ dive: dive,
with_walkers: with_walkers,
parent: function() {
return stack[stack.length - 2]; // last one is current node
14 tmp/test.js
View
@@ -11,6 +11,20 @@ var ast = jsp.parse(code);
ast = pro.ast_lift_variables(ast);
+var w = pro.ast_walker();
+ast = w.with_walkers({
+ "function": function() {
+ var node = w.dive(this); // walk depth first
+ console.log(pro.gen_code(node, { beautify: true }));
+ return node;
+ },
+ "name": function(name) {
+ return [ this[0], "X" ];
+ }
+}, function(){
+ return w.walk(ast);
+});
+
console.log(pro.gen_code(ast, {
beautify: true
}));
Please sign in to comment.
Something went wrong with that request. Please try again.