<s> """<STR_LIT>""" <EOL> import re <EOL> import sys <EOL> EOLre = re . compile ( r'<STR_LIT>' ) <EOL> EOLZre = re . compile ( r'<STR_LIT>' ) <EOL> ENCODINGsearch = re . compile ( "<STR_LIT>" ) . search <EOL> class Error ( Exception ) : <EOL> pass <EOL> class SourceReader ( object ) : <EOL> def __init__ ( self , src , filename = None , breakPoint = None , encoding = None ) : <EOL> self . _src = src <EOL> self . _filename = filename <EOL> self . _srcLen = len ( src ) <EOL> if breakPoint == None : <EOL> self . _breakPoint = self . _srcLen <EOL> else : <EOL> self . setBreakPoint ( breakPoint ) <EOL> self . _pos = <NUM_LIT:0> <EOL> self . _bookmarks = { } <EOL> self . _posTobookmarkMap = { } <EOL> self . _EOLs = [ ] <EOL> pos = <NUM_LIT:0> <EOL> while pos < len ( self ) : <EOL> EOLmatch = EOLZre . search ( src , pos ) <EOL> self . _EOLs . append ( EOLmatch . start ( ) ) <EOL> pos = EOLmatch . end ( ) <EOL> self . _BOLs = [ ] <EOL> for pos in self . _EOLs : <EOL> BOLpos = self . findBOL ( pos ) <EOL> self . _BOLs . append ( BOLpos ) <EOL> def src ( self ) : <EOL> return self . _src <EOL> def filename ( self ) : <EOL> return self . _filename <EOL> def __len__ ( self ) : <EOL> return self . _breakPoint <EOL> def __getitem__ ( self , i ) : <EOL> if not isinstance ( i , int ) : <EOL> self . checkPos ( i . stop ) <EOL> else : <EOL> self . checkPos ( i ) <EOL> return self . _src [ i ] <EOL> def __getslice__ ( self , i , j ) : <EOL> i = max ( i , <NUM_LIT:0> ) ; j = max ( j , <NUM_LIT:0> ) <EOL> return self . _src [ i : j ] <EOL> def splitlines ( self ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _srcLines = self . _src . splitlines ( ) <EOL> return self . _srcLines <EOL> def lineNum ( self , pos = None ) : <EOL> if pos == None : <EOL> pos = self . _pos <EOL> for i in range ( len ( self . _BOLs ) ) : <EOL> if pos >= self . _BOLs [ i ] and pos <= self . _EOLs [ i ] : <EOL> return i <EOL> def getRowCol ( self , pos = None ) : <EOL> if pos == None : <EOL> pos = self . _pos <EOL> lineNum = self . lineNum ( pos ) <EOL> BOL , EOL = self . _BOLs [ lineNum ] , self . _EOLs [ lineNum ] <EOL> return lineNum + <NUM_LIT:1> , pos - BOL + <NUM_LIT:1> <EOL> def getRowColLine ( self , pos = None ) : <EOL> if pos == None : <EOL> pos = self . _pos <EOL> row , col = self . getRowCol ( pos ) <EOL> return row , col , self . splitlines ( ) [ row - <NUM_LIT:1> ] <EOL> def getLine ( self , pos ) : <EOL> if pos == None : <EOL> pos = self . _pos <EOL> lineNum = self . lineNum ( pos ) <EOL> return self . splitlines ( ) [ lineNum ] <EOL> def pos ( self ) : <EOL> return self . _pos <EOL> def setPos ( self , pos ) : <EOL> self . checkPos ( pos ) <EOL> self . _pos = pos <EOL> def validPos ( self , pos ) : <EOL> return pos <= self . _breakPoint and pos >= <NUM_LIT:0> <EOL> def checkPos ( self , pos ) : <EOL> if not pos <= self . _breakPoint : <EOL> raise Error ( "<STR_LIT>" + str ( pos ) + "<STR_LIT>" + <EOL> str ( self . _breakPoint - <NUM_LIT:1> ) + "<STR_LIT:)>" ) <EOL> elif not pos >= <NUM_LIT:0> : <EOL> raise Error ( "<STR_LIT>" + str ( pos ) + "<STR_LIT>" ) <EOL> def breakPoint ( self ) : <EOL> return self . _breakPoint <EOL> def setBreakPoint ( self , pos ) : <EOL> if pos > self . _srcLen : <EOL> raise Error ( "<STR_LIT>" + str ( pos ) + <EOL> "<STR_LIT>" + <EOL> str ( self . _srcLen ) + "<STR_LIT:)>" ) <EOL> elif not pos >= <NUM_LIT:0> : <EOL> raise Error ( "<STR_LIT>" + str ( pos ) + "<STR_LIT>" ) <EOL> self . _breakPoint = pos <EOL> def setBookmark ( self , name ) : <EOL> self . _bookmarks [ name ] = self . _pos <EOL> self . _posTobookmarkMap [ self . _pos ] = name <EOL> def hasBookmark ( self , name ) : <EOL> return name in self . _bookmarks <EOL> def gotoBookmark ( self , name ) : <EOL> if not self . hasBookmark ( name ) : <EOL> raise Error ( "<STR_LIT>" + name + "<STR_LIT>" ) <EOL> pos = self . _bookmarks [ name ] <EOL> if not self . validPos ( pos ) : <EOL> raise Error ( "<STR_LIT>" + name + '<STR_LIT:U+002CU+0020>' + <EOL> str ( pos ) + "<STR_LIT>" ) <EOL> self . _pos = pos <EOL> def atEnd ( self ) : <EOL> return self . _pos >= self . _breakPoint <EOL> def atStart ( self ) : <EOL> return self . _pos == <NUM_LIT:0> <EOL> def peek ( self , offset = <NUM_LIT:0> ) : <EOL> self . checkPos ( self . _pos + offset ) <EOL> pos = self . _pos + offset <EOL> return self . _src [ pos ] <EOL> def getc ( self ) : <EOL> pos = self . _pos <EOL> if self . validPos ( pos + <NUM_LIT:1> ) : <EOL> self . _pos += <NUM_LIT:1> <EOL> return self . _src [ pos ] <EOL> def ungetc ( self , c = None ) : <EOL> if not self . atStart ( ) : <EOL> raise Error ( '<STR_LIT>' ) <EOL> self . _pos -= <NUM_LIT:1> <EOL> if not c == None : <EOL> self . _src [ self . _pos ] = c <EOL> def advance ( self , offset = <NUM_LIT:1> ) : <EOL> self . checkPos ( self . _pos + offset ) <EOL> self . _pos += offset <EOL> def rev ( self , offset = <NUM_LIT:1> ) : <EOL> self . checkPos ( self . _pos - offset ) <EOL> self . _pos -= offset <EOL> def read ( self , offset ) : <EOL> self . checkPos ( self . _pos + offset ) <EOL> start = self . _pos <EOL> self . _pos += offset <EOL> return self . _src [ start : self . _pos ] <EOL> def readTo ( self , to , start = None ) : <EOL> self . checkPos ( to ) <EOL> if start == None : <EOL> start = self . _pos <EOL> self . _pos = to <EOL> return self . _src [ start : to ] <EOL> def readToEOL ( self , start = None , gobble = True ) : <EOL> EOLmatch = EOLZre . search ( self . src ( ) , self . pos ( ) ) <EOL> if gobble : <EOL> pos = EOLmatch . end ( ) <EOL> else : <EOL> pos = EOLmatch . start ( ) <EOL> return self . readTo ( to = pos , start = start ) <EOL> def find ( self , it , pos = None ) : <EOL> if pos == None : <EOL> pos = self . _pos <EOL> return self . _src . find ( it , pos ) <EOL> def startswith ( self , it , pos = None ) : <EOL> if self . find ( it , pos ) == self . pos ( ) : <EOL> return True <EOL> else : <EOL> return False <EOL> def rfind ( self , it , pos ) : <EOL> if pos == None : <EOL> pos = self . _pos <EOL> return self . _src . rfind ( it , pos ) <EOL> def findBOL ( self , pos = None ) : <EOL> if pos == None : <EOL> pos = self . _pos <EOL> src = self . src ( ) <EOL> return max ( src . rfind ( '<STR_LIT:\n>' , <NUM_LIT:0> , pos ) + <NUM_LIT:1> , src . rfind ( '<STR_LIT:\r>' , <NUM_LIT:0> , pos ) + <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> def findEOL ( self , pos = None , gobble = False ) : <EOL> if pos == None : <EOL> pos = self . _pos <EOL> match = EOLZre . search ( self . src ( ) , pos ) <EOL> if gobble : <EOL> return match . end ( ) <EOL> else : <EOL> return match . start ( ) <EOL> def isLineClearToPos ( self , pos = None ) : <EOL> if pos == None : <EOL> pos = self . pos ( ) <EOL> self . checkPos ( pos ) <EOL> src = self . src ( ) <EOL> BOL = self . findBOL ( ) <EOL> return BOL == pos or src [ BOL : pos ] . isspace ( ) <EOL> def matches ( self , strOrRE ) : <EOL> if isinstance ( strOrRE , ( str , unicode ) ) : <EOL> return self . startswith ( strOrRE , pos = self . pos ( ) ) <EOL> else : <EOL> return strOrRE . match ( self . src ( ) , self . pos ( ) ) <EOL> def matchWhiteSpace ( self , WSchars = '<STR_LIT>' ) : <EOL> return ( not self . atEnd ( ) ) and self . peek ( ) in WSchars <EOL> def getWhiteSpace ( self , max = None , WSchars = '<STR_LIT>' ) : <EOL> if not self . matchWhiteSpace ( WSchars ) : <EOL> return '<STR_LIT>' <EOL> start = self . pos ( ) <EOL> breakPoint = self . breakPoint ( ) <EOL> if max is not None : <EOL> breakPoint = min ( breakPoint , self . pos ( ) + max ) <EOL> while self . pos ( ) < breakPoint : <EOL> self . advance ( ) <EOL> if not self . matchWhiteSpace ( WSchars ) : <EOL> break <EOL> return self . src ( ) [ start : self . pos ( ) ] <EOL> def matchNonWhiteSpace ( self , WSchars = '<STR_LIT>' ) : <EOL> return self . atEnd ( ) or not self . peek ( ) in WSchars <EOL> def getNonWhiteSpace ( self , WSchars = '<STR_LIT>' ) : <EOL> if not self . matchNonWhiteSpace ( WSchars ) : <EOL> return '<STR_LIT>' <EOL> start = self . pos ( ) <EOL> while self . pos ( ) < self . breakPoint ( ) : <EOL> self . advance ( ) <EOL> if not self . matchNonWhiteSpace ( WSchars ) : <EOL> break <EOL> return self . src ( ) [ start : self . pos ( ) ] </s>
