<s> """<STR_LIT>""" <EOL> import ellyChar <EOL> import ellyWildcard <EOL> import ellyBuffer <EOL> import ellyBufferEN <EOL> import ellyConfiguration <EOL> import macroTable <EOL> class SubstitutionBuffer ( <EOL> ellyBufferEN . EllyBufferEN if ellyConfiguration . language == '<STR_LIT>' else <EOL> ellyBuffer . EllyBuffer <EOL> ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , macros ) : <EOL> """<STR_LIT>""" <EOL> self . buffer = None <EOL> self . sup = super ( SubstitutionBuffer , self ) <EOL> self . sup . __init__ ( ) <EOL> self . mtb = macros <EOL> def getNext ( self ) : <EOL> """<STR_LIT>""" <EOL> limit = <NUM_LIT:2> * len ( self . buffer ) <EOL> oto = None <EOL> while True : <EOL> to = self . sup . getNext ( ) <EOL> if to == None : return None <EOL> if oto == None : oto = to <EOL> self . putBack ( to ) <EOL> if not self . expand ( ) : break <EOL> if len ( self . buffer ) > limit : <EOL> return None <EOL> self . divided = oto . dvdd <EOL> nto = self . sup . getNext ( ) <EOL> if nto . getRoot ( ) == oto . getRoot ( ) : <EOL> nto = oto <EOL> return nto <EOL> def expand ( self ) : <EOL> """<STR_LIT>""" <EOL> done = False <EOL> while True : <EOL> x = self . peek ( ) <EOL> if x == None : break <EOL> lr = self . mtb . getRules ( x ) <EOL> for r in lr : <EOL> if self . _match ( r ) : <EOL> done = True <EOL> break <EOL> else : <EOL> break <EOL> return done <EOL> def _match ( self , rule ) : <EOL> """<STR_LIT>""" <EOL> capital = self . isCapital ( ) <EOL> pattern = rule [ <NUM_LIT:0> ] <EOL> rewrite = rule [ <NUM_LIT:1> ] <EOL> lim = len ( self . buffer ) <EOL> mbd = ellyWildcard . match ( pattern , self . buffer , <NUM_LIT:0> , lim ) <EOL> if mbd == None : return False <EOL> mbl = len ( mbd ) <EOL> mr = <NUM_LIT:0> <EOL> me = len ( rewrite ) <EOL> ob = [ ] <EOL> while mr < me : <EOL> c = rewrite [ mr ] <EOL> mr += <NUM_LIT:1> <EOL> if c != u'<STR_LIT:\\>' : <EOL> ob . append ( c ) <EOL> elif mr < me : <EOL> x = rewrite [ mr ] <EOL> try : <EOL> k = int ( x ) <EOL> if k < mbl : <EOL> r = mbd [ k ] <EOL> ob . extend ( self . buffer [ r [ <NUM_LIT:0> ] : r [ <NUM_LIT:1> ] ] ) <EOL> except ValueError : <EOL> ob . append ( ellyChar . SPC ) <EOL> mr += <NUM_LIT:1> <EOL> else : <EOL> ob . append ( c ) <EOL> nm = mbd [ <NUM_LIT:0> ] <EOL> self . buffer = self . buffer [ nm : ] <EOL> if self . atToken ( ) : <EOL> self . prepend ( ellyChar . SPC ) <EOL> self . prepend ( ob ) <EOL> if capital : <EOL> self . setCapital ( ) <EOL> return True <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import sys <EOL> import ellyDefinitionReader <EOL> import ellyException <EOL> basn = ellyConfiguration . baseSource + '<STR_LIT:/>' <EOL> filn = sys . argv [ <NUM_LIT:1> ] if len ( sys . argv ) > <NUM_LIT:1> else '<STR_LIT:test>' <EOL> mrdr = ellyDefinitionReader . EllyDefinitionReader ( basn + filn + '<STR_LIT>' ) <EOL> print "<STR_LIT>" , filn <EOL> if mrdr . error != None : <EOL> print >> sys . stderr , mrdr . error <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> mtbl = macroTable . MacroTable ( mrdr ) <EOL> sbuf = SubstitutionBuffer ( mtbl ) <EOL> print '<STR_LIT>' <EOL> while True : <EOL> try : <EOL> sys . stdout . write ( "<STR_LIT>" ) <EOL> line = sys . stdin . readline ( ) <EOL> l = line . decode ( '<STR_LIT:utf8>' ) <EOL> except KeyboardInterrupt : <EOL> break <EOL> l = l . strip ( ) <EOL> if len ( l ) == <NUM_LIT:0> : break <EOL> sbuf . clear ( ) <EOL> sbuf . append ( list ( l ) ) <EOL> print sbuf . buffer <EOL> while True : <EOL> print "<STR_LIT>" , len ( sbuf . buffer ) <EOL> try : <EOL> t = sbuf . getNext ( ) <EOL> except ellyException . StemmingError : <EOL> print >> sys . stderr , '<STR_LIT>' <EOL> break <EOL> if t == None : break <EOL> print "<STR_LIT>" , unicode ( t ) <EOL> print "<STR_LIT>" <EOL> sys . stdout . write ( "<STR_LIT:\n>" ) </s>
