示例#1
0
def jsCheckSyntax(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.js.parse.Parser as jsParser
    import jasy.js.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)}]
    
    return [{}]
示例#2
0
文件: api.py 项目: zynga/jasy
    def process(self, code):
        node = Parser.parse(code)
        ScopeScanner.scan(node)
        data = Data.ApiData("test")
        data.scanTree(node)

        return data
示例#3
0
    def getTree(self, permutation=None, cleanup=True):
        """
        Returns the tree (of nodes from the parser) of the class. This parses the class,
        creates the tree, applies and optional permutation, scans for variables usage 
        and puts the tree into the cache before returning it. The cache works with the
        permutation, so every permutated tree is cached separately.
        """
        
        permutation = self.filterPermutation(permutation)
        
        field = "tree[%s]-%s-%s" % (self.__id, permutation, cleanup)
        tree = self.__cache.read(field, self.__mtime)
        if tree is not None:
            return tree
            
        # Parse tree
        tree = Parser.parse(self.getText(), self.__name)

        # Apply permutation
        if permutation:
            jasy.js.clean.Permutate.patch(tree, permutation)

        # Remove dead code
        if cleanup:
            jasy.js.clean.DeadCode.cleanup(tree)

        # Scan for variable usage
        ScopeScanner.scan(tree)
        
        # Remove unused variables/functions
        if cleanup:
            jasy.js.clean.Unused.cleanup(tree)
        
        self.__cache.store(field, tree, self.__mtime, True)
        return tree
示例#4
0
文件: inject.py 项目: Val9/jasy
 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)
示例#5
0
文件: api.py 项目: Andais/jasy
 def process(self, code):
     node = Parser.parse(code)
     ScopeScanner.scan(node)
     data = Data.ApiData("test")
     data.scanTree(node)
     
     return data
示例#6
0
文件: Class.py 项目: csakatoku/jasy
    def getTree(self, permutation=None, cleanup=True):
        """
        Returns the tree (of nodes from the parser) of the class. This parses the class,
        creates the tree, applies and optional permutation, scans for variables usage 
        and puts the tree into the cache before returning it. The cache works with the
        permutation, so every permutated tree is cached separately.
        """

        permutation = self.filterPermutation(permutation)

        field = "tree[%s]-%s-%s" % (self.__id, permutation, cleanup)
        tree = self.__cache.read(field, self.__mtime)
        if tree is not None:
            return tree

        # Parse tree
        tree = Parser.parse(self.getText(), self.__name)

        # Apply permutation
        if permutation:
            jasy.js.clean.Permutate.patch(tree, permutation)

        # Remove dead code
        if cleanup:
            jasy.js.clean.DeadCode.cleanup(tree)

        # Scan for variable usage
        ScopeScanner.scan(tree)

        # Remove unused variables/functions
        if cleanup:
            jasy.js.clean.Unused.cleanup(tree)

        self.__cache.store(field, tree, self.__mtime, True)
        return tree
示例#7
0
    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)
示例#8
0
 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)
示例#9
0
文件: inject.py 项目: Andais/jasy
 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)    
示例#10
0
文件: Class.py 项目: dadicool/jasy
 def __getTree(self, context=None):
     
     field = "tree[%s]" % self.id
     tree = self.project.getCache().read(field, self.mtime)
     if not tree:
         info("Processing class %s %s...", colorize(self.id, "bold"), colorize("[%s]" % context, "cyan"))
         
         indent()
         tree = Parser.parse(self.getText(), self.id)
         ScopeScanner.scan(tree)
         outdent()
         
         self.project.getCache().store(field, tree, self.mtime, True)
     
     return tree
示例#11
0
文件: jsclbr.py 项目: Darriall/eric
 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
示例#12
0
文件: Class.py 项目: zynga/jasy
    def __getTree(self, context=None):

        field = "tree[%s]" % self.id
        tree = self.project.getCache().read(field, self.mtime)
        if not tree:
            Console.info("Processing class %s %s...",
                         Console.colorize(self.id, "bold"),
                         Console.colorize("[%s]" % context, "cyan"))

            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
示例#13
0
    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
示例#14
0
    def process(self, code):
        node = Parser.parse(code)

        translation = Translation.Translation(
            "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 (noum)": "Unterhaltung",
                "Chat (noum) %1": "Unterhaltung %1",
                "You have got a new mail": "Du hast eine neue E-Mail",
                "You have got new mails": "Du hast neue E-Mails",
                "You have got %1 new mails": "Du hast %1 neue E-Mail erhalten"
            })

        translation.patch(node)

        return Compressor.Compressor().compress(node)
示例#15
0
    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)        
示例#16
0
文件: translation.py 项目: Val9/jasy
    def process(self, code):
        node = Parser.parse(code)

        translation = Translation.Translation("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 (noum)": "Unterhaltung",
            "Chat (noum) %1": "Unterhaltung %1",
            
            "You have got a new mail": "Du hast eine neue E-Mail",
            "You have got new mails": "Du hast neue E-Mails",
            "You have got %1 new mails": "Du hast %1 neue E-Mail erhalten"
            
        })
        
        translation.patch(node)
        
        return Compressor.Compressor().compress(node)        
示例#17
0
 def process(self, code):
     node = Parser.parse(code)
     ScopeScanner.scan(node)
     CombineDeclarations.optimize(node)
     return Compressor.Compressor().compress(node)
示例#18
0
文件: Permutate.py 项目: zynga/jasy
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 != 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 != None:
            if patch(child, permutation):
                modified = True

    return modified
示例#19
0
文件: combinedecl.py 项目: Val9/jasy
 def process(self, code):
     node = Parser.parse(code)
     ScopeScanner.scan(node)
     CombineDeclarations.optimize(node)
     return Compressor.Compressor().compress(node)
示例#20
0
 def process(self, code):
     node = Parser.parse(code)
     BlockReducer.optimize(node)
     return Compressor.Compressor().compress(node)
示例#21
0
文件: privates.py 项目: Val9/jasy
 def process(self, code, contextId=""):
     node = Parser.parse(code)
     CryptPrivates.optimize(node, contextId)
     return Compressor.Compressor().compress(node)        
示例#22
0
 def process(self, code):
     node = Parser.parse(code)
     ScopeScanner.scan(node)
     LocalVariables.optimize(node)
     return Compressor.Compressor().compress(node)
示例#23
0
 def process(self, code):
     node = Parser.parse(code)
     return node
示例#24
0
文件: meta.py 项目: Andais/jasy
 def process(self, code):
     tree = Parser.parse(code)
     meta = MetaData(tree)
     return meta
示例#25
0
文件: Permutate.py 项目: Zenwolf/jasy
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 != 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 != None:
            if patch(child, permutation):
                modified = True

    return modified


    
示例#26
0
 def process(self, code):
     tree = Parser.parse(code)
     meta = MetaData(tree)
     return meta
示例#27
0
文件: compressor.py 项目: Andais/jasy
 def process(self, code):
     return Compressor.Compressor().compress(Parser.parse(code))
示例#28
0
文件: deadcode.py 项目: zynga/jasy
 def process(self, code):
     node = Parser.parse(code)
     DeadCode.cleanup(node)
     return Compressor.Compressor().compress(node)
示例#29
0
 def process(self, code):
     return Compressor.Compressor().compress(Parser.parse(code))
示例#30
0
文件: unused.py 项目: Val9/jasy
 def process(self, code):
     node = Parser.parse(code)
     Unused.cleanup(node)
     return Compressor.Compressor().compress(node)
示例#31
0
 def process(self, code):
     node = Parser.parse(code)
     BlockReducer.optimize(node)
     return Compressor.Compressor().compress(node)
示例#32
0
 def process(self, code):
     node = Parser.parse(code)
     ScopeScanner.scan(node)
     LocalVariables.optimize(node)
     return Compressor.Compressor().compress(node)
示例#33
0
文件: comments.py 项目: Val9/jasy
 def process(self, code):
     node = Parser.parse(code)
     return node
示例#34
0
 def process(self, code, contextId=""):
     node = Parser.parse(code)
     CryptPrivates.optimize(node, contextId)
     return Compressor.Compressor().compress(node)