def optimizeTree(tree): try: if ["comments"] == optimize: # do a mere comment stripping commentoptimizer.patch(tree) # "variants" prunes parts of the tree, so all subsequent optimizations benefit if "variants" in optimize: variantoptimizer.search(tree, variantSet, self.id) # 'statics' has to come before 'privates', as it needs the original key names in tree # if features should be removed recursively, this has to be controlled on the calling # level. if "statics" in optimize: if not featureMap: console.warn("Empty feature map passed to static methods optimization; skipping") elif self.type == "static" and self.id in featureMap: featureoptimizer.patch(tree, self, featureMap) if "basecalls" in optimize: basecalloptimizer.patch(tree) if "privates" in optimize: privatesMap = load_privates() privateoptimizer.patch(tree, id, privatesMap) write_privates(privatesMap) if "strings" in optimize: tree = self._stringOptimizer(tree) if "variables" in optimize: variableoptimizer.search(tree) except Exception, e: raise RuntimeError("Problem optimizing %s; probably a syntax problem?!" % self.id)
def optimizeTree(tree): if "comments" in optimize: commentoptimizer.patch(tree) # "variants" prunes parts of the tree, so all subsequent optimizations benefit if "variants" in optimize: variantoptimizer.search(tree, variantSet, self.id) # 'statics' has to come before 'privates', as it needs the original key names in tree # if features should be removed recursively, this has to be controlled on the calling # level. if "statics" in optimize: if not featureMap: console.warn( "Empty feature map passed to static methods optimization; skipping" ) elif self.type == 'static' and self.id in featureMap: featureoptimizer.patch(tree, self, featureMap) if "basecalls" in optimize: basecalloptimizer.patch(tree) if "privates" in optimize: privatesMap = load_privates() privateoptimizer.patch(tree, id, privatesMap) write_privates(privatesMap) if "strings" in optimize: tree = self._stringOptimizer(tree) if "variables" in optimize: variableoptimizer.search(tree) return tree
def getTree(self, fileId, variants=None): fileEntry = self._classes[fileId] filePath = fileEntry["path"] if variants: cacheId = "tree-%s-%s" % (filePath, util.toString(variants)) else: cacheId = "tree-%s" % filePath tradeSpaceForSpeed = False # Caution: setting this to True seems to make builds slower, at least on some platforms!? tree = self._cache.read(cacheId, filePath, memory=tradeSpaceForSpeed) if tree != None: return tree # Lookup for unoptimized tree if variants != None: tree = self._cache.read("tree-%s" % fileId, filePath, memory=tradeSpaceForSpeed) # Tree still undefined?, create it! if tree == None: self._console.debug("Parsing file: %s..." % fileId) self._console.indent() fileContent = filetool.read(fileEntry["path"], fileEntry["encoding"]) tokens = tokenizer.parseStream(fileContent, fileId) self._console.outdent() self._console.debug("Generating tree: %s..." % fileId) self._console.indent() tree = treegenerator.createSyntaxTree(tokens) # allow exceptions to propagate # store unoptimized tree self._cache.write("tree-%s" % fileId, tree, memory=tradeSpaceForSpeed, writeToFile=True) self._console.outdent() # Call variant optimizer if variants != None: self._console.debug("Selecting variants: %s..." % fileId) self._console.indent() variantoptimizer.search(tree, variants, fileId) self._console.outdent() # store optimized tree self._cache.write(cacheId, tree, memory=tradeSpaceForSpeed, writeToFile=True) return tree
def optimizeTree(tree): if "comments" in optimize: commentoptimizer.patch(tree) # "variants" prunes parts of the tree, so all subsequent optimizations benefit if "variants" in optimize: variantoptimizer.search(tree, variantSet, self.id) # 'statics' has to come before 'privates', as it needs the original key names in tree # if features should be removed recursively, this has to be controlled on the calling # level. if "statics" in optimize: if not featureMap: console.warn( "Empty feature map passed to static methods optimization; skipping" ) elif self.type == 'static' and self.id in featureMap: optimzed_features = featureoptimizer.patch( tree, self, featureMap) if optimzed_features: optimized_statics_overall = load_features() copy = optimized_statics_overall.copy() copy.update(optimzed_features) write_features(copy) if "basecalls" in optimize: basecalloptimizer.patch(tree) if "privates" in optimize: privatesMap = load_privates() privateoptimizer.patch(tree, id, privatesMap) write_privates(privatesMap) if "globals" in optimize: tree = globalsoptimizer.process( tree ) # need to re-assign as this optimizer might change the root node if "strings" in optimize: tree = stringoptimizer.process(tree, self.id) if "variables" in optimize: variableoptimizer.search(tree) return tree
def tree(self, variantSet={}): context = self.context cache = context['cache'] tradeSpaceForSpeed = False # Caution: setting this to True seems to make builds slower, at least on some platforms!? # Construct the right cache id unoptCacheId = "tree-%s-%s" % (self.path, util.toString({})) classVariants = [] tree = None classVariants = self.classVariants(generate=False) # just check the cache if classVariants == None: tree = self._getSourceTree(unoptCacheId, tradeSpaceForSpeed) classVariants= self._variantsFromTree(tree) relevantVariants = self.projectClassVariantsToCurrent(classVariants, variantSet) cacheId = "tree-%s-%s" % (self.path, util.toString(relevantVariants)) # Get the right tree to return if cacheId == unoptCacheId and tree: # early return optimization return tree opttree, cacheMod = cache.read(cacheId, self.path, memory=tradeSpaceForSpeed) if not opttree: # start from source tree if tree: opttree = tree else: opttree = self._getSourceTree(unoptCacheId, tradeSpaceForSpeed) # do we have to optimze? if cacheId == unoptCacheId: return opttree else: context["console"].debug("Selecting variants: %s..." % self.id) context["console"].indent() variantoptimizer.search(opttree, variantSet, self.id) context["console"].outdent() # store optimized tree #print "Caching %s" % cacheId cache.write(cacheId, opttree, memory=tradeSpaceForSpeed, writeToFile=True) return opttree
def optimizeTree(tree): if "comments" in optimize: commentoptimizer.patch(tree) # "variants" prunes parts of the tree, so all subsequent optimizations benefit if "variants" in optimize: variantoptimizer.search(tree, variantSet, self.id) # 'statics' has to come before 'privates', as it needs the original key names in tree # if features should be removed recursively, this has to be controlled on the calling # level. if "statics" in optimize: if not featureMap: console.warn("Empty feature map passed to static methods optimization; skipping") elif self.type == 'static' and self.id in featureMap: optimzed_features = featureoptimizer.patch(tree, self, featureMap) if optimzed_features: optimized_statics_overall = load_features() copy = optimized_statics_overall.copy() copy.update(optimzed_features) write_features(copy) if "basecalls" in optimize: basecalloptimizer.patch(tree) if "privates" in optimize: privatesMap = load_privates() privateoptimizer.patch(tree, id, privatesMap) write_privates(privatesMap) if "globals" in optimize: tree = globalsoptimizer.process(tree) # need to re-assign as this optimizer might change the root node if "strings" in optimize: tree = stringoptimizer.process(tree, self.id) if "variables" in optimize: variableoptimizer.search(tree) return tree
def run_compile(fileName, fileContent, options, args): fileId = fileName tokens = tokenizer.Tokenizer().parseStream(fileContent, fileName) if not options.quiet: print(">>> Creating tree...") tree = treegenerator.createFileTree(tokens) tree = scopes.create_scopes(tree) # optimizing tree if len(options.variants) > 0: if not options.quiet: print(">>> Selecting variants...") varmap = {} for entry in options.variants: pos = entry.index(":") varmap[entry[0:pos]] = entry[pos + 1:] variantoptimizer.search(tree, varmap, fileId) if options.all or options.basecalls: if not options.quiet: print(">>> Optimizing basecalls...") basecalloptimizer.patch(tree) #if options.all or options.inline: # if not options.quiet: # print(">>> Optimizing inline...") # inlineoptimizer.patch(tree) if options.all or options.strings: if not options.quiet: print(">>> Optimizing strings...") _optimizeStrings(tree, fileId) if options.all or options.variables: if not options.quiet: print(">>> Optimizing variables...") variableoptimizer.search(tree) if options.all or options.globals: if not options.quiet: print(">>> Optimizing globals...") tree = globalsoptimizer.process(tree) if options.all or options.privates: if not options.quiet: print(">>> Optimizing privates...") privates = {} if options.cache: cache = Cache(options.cache, interruptRegistry=interruptRegistry) privates, _ = cache.read(options.privateskey) if privates == None: privates = {} privateoptimizer.patch(tree, fileId, privates) if options.cache: cache.write(options.privateskey, privates) if not options.quiet: print(">>> Compiling...") result = [u''] result = Packer().serializeNode(tree, None, result, True) result = u''.join(result) print(result.encode('utf-8')) return
def run_compile(fileName, fileContent, options, args): fileId = fileName tokens = tokenizer.Tokenizer().parseStream(fileContent, fileName) if not options.quiet: print(">>> Creating tree...") tree = treegenerator.createFileTree(tokens) tree = scopes.create_scopes(tree) # optimizing tree if len(options.variants) > 0: if not options.quiet: print(">>> Selecting variants...") varmap = {} for entry in options.variants: pos = entry.index(":") varmap[entry[0:pos]] = entry[pos+1:] variantoptimizer.search(tree, varmap, fileId) if options.all or options.basecalls: if not options.quiet: print(">>> Optimizing basecalls...") basecalloptimizer.patch(tree) #if options.all or options.inline: # if not options.quiet: # print(">>> Optimizing inline...") # inlineoptimizer.patch(tree) if options.all or options.strings: if not options.quiet: print(">>> Optimizing strings...") _optimizeStrings(tree, fileId) if options.all or options.variables: if not options.quiet: print(">>> Optimizing variables...") variableoptimizer.search(tree) if options.all or options.globals: if not options.quiet: print(">>> Optimizing globals...") tree = globalsoptimizer.process(tree) if options.all or options.privates: if not options.quiet: print(">>> Optimizing privates...") privates = {} if options.cache: cache = Cache(options.cache, interruptRegistry=interruptRegistry ) privates, _ = cache.read(options.privateskey) if privates == None: privates = {} privateoptimizer.patch(tree, fileId, privates) if options.cache: cache.write(options.privateskey, privates) if not options.quiet: print(">>> Compiling...") result = [u''] result = Packer().serializeNode(tree, None, result, True) result = u''.join(result) print(result.encode('utf-8')) return
def main(): parser = optparse.OptionParser(option_class=ExtendAction) usage_str = '''%prog [options] file.js,...''' parser.set_usage(usage_str) # General flags parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="verbose output mode (extra verbose)") parser.add_option("-q", "--quiet", action="store_true", dest="quiet", default=False, help="quiet output") # Optimization flags parser.add_option("-n", "--variables", action="store_true", dest="variables", default=False, help="optimize variables") parser.add_option("-s", "--strings", action="store_true", dest="strings", default=False, help="optimize strings") parser.add_option("-p", "--privates", action="store_true", dest="privates", default=False, help="optimize privates") parser.add_option("-b", "--basecalls", action="store_true", dest="basecalls", default=False, help="optimize basecalls") parser.add_option("-i", "--inline", action="store_true", dest="inline", default=False, help="optimize inline") parser.add_option("--all", action="store_true", dest="all", default=False, help="optimize all") # Variant support parser.add_option("--variant", action="extend", dest="variants", metavar="KEY:VALUE", type="string", default=[], help="Selected variants") # Action modifier parser.add_option("--pretty", action="store_true", dest="pretty", default=False, help="print out pretty printed") parser.add_option("--tree", action="store_true", dest="tree", default=False, help="print out tree") parser.add_option("--lint", action="store_true", dest="lint", default=False, help="ecmalint the file") # Cache support parser.add_option("-c", "--cache", dest="cache", metavar="CACHEPATH", type="string", default="", help="path to cache directory") parser.add_option("--privateskey", dest="privateskey", metavar="CACHEKEY", type="string", default="", help="cache key for privates") # # Process arguments # (options, args) = parser.parse_args(sys.argv[1:]) if len(args) == 0: print ">>> Missing filename!" return if not options.quiet: print ">>> Parsing file..." fileName = args[0] fileContent = filetool.read(fileName, "utf-8") fileId = "xxx" tokens = tokenizer.parseStream(fileContent, fileName) if not options.quiet: print ">>> Creating tree..." tree = treegenerator.createSyntaxTree(tokens) # # Optimizing tree # if len(options.variants) > 0: if not options.quiet: print ">>> Selecting variants..." varmap = {} for entry in options.variants: pos = entry.index(":") varmap[entry[0:pos]] = entry[pos + 1:] variantoptimizer.search(tree, varmap, fileId) if options.all or options.basecalls: if not options.quiet: print ">>> Optimizing basecalls..." basecalloptimizer.patch(tree) if options.all or options.inline: if not options.quiet: print ">>> Optimizing inline..." inlineoptimizer.patch(tree) if options.all or options.strings: if not options.quiet: print ">>> Optimizing strings..." _optimizeStrings(tree, fileId) if options.all or options.variables: if not options.quiet: print ">>> Optimizing variables..." variableoptimizer.search(tree) if options.all or options.privates: if not options.quiet: print ">>> Optimizing privates..." if options.cache: cache = Cache(options.cache, Log()) privates, _ = cache.read(options.privateskey) if privates != None: privateoptimizer.load(privates) privateoptimizer.patch(tree, fileId) if options.cache: cache.write(options.privateskey, privateoptimizer.get()) # # Output the result # if options.lint: if not options.quiet: print ">>> Executing ecmalint..." print "Needs implementation" elif options.tree: if not options.quiet: print ">>> Printing out tree..." print tree.toXml().encode('utf-8') else: if not options.quiet: print ">>> Compiling..." compiled = _compileTree(tree, options.pretty) print compiled.encode('utf-8')
def main(): parser = optparse.OptionParser(option_class=ExtendAction) usage_str = '''%prog [options] file.js,...''' parser.set_usage(usage_str) # General flags parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="verbose output mode (extra verbose)") parser.add_option("-q", "--quiet", action="store_true", dest="quiet", default=False, help="quiet output") # Optimization flags parser.add_option("-n", "--variables", action="store_true", dest="variables", default=False, help="optimize variables") parser.add_option("-s", "--strings", action="store_true", dest="strings", default=False, help="optimize strings") parser.add_option("-p", "--privates", action="store_true", dest="privates", default=False, help="optimize privates") parser.add_option("-b", "--basecalls", action="store_true", dest="basecalls", default=False, help="optimize basecalls") parser.add_option("-i", "--inline", action="store_true", dest="inline", default=False, help="optimize inline") parser.add_option("-r", "--variants", action="store_true", dest="variantsopt", default=False, help="optimize variants") parser.add_option("-m", "--comments", action="store_true", dest="comments", default=False, help="optimize comments") parser.add_option("--all", action="store_true", dest="all", default=False, help="optimize all") # Variant support parser.add_option("--variant", action="extend", dest="variants", metavar="KEY:VALUE", type="string", default=[], help="Selected variants") # Action modifier parser.add_option("--pretty", action="store_true", dest="pretty", default=False, help="print out pretty printed") parser.add_option("--tree", action="store_true", dest="tree", default=False, help="print out tree") parser.add_option("--lint", action="store_true", dest="lint", default=False, help="ecmalint the file") # Cache support parser.add_option("-c", "--cache", dest="cache", metavar="CACHEPATH", type="string", default="", help="path to cache directory") parser.add_option("--privateskey", dest="privateskey", metavar="CACHEKEY", type="string", default="", help="cache key for privates") # # Process arguments # (options, args) = parser.parse_args(sys.argv[1:]) if len(args) == 0: print ">>> Missing filename!" return if not options.quiet: print ">>> Parsing file..." fileName = args[0] fileContent = filetool.read(fileName, "utf-8") fileId = "xxx" tokens = tokenizer.parseStream(fileContent, fileName) if not options.quiet: print ">>> Creating tree..." tree = treegenerator.createFileTree(tokens) # # Optimizing tree # if len(options.variants) > 0: if not options.quiet: print ">>> Selecting variants..." varmap = {} for entry in options.variants: pos = entry.index(":") varmap[entry[0:pos]] = entry[pos+1:] variantoptimizer.search(tree, varmap, fileId) if options.all or options.basecalls: if not options.quiet: print ">>> Optimizing basecalls..." basecalloptimizer.patch(tree) if options.all or options.inline: if not options.quiet: print ">>> Optimizing inline..." inlineoptimizer.patch(tree) if options.all or options.strings: if not options.quiet: print ">>> Optimizing strings..." _optimizeStrings(tree, fileId) if options.all or options.variables: if not options.quiet: print ">>> Optimizing variables..." variableoptimizer.search(tree) if options.all or options.privates: if not options.quiet: print ">>> Optimizing privates..." privates = {} if options.cache: cache = Cache(options.cache, interruptRegistry=interruptRegistry ) privates, _ = cache.read(options.privateskey) if privates == None: privates = {} privateoptimizer.patch(tree, fileId, privates) if options.cache: cache.write(options.privateskey, privates) # # Output the result # if options.lint: if not options.quiet: print ">>> Executing ecmalint..." print "Needs implementation" elif options.tree: if not options.quiet: print ">>> Printing out tree..." print tree.toXml().encode('utf-8') else: if not options.quiet: print ">>> Compiling..." compiled = _compileTree(tree, options.pretty) print compiled.encode('utf-8')
def main(): parser = optparse.OptionParser(option_class=ExtendAction) usage_str = '''%prog [options] file.js,...''' parser.set_usage(usage_str) # General flags parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="verbose output mode (extra verbose)") # Optimization flags parser.add_option("-n", "--variables", action="store_true", dest="variables", default=False, help="optimize variables") parser.add_option("-s", "--strings", action="store_true", dest="strings", default=False, help="optimize strings") parser.add_option("-p", "--privates", action="store_true", dest="privates", default=False, help="optimize privates") parser.add_option("-b", "--basecalls", action="store_true", dest="basecalls", default=False, help="optimize basecalls") parser.add_option("-i", "--inline", action="store_true", dest="inline", default=False, help="optimize inline") parser.add_option("--all", action="store_true", dest="all", default=False, help="optimize all") # Variant support parser.add_option("--variant", action="extend", dest="variants", metavar="KEY:VALUE", type="string", default=[], help="Selected variants") # Action modifier parser.add_option("--pretty", action="store_true", dest="pretty", default=False, help="print out pretty printed") parser.add_option("--tree", action="store_true", dest="tree", default=False, help="print out tree") parser.add_option("--apiXml", action="store_true", dest="apiXml", default=False, help="print out api data as XML") parser.add_option("--apiJson", action="store_true", dest="apiJson", default=False, help="print out api data as JSON") parser.add_option("--lint", action="store_true", dest="lint", default=False, help="ecmalint the file") # # Process arguments # (options, args) = parser.parse_args(sys.argv[1:]) if len(args) == 0: print ">>> Missing filename!" return print ">>> Parsing file..." fileName = args[0] fileContent = filetool.read(fileName, "utf-8") fileId = "xxx" tokens = tokenizer.parseStream(fileContent, fileName) print ">>> Creating tree..." tree = treegenerator.createSyntaxTree(tokens) # # Optimizing tree # if len(options.variants) > 0: print ">>> Selecting variants..." variantoptimizer.search(tree, options.variants, fileId) if options.all or options.basecalls: print ">>> Optimizing basecalls..." basecalloptimizer.patch(tree) if options.all or options.inline: print ">>> Optimizing inline..." inlineoptimizer.patch(tree) if options.all or options.strings: print ">>> Optimizing strings..." _optimizeStrings(tree, fileId) if options.all or options.variables: print ">>> Optimizing variables..." variableoptimizer.search(tree) if options.all or options.privates: print ">>> Optimizing privates..." privateoptimizer.patch(tree, fileId) # # Output the result # if options.apiXml or options.apiJson: (data, hasError) = api.createDoc(tree) if hasError: print "Error in API docs!" elif options.apiXml: print ">>> API data as XML..." print data.toXml() else: print ">>> API data as JSON..." print data.toJson() elif options.lint: print ">>> Executing ecmalint..." print "Needs implementation" elif options.tree: print ">>> Printing out tree..." print tree.toXml() else: print ">>> Compiling..." compiled = _compileTree(tree, options.pretty) print compiled