My initial idea, that client code would constantly need to access the individual walker functions directly, isn't actually the case. This prevents unnecessary copying of other interface functions into walkers.
263 lines
9.1 KiB
JavaScript
263 lines
9.1 KiB
JavaScript
// AST walker module for Mozilla Parser API compatible trees
|
|
|
|
(function(exports) {
|
|
"use strict";
|
|
|
|
// A simple walk is one where you simply specify callbacks to be
|
|
// called on specific nodes. The last two arguments are optional. A
|
|
// simple use would be
|
|
//
|
|
// walk.simple(myTree, {
|
|
// Expression: function(node) { ... }
|
|
// });
|
|
//
|
|
// to do something with all expressions. All Parser API node types
|
|
// can be used to identify node types, as well as Expression,
|
|
// Statement, and ScopeBody, which denote categories of nodes.
|
|
//
|
|
// The base argument can be used to pass a custom (recursive)
|
|
// walker, and state can be used to give this walked an initial
|
|
// state.
|
|
exports.simple = function(node, visitors, base, state) {
|
|
if (!base) base = exports.base;
|
|
function c(node, st, override) {
|
|
var type = override || node.type, found = visitors[type];
|
|
base[type](node, st, c);
|
|
if (found) found(node, st);
|
|
}
|
|
c(node, state);
|
|
};
|
|
|
|
// A recursive walk is one where your functions override the default
|
|
// walkers. They can modify and replace the state parameter that's
|
|
// threaded through the walk, and can opt how and whether to walk
|
|
// their child nodes (by calling their third argument on these
|
|
// nodes).
|
|
exports.recursive = function(node, state, funcs, base) {
|
|
var visitor = funcs ? exports.make(funcs, base) : base;
|
|
function c(node, st, override) {
|
|
visitor[override || node.type](node, st, c);
|
|
}
|
|
c(node, state);
|
|
};
|
|
|
|
function Found(node, state) { this.node = node; this.state = state; }
|
|
|
|
// Find a node with a given start, end, and type (all are optional,
|
|
// null can be used as wildcard). Returns a {node, state} object, or
|
|
// undefined when it doesn't find a matching node.
|
|
exports.findNodeAt = function(node, start, end, targetType, base, state) {
|
|
try {
|
|
if (!base) base = exports.base;
|
|
var c = function(node, st, override) {
|
|
var type = override || node.type;
|
|
if ((start == null || node.start <= start) &&
|
|
(end == null || node.end >= end))
|
|
base[type](node, st, c);
|
|
if ((targetType == null || type == targetType) &&
|
|
(start == null || node.start == start) &&
|
|
(end == null || node.end == end))
|
|
throw new Found(node, st);
|
|
};
|
|
c(node, state);
|
|
} catch (e) {
|
|
if (e instanceof Found) return e;
|
|
throw e;
|
|
}
|
|
};
|
|
|
|
// Find the innermost node of a given type that contains the given
|
|
// position. Interface similar to findNodeAt.
|
|
exports.findNodeAround = function(node, pos, targetType, base, state) {
|
|
try {
|
|
if (!base) base = exports.base;
|
|
var c = function(node, st, override) {
|
|
var type = override || node.type;
|
|
var inside = node.start <= pos && node.end >= pos;
|
|
if (inside)
|
|
base[type](node, st, c);
|
|
if (inside && (targetType == null || type == targetType))
|
|
throw new Found(node, st);
|
|
};
|
|
c(node, state);
|
|
} catch (e) {
|
|
if (e instanceof Found) return e;
|
|
throw e;
|
|
}
|
|
};
|
|
|
|
// Used to create a custom walker. Will fill in all missing node
|
|
// type properties with the defaults.
|
|
exports.make = function(funcs, base) {
|
|
if (!base) base = exports.base;
|
|
var visitor = {};
|
|
for (var type in base) visitor[type] = base[type];
|
|
for (var type in funcs) visitor[type] = funcs[type];
|
|
return visitor;
|
|
};
|
|
|
|
function skipThrough(node, st, c) { c(node, st); }
|
|
function ignore(node, st, c) {}
|
|
|
|
// Node walkers.
|
|
|
|
var base = exports.base = {};
|
|
base.Program = base.BlockStatement = function(node, st, c) {
|
|
for (var i = 0; i < node.body.length; ++i)
|
|
c(node.body[i], st, "Statement");
|
|
};
|
|
base.Statement = skipThrough;
|
|
base.EmptyStatement = ignore;
|
|
base.ExpressionStatement = function(node, st, c) {
|
|
c(node.expression, st, "Expression");
|
|
};
|
|
base.IfStatement = function(node, st, c) {
|
|
c(node.test, st, "Expression");
|
|
c(node.consequent, st, "Statement");
|
|
if (node.alternate) c(node.alternate, st, "Statement");
|
|
};
|
|
base.LabeledStatement = function(node, st, c) {
|
|
c(node.body, st, "Statement");
|
|
};
|
|
base.BreakStatement = base.ContinueStatement = ignore;
|
|
base.WithStatement = function(node, st, c) {
|
|
c(node.object, st, "Expression");
|
|
c(node.body, st, "Statement");
|
|
};
|
|
base.SwitchStatement = function(node, st, c) {
|
|
c(node.discriminant, st, "Expression");
|
|
for (var i = 0; i < node.cases.length; ++i) {
|
|
var cs = node.cases[i];
|
|
if (cs.test) c(cs.test, st, "Expression");
|
|
for (var j = 0; j < cs.consequent.length; ++j)
|
|
c(cs.consequent[j], st, "Statement");
|
|
}
|
|
};
|
|
base.ReturnStatement = function(node, st, c) {
|
|
if (node.argument) c(node.argument, st, "Expression");
|
|
};
|
|
base.ThrowStatement = function(node, st, c) {
|
|
c(node.argument, st, "Expression");
|
|
};
|
|
base.TryStatement = function(node, st, c) {
|
|
c(node.block, st, "Statement");
|
|
for (var i = 0; i < node.handlers.length; ++i)
|
|
c(node.handlers[i].body, st, "ScopeBody");
|
|
if (node.finalizer) c(node.finalizer, st, "Statement");
|
|
};
|
|
base.WhileStatement = function(node, st, c) {
|
|
c(node.test, st, "Expression");
|
|
c(node.body, st, "Statement");
|
|
};
|
|
base.DoWhileStatement = base.WhileStatement;
|
|
base.ForStatement = function(node, st, c) {
|
|
if (node.init) c(node.init, st, "ForInit");
|
|
if (node.test) c(node.test, st, "Expression");
|
|
if (node.update) c(node.update, st, "Expression");
|
|
c(node.body, st, "Statement");
|
|
};
|
|
base.ForInStatement = function(node, st, c) {
|
|
c(node.left, st, "ForInit");
|
|
c(node.right, st, "Expression");
|
|
c(node.body, st, "Statement");
|
|
};
|
|
base.ForInit = function(node, st, c) {
|
|
if (node.type == "VariableDeclaration") c(node, st);
|
|
else c(node, st, "Expression");
|
|
};
|
|
base.DebuggerStatement = ignore;
|
|
|
|
base.FunctionDeclaration = function(node, st, c) {
|
|
c(node, st, "Function");
|
|
};
|
|
base.VariableDeclaration = function(node, st, c) {
|
|
for (var i = 0; i < node.declarations.length; ++i) {
|
|
var decl = node.declarations[i];
|
|
if (decl.init) c(decl.init, st, "Expression");
|
|
}
|
|
};
|
|
|
|
base.Function = function(node, st, c) {
|
|
c(node.body, st, "ScopeBody");
|
|
};
|
|
base.ScopeBody = function(node, st, c) {
|
|
c(node, st, "Statement");
|
|
};
|
|
|
|
base.Expression = skipThrough;
|
|
base.ThisExpression = ignore;
|
|
base.ArrayExpression = function(node, st, c) {
|
|
for (var i = 0; i < node.elements.length; ++i) {
|
|
var elt = node.elements[i];
|
|
if (elt) c(elt, st, "Expression");
|
|
}
|
|
};
|
|
base.ObjectExpression = function(node, st, c) {
|
|
for (var i = 0; i < node.properties.length; ++i)
|
|
c(node.properties[i].value, st, "Expression");
|
|
};
|
|
base.FunctionExpression = base.FunctionDeclaration;
|
|
base.SequenceExpression = function(node, st, c) {
|
|
for (var i = 0; i < node.expressions.length; ++i)
|
|
c(node.expressions[i], st, "Expression");
|
|
};
|
|
base.UnaryExpression = base.UpdateExpression = function(node, st, c) {
|
|
c(node.argument, st, "Expression");
|
|
};
|
|
base.BinaryExpression = base.AssignmentExpression = base.LogicalExpression = function(node, st, c) {
|
|
c(node.left, st, "Expression");
|
|
c(node.right, st, "Expression");
|
|
};
|
|
base.ConditionalExpression = function(node, st, c) {
|
|
c(node.test, st, "Expression");
|
|
c(node.consequent, st, "Expression");
|
|
c(node.alternate, st, "Expression");
|
|
};
|
|
base.NewExpression = base.CallExpression = function(node, st, c) {
|
|
c(node.callee, st, "Expression");
|
|
if (node.arguments) for (var i = 0; i < node.arguments.length; ++i)
|
|
c(node.arguments[i], st, "Expression");
|
|
};
|
|
base.MemberExpression = function(node, st, c) {
|
|
c(node.object, st, "Expression");
|
|
if (node.computed) c(node.property, st, "Expression");
|
|
};
|
|
base.Identifier = base.Literal = ignore;
|
|
|
|
// A custom walker that keeps track of the scope chain and the
|
|
// variables defined in it.
|
|
function makeScope(prev) {
|
|
return {vars: Object.create(null), prev: prev};
|
|
}
|
|
exports.scopeVisitor = exports.make({
|
|
Function: function(node, scope, c) {
|
|
var inner = makeScope(scope);
|
|
for (var i = 0; i < node.params.length; ++i)
|
|
inner.vars[node.params[i].name] = {type: "argument", node: node.params[i]};
|
|
if (node.id) {
|
|
var decl = node.type == "FunctionDeclaration";
|
|
(decl ? scope : inner).vars[node.id.name] =
|
|
{type: decl ? "function" : "function name", node: node.id};
|
|
}
|
|
c(node.body, inner, "ScopeBody");
|
|
},
|
|
TryStatement: function(node, scope, c) {
|
|
c(node.block, scope, "Statement");
|
|
for (var i = 0; i < node.handlers.length; ++i) {
|
|
var handler = node.handlers[i], inner = makeScope(scope);
|
|
inner.vars[handler.param.name] = {type: "catch clause", node: handler.param};
|
|
c(handler.body, inner, "ScopeBody");
|
|
}
|
|
if (node.finalizer) c(node.finalizer, scope, "Statement");
|
|
},
|
|
VariableDeclaration: function(node, scope, c) {
|
|
for (var i = 0; i < node.declarations.length; ++i) {
|
|
var decl = node.declarations[i];
|
|
scope.vars[decl.id.name] = {type: "var", node: decl.id};
|
|
if (decl.init) c(decl.init, scope, "Expression");
|
|
}
|
|
}
|
|
});
|
|
|
|
})(typeof exports == "undefined" ? acorn.walk = {} : exports);
|