def __jsSyntaxCheck(file, codestring): """ Function to check a Javascript source file for syntax errors. @param file source filename (string) @param codestring string containing the code to check (string) @return dictionary with the keys 'error' and 'warnings' which hold a list containing details about the error/ warnings (file name, line number, column, codestring (only at syntax errors), the message, a list with arguments for the message) """ import jasy.script.parse.Parser as jsParser import jasy.script.tokenize.Tokenizer as jsTokenizer codestring = normalizeCode(codestring) try: jsParser.parse(codestring, file) except (jsParser.SyntaxError, jsTokenizer.ParseError) as exc: details = exc.args[0] error, details = details.splitlines() fn, line = details.strip().rsplit(":", 1) error = error.split(":", 1)[1].strip() cline = min(len(codestring.splitlines()), int(line)) - 1 code = codestring.splitlines()[cline] return [{'error': (fn, int(line), 0, code, error)}] except IndexError: error = "Incomplete source file" splittedCode = codestring.splitlines() return [{'error': (file, len(splittedCode) + 1, len(splittedCode[-1]), splittedCode[-1], error)}] return [{}]
def process(self, code): node = Parser.parse(code) ScopeScanner.scan(node) data = Data.ApiData("test") data.scanTree(node) return data
def process(self, code, contextId=""): node = Parser.parse(code) permutation = Permutation.Permutation({ 'debug': False, 'legacy': True, 'engine': 'webkit', 'version': 3, 'fullversion': 3.11 }) Permutate.patch(node, permutation) return Compressor.Compressor().compress(node)
def __getTree(self): """Returns the abstract syntax tree.""" field = "script:tree[%s]" % self.id tree = self.project.getCache().read(field, self.mtime) if not tree: Console.info("Processing class %s...", Console.colorize(self.id, "bold")) Console.indent() tree = Parser.parse(self.getText(), self.id) ScopeScanner.scan(tree) Console.outdent() self.project.getCache().store(field, tree, self.mtime, True) return tree
def parse(self): """ Public method to parse the source. @return dictionary containing the parsed information """ try: self.__root = jsParser.parse(self.__source, self.__file) self.__visit(self.__root) except jsParser.SyntaxError: # ignore syntax errors of the parser pass except jsTokenizer.ParseError: # ignore syntax errors of the tokenizer pass return self.__dict
def process(self, code): node = Parser.parse(code) translation = Translation.TranslationItem(None, id="de_DE", table={ "Hello World": "Hallo Welt", "Short": "Kurz", "Thank you for the flowers": "Danke für die Blumen", "Hello %1!": "Hallo: %1!", "Hello %1! %1!": "Hallo: %1! %1!", "Chat[C:Chat (noum)]": "Unterhaltung", "Chat %1[C:Chat (noum) %1]": "Unterhaltung %1", "You have got a new mail[N:You have got new mails]": {0: "Du hast eine neue E-Mail", 1: "Du hast neue E-Mails"}, "You have got a new mail[N:You have got %1 new mails]": {0: "Du hast eine neue E-Mail", 1: "Du hast %1 neue E-Mail erhalten"} }) TranslationOptimizer.optimize(node, translation) return Compressor.Compressor().compress(node)
def patch(node, permutation): """Replaces all occourences with incoming values.""" modified = False if node.type == "dot" and node.parent.type == "call": assembled = assembleDot(node) # jasy.Env.getValue(key) if assembled == "jasy.Env.getValue" and node.parent.type == "call": callNode = node.parent params = callNode[1] name = params[0].value Console.debug("Found jasy.Env.getValue(%s) in line %s", name, node.line) replacement = __translateToJS(permutation.get(name)) if replacement: replacementNode = Parser.parseExpression(replacement) callNode.parent.replace(callNode, replacementNode) modified = True Console.debug("Replaced with %s", replacement) # jasy.Env.isSet(key, expected) # also supports boolean like: jasy.Env.isSet(key) elif assembled == "jasy.Env.isSet" and node.parent.type == "call": callNode = node.parent params = callNode[1] name = params[0].value Console.debug("Found jasy.Env.isSet(%s) in line %s", name, node.line) replacement = __translateToJS(permutation.get(name)) if replacement is not None: # Auto-fill second parameter with boolean "true" expected = params[1] if len(params) > 1 else Parser.parseExpression("true") if expected.type in ("string", "number", "true", "false"): parsedReplacement = Parser.parseExpression(replacement) expectedValue = getattr(expected, "value", None) if expectedValue is not None: if getattr(parsedReplacement, "value", None) is not None: replacementResult = parsedReplacement.value in str(expected.value).split("|") else: replacementResult = parsedReplacement.type in str(expected.value).split("|") else: replacementResult = parsedReplacement.type == expected.type # Do actual replacement replacementNode = Parser.parseExpression("true" if replacementResult else "false") callNode.parent.replace(callNode, replacementNode) modified = True Console.debug("Replaced with %s", "true" if replacementResult else "false") # jasy.Env.select(key, map) elif assembled == "jasy.Env.select" and node.parent.type == "call": Console.debug("Found jasy.Env.select() in line %s", node.line) callNode = node.parent params = callNode[1] replacement = __translateToJS(permutation.get(params[0].value)) if replacement: parsedReplacement = Parser.parseExpression(replacement) if parsedReplacement.type != "string": raise Exception("jasy.Env.select requires that the given replacement is of type string.") # Directly try to find matching identifier in second param (map) objectInit = params[1] if objectInit.type == "object_init": fallbackNode = None for propertyInit in objectInit: if propertyInit[0].value == "default": fallbackNode = propertyInit[1] elif parsedReplacement.value in str(propertyInit[0].value).split("|"): callNode.parent.replace(callNode, propertyInit[1]) modified = True break if not modified and fallbackNode is not None: callNode.parent.replace(callNode, fallbackNode) modified = True Console.debug("Updated with %s", replacement) # Process children for child in reversed(node): if child is not None: if patch(child, permutation): modified = True return modified
def process(self, code): node = Parser.parse(code) ScopeScanner.scan(node) CombineDeclarations.optimize(node) return Compressor.Compressor().compress(node)
def process(self, code): node = Parser.parse(code) BlockReducer.optimize(node) return Compressor.Compressor().compress(node)
def process(self, code): node = Parser.parse(code) ScopeScanner.scan(node) LocalVariables.optimize(node) return Compressor.Compressor().compress(node)
def process(self, code, contextId=""): node = Parser.parse(code) CryptPrivates.optimize(node, contextId) return Compressor.Compressor().compress(node)
def process(self, code): return Compressor.Compressor().compress(Parser.parse(code))
def process(self, code): tree = Parser.parse(code) meta = MetaData(tree) return meta
def process(self, code): node = Parser.parse(code) DeadCode.cleanup(node) return Compressor.Compressor().compress(node)
def process(self, code): node = Parser.parse(code) Unused.cleanup(node) return Compressor.Compressor().compress(node)
def patch(node, permutation): """Replaces all occourences with incoming values.""" modified = False if node.type == "dot" and node.parent.type == "call": assembled = assembleDot(node) # jasy.Env.getValue(key) if assembled == "jasy.Env.getValue" and node.parent.type == "call": callNode = node.parent params = callNode[1] name = params[0].value Console.debug("Found jasy.Env.getValue(%s) in line %s", name, node.line) replacement = __translateToJS(permutation.get(name)) if replacement: replacementNode = Parser.parseExpression(replacement) callNode.parent.replace(callNode, replacementNode) modified = True Console.debug("Replaced with %s", replacement) # jasy.Env.isSet(key, expected) # also supports boolean like: jasy.Env.isSet(key) elif assembled == "jasy.Env.isSet" and node.parent.type == "call": callNode = node.parent params = callNode[1] name = params[0].value Console.debug("Found jasy.Env.isSet(%s) in line %s", name, node.line) replacement = __translateToJS(permutation.get(name)) if replacement is not None: # Auto-fill second parameter with boolean "true" expected = params[1] if len( params) > 1 else Parser.parseExpression("true") if expected.type in ("string", "number", "true", "false"): parsedReplacement = Parser.parseExpression(replacement) expectedValue = getattr(expected, "value", None) if expectedValue is not None: if getattr(parsedReplacement, "value", None) is not None: replacementResult = parsedReplacement.value in str( expected.value).split("|") else: replacementResult = parsedReplacement.type in str( expected.value).split("|") else: replacementResult = parsedReplacement.type == expected.type # Do actual replacement replacementNode = Parser.parseExpression( "true" if replacementResult else "false") callNode.parent.replace(callNode, replacementNode) modified = True Console.debug("Replaced with %s", "true" if replacementResult else "false") # jasy.Env.select(key, map) elif assembled == "jasy.Env.select" and node.parent.type == "call": Console.debug("Found jasy.Env.select() in line %s", node.line) callNode = node.parent params = callNode[1] replacement = __translateToJS(permutation.get(params[0].value)) if replacement: parsedReplacement = Parser.parseExpression(replacement) if parsedReplacement.type != "string": raise Exception( "jasy.Env.select requires that the given replacement is of type string." ) # Directly try to find matching identifier in second param (map) objectInit = params[1] if objectInit.type == "object_init": fallbackNode = None for propertyInit in objectInit: if propertyInit[0].value == "default": fallbackNode = propertyInit[1] elif parsedReplacement.value in str( propertyInit[0].value).split("|"): callNode.parent.replace(callNode, propertyInit[1]) modified = True break if not modified and fallbackNode is not None: callNode.parent.replace(callNode, fallbackNode) modified = True Console.debug("Updated with %s", replacement) # Process children for child in reversed(node): if child is not None: if patch(child, permutation): modified = True return modified