michael@0: /* -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ft=javascript ts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: "use strict"; michael@0: michael@0: const Ci = Components.interfaces; michael@0: const Cu = Components.utils; michael@0: michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: const { DevToolsUtils } = Cu.import("resource://gre/modules/devtools/DevToolsUtils.jsm", {}); michael@0: michael@0: XPCOMUtils.defineLazyModuleGetter(this, michael@0: "Reflect", "resource://gre/modules/reflect.jsm"); michael@0: michael@0: this.EXPORTED_SYMBOLS = ["Parser", "ParserHelpers", "SyntaxTreeVisitor"]; michael@0: michael@0: /** michael@0: * A JS parser using the reflection API. michael@0: */ michael@0: this.Parser = function Parser() { michael@0: this._cache = new Map(); michael@0: this.errors = []; michael@0: }; michael@0: michael@0: Parser.prototype = { michael@0: /** michael@0: * Gets a collection of parser methods for a specified source. michael@0: * michael@0: * @param string aSource michael@0: * The source text content. michael@0: * @param string aUrl [optional] michael@0: * The source url. The AST nodes will be cached, so you can use this michael@0: * identifier to avoid parsing the whole source again. michael@0: */ michael@0: get: function(aSource, aUrl = "") { michael@0: // Try to use the cached AST nodes, to avoid useless parsing operations. michael@0: if (this._cache.has(aUrl)) { michael@0: return this._cache.get(aUrl); michael@0: } michael@0: michael@0: // The source may not necessarily be JS, in which case we need to extract michael@0: // all the scripts. Fastest/easiest way is with a regular expression. michael@0: // Don't worry, the rules of using a