Next / Previous / Contents / Shipman's homepage

16.7. Rank.__scanKeyLen(): Scan the key length field

nomcompile3
# - - -   R a n k . _ _ s c a n K e y L e n

    def __scanKeyLen ( self, scan ):
        '''Process the key-length field.

          [ (scan is a Scan instance whose .length attribute is
            valid) ->
              if the line in scan starts with a valid key length field ->
                scan  :=  scan advanced past that field
                self.keyLen  :=  that field as an int
              else ->
                scan  :=  scan advanced no further than end of line
                Log()  +:=  error message(s)
                raise SyntaxError ]
        '''
        #-- 1 --
        # [ if the line in scan is not empty ->
        #     scan  :=  scan advanced 1
        #     rawLen  :=  next character from scan
        #   else ->
        #     scan  :=  scan advanced no further than end of line
        #     Log()  +:=  error message(s)
        #     raise SyntaxError ]
        try:
            rawLen = scan.move ( 1 )
        except IndexError:
            scan.syntax ( "Expecting the 1-digit key length field." )

        #-- 2 --
        # [ if rawLen is an integer in string form ->
        #     self.keyLen  :=  rawLen as an int
        #   else ->
        #     Log()  +:=  error message
        #     raise SyntaxError ]
        try:
            self.keyLen = int ( rawLen )
        except ValueError:
            scan.syntax ( "Key length must be one digit." )

That takes care of the actual scanning. However, there is an error check. The key length must be zero for the rank whose depth is zero, and it must be positive for the deeper ranks.

nomcompile3
        #-- 3 --
        # [ if ((self.depth is zero) and (self.keyLen is zero)) or
        #   ((self.depth is nonzero) and (self.keyLen is nonzero)) ->
        #     I
        #   else ->
        #     Log()  +:=  error message(s)
        #     raise SyntaxError ]
        if self.depth == 0:
            if self.keyLen != 0:
                scan.syntax ( "Key length must be 0 for depth 0" )
            else:
                pass
        elif self.keyLen == 0:
            scan.syntax ( "Key length must be nonzero for depth %d" %
                          self.depth )