copy | ada2920 | 2014-06-09 23:52:48 +0200 | [diff] [blame] | 1 | // AST walker module for Mozilla Parser API compatible trees |
| 2 | |
| 3 | (function(mod) { |
| 4 | if (typeof exports == "object" && typeof module == "object") return mod(exports); // CommonJS |
| 5 | if (typeof define == "function" && define.amd) return define(["exports"], mod); // AMD |
| 6 | mod((this.acorn || (this.acorn = {})).walk = {}); // Plain browser env |
| 7 | })(function(exports) { |
| 8 | "use strict"; |
| 9 | |
| 10 | // A simple walk is one where you simply specify callbacks to be |
| 11 | // called on specific nodes. The last two arguments are optional. A |
| 12 | // simple use would be |
| 13 | // |
| 14 | // walk.simple(myTree, { |
| 15 | // Expression: function(node) { ... } |
| 16 | // }); |
| 17 | // |
| 18 | // to do something with all expressions. All Parser API node types |
| 19 | // can be used to identify node types, as well as Expression, |
| 20 | // Statement, and ScopeBody, which denote categories of nodes. |
| 21 | // |
| 22 | // The base argument can be used to pass a custom (recursive) |
| 23 | // walker, and state can be used to give this walked an initial |
| 24 | // state. |
| 25 | exports.simple = function(node, visitors, base, state) { |
| 26 | if (!base) base = exports.base; |
| 27 | function c(node, st, override) { |
| 28 | var type = override || node.type, found = visitors[type]; |
| 29 | base[type](node, st, c); |
| 30 | if (found) found(node, st); |
| 31 | } |
| 32 | c(node, state); |
| 33 | }; |
| 34 | |
| 35 | // An ancestor walk builds up an array of ancestor nodes (including |
| 36 | // the current node) and passes them to the callback as the state parameter. |
| 37 | exports.ancestor = function(node, visitors, base, state) { |
| 38 | if (!base) base = exports.base; |
| 39 | if (!state) state = []; |
| 40 | function c(node, st, override) { |
| 41 | var type = override || node.type, found = visitors[type]; |
| 42 | if (node != st[st.length - 1]) { |
| 43 | st = st.slice(); |
| 44 | st.push(node); |
| 45 | } |
| 46 | base[type](node, st, c); |
| 47 | if (found) found(node, st); |
| 48 | } |
| 49 | c(node, state); |
| 50 | }; |
| 51 | |
| 52 | // A recursive walk is one where your functions override the default |
| 53 | // walkers. They can modify and replace the state parameter that's |
| 54 | // threaded through the walk, and can opt how and whether to walk |
| 55 | // their child nodes (by calling their third argument on these |
| 56 | // nodes). |
| 57 | exports.recursive = function(node, state, funcs, base) { |
| 58 | var visitor = funcs ? exports.make(funcs, base) : base; |
| 59 | function c(node, st, override) { |
| 60 | visitor[override || node.type](node, st, c); |
| 61 | } |
| 62 | c(node, state); |
| 63 | }; |
| 64 | |
| 65 | function makeTest(test) { |
| 66 | if (typeof test == "string") |
| 67 | return function(type) { return type == test; }; |
| 68 | else if (!test) |
| 69 | return function() { return true; }; |
| 70 | else |
| 71 | return test; |
| 72 | } |
| 73 | |
| 74 | function Found(node, state) { this.node = node; this.state = state; } |
| 75 | |
| 76 | // Find a node with a given start, end, and type (all are optional, |
| 77 | // null can be used as wildcard). Returns a {node, state} object, or |
| 78 | // undefined when it doesn't find a matching node. |
| 79 | exports.findNodeAt = function(node, start, end, test, base, state) { |
| 80 | test = makeTest(test); |
| 81 | try { |
| 82 | if (!base) base = exports.base; |
| 83 | var c = function(node, st, override) { |
| 84 | var type = override || node.type; |
| 85 | if ((start == null || node.start <= start) && |
| 86 | (end == null || node.end >= end)) |
| 87 | base[type](node, st, c); |
| 88 | if (test(type, node) && |
| 89 | (start == null || node.start == start) && |
| 90 | (end == null || node.end == end)) |
| 91 | throw new Found(node, st); |
| 92 | }; |
| 93 | c(node, state); |
| 94 | } catch (e) { |
| 95 | if (e instanceof Found) return e; |
| 96 | throw e; |
| 97 | } |
| 98 | }; |
| 99 | |
| 100 | // Find the innermost node of a given type that contains the given |
| 101 | // position. Interface similar to findNodeAt. |
| 102 | exports.findNodeAround = function(node, pos, test, base, state) { |
| 103 | test = makeTest(test); |
| 104 | try { |
| 105 | if (!base) base = exports.base; |
| 106 | var c = function(node, st, override) { |
| 107 | var type = override || node.type; |
| 108 | if (node.start > pos || node.end < pos) return; |
| 109 | base[type](node, st, c); |
| 110 | if (test(type, node)) throw new Found(node, st); |
| 111 | }; |
| 112 | c(node, state); |
| 113 | } catch (e) { |
| 114 | if (e instanceof Found) return e; |
| 115 | throw e; |
| 116 | } |
| 117 | }; |
| 118 | |
| 119 | // Find the outermost matching node after a given position. |
| 120 | exports.findNodeAfter = function(node, pos, test, base, state) { |
| 121 | test = makeTest(test); |
| 122 | try { |
| 123 | if (!base) base = exports.base; |
| 124 | var c = function(node, st, override) { |
| 125 | if (node.end < pos) return; |
| 126 | var type = override || node.type; |
| 127 | if (node.start >= pos && test(type, node)) throw new Found(node, st); |
| 128 | base[type](node, st, c); |
| 129 | }; |
| 130 | c(node, state); |
| 131 | } catch (e) { |
| 132 | if (e instanceof Found) return e; |
| 133 | throw e; |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | // Find the outermost matching node before a given position. |
| 138 | exports.findNodeBefore = function(node, pos, test, base, state) { |
| 139 | test = makeTest(test); |
| 140 | if (!base) base = exports.base; |
| 141 | var max; |
| 142 | var c = function(node, st, override) { |
| 143 | if (node.start > pos) return; |
| 144 | var type = override || node.type; |
| 145 | if (node.end <= pos && (!max || max.node.end < node.end) && test(type, node)) |
| 146 | max = new Found(node, st); |
| 147 | base[type](node, st, c); |
| 148 | }; |
| 149 | c(node, state); |
| 150 | return max; |
| 151 | }; |
| 152 | |
| 153 | // Used to create a custom walker. Will fill in all missing node |
| 154 | // type properties with the defaults. |
| 155 | exports.make = function(funcs, base) { |
| 156 | if (!base) base = exports.base; |
| 157 | var visitor = {}; |
| 158 | for (var type in base) visitor[type] = base[type]; |
| 159 | for (var type in funcs) visitor[type] = funcs[type]; |
| 160 | return visitor; |
| 161 | }; |
| 162 | |
| 163 | function skipThrough(node, st, c) { c(node, st); } |
| 164 | function ignore(_node, _st, _c) {} |
| 165 | |
| 166 | // Node walkers. |
| 167 | |
| 168 | var base = exports.base = {}; |
| 169 | base.Program = base.BlockStatement = function(node, st, c) { |
| 170 | for (var i = 0; i < node.body.length; ++i) |
| 171 | c(node.body[i], st, "Statement"); |
| 172 | }; |
| 173 | base.Statement = skipThrough; |
| 174 | base.EmptyStatement = ignore; |
| 175 | base.ExpressionStatement = function(node, st, c) { |
| 176 | c(node.expression, st, "Expression"); |
| 177 | }; |
| 178 | base.IfStatement = function(node, st, c) { |
| 179 | c(node.test, st, "Expression"); |
| 180 | c(node.consequent, st, "Statement"); |
| 181 | if (node.alternate) c(node.alternate, st, "Statement"); |
| 182 | }; |
| 183 | base.LabeledStatement = function(node, st, c) { |
| 184 | c(node.body, st, "Statement"); |
| 185 | }; |
| 186 | base.BreakStatement = base.ContinueStatement = ignore; |
| 187 | base.WithStatement = function(node, st, c) { |
| 188 | c(node.object, st, "Expression"); |
| 189 | c(node.body, st, "Statement"); |
| 190 | }; |
| 191 | base.SwitchStatement = function(node, st, c) { |
| 192 | c(node.discriminant, st, "Expression"); |
| 193 | for (var i = 0; i < node.cases.length; ++i) { |
| 194 | var cs = node.cases[i]; |
| 195 | if (cs.test) c(cs.test, st, "Expression"); |
| 196 | for (var j = 0; j < cs.consequent.length; ++j) |
| 197 | c(cs.consequent[j], st, "Statement"); |
| 198 | } |
| 199 | }; |
| 200 | base.ReturnStatement = function(node, st, c) { |
| 201 | if (node.argument) c(node.argument, st, "Expression"); |
| 202 | }; |
| 203 | base.ThrowStatement = function(node, st, c) { |
| 204 | c(node.argument, st, "Expression"); |
| 205 | }; |
| 206 | base.TryStatement = function(node, st, c) { |
| 207 | c(node.block, st, "Statement"); |
| 208 | if (node.handler) c(node.handler.body, st, "ScopeBody"); |
| 209 | if (node.finalizer) c(node.finalizer, st, "Statement"); |
| 210 | }; |
| 211 | base.WhileStatement = function(node, st, c) { |
| 212 | c(node.test, st, "Expression"); |
| 213 | c(node.body, st, "Statement"); |
| 214 | }; |
| 215 | base.DoWhileStatement = base.WhileStatement; |
| 216 | base.ForStatement = function(node, st, c) { |
| 217 | if (node.init) c(node.init, st, "ForInit"); |
| 218 | if (node.test) c(node.test, st, "Expression"); |
| 219 | if (node.update) c(node.update, st, "Expression"); |
| 220 | c(node.body, st, "Statement"); |
| 221 | }; |
| 222 | base.ForInStatement = function(node, st, c) { |
| 223 | c(node.left, st, "ForInit"); |
| 224 | c(node.right, st, "Expression"); |
| 225 | c(node.body, st, "Statement"); |
| 226 | }; |
| 227 | base.ForInit = function(node, st, c) { |
| 228 | if (node.type == "VariableDeclaration") c(node, st); |
| 229 | else c(node, st, "Expression"); |
| 230 | }; |
| 231 | base.DebuggerStatement = ignore; |
| 232 | |
| 233 | base.FunctionDeclaration = function(node, st, c) { |
| 234 | c(node, st, "Function"); |
| 235 | }; |
| 236 | base.VariableDeclaration = function(node, st, c) { |
| 237 | for (var i = 0; i < node.declarations.length; ++i) { |
| 238 | var decl = node.declarations[i]; |
| 239 | if (decl.init) c(decl.init, st, "Expression"); |
| 240 | } |
| 241 | }; |
| 242 | |
| 243 | base.Function = function(node, st, c) { |
| 244 | c(node.body, st, "ScopeBody"); |
| 245 | }; |
| 246 | base.ScopeBody = function(node, st, c) { |
| 247 | c(node, st, "Statement"); |
| 248 | }; |
| 249 | |
| 250 | base.Expression = skipThrough; |
| 251 | base.ThisExpression = ignore; |
| 252 | base.ArrayExpression = function(node, st, c) { |
| 253 | for (var i = 0; i < node.elements.length; ++i) { |
| 254 | var elt = node.elements[i]; |
| 255 | if (elt) c(elt, st, "Expression"); |
| 256 | } |
| 257 | }; |
| 258 | base.ObjectExpression = function(node, st, c) { |
| 259 | for (var i = 0; i < node.properties.length; ++i) |
| 260 | c(node.properties[i].value, st, "Expression"); |
| 261 | }; |
| 262 | base.FunctionExpression = base.FunctionDeclaration; |
| 263 | base.SequenceExpression = function(node, st, c) { |
| 264 | for (var i = 0; i < node.expressions.length; ++i) |
| 265 | c(node.expressions[i], st, "Expression"); |
| 266 | }; |
| 267 | base.UnaryExpression = base.UpdateExpression = function(node, st, c) { |
| 268 | c(node.argument, st, "Expression"); |
| 269 | }; |
| 270 | base.BinaryExpression = base.AssignmentExpression = base.LogicalExpression = function(node, st, c) { |
| 271 | c(node.left, st, "Expression"); |
| 272 | c(node.right, st, "Expression"); |
| 273 | }; |
| 274 | base.ConditionalExpression = function(node, st, c) { |
| 275 | c(node.test, st, "Expression"); |
| 276 | c(node.consequent, st, "Expression"); |
| 277 | c(node.alternate, st, "Expression"); |
| 278 | }; |
| 279 | base.NewExpression = base.CallExpression = function(node, st, c) { |
| 280 | c(node.callee, st, "Expression"); |
| 281 | if (node.arguments) for (var i = 0; i < node.arguments.length; ++i) |
| 282 | c(node.arguments[i], st, "Expression"); |
| 283 | }; |
| 284 | base.MemberExpression = function(node, st, c) { |
| 285 | c(node.object, st, "Expression"); |
| 286 | if (node.computed) c(node.property, st, "Expression"); |
| 287 | }; |
| 288 | base.Identifier = base.Literal = ignore; |
| 289 | |
| 290 | // A custom walker that keeps track of the scope chain and the |
| 291 | // variables defined in it. |
| 292 | function makeScope(prev, isCatch) { |
| 293 | return {vars: Object.create(null), prev: prev, isCatch: isCatch}; |
| 294 | } |
| 295 | function normalScope(scope) { |
| 296 | while (scope.isCatch) scope = scope.prev; |
| 297 | return scope; |
| 298 | } |
| 299 | exports.scopeVisitor = exports.make({ |
| 300 | Function: function(node, scope, c) { |
| 301 | var inner = makeScope(scope); |
| 302 | for (var i = 0; i < node.params.length; ++i) |
| 303 | inner.vars[node.params[i].name] = {type: "argument", node: node.params[i]}; |
| 304 | if (node.id) { |
| 305 | var decl = node.type == "FunctionDeclaration"; |
| 306 | (decl ? normalScope(scope) : inner).vars[node.id.name] = |
| 307 | {type: decl ? "function" : "function name", node: node.id}; |
| 308 | } |
| 309 | c(node.body, inner, "ScopeBody"); |
| 310 | }, |
| 311 | TryStatement: function(node, scope, c) { |
| 312 | c(node.block, scope, "Statement"); |
| 313 | if (node.handler) { |
| 314 | var inner = makeScope(scope, true); |
| 315 | inner.vars[node.handler.param.name] = {type: "catch clause", node: node.handler.param}; |
| 316 | c(node.handler.body, inner, "ScopeBody"); |
| 317 | } |
| 318 | if (node.finalizer) c(node.finalizer, scope, "Statement"); |
| 319 | }, |
| 320 | VariableDeclaration: function(node, scope, c) { |
| 321 | var target = normalScope(scope); |
| 322 | for (var i = 0; i < node.declarations.length; ++i) { |
| 323 | var decl = node.declarations[i]; |
| 324 | target.vars[decl.id.name] = {type: "var", node: decl.id}; |
| 325 | if (decl.init) c(decl.init, scope, "Expression"); |
| 326 | } |
| 327 | } |
| 328 | }); |
| 329 | |
| 330 | }); |