<s> """<STR_LIT>""" <EOL> import fileinput <EOL> import re <EOL> import sys <EOL> from StringIO import StringIO <EOL> SPEC = "<STR_LIT>" <EOL> COMMA = "<STR_LIT:U+002CU+0020>" <EOL> def load_boilerplate ( file ) : <EOL> f = open ( file ) <EOL> buf = f . read ( ) <EOL> f . close ( ) <EOL> i = buf . find ( '<STR_LIT>' '<STR_LIT>' ) <EOL> j = buf . find ( '<STR_LIT>' '<STR_LIT>' ) <EOL> pro = buf [ i + <NUM_LIT:12> : j ] . strip ( ) <EOL> epi = buf [ j + <NUM_LIT:12> : ] . strip ( ) <EOL> return pro , epi <EOL> def strip_default ( arg ) : <EOL> """<STR_LIT>""" <EOL> i = arg . find ( '<STR_LIT:=>' ) <EOL> if i == - <NUM_LIT:1> : <EOL> return arg <EOL> t = arg [ : i ] . strip ( ) <EOL> return t <EOL> P_NODE = <NUM_LIT:1> <EOL> P_OTHER = <NUM_LIT:2> <EOL> P_NESTED = <NUM_LIT:3> <EOL> P_NONE = <NUM_LIT:4> <EOL> class NodeInfo : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , args ) : <EOL> self . name = name <EOL> self . args = args . strip ( ) <EOL> self . argnames = self . get_argnames ( ) <EOL> self . argprops = self . get_argprops ( ) <EOL> self . nargs = len ( self . argnames ) <EOL> self . init = [ ] <EOL> def get_argnames ( self ) : <EOL> if '<STR_LIT:(>' in self . args : <EOL> i = self . args . find ( '<STR_LIT:(>' ) <EOL> j = self . args . rfind ( '<STR_LIT:)>' ) <EOL> args = self . args [ i + <NUM_LIT:1> : j ] <EOL> else : <EOL> args = self . args <EOL> return [ strip_default ( arg . strip ( ) ) <EOL> for arg in args . split ( '<STR_LIT:U+002C>' ) if arg ] <EOL> def get_argprops ( self ) : <EOL> """<STR_LIT>""" <EOL> d = { } <EOL> hardest_arg = P_NODE <EOL> for i in range ( len ( self . argnames ) ) : <EOL> arg = self . argnames [ i ] <EOL> if arg . endswith ( '<STR_LIT:*>' ) : <EOL> arg = self . argnames [ i ] = arg [ : - <NUM_LIT:1> ] <EOL> d [ arg ] = P_OTHER <EOL> hardest_arg = max ( hardest_arg , P_OTHER ) <EOL> elif arg . endswith ( '<STR_LIT:!>' ) : <EOL> arg = self . argnames [ i ] = arg [ : - <NUM_LIT:1> ] <EOL> d [ arg ] = P_NESTED <EOL> hardest_arg = max ( hardest_arg , P_NESTED ) <EOL> elif arg . endswith ( '<STR_LIT:&>' ) : <EOL> arg = self . argnames [ i ] = arg [ : - <NUM_LIT:1> ] <EOL> d [ arg ] = P_NONE <EOL> hardest_arg = max ( hardest_arg , P_NONE ) <EOL> else : <EOL> d [ arg ] = P_NODE <EOL> self . hardest_arg = hardest_arg <EOL> if hardest_arg > P_NODE : <EOL> self . args = self . args . replace ( '<STR_LIT:*>' , '<STR_LIT>' ) <EOL> self . args = self . args . replace ( '<STR_LIT:!>' , '<STR_LIT>' ) <EOL> self . args = self . args . replace ( '<STR_LIT:&>' , '<STR_LIT>' ) <EOL> return d <EOL> def gen_source ( self ) : <EOL> buf = StringIO ( ) <EOL> print >> buf , "<STR_LIT>" % self . name <EOL> self . _gen_init ( buf ) <EOL> print >> buf <EOL> self . _gen_getChildren ( buf ) <EOL> print >> buf <EOL> self . _gen_getChildNodes ( buf ) <EOL> print >> buf <EOL> self . _gen_repr ( buf ) <EOL> buf . seek ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> return buf . read ( ) <EOL> def _gen_init ( self , buf ) : <EOL> if self . args : <EOL> print >> buf , "<STR_LIT>" % self . args <EOL> else : <EOL> print >> buf , "<STR_LIT>" <EOL> if self . argnames : <EOL> for name in self . argnames : <EOL> print >> buf , "<STR_LIT>" % ( name , name ) <EOL> print >> buf , "<STR_LIT>" <EOL> for line in self . init : <EOL> print >> buf , ( "<STR_LIT:U+0020>" + line ) . rstrip ( ) <EOL> def _gen_getChildren ( self , buf ) : <EOL> print >> buf , "<STR_LIT>" <EOL> if len ( self . argnames ) == <NUM_LIT:0> : <EOL> print >> buf , "<STR_LIT>" <EOL> else : <EOL> if self . hardest_arg < P_NESTED : <EOL> clist = COMMA . join ( [ "<STR_LIT>" % c <EOL> for c in self . argnames ] ) <EOL> if self . nargs == <NUM_LIT:1> : <EOL> print >> buf , "<STR_LIT>" % clist <EOL> else : <EOL> print >> buf , "<STR_LIT>" % clist <EOL> else : <EOL> if len ( self . argnames ) == <NUM_LIT:1> : <EOL> print >> buf , "<STR_LIT>" % self . argnames [ <NUM_LIT:0> ] <EOL> else : <EOL> print >> buf , "<STR_LIT>" <EOL> template = "<STR_LIT>" <EOL> for name in self . argnames : <EOL> if self . argprops [ name ] == P_NESTED : <EOL> print >> buf , template % ( "<STR_LIT>" , "<STR_LIT>" , <EOL> name , "<STR_LIT:)>" ) <EOL> else : <EOL> print >> buf , template % ( "<STR_LIT>" , "<STR_LIT>" , name , "<STR_LIT>" ) <EOL> print >> buf , "<STR_LIT>" <EOL> def _gen_getChildNodes ( self , buf ) : <EOL> print >> buf , "<STR_LIT>" <EOL> if len ( self . argnames ) == <NUM_LIT:0> : <EOL> print >> buf , "<STR_LIT>" <EOL> else : <EOL> if self . hardest_arg < P_NESTED : <EOL> clist = [ "<STR_LIT>" % c <EOL> for c in self . argnames <EOL> if self . argprops [ c ] == P_NODE ] <EOL> if len ( clist ) == <NUM_LIT:0> : <EOL> print >> buf , "<STR_LIT>" <EOL> elif len ( clist ) == <NUM_LIT:1> : <EOL> print >> buf , "<STR_LIT>" % clist [ <NUM_LIT:0> ] <EOL> else : <EOL> print >> buf , "<STR_LIT>" % COMMA . join ( clist ) <EOL> else : <EOL> print >> buf , "<STR_LIT>" <EOL> template = "<STR_LIT>" <EOL> for name in self . argnames : <EOL> if self . argprops [ name ] == P_NONE : <EOL> tmp = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> print >> buf , tmp % ( name , name ) <EOL> elif self . argprops [ name ] == P_NESTED : <EOL> print >> buf , template % ( "<STR_LIT>" , "<STR_LIT>" , <EOL> name , "<STR_LIT:)>" ) <EOL> elif self . argprops [ name ] == P_NODE : <EOL> print >> buf , template % ( "<STR_LIT>" , "<STR_LIT>" , name , "<STR_LIT>" ) <EOL> print >> buf , "<STR_LIT>" <EOL> def _gen_repr ( self , buf ) : <EOL> print >> buf , "<STR_LIT>" <EOL> if self . argnames : <EOL> fmt = COMMA . join ( [ "<STR_LIT:%s>" ] * self . nargs ) <EOL> if '<STR_LIT:(>' in self . args : <EOL> fmt = '<STR_LIT>' % fmt <EOL> vals = [ "<STR_LIT>" % name for name in self . argnames ] <EOL> vals = COMMA . join ( vals ) <EOL> if self . nargs == <NUM_LIT:1> : <EOL> vals = vals + "<STR_LIT:U+002C>" <EOL> print >> buf , '<STR_LIT>' % ( self . name , fmt , vals ) <EOL> else : <EOL> print >> buf , '<STR_LIT>' % self . name <EOL> rx_init = re . compile ( '<STR_LIT>' ) <EOL> def parse_spec ( file ) : <EOL> classes = { } <EOL> cur = None <EOL> for line in fileinput . input ( file ) : <EOL> if line . strip ( ) . startswith ( '<STR_LIT:#>' ) : <EOL> continue <EOL> mo = rx_init . search ( line ) <EOL> if mo is None : <EOL> if cur is None : <EOL> try : <EOL> name , args = line . split ( '<STR_LIT::>' ) <EOL> except ValueError : <EOL> continue <EOL> classes [ name ] = NodeInfo ( name , args ) <EOL> cur = None <EOL> else : <EOL> cur . init . append ( line ) <EOL> else : <EOL> name = mo . group ( <NUM_LIT:1> ) <EOL> cur = classes [ name ] <EOL> return sorted ( classes . values ( ) , key = lambda n : n . name ) <EOL> def main ( ) : <EOL> prologue , epilogue = load_boilerplate ( sys . argv [ - <NUM_LIT:1> ] ) <EOL> print prologue <EOL> print <EOL> classes = parse_spec ( SPEC ) <EOL> for info in classes : <EOL> print info . gen_source ( ) <EOL> print epilogue <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> """<STR_LIT>""" <EOL> from consts import CO_VARARGS , CO_VARKEYWORDS <EOL> def flatten ( seq ) : <EOL> l = [ ] <EOL> for elt in seq : <EOL> t = type ( elt ) <EOL> if t is tuple or t is list : <EOL> for elt2 in flatten ( elt ) : <EOL> l . append ( elt2 ) <EOL> else : <EOL> l . append ( elt ) <EOL> return l <EOL> def flatten_nodes ( seq ) : <EOL> return [ n for n in flatten ( seq ) if isinstance ( n , Node ) ] <EOL> nodes = { } <EOL> class Node ( object ) : <EOL> """<STR_LIT>""" <EOL> def getChildren ( self ) : <EOL> pass <EOL> def __iter__ ( self ) : <EOL> for n in self . getChildren ( ) : <EOL> yield n <EOL> def asList ( self ) : <EOL> return self . getChildren ( ) <EOL> def getChildNodes ( self ) : <EOL> pass <EOL> def _get_lineno ( self ) : <EOL> return self . _lineno <EOL> def _set_lineno ( self , lineno ) : <EOL> if lineno is not None and not isinstance ( lineno , int ) : <EOL> self . _context = lineno <EOL> self . _lineno = lineno [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> else : <EOL> self . _lineno = lineno <EOL> self . _context = None <EOL> lineno = property ( _get_lineno , _set_lineno ) <EOL> class EmptyNode ( Node ) : <EOL> pass <EOL> class Expression ( Node ) : <EOL> nodes [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> def __init__ ( self , node ) : <EOL> Node . __init__ ( self ) <EOL> self . node = node <EOL> def getChildren ( self ) : <EOL> return self . node , <EOL> def getChildNodes ( self ) : <EOL> return self . node , <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( repr ( self . node ) ) <EOL> for name , obj in globals ( ) . items ( ) : <EOL> if isinstance ( obj , type ) and issubclass ( obj , Node ) : <EOL> nodes [ name . lower ( ) ] = obj </s>
