Next / Previous / Contents / Shipman's homepage

11.13. BirdId.scan()

Using the author's Scan instance for parsing an input stream, this static method looks for a simple or compound bird code.

abbr.py
# - - -   B i r d I d . s c a n   - - -

    def scan ( txny, scan ):
        """Scan a simple or compound bird code.
        """

First we check to see if the beginning of the line matches the regular expression RE_ABBR. If not, we can fail right away.

abbr.py
        #-- 1 --
        # [ if scan starts with a simple bird code ->
        #     scan   :=  scan advanced past matching characters
        #     abbr   :=  matching characters
        #     rel    :=  None
        #     abbr2  :=  None
        #   else ->
        #     scan  +:=  error message
        #     raise ValueError ]
        abbr = BirdId.scanAbbr ( txny, scan )
        rel = abbr2 = None

If the next character is a relationship code, it must be followed by a second simple bird code.

abbr.py
        #-- 2 --
        # [ if scan starts with a relationship code followed by
        #   a valid simple bird code ->
        #     scan   :=  scan advanced past all that
        #     rel    :=  the relationship code
        #     abbr2  :=  the simple bird code
        #   else if scan starts with a relationship code not
        #   followed by a valid simple bird code ->
        #     scan  +:=  error message
        #     raise ValueError
        #   else -> I ]
        m = scan.tabReMatch ( RE_REL )
        if  m is not None:
            #-- 2.1 --
            # [ if scan starts with a valid simple bird code ->
            #     rel   :=  matched string from m
            #     scan  :=  scan advanced past the bird code
            #   else ->
            #     scan  +:=  error message
            #     raise ValueError ]
            rel = m.group()
            abbr2 = BirdId.scanAbbr ( txny, scan )

The next character may be "?"; in that case set the q attribute to that string.

abbr.py
        #-- 3 --
        # [ if  scan starts with "?" ->
        #     scan  :=  scan advanced 1
        #     q     :=  "?"
        #   else ->
        #     q  :=  "" ]
        if  scan.tabMatch ( "?" ):
            q = "?"
        else:
            q = ""

Finally we bundle all the parts together as a BirdId instance.

abbr.py
        #-- 4 --
        return  BirdId ( txny, abbr, rel, abbr2, q )

The next line is necessary to make this method static.

abbr.py
    scan = staticmethod(scan)