Next / Previous / Contents / Shipman's homepage

16.6. Rank.__scanRequired(): Scan the required flag field

nomcompile3
# - - -   R a n k . _ _ s c a n R e q u i r e d

    def __scanRequired ( self, scan ):
        '''Process the required-rank flag field.

          [ (scan is a Scan instance) and
            (self.code is as invariant) ->
              if the line in scan starts with a valid required flag
              field ->
                scan  :=  scan advanced past that field
                self.required  :=  that field encoded as True for ' ',
                    False for '?'
              else ->
                scan  :=  scan advanced no further than end of line
                Log()  +:=  error message(s)
                raise SyntaxError ]
        '''
        #-- 1 --
        # [ if the line in scan has at least one character left ->
        #     scan  :=  scan advanced one character
        #     rawFlag  :=  next character from scan
        #   else ->
        #     scan  :=  scan advanced no further than end of line
        #     Log()  +:=  error message(s)
        #     raise SyntaxError ]
        try:
            rawFlag = scan.move ( 1 )
        except IndexError:
            scan.syntax ( "Expecting the optional-rank flag field." )

        #-- 2 --
        # [ if rawFlag == ' ' ->
        #     self.required  :=  True
        #   else if rawFlag is '?' and self.code is not in
        #   (GENUS_CODE, SPECIES_CODE) ->
        #     self.required  :=  False
        #   else ->
        #     Log()  +:=  error message(s)
        #     raise SyntaxError ]
        if rawFlag == ' ':
            self.required = True
        elif rawFlag == '?':
            self.required = False
            if self.code in (GENUS_CODE, SPECIES_CODE):
                scan.syntax ( "Rank '%s' must always be required." %
                              self.code )
        else:
            scan.syntax ( "Optional rank flag must be either '?' or "
                          "blank." )