<s> """<STR_LIT>""" <EOL> import os , sys <EOL> import asdl <EOL> TABSIZE = <NUM_LIT:8> <EOL> MAX_COL = <NUM_LIT> <EOL> def get_c_type ( name ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( name , asdl . Id ) : <EOL> name = name . value <EOL> if name in asdl . builtin_types : <EOL> return name <EOL> else : <EOL> return "<STR_LIT>" % name <EOL> def reflow_lines ( s , depth ) : <EOL> """<STR_LIT>""" <EOL> size = MAX_COL - depth * TABSIZE <EOL> if len ( s ) < size : <EOL> return [ s ] <EOL> lines = [ ] <EOL> cur = s <EOL> padding = "<STR_LIT>" <EOL> while len ( cur ) > size : <EOL> i = cur . rfind ( '<STR_LIT:U+0020>' , <NUM_LIT:0> , size ) <EOL> if i == - <NUM_LIT:1> and '<STR_LIT>' in cur : <EOL> i = size + <NUM_LIT:3> <EOL> assert i != - <NUM_LIT:1> , "<STR_LIT>" % ( size , s ) <EOL> lines . append ( padding + cur [ : i ] ) <EOL> if len ( lines ) == <NUM_LIT:1> : <EOL> j = cur . find ( '<STR_LIT:{>' , <NUM_LIT:0> , i ) <EOL> if j >= <NUM_LIT:0> : <EOL> j += <NUM_LIT:2> <EOL> size -= j <EOL> padding = "<STR_LIT:U+0020>" * j <EOL> else : <EOL> j = cur . find ( '<STR_LIT:(>' , <NUM_LIT:0> , i ) <EOL> if j >= <NUM_LIT:0> : <EOL> j += <NUM_LIT:1> <EOL> size -= j <EOL> padding = "<STR_LIT:U+0020>" * j <EOL> cur = cur [ i + <NUM_LIT:1> : ] <EOL> else : <EOL> lines . append ( padding + cur ) <EOL> return lines <EOL> def is_simple ( sum ) : <EOL> """<STR_LIT>""" <EOL> for t in sum . types : <EOL> if t . fields : <EOL> return False <EOL> return True <EOL> class EmitVisitor ( asdl . VisitorBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , file ) : <EOL> self . file = file <EOL> super ( EmitVisitor , self ) . __init__ ( ) <EOL> def emit ( self , s , depth , reflow = True ) : <EOL> if reflow : <EOL> lines = reflow_lines ( s , depth ) <EOL> else : <EOL> lines = [ s ] <EOL> for line in lines : <EOL> line = ( "<STR_LIT:U+0020>" * TABSIZE * depth ) + line + "<STR_LIT:\n>" <EOL> self . file . write ( line ) <EOL> class TypeDefVisitor ( EmitVisitor ) : <EOL> def visitModule ( self , mod ) : <EOL> for dfn in mod . dfns : <EOL> self . visit ( dfn ) <EOL> def visitType ( self , type , depth = <NUM_LIT:0> ) : <EOL> self . visit ( type . value , type . name , depth ) <EOL> def visitSum ( self , sum , name , depth ) : <EOL> if is_simple ( sum ) : <EOL> self . simple_sum ( sum , name , depth ) <EOL> else : <EOL> self . sum_with_constructors ( sum , name , depth ) <EOL> def simple_sum ( self , sum , name , depth ) : <EOL> enum = [ ] <EOL> for i in range ( len ( sum . types ) ) : <EOL> type = sum . types [ i ] <EOL> enum . append ( "<STR_LIT>" % ( type . name , i + <NUM_LIT:1> ) ) <EOL> enums = "<STR_LIT:U+002CU+0020>" . join ( enum ) <EOL> ctype = get_c_type ( name ) <EOL> s = "<STR_LIT>" % ( name , enums , ctype ) <EOL> self . emit ( s , depth ) <EOL> self . emit ( "<STR_LIT>" , depth ) <EOL> def sum_with_constructors ( self , sum , name , depth ) : <EOL> ctype = get_c_type ( name ) <EOL> s = "<STR_LIT>" % locals ( ) <EOL> self . emit ( s , depth ) <EOL> self . emit ( "<STR_LIT>" , depth ) <EOL> def visitProduct ( self , product , name , depth ) : <EOL> ctype = get_c_type ( name ) <EOL> s = "<STR_LIT>" % locals ( ) <EOL> self . emit ( s , depth ) <EOL> self . emit ( "<STR_LIT>" , depth ) <EOL> class StructVisitor ( EmitVisitor ) : <EOL> """<STR_LIT>""" <EOL> def visitModule ( self , mod ) : <EOL> for dfn in mod . dfns : <EOL> self . visit ( dfn ) <EOL> def visitType ( self , type , depth = <NUM_LIT:0> ) : <EOL> self . visit ( type . value , type . name , depth ) <EOL> def visitSum ( self , sum , name , depth ) : <EOL> if not is_simple ( sum ) : <EOL> self . sum_with_constructors ( sum , name , depth ) <EOL> def sum_with_constructors ( self , sum , name , depth ) : <EOL> def emit ( s , depth = depth ) : <EOL> self . emit ( s % sys . _getframe ( <NUM_LIT:1> ) . f_locals , depth ) <EOL> enum = [ ] <EOL> for i in range ( len ( sum . types ) ) : <EOL> type = sum . types [ i ] <EOL> enum . append ( "<STR_LIT>" % ( type . name , i + <NUM_LIT:1> ) ) <EOL> emit ( "<STR_LIT>" + "<STR_LIT:U+002CU+0020>" . join ( enum ) + "<STR_LIT>" ) <EOL> emit ( "<STR_LIT>" ) <EOL> emit ( "<STR_LIT>" , depth + <NUM_LIT:1> ) <EOL> emit ( "<STR_LIT>" , depth + <NUM_LIT:1> ) <EOL> for t in sum . types : <EOL> self . visit ( t , depth + <NUM_LIT:2> ) <EOL> emit ( "<STR_LIT>" , depth + <NUM_LIT:1> ) <EOL> for field in sum . attributes : <EOL> type = str ( field . type ) <EOL> assert type in asdl . builtin_types , type <EOL> emit ( "<STR_LIT>" % ( type , field . name ) , depth + <NUM_LIT:1> ) ; <EOL> emit ( "<STR_LIT>" ) <EOL> emit ( "<STR_LIT>" ) <EOL> def visitConstructor ( self , cons , depth ) : <EOL> if cons . fields : <EOL> self . emit ( "<STR_LIT>" , depth ) <EOL> for f in cons . fields : <EOL> self . visit ( f , depth + <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" % cons . name , depth ) <EOL> self . emit ( "<STR_LIT>" , depth ) <EOL> else : <EOL> pass <EOL> def visitField ( self , field , depth ) : <EOL> ctype = get_c_type ( field . type ) <EOL> name = field . name <EOL> if field . seq : <EOL> if field . type . value in ( '<STR_LIT>' , ) : <EOL> self . emit ( "<STR_LIT>" % locals ( ) , depth ) <EOL> else : <EOL> self . emit ( "<STR_LIT>" % locals ( ) , depth ) <EOL> else : <EOL> self . emit ( "<STR_LIT>" % locals ( ) , depth ) <EOL> def visitProduct ( self , product , name , depth ) : <EOL> self . emit ( "<STR_LIT>" % locals ( ) , depth ) <EOL> for f in product . fields : <EOL> self . visit ( f , depth + <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , depth ) <EOL> self . emit ( "<STR_LIT>" , depth ) <EOL> class PrototypeVisitor ( EmitVisitor ) : <EOL> """<STR_LIT>""" <EOL> def visitModule ( self , mod ) : <EOL> for dfn in mod . dfns : <EOL> self . visit ( dfn ) <EOL> def visitType ( self , type ) : <EOL> self . visit ( type . value , type . name ) <EOL> def visitSum ( self , sum , name ) : <EOL> if is_simple ( sum ) : <EOL> pass <EOL> else : <EOL> for t in sum . types : <EOL> self . visit ( t , name , sum . attributes ) <EOL> def get_args ( self , fields ) : <EOL> """<STR_LIT>""" <EOL> args = [ ] <EOL> unnamed = { } <EOL> for f in fields : <EOL> if f . name is None : <EOL> name = f . type <EOL> c = unnamed [ name ] = unnamed . get ( name , <NUM_LIT:0> ) + <NUM_LIT:1> <EOL> if c > <NUM_LIT:1> : <EOL> name = "<STR_LIT>" % ( c - <NUM_LIT:1> ) <EOL> else : <EOL> name = f . name <EOL> if f . seq : <EOL> if f . type . value in ( '<STR_LIT>' , ) : <EOL> ctype = "<STR_LIT>" <EOL> else : <EOL> ctype = "<STR_LIT>" <EOL> else : <EOL> ctype = get_c_type ( f . type ) <EOL> args . append ( ( ctype , name , f . opt or f . seq ) ) <EOL> return args <EOL> def visitConstructor ( self , cons , type , attrs ) : <EOL> args = self . get_args ( cons . fields ) <EOL> attrs = self . get_args ( attrs ) <EOL> ctype = get_c_type ( type ) <EOL> self . emit_function ( cons . name , ctype , args , attrs ) <EOL> def emit_function ( self , name , ctype , args , attrs , union = True ) : <EOL> args = args + attrs <EOL> if args : <EOL> argstr = "<STR_LIT:U+002CU+0020>" . join ( [ "<STR_LIT>" % ( atype , aname ) <EOL> for atype , aname , opt in args ] ) <EOL> argstr += "<STR_LIT>" <EOL> else : <EOL> argstr = "<STR_LIT>" <EOL> margs = "<STR_LIT>" <EOL> for i in range ( <NUM_LIT:1> , len ( args ) + <NUM_LIT:1> ) : <EOL> margs += "<STR_LIT>" % i <EOL> self . emit ( "<STR_LIT>" % ( name , margs , name , margs ) , <NUM_LIT:0> , <EOL> reflow = False ) <EOL> self . emit ( "<STR_LIT>" % ( ctype , name , argstr ) , False ) <EOL> def visitProduct ( self , prod , name ) : <EOL> self . emit_function ( name , get_c_type ( name ) , <EOL> self . get_args ( prod . fields ) , [ ] , union = False ) <EOL> class FunctionVisitor ( PrototypeVisitor ) : <EOL> """<STR_LIT>""" <EOL> def emit_function ( self , name , ctype , args , attrs , union = True ) : <EOL> def emit ( s , depth = <NUM_LIT:0> , reflow = True ) : <EOL> self . emit ( s , depth , reflow ) <EOL> argstr = "<STR_LIT:U+002CU+0020>" . join ( [ "<STR_LIT>" % ( atype , aname ) <EOL> for atype , aname , opt in args + attrs ] ) <EOL> if argstr : <EOL> argstr += "<STR_LIT>" <EOL> else : <EOL> argstr = "<STR_LIT>" <EOL> self . emit ( "<STR_LIT:%s>" % ctype , <NUM_LIT:0> ) <EOL> emit ( "<STR_LIT>" % ( name , argstr ) ) <EOL> emit ( "<STR_LIT:{>" ) <EOL> emit ( "<STR_LIT>" % ctype , <NUM_LIT:1> ) <EOL> for argtype , argname , opt in args : <EOL> if not opt and not ( argtype == "<STR_LIT:bool>" or argtype == "<STR_LIT:int>" ) : <EOL> emit ( "<STR_LIT>" % argname , <NUM_LIT:1> ) <EOL> emit ( "<STR_LIT>" , <NUM_LIT:2> ) <EOL> msg = "<STR_LIT>" % ( argname , name ) <EOL> emit ( '<STR_LIT>' % msg , <EOL> <NUM_LIT:2> , reflow = False ) <EOL> emit ( '<STR_LIT>' , <NUM_LIT:2> ) <EOL> emit ( '<STR_LIT:}>' , <NUM_LIT:1> ) <EOL> emit ( "<STR_LIT>" % ctype , <NUM_LIT:1> ) ; <EOL> emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> emit ( "<STR_LIT>" , <NUM_LIT:2> ) <EOL> if union : <EOL> self . emit_body_union ( name , args , attrs ) <EOL> else : <EOL> self . emit_body_struct ( name , args , attrs ) <EOL> emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> emit ( "<STR_LIT:}>" ) <EOL> emit ( "<STR_LIT>" ) <EOL> def emit_body_union ( self , name , args , attrs ) : <EOL> def emit ( s , depth = <NUM_LIT:0> , reflow = True ) : <EOL> self . emit ( s , depth , reflow ) <EOL> emit ( "<STR_LIT>" % name , <NUM_LIT:1> ) <EOL> for argtype , argname , opt in args : <EOL> emit ( "<STR_LIT>" % ( name , argname , argname ) , <NUM_LIT:1> ) <EOL> for argtype , argname , opt in attrs : <EOL> emit ( "<STR_LIT>" % ( argname , argname ) , <NUM_LIT:1> ) <EOL> def emit_body_struct ( self , name , args , attrs ) : <EOL> def emit ( s , depth = <NUM_LIT:0> , reflow = True ) : <EOL> self . emit ( s , depth , reflow ) <EOL> for argtype , argname , opt in args : <EOL> emit ( "<STR_LIT>" % ( argname , argname ) , <NUM_LIT:1> ) <EOL> assert not attrs <EOL> class PickleVisitor ( EmitVisitor ) : <EOL> def visitModule ( self , mod ) : <EOL> for dfn in mod . dfns : <EOL> self . visit ( dfn ) <EOL> def visitType ( self , type ) : <EOL> self . visit ( type . value , type . name ) <EOL> def visitSum ( self , sum , name ) : <EOL> pass <EOL> def visitProduct ( self , sum , name ) : <EOL> pass <EOL> def visitConstructor ( self , cons , name ) : <EOL> pass <EOL> def visitField ( self , sum ) : <EOL> pass <EOL> class Obj2ModPrototypeVisitor ( PickleVisitor ) : <EOL> def visitProduct ( self , prod , name ) : <EOL> code = "<STR_LIT>" <EOL> self . emit ( code % ( name , get_c_type ( name ) ) , <NUM_LIT:0> ) <EOL> visitSum = visitProduct <EOL> class Obj2ModVisitor ( PickleVisitor ) : <EOL> def funcHeader ( self , name ) : <EOL> ctype = get_c_type ( name ) <EOL> self . emit ( "<STR_LIT:int>" , <NUM_LIT:0> ) <EOL> self . emit ( "<STR_LIT>" % ( name , ctype ) , <NUM_LIT:0> ) <EOL> self . emit ( "<STR_LIT:{>" , <NUM_LIT:0> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> def sumTrailer ( self , name ) : <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> error = "<STR_LIT>" % name <EOL> format = "<STR_LIT>" <EOL> self . emit ( format % error , <NUM_LIT:1> , reflow = False ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT:}>" , <NUM_LIT:0> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> def simpleSum ( self , sum , name ) : <EOL> self . funcHeader ( name ) <EOL> for t in sum . types : <EOL> line = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . emit ( line % ( t . name , ) , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:2> ) <EOL> self . emit ( "<STR_LIT:}>" , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" % t . name , <NUM_LIT:2> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:2> ) <EOL> self . emit ( "<STR_LIT:}>" , <NUM_LIT:1> ) <EOL> self . sumTrailer ( name ) <EOL> def buildArgs ( self , fields ) : <EOL> return "<STR_LIT:U+002CU+0020>" . join ( fields + [ "<STR_LIT>" ] ) <EOL> def complexSum ( self , sum , name ) : <EOL> self . funcHeader ( name ) <EOL> for a in sum . attributes : <EOL> self . visitAttributeDeclaration ( a , name , sum = sum ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:2> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:2> ) <EOL> self . emit ( "<STR_LIT:}>" , <NUM_LIT:1> ) <EOL> for a in sum . attributes : <EOL> self . visitField ( a , name , sum = sum , depth = <NUM_LIT:1> ) <EOL> for t in sum . types : <EOL> line = "<STR_LIT>" <EOL> self . emit ( line % ( t . name , ) , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:2> ) <EOL> self . emit ( "<STR_LIT:}>" , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> for f in t . fields : <EOL> self . visitFieldDeclaration ( f , t . name , sum = sum , depth = <NUM_LIT:2> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> for f in t . fields : <EOL> self . visitField ( f , t . name , sum = sum , depth = <NUM_LIT:2> ) <EOL> args = [ f . name . value for f in t . fields ] + [ a . name . value for a in sum . attributes ] <EOL> self . emit ( "<STR_LIT>" % ( t . name , self . buildArgs ( args ) ) , <NUM_LIT:2> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:2> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:2> ) <EOL> self . emit ( "<STR_LIT:}>" , <NUM_LIT:1> ) <EOL> self . sumTrailer ( name ) <EOL> def visitAttributeDeclaration ( self , a , name , sum = sum ) : <EOL> ctype = get_c_type ( a . type ) <EOL> self . emit ( "<STR_LIT>" % ( ctype , a . name ) , <NUM_LIT:1> ) <EOL> def visitSum ( self , sum , name ) : <EOL> if is_simple ( sum ) : <EOL> self . simpleSum ( sum , name ) <EOL> else : <EOL> self . complexSum ( sum , name ) <EOL> def visitProduct ( self , prod , name ) : <EOL> ctype = get_c_type ( name ) <EOL> self . emit ( "<STR_LIT:int>" , <NUM_LIT:0> ) <EOL> self . emit ( "<STR_LIT>" % ( name , ctype ) , <NUM_LIT:0> ) <EOL> self . emit ( "<STR_LIT:{>" , <NUM_LIT:0> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> for f in prod . fields : <EOL> self . visitFieldDeclaration ( f , name , prod = prod , depth = <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> for f in prod . fields : <EOL> self . visitField ( f , name , prod = prod , depth = <NUM_LIT:1> ) <EOL> args = [ f . name . value for f in prod . fields ] <EOL> self . emit ( "<STR_LIT>" % ( name , self . buildArgs ( args ) ) , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT:}>" , <NUM_LIT:0> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> def visitFieldDeclaration ( self , field , name , sum = None , prod = None , depth = <NUM_LIT:0> ) : <EOL> ctype = get_c_type ( field . type ) <EOL> if field . seq : <EOL> if self . isSimpleType ( field ) : <EOL> self . emit ( "<STR_LIT>" % field . name , depth ) <EOL> else : <EOL> self . emit ( "<STR_LIT>" % field . name , depth ) <EOL> else : <EOL> ctype = get_c_type ( field . type ) <EOL> self . emit ( "<STR_LIT>" % ( ctype , field . name ) , depth ) <EOL> def isSimpleSum ( self , field ) : <EOL> return field . type . value in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> def isNumeric ( self , field ) : <EOL> return get_c_type ( field . type ) in ( "<STR_LIT:int>" , "<STR_LIT:bool>" ) <EOL> def isSimpleType ( self , field ) : <EOL> return self . isSimpleSum ( field ) or self . isNumeric ( field ) <EOL> def visitField ( self , field , name , sum = None , prod = None , depth = <NUM_LIT:0> ) : <EOL> ctype = get_c_type ( field . type ) <EOL> self . emit ( "<STR_LIT>" % field . name , depth ) <EOL> self . emit ( "<STR_LIT>" , depth + <NUM_LIT:1> ) <EOL> if field . seq : <EOL> self . emit ( "<STR_LIT>" , depth + <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , depth + <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" % field . name , depth + <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , depth + <NUM_LIT:1> ) <EOL> if field . seq : <EOL> self . emit ( "<STR_LIT>" , depth + <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( name , field . name ) , <EOL> depth + <NUM_LIT:2> , reflow = False ) <EOL> self . emit ( "<STR_LIT>" , depth + <NUM_LIT:2> ) <EOL> self . emit ( "<STR_LIT:}>" , depth + <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , depth + <NUM_LIT:1> ) <EOL> if self . isSimpleType ( field ) : <EOL> self . emit ( "<STR_LIT>" % field . name , depth + <NUM_LIT:1> ) <EOL> else : <EOL> self . emit ( "<STR_LIT>" % field . name , depth + <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" % field . name , depth + <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , depth + <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" % ctype , depth + <NUM_LIT:2> ) <EOL> self . emit ( "<STR_LIT>" % <EOL> field . type , depth + <NUM_LIT:2> , reflow = False ) <EOL> self . emit ( "<STR_LIT>" , depth + <NUM_LIT:2> ) <EOL> self . emit ( "<STR_LIT>" % field . name , depth + <NUM_LIT:2> ) <EOL> self . emit ( "<STR_LIT:}>" , depth + <NUM_LIT:1> ) <EOL> else : <EOL> self . emit ( "<STR_LIT>" % <EOL> ( field . type , field . name ) , depth + <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , depth + <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , depth + <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , depth + <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , depth ) <EOL> if not field . opt : <EOL> message = "<STR_LIT>" % ( field . name , name ) <EOL> format = "<STR_LIT>" <EOL> self . emit ( format % message , depth + <NUM_LIT:1> , reflow = False ) <EOL> self . emit ( "<STR_LIT>" , depth + <NUM_LIT:1> ) <EOL> else : <EOL> if self . isNumeric ( field ) : <EOL> self . emit ( "<STR_LIT>" % field . name , depth + <NUM_LIT:1> ) <EOL> elif not self . isSimpleType ( field ) : <EOL> self . emit ( "<STR_LIT>" % field . name , depth + <NUM_LIT:1> ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" % field . name ) <EOL> self . emit ( "<STR_LIT:}>" , depth ) <EOL> class MarshalPrototypeVisitor ( PickleVisitor ) : <EOL> def prototype ( self , sum , name ) : <EOL> ctype = get_c_type ( name ) <EOL> self . emit ( "<STR_LIT>" <EOL> % ( name , ctype ) , <NUM_LIT:0> ) <EOL> visitProduct = visitSum = prototype <EOL> class PyTypesDeclareVisitor ( PickleVisitor ) : <EOL> def visitProduct ( self , prod , name ) : <EOL> self . emit ( "<STR_LIT>" % name , <NUM_LIT:0> ) <EOL> self . emit ( "<STR_LIT>" % name , <NUM_LIT:0> ) <EOL> if prod . fields : <EOL> self . emit ( "<STR_LIT>" % name , <NUM_LIT:0> ) <EOL> for f in prod . fields : <EOL> self . emit ( '<STR_LIT>' % f . name , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> def visitSum ( self , sum , name ) : <EOL> self . emit ( "<STR_LIT>" % name , <NUM_LIT:0> ) <EOL> if sum . attributes : <EOL> self . emit ( "<STR_LIT>" % name , <NUM_LIT:0> ) <EOL> for a in sum . attributes : <EOL> self . emit ( '<STR_LIT>' % a . name , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> ptype = "<STR_LIT>" <EOL> if is_simple ( sum ) : <EOL> ptype = get_c_type ( name ) <EOL> tnames = [ ] <EOL> for t in sum . types : <EOL> tnames . append ( str ( t . name ) + "<STR_LIT>" ) <EOL> tnames = "<STR_LIT>" . join ( tnames ) <EOL> self . emit ( "<STR_LIT>" % tnames , <NUM_LIT:0> ) <EOL> self . emit ( "<STR_LIT>" % ( name , ptype ) , <NUM_LIT:0> ) <EOL> for t in sum . types : <EOL> self . visitConstructor ( t , name ) <EOL> def visitConstructor ( self , cons , name ) : <EOL> self . emit ( "<STR_LIT>" % cons . name , <NUM_LIT:0> ) <EOL> if cons . fields : <EOL> self . emit ( "<STR_LIT>" % cons . name , <NUM_LIT:0> ) <EOL> for t in cons . fields : <EOL> self . emit ( '<STR_LIT>' % t . name , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> class PyTypesVisitor ( PickleVisitor ) : <EOL> def visitModule ( self , mod ) : <EOL> self . emit ( """<STR_LIT>""" , <NUM_LIT:0> , reflow = False ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> self . emit ( "<STR_LIT:{>" , <NUM_LIT:0> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> for dfn in mod . dfns : <EOL> self . visit ( dfn ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:1> ) ; <EOL> self . emit ( "<STR_LIT:}>" , <NUM_LIT:0> ) <EOL> def visitProduct ( self , prod , name ) : <EOL> if prod . fields : <EOL> fields = name . value + "<STR_LIT>" <EOL> else : <EOL> fields = "<STR_LIT>" <EOL> self . emit ( '<STR_LIT>' % <EOL> ( name , name , fields , len ( prod . fields ) ) , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" % name , <NUM_LIT:1> ) <EOL> def visitSum ( self , sum , name ) : <EOL> self . emit ( '<STR_LIT>' % <EOL> ( name , name ) , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" % name , <NUM_LIT:1> ) <EOL> if sum . attributes : <EOL> self . emit ( "<STR_LIT>" % <EOL> ( name , name , len ( sum . attributes ) ) , <NUM_LIT:1> ) <EOL> else : <EOL> self . emit ( "<STR_LIT>" % name , <NUM_LIT:1> ) <EOL> simple = is_simple ( sum ) <EOL> for t in sum . types : <EOL> self . visitConstructor ( t , name , simple ) <EOL> def visitConstructor ( self , cons , name , simple ) : <EOL> if cons . fields : <EOL> fields = cons . name . value + "<STR_LIT>" <EOL> else : <EOL> fields = "<STR_LIT>" <EOL> self . emit ( '<STR_LIT>' % <EOL> ( cons . name , cons . name , name , fields , len ( cons . fields ) ) , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" % cons . name , <NUM_LIT:1> ) <EOL> if simple : <EOL> self . emit ( "<STR_LIT>" % <EOL> ( cons . name , cons . name ) , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" % cons . name , <NUM_LIT:1> ) <EOL> class ASTModuleVisitor ( PickleVisitor ) : <EOL> def visitModule ( self , mod ) : <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> self . emit ( "<STR_LIT:{>" , <NUM_LIT:0> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . emit ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . emit ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> self . emit ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:2> ) <EOL> self . emit ( '<STR_LIT>' <EOL> % mod . version , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:2> ) <EOL> for dfn in mod . dfns : <EOL> self . visit ( dfn ) <EOL> self . emit ( "<STR_LIT:}>" , <NUM_LIT:0> ) <EOL> def visitProduct ( self , prod , name ) : <EOL> self . addObj ( name ) <EOL> def visitSum ( self , sum , name ) : <EOL> self . addObj ( name ) <EOL> for t in sum . types : <EOL> self . visitConstructor ( t , name ) <EOL> def visitConstructor ( self , cons , name ) : <EOL> self . addObj ( cons . name ) <EOL> def addObj ( self , name ) : <EOL> self . emit ( '<STR_LIT>' % ( name , name ) , <NUM_LIT:1> ) <EOL> _SPECIALIZED_SEQUENCES = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def find_sequence ( fields , doing_specialization ) : <EOL> """<STR_LIT>""" <EOL> for f in fields : <EOL> if f . seq : <EOL> if not doing_specialization : <EOL> return True <EOL> if str ( f . type ) not in _SPECIALIZED_SEQUENCES : <EOL> return True <EOL> return False <EOL> def has_sequence ( types , doing_specialization ) : <EOL> for t in types : <EOL> if find_sequence ( t . fields , doing_specialization ) : <EOL> return True <EOL> return False <EOL> class StaticVisitor ( PickleVisitor ) : <EOL> CODE = '''<STR_LIT>''' <EOL> def visit ( self , object ) : <EOL> self . emit ( self . CODE , <NUM_LIT:0> , reflow = False ) <EOL> class ObjVisitor ( PickleVisitor ) : <EOL> def func_begin ( self , name ) : <EOL> ctype = get_c_type ( name ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> self . emit ( "<STR_LIT>" % ( name ) , <NUM_LIT:0> ) <EOL> self . emit ( "<STR_LIT:{>" , <NUM_LIT:0> ) <EOL> self . emit ( "<STR_LIT>" % ( ctype , ctype ) , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . emit ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:2> ) <EOL> self . emit ( '<STR_LIT>' , <NUM_LIT:2> ) <EOL> self . emit ( "<STR_LIT:}>" , <NUM_LIT:1> ) <EOL> self . emit ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> def func_end ( self ) : <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT:}>" , <NUM_LIT:0> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> def visitSum ( self , sum , name ) : <EOL> if is_simple ( sum ) : <EOL> self . simpleSum ( sum , name ) <EOL> return <EOL> self . func_begin ( name ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> for i in range ( len ( sum . types ) ) : <EOL> t = sum . types [ i ] <EOL> self . visitConstructor ( t , i + <NUM_LIT:1> , name ) <EOL> self . emit ( "<STR_LIT:}>" , <NUM_LIT:1> ) <EOL> for a in sum . attributes : <EOL> self . emit ( "<STR_LIT>" % ( a . type , a . name ) , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . emit ( '<STR_LIT>' % a . name , <NUM_LIT:1> ) <EOL> self . emit ( '<STR_LIT>' , <NUM_LIT:2> ) <EOL> self . emit ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> self . func_end ( ) <EOL> def simpleSum ( self , sum , name ) : <EOL> self . emit ( "<STR_LIT>" % ( name , name ) , <NUM_LIT:0> ) <EOL> self . emit ( "<STR_LIT:{>" , <NUM_LIT:0> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> for t in sum . types : <EOL> self . emit ( "<STR_LIT>" % t . name , <NUM_LIT:2> ) <EOL> self . emit ( "<STR_LIT>" % t . name , <NUM_LIT:3> ) <EOL> self . emit ( "<STR_LIT>" % t . name , <NUM_LIT:3> ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:2> ) <EOL> self . emit ( '<STR_LIT>' , <NUM_LIT:3> ) <EOL> code = "<STR_LIT>" % name <EOL> self . emit ( code , <NUM_LIT:3> , reflow = False ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:3> ) <EOL> self . emit ( "<STR_LIT:}>" , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT:}>" , <NUM_LIT:0> ) <EOL> def visitProduct ( self , prod , name ) : <EOL> self . func_begin ( name ) <EOL> self . emit ( "<STR_LIT>" % name , <NUM_LIT:1> ) ; <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> for field in prod . fields : <EOL> self . visitField ( field , name , <NUM_LIT:1> , True ) <EOL> self . func_end ( ) <EOL> def visitConstructor ( self , cons , enum , name ) : <EOL> self . emit ( "<STR_LIT>" % cons . name , <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" % cons . name , <NUM_LIT:2> ) ; <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:2> ) <EOL> for f in cons . fields : <EOL> self . visitField ( f , cons . name , <NUM_LIT:2> , False ) <EOL> self . emit ( "<STR_LIT>" , <NUM_LIT:2> ) <EOL> def visitField ( self , field , name , depth , product ) : <EOL> def emit ( s , d ) : <EOL> self . emit ( s , depth + d ) <EOL> if product : <EOL> value = "<STR_LIT>" % field . name <EOL> else : <EOL> value = "<STR_LIT>" % ( name , field . name ) <EOL> self . set ( field , value , depth ) <EOL> emit ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> emit ( '<STR_LIT>' % field . name , <NUM_LIT:0> ) <EOL> emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> emit ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> def emitSeq ( self , field , value , depth , emit ) : <EOL> emit ( "<STR_LIT>" % value , <NUM_LIT:0> ) <EOL> emit ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> emit ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> emit ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> emit ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> self . set ( "<STR_LIT:value>" , field , "<STR_LIT>" , depth + <NUM_LIT:1> ) <EOL> emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> emit ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> emit ( "<STR_LIT:}>" , <NUM_LIT:0> ) <EOL> def set ( self , field , value , depth ) : <EOL> if field . seq : <EOL> if field . type . value == "<STR_LIT>" : <EOL> self . emit ( "<STR_LIT:{>" , depth ) <EOL> self . emit ( "<STR_LIT>" % value , depth + <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , depth + <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , depth + <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" , depth + <NUM_LIT:1> ) <EOL> self . emit ( "<STR_LIT>" % value , <EOL> depth + <NUM_LIT:2> , reflow = False ) <EOL> self . emit ( "<STR_LIT:}>" , depth ) <EOL> else : <EOL> self . emit ( "<STR_LIT>" % ( value , field . type ) , depth ) <EOL> else : <EOL> ctype = get_c_type ( field . type ) <EOL> self . emit ( "<STR_LIT>" % ( field . type , value ) , depth , reflow = False ) <EOL> class PartingShots ( StaticVisitor ) : <EOL> CODE = """<STR_LIT>""" <EOL> class ChainOfVisitors : <EOL> def __init__ ( self , * visitors ) : <EOL> self . visitors = visitors <EOL> def visit ( self , object ) : <EOL> for v in self . visitors : <EOL> v . visit ( object ) <EOL> v . emit ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> common_msg = "<STR_LIT>" <EOL> c_file_msg = """<STR_LIT>""" <EOL> def main ( srcfile ) : <EOL> argv0 = sys . argv [ <NUM_LIT:0> ] <EOL> components = argv0 . split ( os . sep ) <EOL> argv0 = os . sep . join ( components [ - <NUM_LIT:2> : ] ) <EOL> auto_gen_msg = common_msg % argv0 <EOL> mod = asdl . parse ( srcfile ) <EOL> mod . version = "<STR_LIT>" <EOL> if not asdl . check ( mod ) : <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if INC_DIR : <EOL> p = "<STR_LIT>" % ( INC_DIR , mod . name ) <EOL> f = open ( p , "<STR_LIT:wb>" ) <EOL> f . write ( auto_gen_msg ) <EOL> f . write ( '<STR_LIT>' ) <EOL> c = ChainOfVisitors ( TypeDefVisitor ( f ) , <EOL> StructVisitor ( f ) , <EOL> PrototypeVisitor ( f ) , <EOL> ) <EOL> c . visit ( mod ) <EOL> f . write ( "<STR_LIT>" ) <EOL> f . write ( "<STR_LIT>" ) <EOL> f . write ( "<STR_LIT>" ) <EOL> f . close ( ) <EOL> if SRC_DIR : <EOL> p = os . path . join ( SRC_DIR , str ( mod . name ) + "<STR_LIT>" ) <EOL> f = open ( p , "<STR_LIT:wb>" ) <EOL> f . write ( auto_gen_msg ) <EOL> f . write ( c_file_msg % mod . version ) <EOL> f . write ( '<STR_LIT>' ) <EOL> f . write ( '<STR_LIT>' % mod . name ) <EOL> f . write ( '<STR_LIT:\n>' ) <EOL> f . write ( "<STR_LIT>" ) <EOL> v = ChainOfVisitors ( <EOL> PyTypesDeclareVisitor ( f ) , <EOL> PyTypesVisitor ( f ) , <EOL> Obj2ModPrototypeVisitor ( f ) , <EOL> FunctionVisitor ( f ) , <EOL> ObjVisitor ( f ) , <EOL> Obj2ModVisitor ( f ) , <EOL> ASTModuleVisitor ( f ) , <EOL> PartingShots ( f ) , <EOL> ) <EOL> v . visit ( mod ) <EOL> f . close ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import sys <EOL> import getopt <EOL> INC_DIR = '<STR_LIT>' <EOL> SRC_DIR = '<STR_LIT>' <EOL> opts , args = getopt . getopt ( sys . argv [ <NUM_LIT:1> : ] , "<STR_LIT>" ) <EOL> if len ( opts ) != <NUM_LIT:1> : <EOL> print "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> for o , v in opts : <EOL> if o == '<STR_LIT>' : <EOL> INC_DIR = v <EOL> if o == '<STR_LIT:-c>' : <EOL> SRC_DIR = v <EOL> if len ( args ) != <NUM_LIT:1> : <EOL> print "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> main ( args [ <NUM_LIT:0> ] ) </s>
