<s> from circularbuffer import * <EOL> class Context_ ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , keep_last_n_lines = <NUM_LIT:5> ) : <EOL> """<STR_LIT>""" <EOL> self . contextLines_ = keep_last_n_lines <EOL> self . data_ = CircularBuffer ( <NUM_LIT> ) <EOL> self . lineNumber_ = <NUM_LIT:1> <EOL> self . charNumber_ = <NUM_LIT:0> <EOL> def addChar ( self , c ) : <EOL> """<STR_LIT>""" <EOL> if ( c == '<STR_LIT:\n>' ) : <EOL> self . lineNumber_ += <NUM_LIT:1> <EOL> self . charNumber_ = <NUM_LIT:0> <EOL> if ( self . data_ . full ( ) ) : <EOL> self . data_ . get ( ) <EOL> self . data_ . put ( c ) <EOL> self . charNumber_ += <NUM_LIT:1> <EOL> def deleteLastChar ( self ) : <EOL> """<STR_LIT>""" <EOL> c = self . data_ . drop ( ) ; <EOL> if ( c == '<STR_LIT:\n>' ) : <EOL> self . lineNumber_ -= <NUM_LIT:1> <EOL> index_of_last_newline = - <NUM_LIT:1> <EOL> for ii in xrange ( <NUM_LIT:0> , len ( self . data_ ) ) : <EOL> if ( self . data_ . peek ( len ( self . data_ ) - ii - <NUM_LIT:1> ) == '<STR_LIT:\n>' ) : <EOL> index_of_last_newline = ii <EOL> break <EOL> self . charNumber_ = index_of_last_newline <EOL> if ( index_of_last_newline == - <NUM_LIT:1> ) : self . charNumber = <NUM_LIT> <EOL> else : <EOL> self . charNumber_ -= <NUM_LIT:1> ; <EOL> def addData ( self , buffer , len ) : <EOL> for ii in xrange ( <NUM_LIT:0> , len ) : <EOL> self . addChar ( buffer [ ii ] ) <EOL> def generateReport ( self ) : <EOL> report = "<STR_LIT>" <EOL> lines = [ ] <EOL> current_line = "<STR_LIT>" <EOL> for ii in xrange ( <NUM_LIT:0> , len ( self . data_ ) ) : <EOL> c = self . data_ . peek ( ii ) <EOL> current_line = current_line + c <EOL> if ( c == '<STR_LIT:\n>' ) : <EOL> lines . append ( current_line ) <EOL> current_line = "<STR_LIT>" <EOL> if ( current_line != "<STR_LIT>" ) : <EOL> current_line = current_line + '<STR_LIT:\n>' <EOL> lines . append ( current_line ) <EOL> context_lines = self . contextLines_ <EOL> if ( len ( lines ) < self . contextLines_ ) : context_lines = len ( lines ) <EOL> if ( context_lines ) : <EOL> start_line = len ( lines ) - context_lines <EOL> report = "<STR_LIT>" + str ( self . lineNumber_ ) + "<STR_LIT>" + str ( context_lines ) + "<STR_LIT>" <EOL> if ( context_lines != <NUM_LIT:1> ) : <EOL> report = report + "<STR_LIT:s>" <EOL> report = report + "<STR_LIT>" + str ( start_line + <NUM_LIT:1> ) + "<STR_LIT:->" + str ( start_line + context_lines ) + "<STR_LIT>" "<STR_LIT>" <EOL> for ii in xrange ( <NUM_LIT:0> , context_lines ) : <EOL> report = report + "<STR_LIT:U+0020>" + lines [ start_line + ii ] <EOL> cursor = "<STR_LIT:->" * ( self . charNumber_ + <NUM_LIT:1> ) + "<STR_LIT>" <EOL> report = report + cursor <EOL> return report <EOL> def syntaxError ( self , s ) : <EOL> """<STR_LIT>""" <EOL> report = self . generateReport ( ) + s <EOL> raise Exception , report <EOL> EOF = "<STR_LIT>" <EOL> class ReaderA ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . context_ = Context_ ( ) <EOL> def syntaxError ( self , s ) : self . context_ . syntaxError ( s ) <EOL> def EOFComing ( self ) : return self . _peekNWSChar ( ) == EOF <EOL> def _getNWSChar ( self ) : pass <EOL> def _peekNWSChar ( self ) : pass <EOL> def _getChar ( self ) : pass <EOL> def _peekChar ( self ) : pass <EOL> def _consumeWS ( self ) : pass <EOL> def _pushback ( self , pushback_char ) : pass <EOL> class StringReader ( ReaderA ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , input ) : <EOL> """<STR_LIT>""" <EOL> ReaderA . __init__ ( self ) <EOL> self . buffer_ = input <EOL> self . current_ = <NUM_LIT:0> <EOL> def _indexOfNextNWSChar ( self ) : <EOL> length = len ( self . buffer_ ) <EOL> cur = self . current_ <EOL> if ( cur == length ) : return cur <EOL> comment_mode = False <EOL> while cur < len : <EOL> if ( comment_mode ) : <EOL> if ( self . buffer_ [ cur ] == '<STR_LIT:\n>' ) : comment_mode = False <EOL> continue <EOL> else : <EOL> if ( self . buffer_ [ cur ] . isspace ( ) ) : continue <EOL> elif ( self . buffer_ [ cur ] == '<STR_LIT:#>' ) : <EOL> comment_mode = True <EOL> continue <EOL> else : <EOL> break <EOL> return cur <EOL> def _getNWSChar ( self ) : <EOL> index = self . _indexOfNextNWSChar ( ) <EOL> old_current = self . current_ <EOL> self . current_ = index <EOL> self . context_ . addData ( self . buffer_ [ old_current : ] , <EOL> self . current_ - old_current ) <EOL> return self . _getChar ( ) <EOL> def _peekNWSChar ( self ) : <EOL> index = self . _indexOfNextNWSChar ( ) <EOL> if ( index >= len ( self . buffer_ ) ) : return EOF <EOL> c = self . buffer_ [ index ] <EOL> return c <EOL> def _getChar ( self ) : <EOL> length = len ( self . buffer_ ) <EOL> if ( self . current_ == length ) : return EOF <EOL> c = self . buffer_ [ self . current_ ] <EOL> self . current_ += <NUM_LIT:1> <EOL> self . context_ . addChar ( c ) <EOL> return c <EOL> def _peekChar ( self ) : <EOL> length = len ( self . buffer_ ) <EOL> if ( self . current_ == length ) : return EOF <EOL> c = self . buffer_ [ self . current_ ] <EOL> return c <EOL> def _consumeWS ( self ) : <EOL> index = self . _indexOfNextNWSChar ( ) <EOL> old_current = self . current_ <EOL> self . current_ = index <EOL> self . context_ . addData ( self . buffer_ [ old_current : ] , <EOL> self . current_ - old_current ) <EOL> if ( index == len ( self . buffer_ ) ) : return EOF <EOL> c = self . buffer_ [ index ] <EOL> return c <EOL> def _pushback ( self , pushback_char ) : <EOL> if ( pushback_char == EOF ) : <EOL> if ( self . current_ != len ( self . buffer_ ) ) : <EOL> self . syntaxError ( "<STR_LIT>" ) <EOL> else : <EOL> return <EOL> if ( self . current_ <= <NUM_LIT:0> ) : <EOL> print "<STR_LIT>" , self . current_ , self . buffer_ <EOL> self . syntaxError ( "<STR_LIT>" ) <EOL> else : <EOL> self . current_ -= <NUM_LIT:1> <EOL> if ( self . buffer_ [ self . current_ ] != pushback_char ) : <EOL> self . syntaxError ( "<STR_LIT>" ) <EOL> self . context_ . deleteLastChar ( ) <EOL> class StreamReader ( ReaderA ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , istream ) : <EOL> """<STR_LIT>""" <EOL> ReaderA . __init__ ( self ) <EOL> self . is_ = istream <EOL> self . cached_ = CircularBuffer ( <NUM_LIT> , True ) <EOL> def _peekIntoNextNWSChar ( self ) : <EOL> peek_ahead = <NUM_LIT:0> <EOL> start_comment = False <EOL> while ( <NUM_LIT:1> ) : <EOL> c = '<STR_LIT>' <EOL> if ( peek_ahead >= len ( self . cached_ ) ) : <EOL> c = self . is_ . read ( <NUM_LIT:1> ) <EOL> self . cached_ . put ( c ) ; <EOL> else : <EOL> c = self . cached_ . peek ( peek_ahead ) <EOL> if ( c == EOF ) : <EOL> return ( c , peek_ahead ) <EOL> elif ( start_comment ) : <EOL> peek_ahead += <NUM_LIT:1> <EOL> start_comment = ( c != '<STR_LIT:\n>' ) <EOL> continue <EOL> elif ( c == '<STR_LIT:#>' ) : <EOL> peek_ahead += <NUM_LIT:1> <EOL> start_comment = True <EOL> continue <EOL> elif ( c . isspace ( ) ) : <EOL> peek_ahead += <NUM_LIT:1> <EOL> continue <EOL> else : <EOL> return ( c , peek_ahead ) <EOL> def _getNWSChar ( self ) : <EOL> ( _ , peek_ahead ) = self . _peekIntoNextNWSChar ( ) <EOL> for ii in xrange ( <NUM_LIT:0> , peek_ahead ) : <EOL> cc_ii = self . cached_ . peek ( ii ) ; <EOL> if ( cc_ii != EOF ) : <EOL> self . context_ . addChar ( cc_ii ) <EOL> self . cached_ . consume ( peek_ahead ) <EOL> return self . _getChar ( ) <EOL> def _peekNWSChar ( self ) : <EOL> ( c , _ ) = self . _peekIntoNextNWSChar ( ) <EOL> return c <EOL> def _getChar ( self ) : <EOL> if ( self . cached_ . empty ( ) ) : <EOL> cc = self . is_ . read ( <NUM_LIT:1> ) <EOL> else : <EOL> cc = self . cached_ . get ( ) <EOL> if ( cc != EOF ) : <EOL> self . context_ . addChar ( cc ) <EOL> return cc <EOL> def _peekChar ( self ) : <EOL> if ( self . cached_ . empty ( ) ) : <EOL> c = self . is_ . read ( <NUM_LIT:1> ) <EOL> self . cached_ . put ( c ) <EOL> return self . cached_ . peek ( ) <EOL> def _consumeWS ( self ) : <EOL> ( c , peek_ahead ) = self . _peekIntoNextNWSChar ( ) <EOL> for ii in xrange ( <NUM_LIT:0> , peek_ahead ) : <EOL> cc_ii = self . cached_ . peek ( ii ) <EOL> if ( cc_ii != EOF ) : <EOL> self . context_ . addChar ( cc_ii ) <EOL> self . cached_ . consume ( peek_ahead ) <EOL> return c <EOL> def _pushback ( self , pushback_char ) : <EOL> if ( pushback_char != EOF ) : self . context_ . deleteLastChar ( ) <EOL> self . cached_ . pushback ( pushback_char ) </s>
