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