toolkit/devtools/acorn/walk.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial