##
## LinesProducingScanner.py
##      - class that yields lists of tokens for physical or virtual benchmark
##      file lines
##

# $Id: LinesProducingScanner.py,v 1.2 2006/01/22 21:22:20 jarda Exp $


# import of required modules {{{
from Plex import *

from ScannerConstants import ScannerConstants
# }}}


# class LinesProducingScanner() {{{
class LinesProducingScanner(Scanner):
    # DOC {{{
    """Class that yields lists of tokens for physical or virtual benchmark file
    lines.
    """
    # }}}


    # METHODS {{{

    def isEOFSymbol(symbolTuple):
        # DOC {{{
        """Tells whether the specified symbol is EOF.

        Parameters

            symbolTuple -- the symbol tuple
        """
        # }}}

        # CODE {{{
        return (symbolTuple[0] == None)
        # }}}
    isEOFSymbol = staticmethod(isEOFSymbol)


    def isNewlineSymbol(symbolTuple):
        # DOC {{{
        """Tells whether the specified symbol is NEWLINE.

        Parameters

            symbolTuple -- the symbol tuple
        """
        # }}}

        # CODE {{{
        return (symbolTuple[0] == ScannerConstants.T_NEWLINE)
        # }}}
    isNewlineSymbol = staticmethod(isNewlineSymbol)


    def tokenLinesGenerator(self):
        # DOC {{{
        """Yields list of tokens for each line.
        """
        # }}}

        # CODE {{{
        symbolTuple = self.read()

        eofWasRead = LinesProducingScanner.isEOFSymbol(symbolTuple)
        
        while (not eofWasRead):
            symbols = []

            newlineWasRead = False

            while ((not eofWasRead) and (not newlineWasRead)):
                if (LinesProducingScanner.isEOFSymbol(symbolTuple)):
                    eofWasRead = True
                elif (LinesProducingScanner.isNewlineSymbol(symbolTuple)):
                    if (self.allowLineBreak(symbols)):
                        newlineWasRead = True
                    else:
                        symbolTuple = self.read()
                else:
                    symbols.append(symbolTuple)
                    symbolTuple = self.read()

            if (len(symbols)):
                yield symbols
                
            if (not eofWasRead):
                symbolTuple = self.read()
        # }}}


    def allowLineBreak(self, symbols):
        # DOC {{{
        """Tells whether a line break is allowed.

        Parameters

            symbols -- list of symbols
        """
        # }}}

        # CODE {{{
        return True
        # }}}
    

    # }}}
# }}}
