<s> """<STR_LIT>""" <EOL> import re , inspect , sys , os , tempfile <EOL> import types <EOL> try : <EOL> import external . ply . lex as lex <EOL> import external . ply . yacc as yacc <EOL> except ImportError : <EOL> import ply . lex as lex <EOL> import ply . yacc as yacc <EOL> from common import capitalize , uncapitalize <EOL> import warnings <EOL> from arguments import * <EOL> from utilitytypes import * <EOL> def verbose ( ) : <EOL> return <NUM_LIT:0> <EOL> def currentfn ( ) : <EOL> try : <EOL> return sys . _getframe ( <NUM_LIT:1> ) . f_code . co_name <EOL> except : <EOL> pass <EOL> class NameParseError ( Exception ) : <EOL> pass <EOL> class ParsingWarning ( UserWarning ) : <EOL> pass <EOL> ProxyUni = proxyClass ( unicode , '<STR_LIT>' , module = __name__ , dataFuncName = '<STR_LIT>' , remove = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> class Parsed ( ProxyUni ) : <EOL> _parser = None <EOL> _accepts = ( ) <EOL> _name = None <EOL> classes = { } <EOL> @ classmethod <EOL> def accepts ( cls , other ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( other , Parsed ) : <EOL> for t in cls . _accepts : <EOL> if t == other . __class__ . __name__ : <EOL> return True <EOL> return False <EOL> def compileName ( self ) : <EOL> newname = u'<STR_LIT>' <EOL> partList = [ ] <EOL> def getParts ( obj , newname ) : <EOL> try : <EOL> for x in obj . parts : <EOL> newname = getParts ( x , newname ) <EOL> except AttributeError : <EOL> newname += unicode ( obj . _name ) <EOL> return newname <EOL> self . _name = getParts ( self , newname ) <EOL> return self . _name <EOL> @ classmethod <EOL> def getParserClass ( cls , parsername ) : <EOL> pass <EOL> @ classmethod <EOL> def classparserbuild ( cls , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> clsname = cls . __name__ <EOL> try : <EOL> parser = cls . _parser <EOL> except : <EOL> parsername = cls . __name__ + "<STR_LIT>" <EOL> parser = Parser . classes . get ( parsername , None ) <EOL> cls . _parser = parser <EOL> warnings . warn ( "<STR_LIT>" % ( cls , parsername , clsname ) , UserWarning ) <EOL> if parser is not None : <EOL> if not isinstance ( parser , Parser ) : <EOL> if inspect . isclass ( parser ) : <EOL> parsername = parser . __name__ <EOL> if not issubclass ( parser , Parser ) : <EOL> raise ValueError , "<STR_LIT>" % ( parsername , cls . __name__ ) <EOL> elif parser in Parser . classes : <EOL> parsername = parser <EOL> parser = Parser . classes [ parsername ] <EOL> else : <EOL> raise ValueError , "<STR_LIT>" % ( parser , cls . __name__ ) <EOL> cls . _parser = parser ( ) <EOL> cls . _parser . build ( ** kwargs ) <EOL> else : <EOL> raise TypeError , "<STR_LIT>" % cls . __name__ <EOL> @ classmethod <EOL> def classparse ( cls , data , ** kwargs ) : <EOL> clsname = cls . __name__ <EOL> data = unicode ( data ) <EOL> debug = kwargs . get ( '<STR_LIT>' , verbose ( ) ) <EOL> errmsg = '<STR_LIT>' <EOL> result = cls . classparser ( ) . parse ( data , debug = debug ) <EOL> if cls . classparser ( ) . errorcount : <EOL> errmsg = "<STR_LIT>" % ( data , clsname , cls . classparser ( ) . errorcount ) <EOL> isValid = False <EOL> elif not isinstance ( result , cls ) : <EOL> errmsg = "<STR_LIT>" % ( data , result . __class__ . __name__ , clsname ) <EOL> isValid = False <EOL> elif not result == data : <EOL> errmsg = "<STR_LIT>" % ( result , data ) <EOL> isValid = False <EOL> else : <EOL> isValid = True <EOL> if not isValid : <EOL> raise NameParseError , errmsg <EOL> if ( result . sub ) : <EOL> result . _pos = result . sub [ <NUM_LIT:0> ] . pos <EOL> else : <EOL> result . _pos = <NUM_LIT:0> <EOL> result . _valid = True <EOL> return result <EOL> @ classmethod <EOL> def classparser ( cls ) : <EOL> """<STR_LIT>""" <EOL> return cls . _parser <EOL> def parse ( self , data , ** kwargs ) : <EOL> return self . __class__ . classparse ( data , ** kwargs ) <EOL> @ property <EOL> def parser ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __class__ . classparser ( ) <EOL> @ property <EOL> def tokens ( self ) : <EOL> """<STR_LIT>""" <EOL> for i in expandArgs ( self . _sub ) : <EOL> if isinstance ( i , Token ) : <EOL> yield i <EOL> @ property <EOL> def sub ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _sub <EOL> def setSubItem ( self , index , value ) : <EOL> """<STR_LIT>""" <EOL> cls = self . _sub [ index ] . __class__ <EOL> sublist = list ( self . _sub ) <EOL> sublist [ index ] = cls ( value ) <EOL> self . _sub = tuple ( sublist ) <EOL> @ property <EOL> def pos ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _pos <EOL> def isValid ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _valid <EOL> def copy ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __class__ ( self . _sub ) <EOL> def findType ( self , type ) : <EOL> res = [ ] <EOL> for x in self . sub : <EOL> if isinstance ( x , type ) : <EOL> res . append ( x ) <EOL> else : <EOL> res += x . findType ( type ) <EOL> return res <EOL> def __new__ ( cls , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> debug = kwargs . get ( '<STR_LIT>' , verbose ( ) ) <EOL> data = None <EOL> if args : <EOL> if len ( args ) == <NUM_LIT:1> : <EOL> data = args [ <NUM_LIT:0> ] <EOL> else : <EOL> data = tuple ( args ) <EOL> ptype = None <EOL> if data is None : <EOL> ptype = '<STR_LIT>' <EOL> elif isinstance ( data , lex . LexToken ) : <EOL> ptype = kwargs . get ( '<STR_LIT:type>' , data . type ) <EOL> elif isinstance ( data , Parsed ) : <EOL> ptype = data . __class__ <EOL> ptype = kwargs . get ( '<STR_LIT:type>' , ptype ) <EOL> if ( cls is Parsed or cls is Token ) : <EOL> if ptype is not None : <EOL> if verbose ( ) : <EOL> print "<STR_LIT>" % ( cls . __name__ , ptype ) <EOL> newcls = Parsed . classes . get ( ptype , None ) <EOL> if newcls is None : <EOL> raise TypeError , "<STR_LIT>" % ( ptype , cls . __name__ ) <EOL> else : <EOL> clsname = newcls . __name__ <EOL> if not issubclass ( newcls , cls ) : <EOL> raise TypeError , "<STR_LIT>" % ( ptype , clsname , cls . __name__ ) <EOL> else : <EOL> raise TypeError , "<STR_LIT>" % ( cls . __name__ ) <EOL> else : <EOL> if verbose ( ) : <EOL> print "<STR_LIT>" % ( cls . __name__ ) <EOL> clsname = cls . __name__ <EOL> newcls = cls <EOL> pos = None <EOL> sub = [ ] <EOL> valid = False <EOL> value = data <EOL> if isinstance ( data , lex . LexToken ) : <EOL> if issubclass ( newcls , Token ) : <EOL> sub = [ ] <EOL> pos = data . lexpos <EOL> value = data . value <EOL> valid = True <EOL> else : <EOL> value = Token ( data . value , ptype = data . type , pos = data . pos ) <EOL> if data is None : <EOL> try : <EOL> value = newcls . default ( ) <EOL> valid = True <EOL> except : <EOL> valid = False <EOL> elif isinstance ( data , newcls ) : <EOL> sub = data . sub <EOL> pos = data . pos <EOL> valid = data . isValid ( ) <EOL> value = unicode ( data ) <EOL> elif newcls . accepts ( data ) : <EOL> sub . append ( data ) <EOL> pos = data . pos <EOL> valid = data . isValid ( ) <EOL> value = unicode ( data ) <EOL> elif isSequence ( data ) and not isinstance ( data , basestring ) : <EOL> if data : <EOL> valid = True <EOL> p = <NUM_LIT:0> <EOL> for arg in data : <EOL> if isinstance ( arg , lex . LexToken ) : <EOL> a = Token ( arg . value , ptype = arg . type , pos = data . pos ) <EOL> else : <EOL> a = arg <EOL> if isinstance ( a , newcls ) : <EOL> sub += a . sub <EOL> elif newcls . accepts ( a ) : <EOL> sub . append ( a ) <EOL> else : <EOL> valid = False <EOL> break <EOL> value = u"<STR_LIT>" . join ( map ( unicode , data ) ) <EOL> if valid : <EOL> pos = sub [ <NUM_LIT:0> ] . pos <EOL> else : <EOL> sub = [ ] <EOL> else : <EOL> value = '<STR_LIT>' <EOL> else : <EOL> if debug : print "<STR_LIT>" , data , repr ( data ) <EOL> value = unicode ( data ) <EOL> if issubclass ( newcls , Token ) and newcls is not Token : <EOL> sub = [ ] <EOL> pos = <NUM_LIT:0> <EOL> valid = True <EOL> else : <EOL> valid = False <EOL> if valid : <EOL> strvalue = unicode ( value ) <EOL> elif isinstance ( value , basestring ) : <EOL> if debug : <EOL> print "<STR_LIT>" % ( clsname , value ) <EOL> newcls . classparserbuild ( debug = debug ) <EOL> if debug : print "<STR_LIT>" , value , type ( value ) <EOL> result = newcls . classparse ( value , debug = debug ) <EOL> if debug : print "<STR_LIT>" , result , type ( result ) , isinstance ( result , newcls ) <EOL> if result is not None and isinstance ( result , newcls ) : <EOL> strvalue = unicode ( result ) <EOL> valid = result . _valid <EOL> sub = result . _sub <EOL> pos = result . _pos <EOL> if debug : print "<STR_LIT>" , sub <EOL> else : <EOL> strvalue = '<STR_LIT>' <EOL> valid = False <EOL> else : <EOL> raise TypeError , "<STR_LIT>" % ( value , clsname ) <EOL> if valid : <EOL> newobj = super ( Parsed , cls ) . __new__ ( newcls ) <EOL> newobj . _name = strvalue <EOL> newobj . _sub = tuple ( sub ) <EOL> newobj . _valid = valid <EOL> pos = kwargs . get ( '<STR_LIT>' , pos ) <EOL> if pos is not None : <EOL> pos += kwargs . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> if pos is None or ( isinstance ( pos , int ) and pos >= <NUM_LIT:0> ) : <EOL> newobj . _pos = pos <EOL> else : <EOL> raise ValueError , "<STR_LIT>" % pos <EOL> return newobj <EOL> def __add__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> cls = self . __class__ <EOL> selfvalid = self . isValid ( ) <EOL> sublist = list ( self . sub ) <EOL> value = unicode ( self ) <EOL> if isinstance ( other , cls ) : <EOL> othervalid = other . isValid ( ) <EOL> sublist += other . sub <EOL> elif self . accepts ( other ) : <EOL> othervalid = other . isValid ( ) <EOL> sublist . append ( other ) <EOL> elif isinstance ( other , basestring ) : <EOL> othervalid = False <EOL> else : <EOL> raise TypeError , "<STR_LIT>" % ( type ( self ) , type ( other ) ) <EOL> if selfvalid and othervalid : <EOL> result = cls ( * sublist ) <EOL> else : <EOL> result = cls ( unicode ( self ) + unicode ( other ) ) <EOL> return result <EOL> def __repr__ ( self ) : <EOL> return u"<STR_LIT>" % ( self . __class__ . __name__ , self , self . pos ) <EOL> class Parser ( object ) : <EOL> """<STR_LIT>""" <EOL> classes = { } <EOL> def __new__ ( cls , * args , ** kwargs ) : <EOL> if cls is Parser : <EOL> ptype = kwargs . get ( '<STR_LIT:type>' , None ) <EOL> if ptype is None : <EOL> raise TypeError , "<STR_LIT>" <EOL> elif isinstance ( ptype , Parser ) and not ptype is Parser : <EOL> parsercls = ptype <EOL> elif ptype in Parser . classes : <EOL> parsercls = Parser . classes [ ptype ] <EOL> else : <EOL> raise TypeError , "<STR_LIT>" % ptype <EOL> else : <EOL> parsercls = cls <EOL> reserved = [ ] <EOL> rulesDict , tokensDict = Parser . getRulesAndTokens ( parsercls ) <EOL> tokens = tokensDict . keys ( ) <EOL> rules = list ( rulesDict . keys ( ) ) <EOL> rules . sort ( lambda x , y : cmp ( rulesDict [ x ] . func_code . co_firstlineno , rulesDict [ y ] . func_code . co_firstlineno ) ) <EOL> rules = tuple ( rules ) <EOL> parsercls . tokens = tuple ( tokens + list ( set ( reserved ) ) ) <EOL> parsercls . rules = rules <EOL> if not '<STR_LIT:start>' in parsercls . __dict__ : <EOL> parsercls . start = None <EOL> return super ( Parser , cls ) . __new__ ( parsercls , * args , ** kwargs ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . errorcount = <NUM_LIT:0> <EOL> self . lexer = None <EOL> self . parser = None <EOL> def t_error ( self , t ) : <EOL> warnings . warn ( "<STR_LIT>" % ( t . lexer . lexdata , t . lexpos , t . value [ <NUM_LIT:0> ] ) , ParsingWarning , stacklevel = <NUM_LIT:1> ) <EOL> self . errorcount += <NUM_LIT:1> <EOL> t . lexer . skip ( <NUM_LIT:1> ) <EOL> def p_error ( self , p ) : <EOL> print "<STR_LIT>" , p <EOL> if p is None : <EOL> warnings . warn ( "<STR_LIT>" , ParsingWarning , stacklevel = <NUM_LIT:1> ) <EOL> else : <EOL> warnings . warn ( "<STR_LIT>" % ( p . lexer . lexdata , p . lexpos , p . value ) , ParsingWarning , stacklevel = <NUM_LIT:1> ) <EOL> self . errorcount += <NUM_LIT:1> <EOL> @ staticmethod <EOL> def getRulesAndTokens ( parsercls ) : <EOL> """<STR_LIT>""" <EOL> tokensDict = { } <EOL> rulesDict = { } <EOL> for name , obj in inspect . getmembers ( parsercls ) : <EOL> if name . startswith ( '<STR_LIT>' ) and name != '<STR_LIT>' : <EOL> if isinstance ( obj , basestring ) : <EOL> v = obj <EOL> elif inspect . isfunction ( obj ) or inspect . ismethod ( obj ) : <EOL> v = obj . __doc__ <EOL> else : <EOL> raise SyntaxError , "<STR_LIT>" % m [ <NUM_LIT:0> ] <EOL> k = name [ <NUM_LIT:2> : ] <EOL> tokensDict [ k ] = obj <EOL> elif name . startswith ( '<STR_LIT>' ) and inspect . ismethod ( obj ) and name != '<STR_LIT>' : <EOL> k = name [ <NUM_LIT:2> : ] <EOL> rulesDict [ k ] = obj <EOL> return rulesDict , tokensDict <EOL> def build ( self , ** kwargs ) : <EOL> debug = kwargs . get ( '<STR_LIT>' , verbose ( ) ) <EOL> start = kwargs . get ( '<STR_LIT:start>' , self . __class__ . start ) <EOL> parserspath = kwargs . get ( '<STR_LIT>' , tempfile . gettempdir ( ) ) <EOL> if debug : <EOL> print "<STR_LIT>" , parserspath <EOL> method = kwargs . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if debug : <EOL> print "<STR_LIT>" , self . __class__ . __name__ <EOL> print "<STR_LIT>" <EOL> for t in self . __class__ . tokens : <EOL> print "<STR_LIT>" % ( t ) <EOL> print "<STR_LIT>" <EOL> for t in self . __class__ . rules : <EOL> print "<STR_LIT>" % ( t ) <EOL> print "<STR_LIT>" % start <EOL> if self . lexer is None : <EOL> lextab = self . __class__ . __name__ + "<STR_LIT>" <EOL> lkwargs = { '<STR_LIT>' : debug , '<STR_LIT>' : lextab } <EOL> self . lexer = lex . lex ( object = self , ** lkwargs ) <EOL> if self . parser is None : <EOL> tabmodule = self . __class__ . __name__ + "<STR_LIT>" + start <EOL> pkwargs = { '<STR_LIT>' : parserspath , '<STR_LIT>' : debug , '<STR_LIT>' : tabmodule , '<STR_LIT:start>' : start , '<STR_LIT>' : method } <EOL> self . parser = yacc . yacc ( module = self , ** pkwargs ) <EOL> def parse ( self , data , ** kwargs ) : <EOL> self . errorcount = <NUM_LIT:0> <EOL> return self . parser . parse ( data , lexer = self . lexer , ** kwargs ) <EOL> class Token ( Parsed ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class TokenParser ( Parser ) : <EOL> """<STR_LIT>""" <EOL> _pattern = None <EOL> _type = None <EOL> def build ( self , ** kwargs ) : <EOL> pattern = kwargs . get ( '<STR_LIT>' , self . __class__ . _pattern ) <EOL> try : <EOL> self . parser = re . compile ( pattern ) <EOL> except : <EOL> raise ValueError , "<STR_LIT>" % pattern <EOL> def parse ( self , data , ** kwargs ) : <EOL> self . errorcount = <NUM_LIT:0> <EOL> if self . parser . match ( data ) is not None : <EOL> return Token ( data , type = self . _type , pos = <NUM_LIT:0> ) <EOL> else : <EOL> warnings . warn ( "<STR_LIT>" % ( data , self . __class__ . __name__ , self . _pattern ) ) <EOL> self . errorcount += <NUM_LIT:1> <EOL> class EmptyTokenParser ( Parser ) : <EOL> def build ( self , ** kwargs ) : <EOL> pass <EOL> def parse ( self , data , ** kwargs ) : <EOL> self . errorcount = <NUM_LIT:0> <EOL> if data : <EOL> self . errorcount = <NUM_LIT:1> <EOL> else : <EOL> return Empty ( ) <EOL> class EmptyParser ( Parser ) : <EOL> """<STR_LIT>""" <EOL> start = '<STR_LIT>' <EOL> def p_empty ( self , p ) : <EOL> '<STR_LIT>' <EOL> pass <EOL> def isParsedClass ( x ) : <EOL> try : <EOL> return issubclass ( x , Parsed ) <EOL> except : <EOL> return False <EOL> def isParserClass ( x ) : <EOL> try : <EOL> return issubclass ( x , Parser ) <EOL> except : <EOL> return False <EOL> def _printClassTree ( cls ) : <EOL> def _printTree ( data , level ) : <EOL> if isinstance ( data , tuple ) : <EOL> level += <NUM_LIT:1> <EOL> for d in data : <EOL> _printTree ( d , level ) <EOL> else : <EOL> print data <EOL> print cls <EOL> tree = inspect . getclasstree ( cls ) <EOL> print tree <EOL> _printTree ( tree ) <EOL> class autoparsed ( type ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( mcl , classname , bases , classdict ) : <EOL> newcls = super ( autoparsed , mcl ) . __new__ ( mcl , classname , bases , classdict ) <EOL> module = __import__ ( newcls . __module__ , globals ( ) , locals ( ) , [ '<STR_LIT>' ] ) <EOL> if issubclass ( newcls , Parsed ) : <EOL> parsercls = classdict . get ( '<STR_LIT>' , None ) <EOL> if isParserClass ( parsercls ) or ( parsercls is None and '<STR_LIT>' in classdict ) : <EOL> rules = classdict . get ( '<STR_LIT>' , None ) <EOL> if rules is not None : <EOL> if not isIterable ( rules ) : <EOL> raise TypeError , "<STR_LIT>" <EOL> parserdict = { } <EOL> for i , rule in enumerate ( rules ) : <EOL> attrname = '<STR_LIT>' % ( classname , i + <NUM_LIT:1> ) <EOL> parserdict [ attrname ] = rule <EOL> else : <EOL> parserdict = parsercls . __dict__ <EOL> updatedict = { } <EOL> if '<STR_LIT:start>' not in parserdict : <EOL> updatedict [ '<STR_LIT:start>' ] = classname <EOL> tokensDict = { } <EOL> rulesDict = { } <EOL> reservedDict = { } <EOL> accepts_set = set ( classdict . get ( '<STR_LIT>' , [ ] ) ) <EOL> classNameReg = re . compile ( '<STR_LIT>' ) <EOL> for name , obj in parserdict . items ( ) : <EOL> if name . startswith ( '<STR_LIT>' ) : <EOL> shortname = name [ <NUM_LIT:2> : ] <EOL> if isinstance ( obj , basestring ) : <EOL> v = obj <EOL> needsWrapping = True <EOL> elif inspect . isfunction ( obj ) or inspect . ismethod ( obj ) : <EOL> v = inspect . getdoc ( obj ) <EOL> needsWrapping = False <EOL> else : <EOL> raise SyntaxError , "<STR_LIT>" % name <EOL> accepts = [ ] <EOL> colonSplit = [ x . strip ( ) for x in v . split ( '<STR_LIT::>' ) ] <EOL> if len ( colonSplit ) == <NUM_LIT:1> : <EOL> v = classname + '<STR_LIT>' + v <EOL> colonSplit = [ classname , colonSplit [ <NUM_LIT:0> ] ] <EOL> if len ( colonSplit ) == <NUM_LIT:2> and colonSplit [ <NUM_LIT:0> ] != '<STR_LIT>' : <EOL> pipeSplit = [ x . strip ( ) for x in colonSplit [ <NUM_LIT:1> ] . split ( '<STR_LIT:|>' ) ] <EOL> for grp in pipeSplit : <EOL> accepts += [ x for x in grp . split ( ) if classNameReg . match ( x ) and x != classname ] <EOL> accepts_set . update ( accepts ) <EOL> if needsWrapping : <EOL> def p_func ( self , p ) : <EOL> p [ <NUM_LIT:0> ] = newcls ( * p [ <NUM_LIT:1> : len ( p ) ] ) <EOL> p_func . __name__ = name <EOL> p_func . __doc__ = v <EOL> if verbose ( ) : <EOL> print "<STR_LIT>" % ( classname , name , v ) <EOL> updatedict [ name ] = p_func <EOL> rulesDict [ name ] = p_func <EOL> else : <EOL> rulesDict [ name ] = obj <EOL> elif name . startswith ( '<STR_LIT>' ) and name != '<STR_LIT>' : <EOL> shortname = name [ <NUM_LIT:2> : ] <EOL> if isinstance ( obj , basestring ) : <EOL> v = obj <EOL> elif inspect . isfunction ( obj ) or inspect . ismethod ( obj ) : <EOL> v = obj . __doc__ <EOL> else : <EOL> raise SyntaxError , "<STR_LIT>" % name <EOL> tokensDict [ name ] = obj <EOL> if verbose ( ) : <EOL> print "<STR_LIT>" % ( newcls . __name__ , list ( accepts_set ) ) <EOL> bases = [ ] <EOL> for parsedname in accepts_set : <EOL> try : <EOL> parsedcls = getattr ( module , parsedname ) <EOL> except AttributeError : <EOL> warnings . warn ( "<STR_LIT>" % ( parsedname , module ) , UserWarning ) <EOL> continue <EOL> try : <EOL> parser = parsedcls . _parser <EOL> except AttributeError : <EOL> try : <EOL> parser = getattr ( module , parsedname + '<STR_LIT>' ) <EOL> except AttributeError : <EOL> raise SyntaxError , "<STR_LIT>" % ( parsedname , parsedname , parsedname ) <EOL> assert isinstance ( parser , Parser ) or ( inspect . isclass ( parser ) and issubclass ( parser , Parser ) ) , '<STR_LIT>' % ( parsedname , parser ) <EOL> bases . append ( parser ) <EOL> newbases = [ ] <EOL> for i in bases : <EOL> issuperclass = False <EOL> for j in bases : <EOL> if i in inspect . getmro ( j ) [ <NUM_LIT:1> : ] : <EOL> issuperclass = True <EOL> break <EOL> if not issuperclass : <EOL> newbases . append ( i ) <EOL> newbases = sorted ( newbases , key = lambda x : len ( inspect . getmro ( x ) ) , reverse = True ) <EOL> for base in newbases : <EOL> tokensDict . update ( base . tokensDict ) <EOL> rulesDict . update ( base . rulesDict ) <EOL> updatedict . update ( tokensDict ) <EOL> updatedict . update ( rulesDict ) <EOL> if rules is not None : <EOL> parserName = classname + '<STR_LIT>' <EOL> updatedict [ '<STR_LIT>' ] = tokensDict <EOL> updatedict [ '<STR_LIT>' ] = rulesDict <EOL> parserdict . update ( updatedict ) <EOL> parsercls = type ( parserName , ( Parser , ) , parserdict ) <EOL> parsercls . __module__ = module . __name__ <EOL> setattr ( newcls , '<STR_LIT>' , parsercls ) <EOL> else : <EOL> for k , v in updatedict . iteritems ( ) : <EOL> setattr ( parsercls , k , v ) <EOL> setattr ( newcls , '<STR_LIT>' , tuple ( accepts_set ) ) <EOL> elif issubclass ( newcls , Token ) : <EOL> token = classdict . get ( '<STR_LIT>' , None ) <EOL> if token is None : <EOL> raise TypeError , "<STR_LIT>" <EOL> tokenBasename = classname + '<STR_LIT>' <EOL> tokenName = '<STR_LIT>' + tokenBasename <EOL> ruleName = '<STR_LIT>' + tokenBasename <EOL> parserName = classname + '<STR_LIT>' <EOL> if hasattr ( token , '<STR_LIT>' ) : <EOL> token . __name__ = tokenName <EOL> parserdict = { } <EOL> parserdict [ tokenName ] = token <EOL> tokensDict = { tokenName : token } <EOL> parserdict [ '<STR_LIT>' ] = tokensDict <EOL> doc = '''<STR_LIT>''' % ( classname , tokenBasename ) <EOL> def rule ( self , p ) : <EOL> p [ <NUM_LIT:0> ] = newcls ( p [ <NUM_LIT:1> ] , pos = p . lexpos ( <NUM_LIT:1> ) ) <EOL> rule . __name__ = ruleName <EOL> rule . __doc__ = doc <EOL> parserdict [ ruleName ] = rule <EOL> parserdict [ '<STR_LIT>' ] = { ruleName : rule } <EOL> print "<STR_LIT>" % parserName <EOL> parsercls = type ( parserName , ( Parser , ) , parserdict ) <EOL> parsercls . __module__ = module . __name__ <EOL> setattr ( newcls , '<STR_LIT>' , parsercls ) <EOL> return newcls <EOL> def _getTokenPatterns ( parsercls ) : <EOL> tokensDict = { } <EOL> for name , obj in parsercls . __dict__ . items ( ) : <EOL> if name . startswith ( '<STR_LIT>' ) and name != '<STR_LIT>' : <EOL> k = name [ <NUM_LIT:2> : ] <EOL> if isinstance ( obj , basestring ) : <EOL> v = obj <EOL> elif inspect . isfunction ( obj ) or inspect . ismethod ( obj ) : <EOL> v = obj . __doc__ <EOL> else : <EOL> raise SyntaxError , "<STR_LIT>" % name <EOL> k = name [ <NUM_LIT:2> : ] <EOL> if k in tokensDict : <EOL> warnings . warn ( "<STR_LIT>" % ( k , parser ) , UserWarning ) <EOL> tokensDict [ k ] = v <EOL> return tokensDict <EOL> Parsed . classes = { } <EOL> Parser . classes = { } <EOL> def process ( module = None ) : <EOL> """<STR_LIT>""" <EOL> if module : <EOL> if isinstance ( module , types . ModuleType ) : <EOL> module_dict = module . __dict__ <EOL> else : <EOL> raise ValueError , "<STR_LIT>" <EOL> else : <EOL> try : <EOL> raise RuntimeError <EOL> except RuntimeError : <EOL> e , b , t = sys . exc_info ( ) <EOL> f = t . tb_frame <EOL> f = f . f_back <EOL> module_dict = f . f_globals <EOL> parserDict = { } <EOL> parsedDict = { } <EOL> tokensDict = { } <EOL> for name , obj in module_dict . iteritems ( ) : <EOL> if isParserClass ( obj ) : <EOL> parserDict [ name ] = obj <EOL> tokensDict . update ( _getTokenPatterns ( obj ) ) <EOL> elif isParsedClass ( obj ) : <EOL> parsedDict [ name ] = obj <EOL> if hasattr ( obj , '<STR_LIT>' ) : <EOL> if isParserClass ( obj . _parser ) : <EOL> tokensDict . update ( _getTokenPatterns ( obj . _parser ) ) <EOL> for token in tokensDict : <EOL> pattern = tokensDict [ token ] <EOL> parsedName = token <EOL> parserName = token + "<STR_LIT>" <EOL> if verbose ( ) : <EOL> print "<STR_LIT>" % ( parserName , token , pattern ) <EOL> class ThisTokenParser ( TokenParser ) : <EOL> """<STR_LIT>""" <EOL> ThisTokenParser . __name__ = parserName <EOL> ThisTokenParser . __module__ = __name__ <EOL> ThisTokenParser . _pattern = pattern <EOL> ThisTokenParser . _type = token <EOL> module_dict [ parserName ] = ThisTokenParser <EOL> parserDict [ parserName ] = ThisTokenParser <EOL> if verbose ( ) : <EOL> print "<STR_LIT>" % ( parsedName , token , pattern ) <EOL> class ThisToken ( Token ) : <EOL> """<STR_LIT>""" <EOL> ThisToken . __name__ = parsedName <EOL> ThisToken . __module__ = __name__ <EOL> ThisToken . _parser = ThisTokenParser <EOL> module_dict [ parsedName ] = ThisToken <EOL> parsedDict [ parsedName ] = ThisToken <EOL> Parser . classes = parserDict <EOL> Parsed . classes = parsedDict </s>
