toolkit/devtools/acorn/walk.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/devtools/acorn/walk.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,313 @@
     1.4 +// AST walker module for Mozilla Parser API compatible trees
     1.5 +
     1.6 +(function(mod) {
     1.7 +  if (typeof exports == "object" && typeof module == "object") return mod(exports); // CommonJS
     1.8 +  if (typeof define == "function" && define.amd) return define(["exports"], mod); // AMD
     1.9 +  mod((this.acorn || (this.acorn = {})).walk = {}); // Plain browser env
    1.10 +})(function(exports) {
    1.11 +  "use strict";
    1.12 +
    1.13 +  // A simple walk is one where you simply specify callbacks to be
    1.14 +  // called on specific nodes. The last two arguments are optional. A
    1.15 +  // simple use would be
    1.16 +  //
    1.17 +  //     walk.simple(myTree, {
    1.18 +  //         Expression: function(node) { ... }
    1.19 +  //     });
    1.20 +  //
    1.21 +  // to do something with all expressions. All Parser API node types
    1.22 +  // can be used to identify node types, as well as Expression,
    1.23 +  // Statement, and ScopeBody, which denote categories of nodes.
    1.24 +  //
    1.25 +  // The base argument can be used to pass a custom (recursive)
    1.26 +  // walker, and state can be used to give this walked an initial
    1.27 +  // state.
    1.28 +  exports.simple = function(node, visitors, base, state) {
    1.29 +    if (!base) base = exports.base;
    1.30 +    function c(node, st, override) {
    1.31 +      var type = override || node.type, found = visitors[type];
    1.32 +      base[type](node, st, c);
    1.33 +      if (found) found(node, st);
    1.34 +    }
    1.35 +    c(node, state);
    1.36 +  };
    1.37 +
    1.38 +  // A recursive walk is one where your functions override the default
    1.39 +  // walkers. They can modify and replace the state parameter that's
    1.40 +  // threaded through the walk, and can opt how and whether to walk
    1.41 +  // their child nodes (by calling their third argument on these
    1.42 +  // nodes).
    1.43 +  exports.recursive = function(node, state, funcs, base) {
    1.44 +    var visitor = funcs ? exports.make(funcs, base) : base;
    1.45 +    function c(node, st, override) {
    1.46 +      visitor[override || node.type](node, st, c);
    1.47 +    }
    1.48 +    c(node, state);
    1.49 +  };
    1.50 +
    1.51 +  function makeTest(test) {
    1.52 +    if (typeof test == "string")
    1.53 +      return function(type) { return type == test; };
    1.54 +    else if (!test)
    1.55 +      return function() { return true; };
    1.56 +    else
    1.57 +      return test;
    1.58 +  }
    1.59 +
    1.60 +  function Found(node, state) { this.node = node; this.state = state; }
    1.61 +
    1.62 +  // Find a node with a given start, end, and type (all are optional,
    1.63 +  // null can be used as wildcard). Returns a {node, state} object, or
    1.64 +  // undefined when it doesn't find a matching node.
    1.65 +  exports.findNodeAt = function(node, start, end, test, base, state) {
    1.66 +    test = makeTest(test);
    1.67 +    try {
    1.68 +      if (!base) base = exports.base;
    1.69 +      var c = function(node, st, override) {
    1.70 +        var type = override || node.type;
    1.71 +        if ((start == null || node.start <= start) &&
    1.72 +            (end == null || node.end >= end))
    1.73 +          base[type](node, st, c);
    1.74 +        if (test(type, node) &&
    1.75 +            (start == null || node.start == start) &&
    1.76 +            (end == null || node.end == end))
    1.77 +          throw new Found(node, st);
    1.78 +      };
    1.79 +      c(node, state);
    1.80 +    } catch (e) {
    1.81 +      if (e instanceof Found) return e;
    1.82 +      throw e;
    1.83 +    }
    1.84 +  };
    1.85 +
    1.86 +  // Find the innermost node of a given type that contains the given
    1.87 +  // position. Interface similar to findNodeAt.
    1.88 +  exports.findNodeAround = function(node, pos, test, base, state) {
    1.89 +    test = makeTest(test);
    1.90 +    try {
    1.91 +      if (!base) base = exports.base;
    1.92 +      var c = function(node, st, override) {
    1.93 +        var type = override || node.type;
    1.94 +        if (node.start > pos || node.end < pos) return;
    1.95 +        base[type](node, st, c);
    1.96 +        if (test(type, node)) throw new Found(node, st);
    1.97 +      };
    1.98 +      c(node, state);
    1.99 +    } catch (e) {
   1.100 +      if (e instanceof Found) return e;
   1.101 +      throw e;
   1.102 +    }
   1.103 +  };
   1.104 +
   1.105 +  // Find the outermost matching node after a given position.
   1.106 +  exports.findNodeAfter = function(node, pos, test, base, state) {
   1.107 +    test = makeTest(test);
   1.108 +    try {
   1.109 +      if (!base) base = exports.base;
   1.110 +      var c = function(node, st, override) {
   1.111 +        if (node.end < pos) return;
   1.112 +        var type = override || node.type;
   1.113 +        if (node.start >= pos && test(type, node)) throw new Found(node, st);
   1.114 +        base[type](node, st, c);
   1.115 +      };
   1.116 +      c(node, state);
   1.117 +    } catch (e) {
   1.118 +      if (e instanceof Found) return e;
   1.119 +      throw e;
   1.120 +    }
   1.121 +  };
   1.122 +
   1.123 +  // Find the outermost matching node before a given position.
   1.124 +  exports.findNodeBefore = function(node, pos, test, base, state) {
   1.125 +    test = makeTest(test);
   1.126 +    if (!base) base = exports.base;
   1.127 +    var max;
   1.128 +    var c = function(node, st, override) {
   1.129 +      if (node.start > pos) return;
   1.130 +      var type = override || node.type;
   1.131 +      if (node.end <= pos && (!max || max.node.end < node.end) && test(type, node))
   1.132 +        max = new Found(node, st);
   1.133 +      base[type](node, st, c);
   1.134 +    };
   1.135 +    c(node, state);
   1.136 +    return max;
   1.137 +  };
   1.138 +
   1.139 +  // Used to create a custom walker. Will fill in all missing node
   1.140 +  // type properties with the defaults.
   1.141 +  exports.make = function(funcs, base) {
   1.142 +    if (!base) base = exports.base;
   1.143 +    var visitor = {};
   1.144 +    for (var type in base) visitor[type] = base[type];
   1.145 +    for (var type in funcs) visitor[type] = funcs[type];
   1.146 +    return visitor;
   1.147 +  };
   1.148 +
   1.149 +  function skipThrough(node, st, c) { c(node, st); }
   1.150 +  function ignore(_node, _st, _c) {}
   1.151 +
   1.152 +  // Node walkers.
   1.153 +
   1.154 +  var base = exports.base = {};
   1.155 +  base.Program = base.BlockStatement = function(node, st, c) {
   1.156 +    for (var i = 0; i < node.body.length; ++i)
   1.157 +      c(node.body[i], st, "Statement");
   1.158 +  };
   1.159 +  base.Statement = skipThrough;
   1.160 +  base.EmptyStatement = ignore;
   1.161 +  base.ExpressionStatement = function(node, st, c) {
   1.162 +    c(node.expression, st, "Expression");
   1.163 +  };
   1.164 +  base.IfStatement = function(node, st, c) {
   1.165 +    c(node.test, st, "Expression");
   1.166 +    c(node.consequent, st, "Statement");
   1.167 +    if (node.alternate) c(node.alternate, st, "Statement");
   1.168 +  };
   1.169 +  base.LabeledStatement = function(node, st, c) {
   1.170 +    c(node.body, st, "Statement");
   1.171 +  };
   1.172 +  base.BreakStatement = base.ContinueStatement = ignore;
   1.173 +  base.WithStatement = function(node, st, c) {
   1.174 +    c(node.object, st, "Expression");
   1.175 +    c(node.body, st, "Statement");
   1.176 +  };
   1.177 +  base.SwitchStatement = function(node, st, c) {
   1.178 +    c(node.discriminant, st, "Expression");
   1.179 +    for (var i = 0; i < node.cases.length; ++i) {
   1.180 +      var cs = node.cases[i];
   1.181 +      if (cs.test) c(cs.test, st, "Expression");
   1.182 +      for (var j = 0; j < cs.consequent.length; ++j)
   1.183 +        c(cs.consequent[j], st, "Statement");
   1.184 +    }
   1.185 +  };
   1.186 +  base.ReturnStatement = function(node, st, c) {
   1.187 +    if (node.argument) c(node.argument, st, "Expression");
   1.188 +  };
   1.189 +  base.ThrowStatement = function(node, st, c) {
   1.190 +    c(node.argument, st, "Expression");
   1.191 +  };
   1.192 +  base.TryStatement = function(node, st, c) {
   1.193 +    c(node.block, st, "Statement");
   1.194 +    if (node.handler) c(node.handler.body, st, "ScopeBody");
   1.195 +    if (node.finalizer) c(node.finalizer, st, "Statement");
   1.196 +  };
   1.197 +  base.WhileStatement = function(node, st, c) {
   1.198 +    c(node.test, st, "Expression");
   1.199 +    c(node.body, st, "Statement");
   1.200 +  };
   1.201 +  base.DoWhileStatement = base.WhileStatement;
   1.202 +  base.ForStatement = function(node, st, c) {
   1.203 +    if (node.init) c(node.init, st, "ForInit");
   1.204 +    if (node.test) c(node.test, st, "Expression");
   1.205 +    if (node.update) c(node.update, st, "Expression");
   1.206 +    c(node.body, st, "Statement");
   1.207 +  };
   1.208 +  base.ForInStatement = function(node, st, c) {
   1.209 +    c(node.left, st, "ForInit");
   1.210 +    c(node.right, st, "Expression");
   1.211 +    c(node.body, st, "Statement");
   1.212 +  };
   1.213 +  base.ForInit = function(node, st, c) {
   1.214 +    if (node.type == "VariableDeclaration") c(node, st);
   1.215 +    else c(node, st, "Expression");
   1.216 +  };
   1.217 +  base.DebuggerStatement = ignore;
   1.218 +
   1.219 +  base.FunctionDeclaration = function(node, st, c) {
   1.220 +    c(node, st, "Function");
   1.221 +  };
   1.222 +  base.VariableDeclaration = function(node, st, c) {
   1.223 +    for (var i = 0; i < node.declarations.length; ++i) {
   1.224 +      var decl = node.declarations[i];
   1.225 +      if (decl.init) c(decl.init, st, "Expression");
   1.226 +    }
   1.227 +  };
   1.228 +
   1.229 +  base.Function = function(node, st, c) {
   1.230 +    c(node.body, st, "ScopeBody");
   1.231 +  };
   1.232 +  base.ScopeBody = function(node, st, c) {
   1.233 +    c(node, st, "Statement");
   1.234 +  };
   1.235 +
   1.236 +  base.Expression = skipThrough;
   1.237 +  base.ThisExpression = ignore;
   1.238 +  base.ArrayExpression = function(node, st, c) {
   1.239 +    for (var i = 0; i < node.elements.length; ++i) {
   1.240 +      var elt = node.elements[i];
   1.241 +      if (elt) c(elt, st, "Expression");
   1.242 +    }
   1.243 +  };
   1.244 +  base.ObjectExpression = function(node, st, c) {
   1.245 +    for (var i = 0; i < node.properties.length; ++i)
   1.246 +      c(node.properties[i].value, st, "Expression");
   1.247 +  };
   1.248 +  base.FunctionExpression = base.FunctionDeclaration;
   1.249 +  base.SequenceExpression = function(node, st, c) {
   1.250 +    for (var i = 0; i < node.expressions.length; ++i)
   1.251 +      c(node.expressions[i], st, "Expression");
   1.252 +  };
   1.253 +  base.UnaryExpression = base.UpdateExpression = function(node, st, c) {
   1.254 +    c(node.argument, st, "Expression");
   1.255 +  };
   1.256 +  base.BinaryExpression = base.AssignmentExpression = base.LogicalExpression = function(node, st, c) {
   1.257 +    c(node.left, st, "Expression");
   1.258 +    c(node.right, st, "Expression");
   1.259 +  };
   1.260 +  base.ConditionalExpression = function(node, st, c) {
   1.261 +    c(node.test, st, "Expression");
   1.262 +    c(node.consequent, st, "Expression");
   1.263 +    c(node.alternate, st, "Expression");
   1.264 +  };
   1.265 +  base.NewExpression = base.CallExpression = function(node, st, c) {
   1.266 +    c(node.callee, st, "Expression");
   1.267 +    if (node.arguments) for (var i = 0; i < node.arguments.length; ++i)
   1.268 +      c(node.arguments[i], st, "Expression");
   1.269 +  };
   1.270 +  base.MemberExpression = function(node, st, c) {
   1.271 +    c(node.object, st, "Expression");
   1.272 +    if (node.computed) c(node.property, st, "Expression");
   1.273 +  };
   1.274 +  base.Identifier = base.Literal = ignore;
   1.275 +
   1.276 +  // A custom walker that keeps track of the scope chain and the
   1.277 +  // variables defined in it.
   1.278 +  function makeScope(prev, isCatch) {
   1.279 +    return {vars: Object.create(null), prev: prev, isCatch: isCatch};
   1.280 +  }
   1.281 +  function normalScope(scope) {
   1.282 +    while (scope.isCatch) scope = scope.prev;
   1.283 +    return scope;
   1.284 +  }
   1.285 +  exports.scopeVisitor = exports.make({
   1.286 +    Function: function(node, scope, c) {
   1.287 +      var inner = makeScope(scope);
   1.288 +      for (var i = 0; i < node.params.length; ++i)
   1.289 +        inner.vars[node.params[i].name] = {type: "argument", node: node.params[i]};
   1.290 +      if (node.id) {
   1.291 +        var decl = node.type == "FunctionDeclaration";
   1.292 +        (decl ? normalScope(scope) : inner).vars[node.id.name] =
   1.293 +          {type: decl ? "function" : "function name", node: node.id};
   1.294 +      }
   1.295 +      c(node.body, inner, "ScopeBody");
   1.296 +    },
   1.297 +    TryStatement: function(node, scope, c) {
   1.298 +      c(node.block, scope, "Statement");
   1.299 +      if (node.handler) {
   1.300 +        var inner = makeScope(scope, true);
   1.301 +        inner.vars[node.handler.param.name] = {type: "catch clause", node: node.handler.param};
   1.302 +        c(node.handler.body, inner, "ScopeBody");
   1.303 +      }
   1.304 +      if (node.finalizer) c(node.finalizer, scope, "Statement");
   1.305 +    },
   1.306 +    VariableDeclaration: function(node, scope, c) {
   1.307 +      var target = normalScope(scope);
   1.308 +      for (var i = 0; i < node.declarations.length; ++i) {
   1.309 +        var decl = node.declarations[i];
   1.310 +        target.vars[decl.id.name] = {type: "var", node: decl.id};
   1.311 +        if (decl.init) c(decl.init, scope, "Expression");
   1.312 +      }
   1.313 +    }
   1.314 +  });
   1.315 +
   1.316 +});

mercurial