Next / Previous / Contents / Shipman's homepage

17.17. Txny.__scanAbbr(): Scan a code

nomcompile3
# - - -    T x n y . _ _ s c a n A b b r

    def __scanAbbr ( self, scan ):
        '''Scan a bird code.

          [ scan is a Scan instance ->
              if the line in scan starts with a valid bird code,
              right-padded to abbrMod.ABBR_L ->
                scan  :=  scan advanced by abbrMod.ABBR_L
                return the bird code, stripped and uppercased
              else ->
                scan  :=  scan advanced no further than end of line
                Log()  +:=  error message(s)
                raise SyntaxError ]
        '''

Recognizing the actual code, which may not be full-length, is handled by the regular expression RE_ABBR from the abbr module.

nomcompile3
        #-- 1 --
        # [ if the line in scan starts with a string that matches
        #   abbrMod.RE_ABBR ->
        #     scan  :=  scan advanced past the matching part
        #     code  :=  the matching part
        #   else ->
        #     Log()  +:=  error message(s)
        #     raise SyntaxError ]
        m = scan.tabReMatch ( abbrMod.RE_ABBR )
        if m is None:
            scan.syntax ( "Expecting a %d-character bird code." %
                          abbrMod.ABBR_L )
        else:
            code = m.group()

If the code is not the full length, we must insure that the rest of the field is blank.

nomcompile3
        #-- 2 --
        # [ if (len(code) == abbrMod.ABBR_L) ->
        #     I
        #   else if the next (abbrMod.ABBR_L - len(code)) characters
        #   in the line in scan are blank) ->
        #     scan  :=  scan advance (abbrMod.ABBR_L-len(code))
        #   else ->
        #     Log()  +:=  error message(s)
        #     raise SyntaxError ]
        howShort = abbrMod.ABBR_L - len(code)
        if howShort > 0:
            padding = scan.move ( howShort )
            if padding.strip() != '':
                scan.syntax ( "The bird code must be made entirely "
                    "of letters, right-padded to length %d with "
                    "blanks if necessary." % abbrMod.ABBR_L )

        #-- 3 --
        return code.upper()