예제 #1
0
    def __call__(self, lex):
        """ This function is the start of the heavy lifting 
        for computing the various metrics produced by PyMetrics."""

        for m in self.metricInstance.keys():
            self.metricInstance[m].processSrcLines(lex.srcLines)

        # Loop through list of tokens and count types as needed.

        # skipUntil is set after an error is detected. It is an attempt to
        # find a point to restart the analysis so that we do not get
        # cascading errors. This problem often occurs in analysing
        # foreign character sets when normal ascii was expected.
        tokenList = lex.tokenlist

        skipUntil = None
        tokCount = 0
        invalidToken = mytoken.MyToken()

        for tok in tokenList:
            if skipUntil:
                if tok.text in skipUntil:  # found where to restart analysis
                    skipUntil = None
                elif tok.type == WS:  # count, but otherwise ignore, whitespace
                    tokCount = tokCount + 1
                    self.metrics['numTokens'] = tokCount
                    self.__postToken(tok)
                    continue
                elif tok.type == ERRORTOKEN:
                    invalidToken.text += tok.text
                    continue

            tokCount = self.handleToken(tokCount, tok)

        return self.metrics
예제 #2
0
    def __push(self, toktype, semtype, toktext, srow, scol, line):
        "Append given token to final list of tokens."

        self.tokenlist.append(
            mytoken.MyToken(type=toktype,
                            semtype=semtype,
                            text=toktext,
                            row=srow,
                            col=scol,
                            line=line))
        if toktype in [NEWLINE, INDENT, DEDENT, EMPTY, ENDMARKER]:
            self.prevToktype = None
            self.prevSemtype = None
            self.prevToktext = None
        elif toktype != WS:
            self.prevToktype = toktype
            self.prevSemtype = semtype
            self.prevToktext = toktext
예제 #3
0
 def doHeaders(self, tok):
     """ Process both class and function headers. 
     Create the fully qualified names and deal 
     with possibly erroneous headers."""
     result = False
     if self.defFunction:
         if tok.type == NAME:
             self.__incr('numFunctions')
             self.fqnName.append(tok.text)
             self.fcnName = self.__genFQN(self.fqnName)
             self.fqnFunction.append(
                 (self.fcnName, self.blockDepth, FCNNAME))
             self.defFunction = False
             self.findFcnHdrEnd = True
             if self.pa.verbose > 0:
                 print("fqnFunction=%s" %
                       toTypeName(self.context, self.fqnFunction))
             self.__postToken(tok)
             result = True
         elif tok.type == ERRORTOKEN:
             # we must compensate for the simple scanner mishandling errors
             self.findFcnHdrEnd = True
             self.invalidToken = mytoken.MyToken(type=NAME,
                                                 semtype=FCNNAME,
                                                 text=tok.text,
                                                 row=tok.row,
                                                 col=tok.col,
                                                 line=tok.line)
             self.skipUntil = '(:\n'
             result = True
     elif self.defClass and tok.type == NAME:
         self.__incr('numClasses')
         self.fqnName.append(tok.text)
         self.className = self.__genFQN(self.fqnName)
         self.fqnClass.append((self.className, self.blockDepth, CLASSNAME))
         self.defClass = False
         self.findClassHdrEnd = True
         if self.pa.verbose > 0:
             print "fqnClass=%s" % toTypeName(self.context, self.fqnClass)
         self.__postToken(tok)
         result = True
     return result