예제 #1
0
 def __init__(self):
     "Creates the database used to store cross referencing information."
     self.__xrefDb = XrefDb()
예제 #2
0
class LispXrefer:
    "Cross referencing application for Lisp code."
    
    def __init__(self):
        "Creates the database used to store cross referencing information."
        self.__xrefDb = XrefDb()

    def xrefFile(self, filename):
        """Cross references the give file.

        FILENAME is the name of the file to cross reference
        returns the number of defuns in the file, or -1 if an error occurred
        """
        try:
            f = file(filename, "r")
        except:
            return -1
        num_defuns = 0
        lispReader = LispReader(f)
        form = lispReader.read()
        while form != LispReader.SYNTAX_ERROR and \
                  form != LispReader.END_OF_INPUT:
            if isList(form) and form[0] == "defun":
                self.__parseDefun(form, filename)
                num_defuns = num_defuns + 1
            form = lispReader.read()
        f.close()        
        if form == LispReader.SYNTAX_ERROR:
            return -1
        return num_defuns

    def __parseDefun(self, form, filename):
        """Parses a defun form, performing cross referencing.

        FORM is the defun form to parse
        FILENAME is the name of the file containing the defun
        """
        functionName = form[1]
        self.__xrefDb.registerFunction(functionName, filename)
        for f in form[3:]: self.__parseForm(f, functionName)

    def __parseForm(self, form, functionName):
        """Parses a form, performing cross referencing.

        FORM is the form to parse
        FUNCTIONNAME is the name of the function containing the form
        """
        if isString(form) or isSymbol(form) or isInteger(form) or \
               form[0] == "quote" or form[0] == "function":
            pass # ignore
        elif form[0] == "cond":
            self.__parseCond(form, functionName)
        elif form[0] == "condition-case":
            self.__parseConditionCase(form, functionName)
        elif form[0] == "dolist":
            self.__parseDoList(form, functionName)
        elif form[0] == "let":
            self.__parseLet(form, functionName)
        else:
            self.__xrefDb.registerCaller(form[0], functionName)
            for f in form[1:]: self.__parseForm(f, functionName)
            
    def __parseCond(self, form, functionName):
        """Parses a cond form, performing cross referencing.

        FORM is the cond form to parse
        FUNCTIONNAME is the name of the function containing the form
        """
        # parse the cond clauses
        for f in form[1:]:
            for g in f: self.__parseForm(g, functionName)

    def __parseConditionCase(self, form, functionName):
        """Parses a condition-case form, performing cross referencing.

        FORM is the condition-case form to parse
        FUNCTIONNAME is the name of the function containing the form
        """
        # parse the body
        self.__parseForm(form[2], functionName)
        # parse the error handlers
        for f in form[3:]:
            for g in f[1:]: self.__parseForm(g, functionName)

    def __parseDoList(self, form, functionName):
        """Parses a dolist form, performing cross referencing.

        FORM is the dolist form to parse
        FUNCTIONNAME is the name of the function containing the form
        """
        # parse the dolist bindings
        for f in form[1][1:]: self.__parseForm(f, functionName)
        # parse the dolist body
        for f in form[2:]: self.__parseForm(f, functionName)

    def __parseLet(self, form, functionName):
        """Parses a let form, performing cross referencing.

        FORM is the let form to parse
        FUNCTIONNAME is the name of the function containing the form
        """
        # parse the let bindings
        for f in form[1]:
            if type(f) == list: self.__parseForm(f[1], functionName)
        # parse the let body
        for f in form[2:]: self.__parseForm(f, functionName)

    def getDefiningFile(self, function):
        """Retrives the defining file for the given function.

        FUNCTION the name of the function to retrieve the defining file for
        returns the name of the defining file or None if function not found
        """
        return self.__xrefDb.getDefiningFile(function)

    def getCallers(self, function):
        """Retrieves a list of the callers of the given function.

        FUNCTION is the name of the function to retrieve list of callers for
        returns the list of callers, possibly None
        """
        return self.__xrefDb.getCallers(function)

    def getDbString(self, filter):
        """Returns a string representation of the contents in the database.

        FILTER set to true to filter those without a defining file
        returns the database string
        """
        return self.__xrefDb.getDbString(filter)

    def clearDb(self):
        "Clears all entries in the database."
        self.__xrefDb.clearDb()