Exemplo n.º 1
0
    def _get_external_funcs(self):
        """
        Get a list of external functions called in the macros.
        """

        # Get the names of all called functions, local functions, and defined variables.
        call_visitor = function_call_visitor()
        defn_visitor = function_defn_visitor()
        var_visitor = var_defn_visitor()
        import_visitor = function_import_visitor()
        for module in self.modules:
            module.accept(call_visitor)
            module.accept(defn_visitor)
            module.accept(var_visitor)
            module.accept(import_visitor)

        # Eliminate variables and local functions from the list of called functions.
        r = []
        for f in call_visitor.called_funcs:
            if ((f in defn_visitor.funcs) or (f in var_visitor.variables)
                    or (len(f) == 0) or (("." in f) and (not "Shell" in f))):
                continue

            # Resolve aliases of imported functions to the actual function.
            if (f in import_visitor.aliases):
                if (len(import_visitor.funcs[f]) > 0):
                    r.append(import_visitor.funcs[f])
                continue

            # Regular local function call.
            r.append(f)

        # Sort and return the fingerprint function list.
        r.sort()
        return r
Exemplo n.º 2
0
    def _handle_func_decls(self, tokens):
        """
        Look for functions/subs declared anywhere, including inside the body 
        of other functions/subs.
        """

        # Look through each parsed item in the module for function/sub
        # definitions.
        for token in tokens:
            if (not hasattr(token, "accept")):
                continue
            func_visitor = function_defn_visitor()
            token.accept(func_visitor)
            for i in func_visitor.func_objects:

                # Sub to add?
                if isinstance(i, Sub):
                    if (log.getEffectiveLevel() == logging.DEBUG):
                        log.debug("saving sub decl: %r" % i.name)
                    self.subs[i.name] = i

                # Func to add?
                elif isinstance(i, Function):
                    if (log.getEffectiveLevel() == logging.DEBUG):
                        log.debug("saving func decl: %r" % i.name)
                    self.functions[i.name] = i
Exemplo n.º 3
0
    def _get_external_funcs(self):
        """
        Get a list of external functions called in the macros.
        """

        # Get the names of all called functions, local functions, and defined variables.
        call_visitor = function_call_visitor()
        defn_visitor = function_defn_visitor()
        var_visitor = var_defn_visitor()
        for module in self.modules:
            module.accept(call_visitor)
            module.accept(defn_visitor)
            module.accept(var_visitor)
        """
        print("************ CALLED FUNCS *************************")
        print(call_visitor.called_funcs)
        print("************ DEFINED FUNCS *************************")
        print(defn_visitor.funcs)
        print("************ DEFINED VARIABLES *************************")
        print(var_visitor.variables)
        """

        # Eliminate variables and local functions from the list of called functions.
        r = []
        for f in call_visitor.called_funcs:
            if ((f in defn_visitor.funcs) or (f in var_visitor.variables)
                    or (len(f) == 0) or (("." in f) and (not "Shell" in f))):
                continue
            r.append(f)
        r.sort()
        return r
Exemplo n.º 4
0
    def __init__(self, original_str, location, tokens):

        super(Module, self).__init__(original_str, location, tokens)

        self.name = None
        self.code = None  # set by ViperMonkey after parsing
        self.attributes = {}
        self.options = []
        self.functions = {}
        self.external_functions = {}
        self.subs = {}
        self.global_vars = {}
        self.loose_lines = []

        # Save all function/sub definitions.
        visitor = function_defn_visitor()
        self.accept(visitor)
        for f in visitor.func_objects:
            log.debug("saving func decl: %r" % f.name)
            self.functions[f.name] = f
        
        for token in tokens:
            if isinstance(token, If_Statement_Macro):
                for n in token.external_functions.keys():
                    log.debug("saving external func decl: %r" % n)
                    self.external_functions[n] = token.external_functions[n]
            if isinstance(token, Sub):
                log.debug("saving sub decl: %r" % token.name)
                self.subs[token.name] = token
            #if isinstance(token, Function):
            #    log.debug("saving func decl: %r" % token.name)
            #    self.functions[token.name] = token
            if isinstance(token, External_Function):
                log.debug("saving external func decl: %r" % token.name)
                self.external_functions[token.name] = token
            elif isinstance(token, Attribute_Statement):
                log.debug("saving attrib decl: %r" % token.name)
                self.attributes[token.name] = token.value
            elif isinstance(token, Global_Var_Statement):

                # Global variable initialization is now handled by emulating the
                # LooseLines blocks of code in the module.
                self.loose_lines.append(token)

            elif isinstance(token, Dim_Statement):

                # Global variable initialization is now handled by emulating the
                # LooseLines blocks of code in the module.
                self.loose_lines.append(token)

            elif isinstance(token, LooseLines):
                self.loose_lines.append(token)
                    
        self.name = self.attributes.get('VB_Name', None)
        # TODO: should not use print
        print(self)