Next / Previous / Contents / Shipman's homepage

43. Scan.integer(): Parse an integer

logscan.py
# - - -   S c a n . i n t e g e r

    def integer(self, maxLen=None):
        '''Parse a whole number.
        '''

For the regular expression used in this logic, see Section 9.5, “INT_PATTERN.

logscan.py
        #-- 1 --
        # [ if the line in self starts with a string that matches
        #   INT_PATTERN ->
        #       m  :=  a MatchObject representing the matching text
        #   else ->
        #       m  :=  None ]
        m = self.reMatch(INT_PATTERN)

        #-- 2 --
        # [ if m is None ->
        #     return None 
        #   else ->
        #     rawInt  :=  matching text from m ]
        if m is None:
            return None
        else:
            rawInt = m.group()

At this point, m.group() has the longest matching integer. If the caller specified a maxLen, we must truncate it it.

logscan.py
        #-- 3 --
        if((maxLen is not None) and
             (len(rawInt) > maxLen)):
            rawInt = rawInt[:maxLen]

        #-- 4 --
        return int(rawInt)