<s> """<STR_LIT>""" <EOL> import sys <EOL> import ellyChar <EOL> import ellyWildcard <EOL> import ellyException <EOL> import syntaxSpecification <EOL> class Link ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , syms , dfls ) : <EOL> """<STR_LIT>""" <EOL> if len ( dfls ) != <NUM_LIT:3> : <EOL> raise ellyException . FormatFailure <EOL> else : <EOL> if dfls [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> self . patn = u'<STR_LIT:\x00>' <EOL> else : <EOL> self . patn = ellyWildcard . convert ( dfls [ <NUM_LIT:0> ] ) <EOL> if dfls [ <NUM_LIT:0> ] != '<STR_LIT:$>' : <EOL> if self . patn == None or ellyWildcard . minMatch ( self . patn ) == <NUM_LIT:0> : <EOL> print >> sys . stderr , '<STR_LIT>' , dfls [ <NUM_LIT:0> ] <EOL> raise ellyException . FormatFailure <EOL> self . catg = None <EOL> self . synf = None <EOL> sss = dfls [ <NUM_LIT:1> ] . lower ( ) <EOL> if sss != '<STR_LIT:->' : <EOL> syx = syntaxSpecification . SyntaxSpecification ( syms , sss ) <EOL> if syx != None : <EOL> self . catg = syx . catg <EOL> self . synf = syx . synf . positive <EOL> try : <EOL> n = int ( dfls [ <NUM_LIT:2> ] ) <EOL> except : <EOL> raise ellyException . FormatFailure <EOL> if n < <NUM_LIT:0> : <EOL> if self . patn == u'<STR_LIT:\x00>' : <EOL> raise ellyException . FormatFailure <EOL> pe = self . patn [ - <NUM_LIT:1> ] <EOL> if ( pe != ellyWildcard . cALL and <EOL> pe != ellyWildcard . cEND ) : <EOL> self . patn += ellyWildcard . cEND <EOL> print >> sys . stderr , '<STR_LIT>' , list ( self . patn ) <EOL> self . nxts = n <EOL> def __unicode__ ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . patn == None : <EOL> return u'<STR_LIT:None>' <EOL> else : <EOL> if self . patn == u'<STR_LIT:\x00>' : <EOL> p = '<STR_LIT>' <EOL> else : <EOL> p = u'<STR_LIT>' . format ( ellyWildcard . deconvert ( self . patn ) ) <EOL> c = unicode ( self . catg ) <EOL> f = u'<STR_LIT:None>' if self . synf == None else self . synf . hexadecimal ( False ) <EOL> return p + '<STR_LIT:U+0020>' + '<STR_LIT:U+0020>' + c + '<STR_LIT:U+0020>' + f + '<STR_LIT>' + unicode ( self . nxts ) <EOL> Trmls = ellyWildcard . Trmls <EOL> def bound ( segm ) : <EOL> """<STR_LIT>""" <EOL> lm = len ( segm ) <EOL> ll = <NUM_LIT:0> <EOL> while ll < lm : <EOL> if segm [ ll ] == '<STR_LIT:U+0020>' : break <EOL> ll += <NUM_LIT:1> <EOL> ll -= <NUM_LIT:1> <EOL> while ll > <NUM_LIT:0> : <EOL> c = segm [ ll ] <EOL> if c in Trmls or c == '<STR_LIT:*>' or ellyChar . isLetterOrDigit ( c ) : break <EOL> ll -= <NUM_LIT:1> <EOL> return ll + <NUM_LIT:1> <EOL> class PatternTable ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , syms = None , fsa = None ) : <EOL> """<STR_LIT>""" <EOL> self . indx = [ ] <EOL> self . _errcount = <NUM_LIT:0> <EOL> if fsa != None and syms != None : <EOL> self . load ( syms , fsa ) <EOL> def _err ( self , s = '<STR_LIT>' , l = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> self . _errcount += <NUM_LIT:1> <EOL> print >> sys . stderr , '<STR_LIT>' , s <EOL> if l != '<STR_LIT>' : <EOL> print >> sys . stderr , '<STR_LIT>' , l , '<STR_LIT:]>' <EOL> def load ( self , syms , fsa ) : <EOL> """<STR_LIT>""" <EOL> ins = [ <NUM_LIT:0> ] <EOL> sss = [ ] <EOL> lss = [ <NUM_LIT:0> ] <EOL> nm = <NUM_LIT:0> <EOL> while True : <EOL> line = fsa . readline ( ) <EOL> if len ( line ) == <NUM_LIT:0> : break <EOL> ls = line . strip ( ) . split ( '<STR_LIT:U+0020>' ) <EOL> sts = ls . pop ( <NUM_LIT:0> ) <EOL> try : <EOL> stn = int ( sts ) <EOL> except ValueError : <EOL> self . _err ( '<STR_LIT>' , line ) <EOL> continue <EOL> n = len ( self . indx ) <EOL> while stn >= n : <EOL> self . indx . append ( [ ] ) <EOL> n += <NUM_LIT:1> <EOL> try : <EOL> lk = Link ( syms , ls ) <EOL> except ellyException . FormatFailure : <EOL> self . _err ( '<STR_LIT>' , line ) <EOL> continue <EOL> if lk . nxts < <NUM_LIT:0> : <EOL> if lk . catg != None : <EOL> nm += <NUM_LIT:1> <EOL> else : <EOL> self . _err ( '<STR_LIT>' , line ) <EOL> continue <EOL> elif lk . catg != None : <EOL> self . _err ( '<STR_LIT>' , line ) <EOL> if not stn in lss : lss . append ( stn ) <EOL> if not stn in sss : sss . append ( stn ) <EOL> if lk . nxts >= <NUM_LIT:0> : <EOL> if not lk . nxts in lss : lss . append ( lk . nxts ) <EOL> if not lk . nxts in ins : ins . append ( lk . nxts ) <EOL> self . indx [ stn ] . append ( lk ) <EOL> if len ( self . indx ) == <NUM_LIT:0> and self . _errcount == <NUM_LIT:0> : <EOL> return <EOL> if nm == <NUM_LIT:0> : <EOL> self . _err ( '<STR_LIT>' ) <EOL> ns = len ( lss ) <EOL> if len ( ins ) != ns : <EOL> self . _err ( '<STR_LIT>' ) <EOL> if len ( sss ) != ns : <EOL> self . _err ( '<STR_LIT>' ) <EOL> if self . _errcount > <NUM_LIT:0> : <EOL> print >> sys . stderr , '<STR_LIT>' , self . _errcount , '<STR_LIT>' <EOL> print >> sys . stderr , '<STR_LIT>' <EOL> raise ellyException . TableFailure <EOL> def match ( self , segm , tree ) : <EOL> """<STR_LIT>""" <EOL> if len ( self . indx ) == <NUM_LIT:0> : return <NUM_LIT:0> <EOL> lim = bound ( segm ) <EOL> mtl = <NUM_LIT:0> <EOL> mtls = <NUM_LIT:0> <EOL> state = <NUM_LIT:0> <EOL> stk = [ ] <EOL> ls = self . indx [ state ] <EOL> ix = <NUM_LIT:0> <EOL> sg = segm [ : lim ] <EOL> capd = False if len ( sg ) == <NUM_LIT:0> else ellyChar . isUpperCaseLetter ( sg [ <NUM_LIT:0> ] ) <EOL> while True : <EOL> nls = len ( ls ) <EOL> if ix == nls : <EOL> if len ( stk ) == <NUM_LIT:0> : break <EOL> r = stk . pop ( ) <EOL> state = r [ <NUM_LIT:0> ] <EOL> ls = r [ <NUM_LIT:1> ] <EOL> sg = r [ <NUM_LIT:2> ] <EOL> mtl = r [ <NUM_LIT:3> ] <EOL> ix = <NUM_LIT:0> <EOL> continue <EOL> m = <NUM_LIT:0> <EOL> while ix < nls : <EOL> lk = ls [ ix ] <EOL> ix += <NUM_LIT:1> <EOL> if lk . patn == u'<STR_LIT:\x00>' : <EOL> m = <NUM_LIT:0> <EOL> else : <EOL> bds = ellyWildcard . match ( lk . patn , sg ) <EOL> if bds == None : continue <EOL> m = bds [ <NUM_LIT:0> ] <EOL> if lk . nxts < <NUM_LIT:0> : <EOL> if tree . addLiteralPhrase ( lk . catg , lk . synf , False , capd ) : <EOL> mtls = mtl + m <EOL> tree . lastph . lens = mtls <EOL> if ix < nls : <EOL> r = [ state , ls [ ix : ] , sg , mtl ] <EOL> stk . append ( r ) <EOL> mtl += m <EOL> break <EOL> else : <EOL> continue <EOL> ix = <NUM_LIT:0> <EOL> sg = sg [ m : ] <EOL> state = lk . nxts <EOL> if state < <NUM_LIT:0> : <EOL> ls = [ ] <EOL> else : <EOL> ls = self . indx [ state ] <EOL> return mtls <EOL> def dump ( self ) : <EOL> """<STR_LIT>""" <EOL> nn = <NUM_LIT:0> <EOL> for k in range ( len ( self . indx ) ) : <EOL> lks = self . indx [ k ] <EOL> if lks == None or lks == [ ] : <EOL> continue <EOL> print '<STR_LIT>' + str ( k ) + '<STR_LIT:]>' <EOL> for lk in lks : <EOL> print u'<STR_LIT:U+0020>' + unicode ( lk ) <EOL> if lk . patn == u'<STR_LIT:\x00>' : nn += <NUM_LIT:1> <EOL> print nn , '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import ellyConfiguration <EOL> import ellyDefinitionReader <EOL> import dumpEllyGrammar <EOL> import parseTest <EOL> import stat <EOL> import os <EOL> mode = os . fstat ( <NUM_LIT:0> ) . st_mode <EOL> interact = not ( stat . S_ISFIFO ( mode ) or stat . S_ISREG ( mode ) ) <EOL> tre = parseTest . Tree ( ) <EOL> ctx = parseTest . Context ( ) <EOL> print '<STR_LIT>' <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> inp = ellyDefinitionReader . EllyDefinitionReader ( basn + filn + '<STR_LIT>' ) <EOL> print '<STR_LIT>' , '<STR_LIT:<>' + filn + '<STR_LIT:>>' <EOL> if inp . error != None : <EOL> print inp . error <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> patn = None <EOL> try : <EOL> patn = PatternTable ( ctx . syms , inp ) <EOL> except ellyException . TableFailure : <EOL> print '<STR_LIT>' <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> print len ( patn . indx ) , '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> dumpEllyGrammar . dumpCategories ( ctx . syms ) <EOL> print '<STR_LIT>' <EOL> patn . dump ( ) <EOL> print '<STR_LIT>' <EOL> while True : <EOL> if interact : sys . stdout . write ( '<STR_LIT>' ) <EOL> t = sys . stdin . readline ( ) . strip ( ) <EOL> if len ( t ) == <NUM_LIT:0> : break <EOL> print '<STR_LIT>' , '<STR_LIT:[>' , t , '<STR_LIT:]>' <EOL> nma = patn . match ( list ( t ) , tre ) <EOL> print '<STR_LIT>' + t + '<STR_LIT:>>' , nma , '<STR_LIT>' , '<STR_LIT>' + t [ nma : ] + '<STR_LIT:>>' <EOL> if interact : sys . stdout . write ( '<STR_LIT:\n>' ) <EOL> tre . showQueue ( ) </s>
