<s> import sys <EOL> import os <EOL> import re <EOL> from lib2to3 . pgen2 . driver import Driver <EOL> from lib2to3 import pygram , pytree <EOL> from lib2to3 . pytree import Node , Leaf , type_repr <EOL> from lib2to3 . pygram import python_symbols <EOL> def sym_type ( name ) : <EOL> return getattr ( python_symbols , name ) <EOL> def new_node ( name ) : <EOL> return Node ( sym_type ( name ) , [ ] ) <EOL> import __Pyjamas__ <EOL> from __Future__ import __Future__ <EOL> JS_ESCAPES = ( <EOL> ( '<STR_LIT:\\>' , r'<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , r'<STR_LIT>' ) , <EOL> ( '<STR_LIT:">' , r'<STR_LIT>' ) , <EOL> ( '<STR_LIT:>>' , r'<STR_LIT>' ) , <EOL> ( '<STR_LIT:<>' , r'<STR_LIT>' ) , <EOL> ( '<STR_LIT:&>' , r'<STR_LIT>' ) , <EOL> ( '<STR_LIT:;>' , r'<STR_LIT>' ) <EOL> ) + tuple ( [ ( '<STR_LIT>' % z , '<STR_LIT>' % z ) for z in range ( <NUM_LIT:32> ) ] ) <EOL> def escapejs ( value ) : <EOL> """<STR_LIT>""" <EOL> for bad , good in JS_ESCAPES : <EOL> value = value . replace ( bad , good ) <EOL> return value <EOL> re_js_string_escape = '<STR_LIT>' . join ( [ chr ( i ) for i in range ( <NUM_LIT:32> ) ] ) <EOL> re_js_string_escape += '''<STR_LIT>''' <EOL> re_js_string_escape = re . compile ( "<STR_LIT>" % re_js_string_escape ) <EOL> re_int = re . compile ( '<STR_LIT>' ) <EOL> re_long = re . compile ( '<STR_LIT>' ) <EOL> re_hex_int = re . compile ( '<STR_LIT>' ) <EOL> re_hex_long = re . compile ( '<STR_LIT>' ) <EOL> re_oct_int = re . compile ( '<STR_LIT>' ) <EOL> re_oct_long = re . compile ( '<STR_LIT>' ) <EOL> builtin_names = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:False>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:None>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:True>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:_>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:all>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:bool>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:file>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:float>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:id>' , <EOL> '<STR_LIT:input>' , <EOL> '<STR_LIT:int>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:list>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:object>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:str>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:type>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> class TranslateOptions ( object ) : <EOL> def __init__ ( self , ** kwargs ) : <EOL> for k , v in kwargs . iteritems ( ) : <EOL> setattr ( self , k , v ) <EOL> def __getattribute__ ( self , name ) : <EOL> try : <EOL> return object . __getattribute__ ( self , name ) <EOL> except : <EOL> return None <EOL> class Name ( object ) : <EOL> def __init__ ( self , name , reflineno , glob = False , to_js = None ) : <EOL> self . name = name <EOL> self . reflineno = reflineno <EOL> self . glob = glob <EOL> self . to_js = to_js <EOL> self . depth = None <EOL> self . builtin = False <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . name , self . reflineno , self . glob ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . name , self . reflineno , self . glob ) <EOL> class Names ( dict ) : <EOL> pass <EOL> class ClassNames ( Names ) : <EOL> pass <EOL> class AstNode ( object ) : <EOL> pass <EOL> class Argument ( AstNode ) : <EOL> def __init__ ( self , name , value = None ) : <EOL> self . name = name <EOL> self . value = value <EOL> class Attribute ( AstNode ) : <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> class Code ( AstNode ) : <EOL> def __new__ ( cls , code , lineno ) : <EOL> if code is None : <EOL> return None <EOL> return object . __new__ ( cls ) <EOL> def __init__ ( self , code , lineno ) : <EOL> self . code = code <EOL> self . lineno = lineno <EOL> def __str__ ( self ) : <EOL> if self . code is None : <EOL> return None <EOL> return str ( self . code ) <EOL> def __repr__ ( self ) : <EOL> if self . code is None : <EOL> return None <EOL> return repr ( self . code ) <EOL> class Decorator ( AstNode ) : <EOL> def __init__ ( self , name , lineno ) : <EOL> self . name = name <EOL> self . lineno = lineno <EOL> class Import ( AstNode ) : <EOL> def __init__ ( self , modname , assname = None , fromlist = None ) : <EOL> self . modname = '<STR_LIT:.>' . join ( modname ) <EOL> if assname is None : <EOL> self . assname = modname [ <NUM_LIT:0> ] <EOL> else : <EOL> self . assname = assname <EOL> self . fromlist = fromlist <EOL> class Parameters ( AstNode ) : <EOL> def __init__ ( self , args , star_args , dstar_args , defaults ) : <EOL> assert isinstance ( args , list ) <EOL> self . args = [ ] <EOL> self . named_args = { } <EOL> for arg in args : <EOL> if not isinstance ( arg , Argument ) : <EOL> self . args . append ( arg ) <EOL> else : <EOL> if arg . name == '<STR_LIT:*>' : <EOL> assert star_args is None <EOL> star_args = arg . value <EOL> continue <EOL> if arg . name == '<STR_LIT>' : <EOL> assert dstar_args is None <EOL> dstar_args = arg . value <EOL> continue <EOL> self . named_args [ arg . name ] = arg . value <EOL> if not self . named_args : <EOL> self . named_args = None <EOL> self . star_args = star_args <EOL> self . dstar_args = dstar_args <EOL> self . all_args = args [ : ] <EOL> if star_args is not None : <EOL> self . all_args . append ( star_args ) <EOL> if dstar_args is not None : <EOL> self . all_args . append ( dstar_args ) <EOL> self . defaults = defaults <EOL> class Slice ( AstNode ) : <EOL> def __init__ ( self , items ) : <EOL> assert isinstance ( items , tuple ) <EOL> self . items = items <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( self . items , ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . items , ) <EOL> leaf_type = { <EOL> <NUM_LIT:1> : '<STR_LIT:name>' , <EOL> <NUM_LIT:2> : '<STR_LIT>' , <EOL> <NUM_LIT:3> : '<STR_LIT:str>' , <EOL> } <EOL> func_type = { <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : <NUM_LIT:3> , <EOL> } <EOL> short_names = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:None>' : '<STR_LIT>' , <EOL> '<STR_LIT:True>' : '<STR_LIT>' , <EOL> '<STR_LIT:False>' : '<STR_LIT>' , <EOL> } <EOL> op_names1 = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> op_names2 = { <EOL> '<STR_LIT:+>' : '<STR_LIT>' , <EOL> '<STR_LIT:->' : '<STR_LIT>' , <EOL> '<STR_LIT:*>' : '<STR_LIT>' , <EOL> '<STR_LIT:/>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:%>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:&>' : '<STR_LIT>' , <EOL> '<STR_LIT:|>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> op_compare = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:<>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:>>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> class Translator ( object ) : <EOL> jsvars = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:str>' : '<STR_LIT>' , <EOL> '<STR_LIT:int>' : '<STR_LIT>' , <EOL> '<STR_LIT:bool>' : '<STR_LIT>' , <EOL> '<STR_LIT:None>' : '<STR_LIT>' , <EOL> '<STR_LIT:True>' : '<STR_LIT>' , <EOL> '<STR_LIT:False>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT:list>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> } <EOL> jsvars . update ( short_names ) <EOL> indent_str = '<STR_LIT:\t>' <EOL> __future__ = __Future__ ( ) <EOL> class TranslationError ( RuntimeError ) : <EOL> filename = None <EOL> def __init__ ( self , msg , lineno = None , filename = None ) : <EOL> self . msg = msg <EOL> self . lineno = lineno <EOL> if filename is not None : <EOL> self . filename = filename <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( <EOL> self . filename , self . lineno , self . msg , <EOL> ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( <EOL> self . filename , self . lineno , self . msg , <EOL> ) <EOL> def __init__ ( self , srcfile , module_name , options ) : <EOL> self . op_names2 = op_names2 . copy ( ) <EOL> self . lines = [ ] <EOL> self . imported_modules = { } <EOL> self . imported_js = { } <EOL> self . indent_level = <NUM_LIT:0> <EOL> self . depth = <NUM_LIT:0> <EOL> self . names = [ Names ( ) ] <EOL> self . const_int = { } <EOL> self . const_long = { } <EOL> self . const_float = { } <EOL> self . const_str = { } <EOL> self . tmp_jsname = { } <EOL> self . assign_state = False <EOL> self . inloop = <NUM_LIT:0> <EOL> self . next_func_type = [ func_type [ '<STR_LIT>' ] ] <EOL> self . last_lineno = <NUM_LIT:0> <EOL> self . jsvars = self . jsvars . copy ( ) <EOL> self . srcfile = srcfile <EOL> self . tree = None <EOL> self . driver = None <EOL> self . TranslationError . filename = srcfile <EOL> if not module_name : <EOL> module_name , extension = os . path . splitext ( os . path . basename ( srcfile ) ) <EOL> self . jsvars [ '<STR_LIT>' ] = module_name <EOL> self . options = TranslateOptions ( ** options ) <EOL> def ast_tree_creator ( self , srcfile = None ) : <EOL> if srcfile is None : <EOL> srcfile = self . srcfile <EOL> if self . driver is None : <EOL> self . driver = Driver ( pygram . python_grammar , pytree . convert ) <EOL> return self . driver . parse_file ( srcfile ) <EOL> def tree_merge ( self , dst , src , flags = None ) : <EOL> if flags and '<STR_LIT>' in flags : <EOL> return src <EOL> for child in src . children : <EOL> if isinstance ( child , Node ) : <EOL> if type_repr ( child . type ) == '<STR_LIT>' : <EOL> self . tree_replace_function ( dst , child ) <EOL> elif type_repr ( child . type ) == '<STR_LIT>' : <EOL> self . tree_merge_class ( dst , child ) <EOL> return dst <EOL> def tree_merge_class ( self , dst , src ) : <EOL> if isinstance ( src . children [ <NUM_LIT:0> ] , Leaf ) and src . children [ <NUM_LIT:0> ] . value == '<STR_LIT:class>' and isinstance ( src . children [ <NUM_LIT:1> ] , Leaf ) and isinstance ( src . children [ - <NUM_LIT:1> ] , Node ) : <EOL> class_name = src . children [ <NUM_LIT:1> ] . value <EOL> if type_repr ( src . children [ - <NUM_LIT:1> ] . type ) == '<STR_LIT>' : <EOL> src_children = src . children [ - <NUM_LIT:1> ] . children <EOL> else : <EOL> src_children = [ src . children [ - <NUM_LIT:1> ] ] <EOL> else : <EOL> raise self . TranslationError ( <EOL> "<STR_LIT>" % src <EOL> ) <EOL> for dst_child in dst . children : <EOL> if type_repr ( dst_child . type ) == '<STR_LIT>' and dst_child . children [ <NUM_LIT:0> ] . value == '<STR_LIT:class>' and isinstance ( dst_child . children [ <NUM_LIT:1> ] , Leaf ) and dst_child . children [ <NUM_LIT:1> ] . value == class_name and isinstance ( dst_child . children [ - <NUM_LIT:1> ] , Node ) and type_repr ( dst_child . children [ - <NUM_LIT:1> ] . type ) == '<STR_LIT>' : <EOL> dst_node = dst_child . children [ - <NUM_LIT:1> ] <EOL> for src_child in src_children : <EOL> if type_repr ( src_child . type ) == '<STR_LIT>' : <EOL> self . tree_replace_function ( dst_node , src_child ) <EOL> elif type_repr ( src_child . type ) == '<STR_LIT>' : <EOL> self . tree_replace_stmt ( dst_node , src_child ) <EOL> return <EOL> raise self . TranslationError ( <EOL> "<STR_LIT>" % class_name <EOL> ) <EOL> def tree_replace_function ( self , dst , func_node ) : <EOL> if isinstance ( func_node . children [ <NUM_LIT:0> ] , Leaf ) and func_node . children [ <NUM_LIT:0> ] . value == '<STR_LIT>' and isinstance ( func_node . children [ <NUM_LIT:1> ] , Leaf ) : <EOL> func_name = func_node . children [ <NUM_LIT:1> ] . value <EOL> else : <EOL> raise self . TranslationError ( <EOL> "<STR_LIT>" % func_node <EOL> ) <EOL> for child in dst . children : <EOL> if isinstance ( child , Node ) and type_repr ( child . type ) == '<STR_LIT>' : <EOL> if isinstance ( child . children [ <NUM_LIT:0> ] , Leaf ) and child . children [ <NUM_LIT:0> ] . value == '<STR_LIT>' and isinstance ( func_node . children [ <NUM_LIT:1> ] , Leaf ) and child . children [ <NUM_LIT:1> ] . value == func_name : <EOL> child . children = func_node . children <EOL> child . changed ( ) <EOL> return <EOL> raise self . TranslationError ( <EOL> "<STR_LIT>" % func_name <EOL> ) <EOL> def tree_replace_stmt ( self , dst , stmt_node ) : <EOL> if isinstance ( stmt_node . children [ <NUM_LIT:0> ] , Leaf ) : <EOL> if stmt_node . children [ <NUM_LIT:0> ] . value == '<STR_LIT>' : <EOL> return <EOL> else : <EOL> node = stmt_node . children [ <NUM_LIT:0> ] <EOL> if type_repr ( node . type ) == '<STR_LIT>' : <EOL> if isinstance ( node . children [ <NUM_LIT:0> ] , Leaf ) and isinstance ( node . children [ <NUM_LIT:1> ] , Leaf ) and node . children [ <NUM_LIT:1> ] . value == '<STR_LIT:=>' : <EOL> for child in dst . children : <EOL> if isinstance ( child , Node ) and type_repr ( child . type ) == '<STR_LIT>' and isinstance ( child . children [ <NUM_LIT:0> ] , Node ) and type_repr ( child . children [ <NUM_LIT:0> ] . type ) == '<STR_LIT>' : <EOL> dst_node = child . children [ <NUM_LIT:0> ] <EOL> if isinstance ( dst_node . children [ <NUM_LIT:0> ] , Leaf ) and isinstance ( dst_node . children [ <NUM_LIT:1> ] , Leaf ) and dst_node . children [ <NUM_LIT:1> ] . value == '<STR_LIT:=>' and dst_node . children [ <NUM_LIT:0> ] . value == node . children [ <NUM_LIT:0> ] . value : <EOL> dst_node . children = node . children <EOL> dst_node . changed ( ) <EOL> return <EOL> dst . append_child ( stmt_node ) <EOL> return <EOL> raise self . TranslationError ( <EOL> "<STR_LIT>" % stmt_node <EOL> ) <EOL> def dispatch_file ( self , tree ) : <EOL> if isinstance ( tree , Leaf ) : <EOL> assert tree . value == '<STR_LIT>' , self . TranslationError ( repr ( tree . value ) , self . get_lineno ( tree ) ) <EOL> tree = new_node ( '<STR_LIT>' ) <EOL> return self . dispatch ( tree ) <EOL> def get_javascript ( self ) : <EOL> code = [ ] <EOL> for indent_level , line in self . lines : <EOL> code . append ( <EOL> '<STR_LIT>' % ( <EOL> self . indent_str * indent_level , <EOL> line , <EOL> ) <EOL> ) <EOL> return '<STR_LIT:\n>' . join ( code ) <EOL> def add_import ( self , modname , fromlist = None ) : <EOL> if modname == '<STR_LIT>' : <EOL> return <EOL> if fromlist is None : <EOL> fromlist = [ None ] <EOL> fl = self . imported_modules . get ( modname , None ) <EOL> if fl is None : <EOL> self . imported_modules [ modname ] = fromlist <EOL> for f in fromlist : <EOL> if f is not None : <EOL> self . add_import ( "<STR_LIT>" % ( modname , f ) ) <EOL> else : <EOL> for f in fromlist : <EOL> if not f in fl : <EOL> fl . append ( f ) <EOL> if f is not None : <EOL> self . add_import ( "<STR_LIT>" % ( modname , f ) ) <EOL> def indent ( self , n = <NUM_LIT:1> ) : <EOL> self . indent_level += n <EOL> return self . indent_level <EOL> def dedent ( self , n = <NUM_LIT:1> ) : <EOL> indent_level = self . indent_level <EOL> self . indent_level -= n <EOL> assert self . indent_level >= <NUM_LIT:0> , self . TranslationError ( "<STR_LIT>" % self . indent_level , self . get_lineno ( self . last_lineno ) ) <EOL> return indent_level <EOL> def get_lineno ( self , node ) : <EOL> if getattr ( node , '<STR_LIT>' , None ) is not None : <EOL> return node . lineno <EOL> for child in node . children : <EOL> if getattr ( child , '<STR_LIT>' , None ) is not None : <EOL> return child . lineno <EOL> lineno = self . get_lineno ( child ) <EOL> if lineno is not None : <EOL> return lineno <EOL> def track_lineno ( self , node ) : <EOL> return self . get_lineno ( node ) <EOL> def add_lines ( self , lines , lineno = None , split = True ) : <EOL> if lineno != None : <EOL> track = "<STR_LIT>" % self . jsvars <EOL> while len ( self . lines ) > <NUM_LIT:0> and self . lines [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] . strip ( ) . startswith ( track ) : <EOL> self . lines . pop ( ) <EOL> line = lines [ <NUM_LIT:0> ] <EOL> level = self . indent_level <EOL> if line and line [ <NUM_LIT:0> ] == '<STR_LIT:+>' : <EOL> while line [ <NUM_LIT:0> ] == '<STR_LIT:+>' : <EOL> level += <NUM_LIT:1> <EOL> line = line [ <NUM_LIT:1> : ] <EOL> self . lines . append ( [ level , '<STR_LIT>' % ( track , lineno ) ] ) <EOL> if split : <EOL> lines = lines . split ( "<STR_LIT:\n>" ) <EOL> for line in lines : <EOL> level = self . indent_level <EOL> if line and line [ <NUM_LIT:0> ] == '<STR_LIT:+>' : <EOL> while line [ <NUM_LIT:0> ] == '<STR_LIT:+>' : <EOL> level += <NUM_LIT:1> <EOL> line = line [ <NUM_LIT:1> : ] <EOL> line = line . lstrip ( ) <EOL> self . lines . append ( [ level , line ] ) <EOL> def add_name ( self , name , reflineno = None , glob = False , to_js = None , force = False ) : <EOL> if not force and ( self . assign_state is False and isinstance ( self . names [ - <NUM_LIT:1> ] , ClassNames ) ) : <EOL> return <EOL> depth = <NUM_LIT:0> <EOL> for names in self . names : <EOL> if not isinstance ( names , ClassNames ) : <EOL> depth += <NUM_LIT:1> <EOL> if name in self . names [ - <NUM_LIT:1> ] : <EOL> _name = self . names [ - <NUM_LIT:1> ] [ name ] <EOL> if reflineno is None and len ( self . names ) > <NUM_LIT:1> and not _name . glob and _name . reflineno is not None and _name . depth == depth and force is False : <EOL> print _name . name , _name . reflineno , _name . glob , _name . depth , len ( self . names ) - <NUM_LIT:1> <EOL> reflineno = _name . reflineno <EOL> _name . reflineno = None <EOL> raise self . TranslationError ( <EOL> "<STR_LIT>" % ( name , reflineno ) <EOL> ) <EOL> else : <EOL> done = [ ] <EOL> if len ( self . names ) == <NUM_LIT:1> : <EOL> _name = Name ( name , reflineno , glob , to_js ) <EOL> _name . depth = <NUM_LIT:1> <EOL> elif reflineno is not None : <EOL> _name = None <EOL> names = self . names [ : - <NUM_LIT:1> ] <EOL> while len ( names ) : <EOL> if name in names [ - <NUM_LIT:1> ] : <EOL> _name = names [ - <NUM_LIT:1> ] [ name ] <EOL> break <EOL> done . append ( names . pop ( ) ) <EOL> if _name is None : <EOL> _name = Name ( name , reflineno , to_js = to_js ) <EOL> _name . depth = <NUM_LIT:1> <EOL> else : <EOL> _name = Name ( name , reflineno , glob , to_js ) <EOL> _name . depth = depth <EOL> if reflineno is not None and _name . depth == <NUM_LIT:0> : <EOL> if not _name . builtin and name in builtin_names : <EOL> _name . builtin = True <EOL> if not force and self . assign_state is False and _name . depth == <NUM_LIT:1> and len ( self . names ) > <NUM_LIT:1> : <EOL> return <EOL> while len ( done ) : <EOL> done [ - <NUM_LIT:1> ] [ name ] = _name <EOL> done . pop ( ) <EOL> self . names [ - <NUM_LIT:1> ] [ name ] = _name <EOL> def get_names_depth ( self , skip_class_names = True ) : <EOL> depth = <NUM_LIT:0> <EOL> for names in self . names : <EOL> if skip_class_names and isinstance ( names , ClassNames ) : <EOL> continue <EOL> depth += <NUM_LIT:1> <EOL> return depth <EOL> def get_jsname ( self , name ) : <EOL> if isinstance ( name , Code ) : <EOL> return str ( name ) <EOL> jsvars = self . jsvars . copy ( ) <EOL> jsvars . update ( name = name ) <EOL> jsname = None <EOL> if name in self . names [ - <NUM_LIT:1> ] : <EOL> if self . names [ - <NUM_LIT:1> ] [ name ] . to_js is not None : <EOL> return self . names [ - <NUM_LIT:1> ] [ name ] . to_js <EOL> if not self . names [ - <NUM_LIT:1> ] [ name ] . glob : <EOL> if isinstance ( self . names [ - <NUM_LIT:1> ] , ClassNames ) : <EOL> jsname = """<STR_LIT>""" % jsvars <EOL> else : <EOL> jsname = "<STR_LIT>" % jsvars <EOL> jsvars . update ( jsname = jsname ) <EOL> else : <EOL> for names in self . names [ <NUM_LIT:1> : - <NUM_LIT:1> ] : <EOL> if isinstance ( names , ClassNames ) : <EOL> continue <EOL> if name in names : <EOL> if isinstance ( self . names [ - <NUM_LIT:1> ] , ClassNames ) : <EOL> cn = self . names . pop ( ) <EOL> else : <EOL> cn = None <EOL> self . add_name ( name ) <EOL> jsname = self . get_jsname ( name ) <EOL> if cn is not None : <EOL> self . names . append ( cn ) <EOL> return jsname <EOL> if jsname is None : <EOL> if name in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> src = "<STR_LIT>" % name <EOL> return ( "<STR_LIT>" + src + "<STR_LIT:)>" ) % jsvars <EOL> if name in self . names [ <NUM_LIT:0> ] and self . names [ <NUM_LIT:0> ] [ name ] . to_js is not None : <EOL> return self . names [ <NUM_LIT:0> ] [ name ] . to_js <EOL> jsname = '<STR_LIT>' % ( jsvars ) <EOL> if self . options . check_defined : <EOL> return "<STR_LIT>" % jsvars <EOL> return jsname <EOL> def get_tmp_jsname ( self , prefix ) : <EOL> if not prefix in self . tmp_jsname : <EOL> self . tmp_jsname [ prefix ] = <NUM_LIT:1> <EOL> else : <EOL> self . tmp_jsname [ prefix ] += <NUM_LIT:1> <EOL> return '<STR_LIT>' % ( prefix , self . tmp_jsname [ prefix ] ) <EOL> def add_const_int ( self , value ) : <EOL> value = str ( value ) <EOL> if not value in self . const_int : <EOL> name = '<STR_LIT>' % ( <EOL> self . jsvars [ '<STR_LIT>' ] , <EOL> len ( self . const_int ) , <EOL> ) <EOL> self . const_int [ value ] = name <EOL> return self . const_int [ value ] <EOL> def add_const_long ( self , value ) : <EOL> value = str ( long ( value ) ) <EOL> if not value in self . const_long : <EOL> name = '<STR_LIT>' % ( <EOL> self . jsvars [ '<STR_LIT>' ] , <EOL> len ( self . const_long ) , <EOL> ) <EOL> self . const_long [ value ] = name <EOL> return self . const_long [ value ] <EOL> def add_const_float ( self , value ) : <EOL> if not value in self . const_float : <EOL> name = '<STR_LIT>' % ( <EOL> self . jsvars [ '<STR_LIT>' ] , <EOL> len ( self . const_float ) , <EOL> ) <EOL> self . const_float [ value ] = name <EOL> return self . const_float [ value ] <EOL> def add_const_str ( self , value ) : <EOL> if not value in self . const_str : <EOL> name = '<STR_LIT>' % ( <EOL> self . jsvars [ '<STR_LIT>' ] , <EOL> len ( self . const_str ) , <EOL> ) <EOL> self . const_str [ value ] = name <EOL> return self . const_str [ value ] <EOL> def assert_value ( self , node , value , expected_value ) : <EOL> if isinstance ( expected_value , list ) or isinstance ( expected_value , tuple ) : <EOL> assert value in expected_value , self . TranslationError ( <EOL> "<STR_LIT>" % ( expected_value , value ) , <EOL> self . get_lineno ( node ) , <EOL> ) <EOL> else : <EOL> assert value == expected_value , self . TranslationError ( <EOL> "<STR_LIT>" % ( expected_value , value ) , <EOL> self . get_lineno ( node ) , <EOL> ) <EOL> def assert_type ( self , node , type_ , expected_type ) : <EOL> type_ = type_repr ( type_ ) <EOL> assert type_ == expected_type , self . TranslationError ( <EOL> "<STR_LIT>" % ( expected_type , type_ ) , <EOL> self . get_lineno ( node ) , <EOL> ) <EOL> def assert_instance ( self , node , inst , expected_class ) : <EOL> assert isinstance ( inst , expected_class ) , self . TranslationError ( <EOL> "<STR_LIT>" % ( expected_class . __name__ , inst ) , <EOL> self . get_lineno ( node ) , <EOL> ) <EOL> def assert_dedent ( self , level , expected_level ) : <EOL> assert level == expected_level , self . TranslationError ( <EOL> "<STR_LIT>" % ( expected_level , level ) , <EOL> self . last_lineno , <EOL> ) <EOL> def dispatch ( self , x , assign = False ) : <EOL> if isinstance ( x , Code ) : <EOL> return x <EOL> assign_state = self . assign_state <EOL> if assign : <EOL> self . assign_state = assign <EOL> lineno = self . get_lineno ( x ) <EOL> if lineno is not None : <EOL> self . last_lineno = lineno <EOL> else : <EOL> lineno = self . last_lineno <EOL> try : <EOL> if isinstance ( x , Node ) : <EOL> visit = getattr ( self , '<STR_LIT>' % type_repr ( x . type ) , None ) <EOL> if visit is None : <EOL> raise self . TranslationError ( "<STR_LIT>" % ( x . type , x ) ) <EOL> else : <EOL> visit = getattr ( self , '<STR_LIT>' % type_repr ( x . value ) , None ) <EOL> if visit is None : <EOL> if x . type in leaf_type : <EOL> visit = getattr ( self , '<STR_LIT>' % leaf_type . get ( x . type ) , None ) <EOL> if visit is None : <EOL> if x . value in [ '<STR_LIT>' , '<STR_LIT:\n>' , '<STR_LIT:\r\n>' , '<STR_LIT:;>' ] : <EOL> self . assign_state = assign_state <EOL> return <EOL> raise self . TranslationError ( "<STR_LIT>" % ( x . type , x ) ) <EOL> code = visit ( x ) <EOL> self . assign_state = assign_state <EOL> return code <EOL> except : <EOL> exc_type , exc_value , exc_traceback = sys . exc_info ( ) <EOL> if isinstance ( exc_value , self . TranslationError ) : <EOL> if exc_value . lineno is None : <EOL> exc_value . lineno = lineno <EOL> raise <EOL> else : <EOL> print "<STR_LIT>" % ( self . srcfile , lineno ) <EOL> raise <EOL> def not_implemented ( self , node ) : <EOL> print repr ( node ) <EOL> print dir ( node ) <EOL> raise NotImplementedError ( repr ( node ) ) <EOL> def get_test ( self , node , name = None ) : <EOL> if isinstance ( node , Node ) : <EOL> t = type_repr ( node . type ) <EOL> if t in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if name is None : <EOL> return getattr ( self , '<STR_LIT>' % t ) ( node , True ) <EOL> return getattr ( self , '<STR_LIT>' % t ) ( node , name = name ) <EOL> elif t in [ '<STR_LIT>' ] : <EOL> test = self . dispatch ( node ) <EOL> if name is None : <EOL> return "<STR_LIT>" % test <EOL> else : <EOL> return "<STR_LIT>" % ( name , test ) <EOL> test = self . dispatch ( node ) <EOL> if name is None : <EOL> return "<STR_LIT>" % ( self . jsvars [ '<STR_LIT>' ] , test ) <EOL> return "<STR_LIT>" % ( self . jsvars [ '<STR_LIT>' ] , name , test ) <EOL> def get_bit_expr ( self , node ) : <EOL> jsvars = self . jsvars . copy ( ) <EOL> childs = node . children . __iter__ ( ) <EOL> args = [ self . dispatch ( childs . next ( ) ) ] <EOL> op = child = childs . next ( ) <EOL> while child is not None : <EOL> op = child <EOL> op_name = self . op_names2 . get ( op . value , None ) <EOL> if op is None : <EOL> self . not_implemented ( node ) <EOL> try : <EOL> while True : <EOL> args . append ( self . dispatch ( childs . next ( ) ) ) <EOL> child = childs . next ( ) <EOL> if child . value != op . value : <EOL> break <EOL> except StopIteration : <EOL> child = None <EOL> if len ( args ) == <NUM_LIT:2> : <EOL> left , right = args <EOL> jsvars . update ( locals ( ) ) <EOL> code = "<STR_LIT>" % jsvars <EOL> else : <EOL> args = '<STR_LIT:U+002CU+0020>' . join ( str ( i ) for i in args ) <EOL> jsvars . update ( locals ( ) ) <EOL> code = "<STR_LIT>" % jsvars <EOL> args = [ code ] <EOL> return code <EOL> def get_op_item ( self , node , op_item , op_slice ) : <EOL> op = op_item <EOL> if isinstance ( node , Leaf ) and node . value == '<STR_LIT::>' : <EOL> what = Slice ( ( <EOL> self . add_const_int ( <NUM_LIT:0> ) , <EOL> self . add_const_int ( <NUM_LIT> ) , <EOL> ) ) <EOL> else : <EOL> what = self . dispatch ( node ) <EOL> if isinstance ( what , Slice ) : <EOL> slice = [ ] <EOL> for i in what . items : <EOL> if i is None : <EOL> slice . append ( self . jsvars [ '<STR_LIT:None>' ] ) <EOL> else : <EOL> slice . append ( i ) <EOL> what = "<STR_LIT>" % '<STR_LIT:U+002CU+0020>' . join ( slice ) <EOL> op = op_slice <EOL> return op , what <EOL> def collect_locals ( self , node ) : <EOL> for child in node . children : <EOL> if isinstance ( child , Node ) : <EOL> if type_repr ( child . type ) == '<STR_LIT>' : <EOL> self . add_name ( child . children [ <NUM_LIT:1> ] . value , force = True ) <EOL> elif type_repr ( child . type ) == '<STR_LIT>' : <EOL> self . add_name ( child . children [ <NUM_LIT:1> ] . value , force = True ) <EOL> elif type_repr ( child . type ) == '<STR_LIT>' : <EOL> self . collect_locals ( child ) <EOL> elif type_repr ( child . type ) == '<STR_LIT>' : <EOL> child0 = child . children [ <NUM_LIT:0> ] <EOL> if isinstance ( child0 , Node ) : <EOL> children = child0 . children <EOL> if type_repr ( child0 . type ) == '<STR_LIT>' : <EOL> if isinstance ( children [ <NUM_LIT:1> ] , Leaf ) and children [ <NUM_LIT:1> ] . value == '<STR_LIT:=>' : <EOL> if isinstance ( children [ <NUM_LIT:0> ] , Leaf ) : <EOL> self . add_name ( children [ <NUM_LIT:0> ] . value , force = True ) <EOL> elif type_repr ( child0 . type ) == '<STR_LIT>' : <EOL> for c in children : <EOL> if c . value == '<STR_LIT:U+002C>' : <EOL> continue <EOL> self . add_name ( c . value , glob = True , force = True ) <EOL> return <EOL> def create_assign_stmt ( self , lhs = '<STR_LIT>' , rhs = '<STR_LIT>' , subst_lhs = None , subst_rhs = None ) : <EOL> stmt = self . driver . parse_string ( <EOL> "<STR_LIT>" % locals ( ) , <EOL> True , <EOL> ) . children [ <NUM_LIT:0> ] <EOL> if subst_lhs is not None : <EOL> children = stmt . children [ <NUM_LIT:0> ] . children <EOL> if isinstance ( subst_lhs , basestring ) : <EOL> children [ <NUM_LIT:0> ] . value = subst_lhs <EOL> else : <EOL> children [ <NUM_LIT:0> ] = subst_lhs <EOL> if subst_rhs is not None : <EOL> children = stmt . children [ <NUM_LIT:0> ] . children <EOL> if isinstance ( subst_rhs , basestring ) : <EOL> children [ <NUM_LIT:2> ] . value = subst_rhs <EOL> else : <EOL> children [ <NUM_LIT:2> ] = subst_rhs <EOL> return stmt <EOL> def create_call_stmt ( self , fname = '<STR_LIT>' , args = None , subst_base = None , subst_args = None ) : <EOL> if args is None or len ( args ) == <NUM_LIT:0> : <EOL> a = '<STR_LIT>' <EOL> else : <EOL> a = [ ] <EOL> for arg in args : <EOL> if isinstance ( arg , Node ) or isinstance ( arg , Leaf ) : <EOL> a . append ( '<STR_LIT:1>' ) <EOL> else : <EOL> a . append ( arg ) <EOL> a = '<STR_LIT:U+002CU+0020>' . join ( a ) <EOL> stmt = self . driver . parse_string ( <EOL> "<STR_LIT>" % locals ( ) , <EOL> True , <EOL> ) . children [ <NUM_LIT:0> ] <EOL> trailer_childs = stmt . children [ <NUM_LIT:0> ] . children [ - <NUM_LIT:1> ] . children <EOL> if args is None or len ( args ) == <NUM_LIT:0> : <EOL> pass <EOL> elif len ( args ) == <NUM_LIT:1> : <EOL> arg = args [ <NUM_LIT:0> ] <EOL> if isinstance ( arg , Node ) or isinstance ( arg , Leaf ) : <EOL> trailer_childs [ <NUM_LIT:1> ] = arg <EOL> else : <EOL> self . assert_type ( stmt , trailer_childs [ <NUM_LIT:1> ] . type , '<STR_LIT>' ) <EOL> arglist_childs = trailer_childs [ <NUM_LIT:1> ] . children <EOL> for idx , arg in enumerate ( args ) : <EOL> idx *= <NUM_LIT:2> <EOL> if isinstance ( arg , Node ) or isinstance ( arg , Leaf ) : <EOL> arglist_childs [ idx ] = arg <EOL> if subst_base is not None : <EOL> if isinstance ( subst_base , basestring ) : <EOL> stmt . children [ <NUM_LIT:0> ] . children [ <NUM_LIT:0> ] . value = subst_base <EOL> else : <EOL> stmt . children [ <NUM_LIT:0> ] . children [ <NUM_LIT:0> ] = subst_base <EOL> if subst_args is not None : <EOL> self . assert_type ( stmt , trailer_childs [ <NUM_LIT:1> ] . type , '<STR_LIT>' ) <EOL> arglist_childs = trailer_childs [ <NUM_LIT:1> ] . children <EOL> if len ( args ) == <NUM_LIT:1> : <EOL> arg = args [ <NUM_LIT:0> ] <EOL> if arg is None : <EOL> pass <EOL> elif isinstance ( arg , basestring ) : <EOL> trailer_childs [ <NUM_LIT:1> ] . value = arg <EOL> else : <EOL> trailer_childs [ <NUM_LIT:1> ] = arg <EOL> elif len ( args ) > <NUM_LIT:1> : <EOL> for idx , arg in enumerate ( subst_args ) : <EOL> idx *= <NUM_LIT:2> <EOL> if arg is None : <EOL> pass <EOL> elif isinstance ( arg , basestring ) : <EOL> arglist_childs [ idx ] . value = arg <EOL> else : <EOL> arglist_childs [ idx ] = arg <EOL> return stmt <EOL> def create_getindex_stmt ( self , base = '<STR_LIT>' , idx = '<STR_LIT>' , subst_base = None , subst_idx = None ) : <EOL> stmt = self . driver . parse_string ( <EOL> "<STR_LIT>" % locals ( ) , <EOL> True , <EOL> ) . children [ <NUM_LIT:0> ] <EOL> if subst_base is not None : <EOL> children = stmt . children [ <NUM_LIT:0> ] . children <EOL> if isinstance ( subst_base , basestring ) : <EOL> children [ <NUM_LIT:0> ] . value = subst_base <EOL> else : <EOL> children [ <NUM_LIT:0> ] = subst_base <EOL> if subst_idx is not None : <EOL> children = stmt . children [ <NUM_LIT:0> ] . children [ <NUM_LIT:1> ] . children <EOL> if isinstance ( subst_idx , basestring ) : <EOL> children [ <NUM_LIT:1> ] . value = subst_idx <EOL> else : <EOL> children [ <NUM_LIT:1> ] = subst_idx <EOL> return stmt <EOL> def node_and_expr ( self , node ) : <EOL> return self . get_bit_expr ( node ) <EOL> def node_and_test ( self , node , jsbool = False , name = None ) : <EOL> jsvars = self . jsvars . copy ( ) <EOL> args = [ ] <EOL> childs = node . children . __iter__ ( ) <EOL> if jsbool : <EOL> name = None <EOL> elif name is None : <EOL> name = self . get_tmp_jsname ( '<STR_LIT>' ) <EOL> self . add_lines ( "<STR_LIT>" % name ) <EOL> args . append ( self . get_test ( childs . next ( ) , name ) ) <EOL> try : <EOL> while True : <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT>' ) <EOL> args . append ( self . get_test ( childs . next ( ) , name ) ) <EOL> except StopIteration : <EOL> pass <EOL> test = '<STR_LIT>' . join ( args ) <EOL> if jsbool : <EOL> return test <EOL> return '<STR_LIT>' % ( test , name , name ) <EOL> def node_arglist ( self , node ) : <EOL> childs = node . children . __iter__ ( ) <EOL> arglist = [ ] <EOL> try : <EOL> while True : <EOL> child = childs . next ( ) <EOL> if isinstance ( child , Leaf ) : <EOL> if child . value == '<STR_LIT:U+002C>' : <EOL> continue <EOL> if child . value in [ '<STR_LIT:*>' , '<STR_LIT>' ] : <EOL> value = self . dispatch ( childs . next ( ) ) <EOL> arglist . append ( Argument ( child . value , value ) ) <EOL> continue <EOL> arglist . append ( self . dispatch ( child ) ) <EOL> else : <EOL> arglist . append ( self . dispatch ( child ) ) <EOL> except StopIteration : <EOL> pass <EOL> return arglist <EOL> def node_argument ( self , node ) : <EOL> if isinstance ( node . children [ <NUM_LIT:1> ] , Leaf ) : <EOL> if node . children [ <NUM_LIT:1> ] . value == '<STR_LIT:=>' : <EOL> if len ( node . children ) == <NUM_LIT:3> : <EOL> name = node . children [ <NUM_LIT:0> ] . value <EOL> value = self . dispatch ( node . children [ <NUM_LIT:2> ] ) <EOL> return Argument ( name , value ) <EOL> else : <EOL> if type_repr ( node . children [ <NUM_LIT:1> ] . type ) == '<STR_LIT>' : <EOL> return self . node_listmaker ( node ) <EOL> self . not_implemented ( node ) <EOL> def node_arith_expr ( self , node ) : <EOL> jsvars = self . jsvars . copy ( ) <EOL> childs = node . children . __iter__ ( ) <EOL> left = self . dispatch ( childs . next ( ) ) <EOL> try : <EOL> while True : <EOL> op = childs . next ( ) <EOL> op = self . op_names2 . get ( op . value , None ) <EOL> if op is None : <EOL> self . not_implemented ( node ) <EOL> right = self . dispatch ( childs . next ( ) ) <EOL> jsvars . update ( locals ( ) ) <EOL> left = "<STR_LIT>" % jsvars <EOL> except StopIteration : <EOL> pass <EOL> return left <EOL> def node_assert_stmt ( self , node ) : <EOL> jsvars = self . jsvars . copy ( ) <EOL> childs = node . children . __iter__ ( ) <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT>' ) <EOL> test = self . dispatch ( childs . next ( ) ) <EOL> arg = '<STR_LIT>' <EOL> if len ( node . children ) > <NUM_LIT:2> : <EOL> child = childs . next ( ) <EOL> self . assert_value ( node , child . value , '<STR_LIT:U+002C>' ) <EOL> arg = '<STR_LIT>' % self . dispatch ( childs . next ( ) ) <EOL> jsvars . update ( locals ( ) ) <EOL> return """<STR_LIT>""" % jsvars <EOL> def node_atom ( self , node ) : <EOL> jsvars = self . jsvars . copy ( ) <EOL> items = [ ] <EOL> cls = None <EOL> if node . children [ <NUM_LIT:0> ] . value == '<STR_LIT:(>' : <EOL> cls = jsvars [ '<STR_LIT>' ] <EOL> if len ( node . children ) == <NUM_LIT:3> : <EOL> items = self . dispatch ( node . children [ <NUM_LIT:1> ] ) <EOL> if not isinstance ( items , list ) : <EOL> return items <EOL> elif len ( node . children ) != <NUM_LIT:2> : <EOL> self . not_implemented ( node ) <EOL> elif node . children [ <NUM_LIT:0> ] . value == '<STR_LIT:[>' : <EOL> cls = jsvars [ '<STR_LIT:list>' ] <EOL> if len ( node . children ) == <NUM_LIT:3> : <EOL> items = self . dispatch ( node . children [ <NUM_LIT:1> ] ) <EOL> if not isinstance ( items , list ) : <EOL> if isinstance ( node . children [ <NUM_LIT:1> ] , Leaf ) : <EOL> items = [ items ] <EOL> elif type_repr ( node . children [ <NUM_LIT:1> ] . type ) == '<STR_LIT>' : <EOL> pass <EOL> else : <EOL> items = [ items ] <EOL> elif len ( node . children ) != <NUM_LIT:2> : <EOL> self . not_implemented ( node ) <EOL> elif node . children [ <NUM_LIT:0> ] . value == '<STR_LIT:{>' : <EOL> cls = jsvars [ '<STR_LIT>' ] <EOL> if len ( node . children ) == <NUM_LIT:3> : <EOL> items = self . dispatch ( node . children [ <NUM_LIT:1> ] ) <EOL> if not isinstance ( items , dict ) : <EOL> self . not_implemented ( node ) <EOL> elif len ( node . children ) != <NUM_LIT:2> : <EOL> self . not_implemented ( node ) <EOL> if items : <EOL> items = [ "<STR_LIT>" % ( k , v ) for k , v in items . iteritems ( ) ] <EOL> else : <EOL> items = [ ] <EOL> elif node . children [ <NUM_LIT:0> ] . value == '<STR_LIT>' : <EOL> assert len ( node . children ) == <NUM_LIT:3> <EOL> what = self . dispatch ( node . children [ <NUM_LIT:1> ] ) <EOL> jsvars . update ( locals ( ) ) <EOL> return "<STR_LIT>" % jsvars <EOL> elif leaf_type . get ( node . children [ <NUM_LIT:0> ] . type ) == '<STR_LIT:str>' : <EOL> s = '<STR_LIT>' <EOL> for child in node . children : <EOL> assert leaf_type . get ( child . type ) == '<STR_LIT:str>' <EOL> s += child . value <EOL> return self . add_const_str ( eval ( s ) ) <EOL> else : <EOL> self . not_implemented ( node ) <EOL> if isinstance ( items , list ) : <EOL> items = '<STR_LIT>' % '<STR_LIT:U+002CU+0020>' . join ( [ str ( i ) for i in items ] ) <EOL> jsvars . update ( locals ( ) ) <EOL> return "<STR_LIT>" % jsvars <EOL> def node_augassign ( self , node ) : <EOL> self . not_implemented ( node ) <EOL> def node_break_stmt ( self , node ) : <EOL> return '<STR_LIT>' <EOL> def node_classdef ( self , node ) : <EOL> jsvars = self . jsvars . copy ( ) <EOL> childs = node . children . __iter__ ( ) <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT:class>' ) <EOL> name = childs . next ( ) . value <EOL> self . add_name ( name ) <EOL> tok = childs . next ( ) <EOL> if tok . value == '<STR_LIT::>' : <EOL> bases = self . get_jsname ( '<STR_LIT:object>' ) <EOL> else : <EOL> self . assert_value ( node , tok . value , '<STR_LIT:(>' ) <EOL> bases = childs . next ( ) <EOL> if isinstance ( bases , Leaf ) : <EOL> if bases . value == '<STR_LIT:)>' : <EOL> bases = None <EOL> else : <EOL> bases = [ self . get_jsname ( bases . value ) ] <EOL> else : <EOL> bases = self . dispatch ( bases ) <EOL> if bases is None : <EOL> bases = self . get_jsname ( '<STR_LIT:object>' ) <EOL> elif isinstance ( bases , list ) : <EOL> bases = '<STR_LIT:U+002CU+0020>' . join ( [ str ( i ) for i in bases ] ) <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT:)>' ) <EOL> else : <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT:)>' ) <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT::>' ) <EOL> lineno = self . track_lineno ( node ) <EOL> jsvars . update ( locals ( ) ) <EOL> if isinstance ( self . names [ - <NUM_LIT:1> ] , ClassNames ) : <EOL> namespace = "<STR_LIT>" % jsvars <EOL> else : <EOL> namespace = "<STR_LIT>" % jsvars <EOL> jsvars . update ( namespace = namespace ) <EOL> self . add_lines ( """<STR_LIT>""" % jsvars ) <EOL> names = ClassNames ( ) <EOL> self . names . append ( names ) <EOL> indent_level = self . indent ( ) <EOL> self . next_func_type . append ( func_type [ '<STR_LIT>' ] ) <EOL> try : <EOL> while True : <EOL> child = childs . next ( ) <EOL> self . dispatch ( child ) <EOL> except StopIteration : <EOL> pass <EOL> self . next_func_type . pop ( ) <EOL> self . assert_dedent ( self . dedent ( ) , indent_level ) <EOL> assert names is self . names . pop ( ) , self . TranslationError ( "<STR_LIT>" , self . get_lineno ( node ) ) <EOL> if '<STR_LIT>' in names : <EOL> self . add_lines ( """<STR_LIT>""" % jsvars ) <EOL> self . add_lines ( "<STR_LIT>" % jsvars ) <EOL> return name <EOL> def node_comp_for ( self , node ) : <EOL> assert False , "<STR_LIT>" <EOL> def node_comp_if ( self , node ) : <EOL> assert False , "<STR_LIT>" <EOL> def node_comp_iter ( self , node ) : <EOL> self . not_implemented ( node ) <EOL> def node_comp_op ( self , node ) : <EOL> if node . children [ <NUM_LIT:0> ] . value == '<STR_LIT>' : <EOL> if node . children [ <NUM_LIT:1> ] . value == '<STR_LIT>' : <EOL> return op_compare [ '<STR_LIT>' ] <EOL> elif node . children [ <NUM_LIT:0> ] . value == '<STR_LIT>' : <EOL> if node . children [ <NUM_LIT:1> ] . value == '<STR_LIT>' : <EOL> return op_compare [ '<STR_LIT>' ] <EOL> self . not_implemented ( node ) <EOL> def node_comparison ( self , node ) : <EOL> jsvars = self . jsvars . copy ( ) <EOL> left = op = right = None <EOL> childs = node . children . __iter__ ( ) <EOL> first_left = left = self . dispatch ( childs . next ( ) ) <EOL> prev_right = None <EOL> cmp_expr = [ ] <EOL> tmp = None <EOL> try : <EOL> while True : <EOL> op_node = childs . next ( ) <EOL> right = self . dispatch ( childs . next ( ) ) <EOL> if isinstance ( op_node , Leaf ) : <EOL> op = op_compare [ op_node . value ] <EOL> elif type_repr ( op_node . type ) == '<STR_LIT>' : <EOL> op = self . dispatch ( op_node ) <EOL> jsvars . update ( locals ( ) ) <EOL> if prev_right is None : <EOL> cmp_expr . append ( """<STR_LIT>""" % jsvars ) <EOL> else : <EOL> if tmp is None : <EOL> tmp = self . get_tmp_jsname ( '<STR_LIT>' ) <EOL> jsvars [ '<STR_LIT>' ] = tmp <EOL> self . add_lines ( "<STR_LIT>" % tmp ) <EOL> cmp_expr = [ """<STR_LIT>""" % jsvars ] <EOL> cmp_expr . append ( """<STR_LIT>""" % jsvars ) <EOL> left = right <EOL> prev_right = right <EOL> except StopIteration : <EOL> pass <EOL> if cmp_expr : <EOL> if len ( cmp_expr ) == <NUM_LIT:1> : <EOL> return cmp_expr [ <NUM_LIT:0> ] <EOL> s = '<STR_LIT>' . join ( [ <EOL> "<STR_LIT>" % i <EOL> for i in cmp_expr <EOL> ] ) <EOL> jsvars . update ( locals ( ) ) <EOL> return "<STR_LIT>" % jsvars <EOL> self . not_implemented ( node ) <EOL> def node_compound_stmt ( self , node ) : <EOL> self . not_implemented ( node ) <EOL> def node_continue_stmt ( self , node ) : <EOL> self . not_implemented ( node ) <EOL> def node_decorated ( self , node ) : <EOL> self . assert_instance ( node , node . children [ <NUM_LIT:0> ] , Node ) <EOL> self . assert_instance ( node , node . children [ <NUM_LIT:1> ] , Node ) <EOL> assert len ( node . children ) == <NUM_LIT:2> <EOL> decorators = self . dispatch ( node . children [ <NUM_LIT:0> ] ) <EOL> func = node . children [ <NUM_LIT:1> ] <EOL> if not isinstance ( decorators , list ) : <EOL> decorators = [ decorators ] <EOL> lineno = self . track_lineno ( decorators [ <NUM_LIT:0> ] ) <EOL> next_func_type = None <EOL> if isinstance ( self . names [ - <NUM_LIT:1> ] , ClassNames ) : <EOL> next_func_type = func_type [ '<STR_LIT>' ] <EOL> if decorators [ - <NUM_LIT:1> ] . name == '<STR_LIT>' : <EOL> next_func_type = func_type [ '<STR_LIT>' ] <EOL> elif decorators [ - <NUM_LIT:1> ] . name == '<STR_LIT>' : <EOL> next_func_type = func_type [ '<STR_LIT>' ] <EOL> self . next_func_type . append ( next_func_type ) <EOL> self . assert_value ( func , func . children [ <NUM_LIT:0> ] . value , '<STR_LIT>' ) <EOL> name = func . children [ <NUM_LIT:1> ] . value <EOL> self . add_name ( name , self . track_lineno ( func ) , force = True ) <EOL> src = self . get_jsname ( name ) <EOL> dst = "<STR_LIT>" % src <EOL> jsvars = self . jsvars . copy ( ) <EOL> for decorator in decorators : <EOL> if decorator is decorators [ - <NUM_LIT:1> ] and next_func_type is not None and decorator . name in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> pass <EOL> else : <EOL> deco = decorator . name . split ( '<STR_LIT:.>' ) <EOL> if len ( deco ) == <NUM_LIT:1> : <EOL> deco = self . get_jsname ( decorator . name ) <EOL> else : <EOL> dst = '<STR_LIT>' <EOL> deco = "<STR_LIT>" % ( <EOL> jsvars [ '<STR_LIT>' ] , <EOL> self . get_jsname ( deco [ <NUM_LIT:0> ] ) , <EOL> "<STR_LIT>" . join ( deco [ <NUM_LIT:1> : ] ) , <EOL> ) <EOL> jsvars . update ( locals ( ) ) <EOL> self . add_lines ( """<STR_LIT>""" % jsvars ) <EOL> dst = '<STR_LIT>' <EOL> self . indent ( ) <EOL> name = self . dispatch ( func ) <EOL> for decorator in decorators : <EOL> if decorator is decorators [ - <NUM_LIT:1> ] and next_func_type is not None and decorator . name in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> pass <EOL> else : <EOL> self . dedent ( ) <EOL> assert self . lines [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] [ - <NUM_LIT:1> ] == '<STR_LIT:;>' <EOL> self . lines [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] = self . lines [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] [ : - <NUM_LIT:1> ] <EOL> self . add_lines ( "<STR_LIT>" ) <EOL> if next_func_type is not None : <EOL> self . next_func_type . pop ( ) <EOL> return <EOL> def node_decorator ( self , node ) : <EOL> self . assert_value ( node , node . children [ <NUM_LIT:0> ] . value , '<STR_LIT:@>' ) <EOL> self . assert_value ( node , node . children [ - <NUM_LIT:1> ] . value , [ '<STR_LIT:\n>' , '<STR_LIT:\r\n>' ] ) <EOL> childs = node . children . __iter__ ( ) <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT:@>' ) <EOL> child = childs . next ( ) <EOL> if isinstance ( child , Leaf ) : <EOL> name = child . value <EOL> else : <EOL> name = self . dispatch ( child ) <EOL> name = '<STR_LIT:.>' . join ( name ) <EOL> self . assert_value ( node , childs . next ( ) . value , [ '<STR_LIT:\n>' , '<STR_LIT:\r\n>' ] ) <EOL> return Decorator ( name , node . children [ <NUM_LIT:0> ] . lineno ) <EOL> def node_decorators ( self , node ) : <EOL> decorators = [ ] <EOL> for child in node . children : <EOL> decorators . append ( self . dispatch ( child ) ) <EOL> return decorators <EOL> def node_del_stmt ( self , node ) : <EOL> jsvars = self . jsvars . copy ( ) <EOL> lineno = self . track_lineno ( node ) <EOL> childs = node . children . __iter__ ( ) <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT>' ) <EOL> child = childs . next ( ) <EOL> if isinstance ( child , Node ) and type_repr ( child . type ) == '<STR_LIT>' : <EOL> childs = child . children . __iter__ ( ) <EOL> child = childs . next ( ) <EOL> try : <EOL> while True : <EOL> if isinstance ( child , Leaf ) : <EOL> name = self . get_jsname ( child . value ) <EOL> self . add_lines ( "<STR_LIT>" % name , lineno ) <EOL> elif type_repr ( child . type ) == '<STR_LIT>' : <EOL> jsvars = self . jsvars . copy ( ) <EOL> tail = child . children . pop ( ) <EOL> if len ( child . children ) > <NUM_LIT:1> : <EOL> base = self . dispatch ( child ) <EOL> else : <EOL> base = self . dispatch ( child . children [ <NUM_LIT:0> ] ) <EOL> if type_repr ( tail . type ) == '<STR_LIT>' : <EOL> what = tail . children [ <NUM_LIT:1> ] <EOL> if tail . children [ <NUM_LIT:0> ] . value == '<STR_LIT:.>' : <EOL> op = '<STR_LIT>' <EOL> if isinstance ( what , Leaf ) : <EOL> what = repr ( what . value ) <EOL> else : <EOL> what = self . dispatch ( what ) <EOL> elif tail . children [ <NUM_LIT:0> ] . value == '<STR_LIT:[>' : <EOL> op , what = self . get_op_item ( <EOL> what , '<STR_LIT>' , '<STR_LIT>' , <EOL> ) <EOL> else : <EOL> self . not_implemented ( node ) <EOL> jsvars . update ( locals ( ) ) <EOL> self . add_lines ( """<STR_LIT>""" % jsvars ) <EOL> return <EOL> else : <EOL> self . not_implemented ( node ) <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT:U+002C>' ) <EOL> child = childs . next ( ) <EOL> except StopIteration : <EOL> pass <EOL> def node_dictsetmaker ( self , node ) : <EOL> values = { } <EOL> childs = node . children . __iter__ ( ) <EOL> try : <EOL> while True : <EOL> k = self . dispatch ( childs . next ( ) ) <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT::>' ) <EOL> v = self . dispatch ( childs . next ( ) ) <EOL> values [ k ] = v <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT:U+002C>' ) <EOL> except StopIteration : <EOL> pass <EOL> return values <EOL> def node_dotted_as_name ( self , node ) : <EOL> assert len ( node . children ) == <NUM_LIT:3> <EOL> if isinstance ( node . children [ <NUM_LIT:0> ] , Leaf ) : <EOL> mod = [ node . children [ <NUM_LIT:0> ] . value ] <EOL> else : <EOL> mod = self . dispatch ( node . children [ <NUM_LIT:0> ] ) <EOL> name = node . children [ <NUM_LIT:2> ] . value <EOL> return Import ( mod , name ) <EOL> def node_dotted_as_names ( self , node ) : <EOL> imports = [ ] <EOL> childs = node . children . __iter__ ( ) <EOL> try : <EOL> while True : <EOL> child = childs . next ( ) <EOL> if isinstance ( child , Leaf ) : <EOL> if child . value == '<STR_LIT:U+002C>' : <EOL> continue <EOL> imports . append ( Import ( [ child . value ] ) ) <EOL> else : <EOL> imp = self . dispatch ( child ) <EOL> if isinstance ( imp , list ) : <EOL> imp = Import ( imp ) <EOL> else : <EOL> imp = Import ( [ imp ] ) <EOL> imports . append ( imp ) <EOL> except StopIteration : <EOL> pass <EOL> return imports <EOL> def node_dotted_name ( self , node ) : <EOL> names = [ ] <EOL> for child in node . children : <EOL> if child . value == '<STR_LIT:.>' : <EOL> continue <EOL> names . append ( child . value ) <EOL> return names <EOL> def node_encoding_decl ( self , node ) : <EOL> self . not_implemented ( node ) <EOL> def node_eval_input ( self , node ) : <EOL> self . not_implemented ( node ) <EOL> def node_except_clause ( self , node ) : <EOL> jsvars = self . jsvars . copy ( ) <EOL> childs = node . children . __iter__ ( ) <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT>' ) <EOL> classes = None <EOL> varname = None <EOL> try : <EOL> child = childs . next ( ) <EOL> if isinstance ( child , Node ) and type_repr ( child . type ) == '<STR_LIT>' : <EOL> self . assert_value ( node , child . children [ <NUM_LIT:0> ] . value , '<STR_LIT:(>' ) <EOL> self . assert_value ( node , child . children [ - <NUM_LIT:1> ] . value , '<STR_LIT:)>' ) <EOL> classes = self . dispatch ( child . children [ <NUM_LIT:1> ] ) <EOL> else : <EOL> classes = self . dispatch ( child ) <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT:U+002C>' ) <EOL> varname = self . dispatch ( childs . next ( ) , assign = True ) <EOL> except StopIteration : <EOL> pass <EOL> if classes is not None : <EOL> if not isinstance ( classes , list ) : <EOL> classes = [ classes ] <EOL> tests = [ ] <EOL> return classes , varname <EOL> return None , None <EOL> def node_exec_stmt ( self , node ) : <EOL> self . not_implemented ( node ) <EOL> def node_expr ( self , node ) : <EOL> if len ( node . children ) > <NUM_LIT:2> : <EOL> if isinstance ( node . children [ <NUM_LIT:1> ] , Leaf ) : <EOL> if node . children [ <NUM_LIT:1> ] . value == '<STR_LIT:|>' : <EOL> return self . get_bit_expr ( node ) <EOL> self . not_implemented ( node ) <EOL> def _assign ( self , node , left , rest ) : <EOL> jsvars = self . jsvars . copy ( ) <EOL> lineno = self . track_lineno ( node ) <EOL> if len ( rest ) == <NUM_LIT:1> : <EOL> right = self . dispatch ( rest [ <NUM_LIT:0> ] ) <EOL> else : <EOL> self . assert_value ( node , rest [ <NUM_LIT:1> ] . value , '<STR_LIT:=>' ) <EOL> right = self . _assign ( node , rest [ <NUM_LIT:0> ] , rest [ <NUM_LIT:2> : ] ) <EOL> if not isinstance ( left , Node ) : <EOL> if isinstance ( self . names [ - <NUM_LIT:1> ] , ClassNames ) : <EOL> self . dispatch ( left , assign = right ) <EOL> left = left . value <EOL> jsvars . update ( locals ( ) ) <EOL> return "<STR_LIT>" % jsvars <EOL> left = self . dispatch ( left , assign = right ) <EOL> elif type_repr ( left . type ) in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> head_lines = [ ] <EOL> lines = [ ] <EOL> def tuple_assign ( left , right ) : <EOL> if type_repr ( left . type ) == '<STR_LIT>' : <EOL> children = left . children [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> if isinstance ( children [ <NUM_LIT:0> ] , Node ) and type_repr ( children [ <NUM_LIT:0> ] . type ) in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> children = children [ <NUM_LIT:0> ] . children [ : ] <EOL> else : <EOL> children = left . children [ : ] <EOL> n = len ( children ) / <NUM_LIT:2> + <NUM_LIT:1> <EOL> rhs = self . get_tmp_jsname ( '<STR_LIT>' ) <EOL> jsvars . update ( locals ( ) ) <EOL> self . add_lines ( "<STR_LIT>" % jsvars ) <EOL> head_lines . append ( <EOL> "<STR_LIT>" % jsvars <EOL> ) <EOL> idx = - <NUM_LIT:1> <EOL> for child in children : <EOL> if isinstance ( child , Leaf ) and child . value == '<STR_LIT:U+002C>' : <EOL> continue <EOL> idx += <NUM_LIT:1> <EOL> right = self . create_getindex_stmt ( <EOL> idx = <NUM_LIT:1> , <EOL> subst_base = rhs , <EOL> subst_idx = str ( idx ) , <EOL> ) . children [ <NUM_LIT:0> ] <EOL> if isinstance ( child , Node ) and type_repr ( child . type ) == '<STR_LIT>' : <EOL> self . assert_value ( node , child . children [ <NUM_LIT:0> ] . value , [ '<STR_LIT:(>' , '<STR_LIT:[>' ] ) <EOL> right = self . dispatch ( right ) <EOL> tuple_assign ( child , right ) <EOL> else : <EOL> self . dispatch ( child , assign = right ) <EOL> n = self . create_assign_stmt ( <EOL> subst_lhs = child , <EOL> subst_rhs = right , <EOL> ) . children [ <NUM_LIT:0> ] <EOL> line = self . node_expr_stmt ( n ) <EOL> if line [ - <NUM_LIT:1> ] != '<STR_LIT:;>' : <EOL> line += '<STR_LIT:;>' <EOL> lines . append ( line ) <EOL> tuple_assign ( left , right ) <EOL> lines = head_lines + lines <EOL> return '<STR_LIT:\n>' . join ( lines ) <EOL> elif type_repr ( left . type ) == '<STR_LIT>' : <EOL> tail = left . children . pop ( ) <EOL> if len ( left . children ) > <NUM_LIT:1> : <EOL> base = self . dispatch ( left ) <EOL> else : <EOL> base = self . dispatch ( left . children [ <NUM_LIT:0> ] ) <EOL> if isinstance ( base , Code ) : <EOL> base = str ( base ) <EOL> self . assert_instance ( node , base , basestring ) <EOL> args = [ ] <EOL> if type_repr ( tail . type ) == '<STR_LIT>' : <EOL> what = tail . children [ <NUM_LIT:1> ] <EOL> if tail . children [ <NUM_LIT:0> ] . value == '<STR_LIT:.>' : <EOL> op = '<STR_LIT>' <EOL> if isinstance ( what , Leaf ) : <EOL> what = repr ( what . value ) <EOL> else : <EOL> what = self . dispatch ( what ) <EOL> what = "<STR_LIT>" % what <EOL> elif tail . children [ <NUM_LIT:0> ] . value == '<STR_LIT:[>' : <EOL> op , what = self . get_op_item ( <EOL> what , '<STR_LIT>' , '<STR_LIT>' , <EOL> ) <EOL> else : <EOL> self . not_implemented ( node ) <EOL> jsvars . update ( locals ( ) ) <EOL> return """<STR_LIT>""" % jsvars <EOL> else : <EOL> self . not_implemented ( node ) <EOL> else : <EOL> left = self . dispatch ( left ) <EOL> return '<STR_LIT>' % ( left , right ) <EOL> def node_expr_stmt ( self , node ) : <EOL> jsvars = self . jsvars . copy ( ) <EOL> lineno = self . track_lineno ( node ) <EOL> if node . children [ <NUM_LIT:1> ] . value == '<STR_LIT:=>' : <EOL> return self . _assign ( node , node . children [ <NUM_LIT:0> ] , node . children [ <NUM_LIT:2> : ] ) <EOL> elif node . children [ <NUM_LIT:1> ] . value [ - <NUM_LIT:1> ] == '<STR_LIT:=>' and node . children [ <NUM_LIT:1> ] . value in self . op_names2 : <EOL> jsvars = self . jsvars . copy ( ) <EOL> op_node = node . children [ <NUM_LIT:1> ] <EOL> op = self . op_names2 . get ( op_node . value , None ) <EOL> if op is None : <EOL> self . not_implemented ( node ) <EOL> right = self . dispatch ( node . children [ <NUM_LIT:2> ] ) <EOL> left = node . children [ <NUM_LIT:0> ] <EOL> get_left = self . dispatch ( left ) <EOL> jsvars . update ( locals ( ) ) <EOL> if op_node . value [ : - <NUM_LIT:1> ] in [ '<STR_LIT:&>' , '<STR_LIT:|>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> stmt = "<STR_LIT>" % jsvars <EOL> else : <EOL> stmt = "<STR_LIT>" % jsvars <EOL> if isinstance ( left , Leaf ) : <EOL> set_left = left . value <EOL> else : <EOL> set_left = self . dispatch ( left , stmt ) <EOL> return set_left <EOL> stmt = self . create_assign_stmt ( <EOL> subst_lhs = set_left , <EOL> subst_rhs = Code ( stmt , lineno ) , <EOL> ) . children [ <NUM_LIT:0> ] <EOL> stmt = self . dispatch ( stmt ) <EOL> return stmt <EOL> self . not_implemented ( node ) <EOL> def node_exprlist ( self , node ) : <EOL> self . not_implemented ( node ) <EOL> def node_factor ( self , node ) : <EOL> if len ( node . children ) == <NUM_LIT:2> : <EOL> if node . children [ <NUM_LIT:0> ] . value == '<STR_LIT:->' : <EOL> right = node . children [ <NUM_LIT:1> ] <EOL> if isinstance ( right , Leaf ) and leaf_type . get ( right . type ) == '<STR_LIT>' : <EOL> v = right . value <EOL> right . value = '<STR_LIT:->' + right . value <EOL> r = self . dispatch ( right ) <EOL> right . value = v <EOL> return r <EOL> value = self . dispatch ( right ) <EOL> return "<STR_LIT>" % ( <EOL> self . jsvars [ '<STR_LIT>' ] , op_names1 [ '<STR_LIT>' ] , value , <EOL> ) <EOL> if node . children [ <NUM_LIT:0> ] . value == '<STR_LIT>' : <EOL> right = node . children [ <NUM_LIT:1> ] <EOL> value = self . dispatch ( right ) <EOL> return "<STR_LIT>" % ( <EOL> self . jsvars [ '<STR_LIT>' ] , op_names1 [ '<STR_LIT>' ] , value , <EOL> ) <EOL> self . not_implemented ( node ) <EOL> def node_file_input ( self , node ) : <EOL> jsvars = self . jsvars . copy ( ) <EOL> self . add_lines ( """<STR_LIT>""" % jsvars ) <EOL> if jsvars [ '<STR_LIT>' ] != jsvars [ '<STR_LIT>' ] : <EOL> self . add_lines ( """<STR_LIT>""" % jsvars ) <EOL> if os . path . basename ( self . srcfile ) == '<STR_LIT>' : <EOL> self . add_lines ( """<STR_LIT>""" % jsvars ) <EOL> jsvars . update ( locals ( ) ) <EOL> if len ( node . children ) > <NUM_LIT:0> : <EOL> self . add_lines ( """<STR_LIT>""" % jsvars ) <EOL> if jsvars [ '<STR_LIT>' ] != jsvars [ '<STR_LIT>' ] : <EOL> self . add_lines ( """<STR_LIT>""" % jsvars ) <EOL> self . indent ( <NUM_LIT:2> ) <EOL> for child in node . children : <EOL> self . dispatch ( child ) <EOL> self . dedent ( <NUM_LIT:2> ) <EOL> self . assert_dedent ( self . indent_level , <NUM_LIT:0> ) <EOL> self . add_lines ( """<STR_LIT>""" % jsvars ) <EOL> if len ( node . children ) > <NUM_LIT:0> : <EOL> self . add_lines ( """<STR_LIT>""" % jsvars ) <EOL> for name , value in sorted ( [ ( v , k ) for k , v in self . const_int . iteritems ( ) ] ) : <EOL> if name is not None : <EOL> v = abs ( int ( value ) ) <EOL> if v > ( <NUM_LIT:1> << <NUM_LIT:30> ) : <EOL> value = "<STR_LIT>" % ( jsvars [ '<STR_LIT:str>' ] , value ) <EOL> jsvars [ '<STR_LIT:value>' ] = value <EOL> jsvars [ '<STR_LIT:name>' ] = name <EOL> self . add_lines ( """<STR_LIT>""" % jsvars ) <EOL> for name , value in sorted ( [ ( v , k ) for k , v in self . const_long . iteritems ( ) ] ) : <EOL> if name is not None : <EOL> v = abs ( int ( value ) ) <EOL> if v > ( <NUM_LIT:1> << <NUM_LIT:30> ) : <EOL> value = "<STR_LIT>" % ( jsvars [ '<STR_LIT:str>' ] , value ) <EOL> jsvars [ '<STR_LIT:value>' ] = value <EOL> jsvars [ '<STR_LIT:name>' ] = name <EOL> self . add_lines ( """<STR_LIT>""" % jsvars ) <EOL> for name , value in sorted ( [ ( v , k ) for k , v in self . const_float . iteritems ( ) ] ) : <EOL> if name is not None : <EOL> jsvars [ '<STR_LIT:value>' ] = value <EOL> jsvars [ '<STR_LIT:name>' ] = name <EOL> self . add_lines ( """<STR_LIT>""" % jsvars ) <EOL> for name , value in sorted ( [ ( v , k ) for k , v in self . const_str . iteritems ( ) ] ) : <EOL> value = "<STR_LIT>" % re_js_string_escape . sub ( self . substitute_js_chars , value ) <EOL> if isinstance ( value , unicode ) : <EOL> value = value . encode ( '<STR_LIT:ascii>' , '<STR_LIT>' ) <EOL> if name is not None : <EOL> jsvars [ '<STR_LIT:value>' ] = value <EOL> jsvars [ '<STR_LIT:name>' ] = name <EOL> self . add_lines ( """<STR_LIT>""" % jsvars ) <EOL> self . add_lines ( """<STR_LIT>""" % jsvars ) <EOL> self . add_lines ( """<STR_LIT>""" % jsvars ) <EOL> pyjs_deps = self . imported_modules . keys ( ) <EOL> if pyjs_deps : <EOL> pyjs_deps . sort ( ) <EOL> jsvars . update ( pyjs_deps = pyjs_deps ) <EOL> self . add_lines ( """<STR_LIT>""" % jsvars ) <EOL> pyjs_js = self . imported_js . keys ( ) <EOL> if pyjs_js : <EOL> pyjs_js . sort ( ) <EOL> jsvars . update ( pyjs_js = pyjs_js ) <EOL> self . add_lines ( """<STR_LIT>""" % jsvars ) <EOL> def node_flow_stmt ( self , node ) : <EOL> self . not_implemented ( node ) <EOL> def node_for_stmt ( self , node ) : <EOL> jsvars = self . jsvars . copy ( ) <EOL> lineno = self . track_lineno ( node ) <EOL> inloop = self . inloop <EOL> self . inloop += <NUM_LIT:1> <EOL> childs = node . children . __iter__ ( ) <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT>' ) <EOL> assign = childs . next ( ) <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT>' ) <EOL> iterable = self . dispatch ( childs . next ( ) ) <EOL> if isinstance ( iterable , list ) : <EOL> iterable = '<STR_LIT>' % '<STR_LIT:U+002CU+0020>' . join ( [ str ( i ) for i in iterable ] ) <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT::>' ) <EOL> if True : <EOL> floop = self . get_tmp_jsname ( '<STR_LIT>' ) <EOL> floopdecl = '<STR_LIT>' % floop <EOL> floopass = '<STR_LIT>' % floop <EOL> floopass = '<STR_LIT>' <EOL> else : <EOL> floop = None <EOL> floopdecl = floopass = '<STR_LIT>' <EOL> iter = self . get_tmp_jsname ( '<STR_LIT>' ) <EOL> loop = self . get_tmp_jsname ( '<STR_LIT>' ) <EOL> jsvars . update ( locals ( ) ) <EOL> self . add_lines ( """<STR_LIT>""" % jsvars , lineno ) <EOL> indent_level = self . indent ( <NUM_LIT:2> ) <EOL> n = self . create_assign_stmt ( <EOL> subst_lhs = assign , <EOL> subst_rhs = loop , <EOL> ) . children [ <NUM_LIT:0> ] <EOL> line = self . node_expr_stmt ( n ) <EOL> if line . rstrip ( ) [ - <NUM_LIT:1> ] != '<STR_LIT:;>' : <EOL> line += '<STR_LIT:;>' <EOL> self . add_lines ( line ) <EOL> n = len ( self . lines ) <EOL> self . dispatch ( childs . next ( ) ) <EOL> self . assert_dedent ( self . dedent ( <NUM_LIT:2> ) , indent_level ) <EOL> self . add_lines ( """<STR_LIT>""" % jsvars ) <EOL> if len ( node . children ) > <NUM_LIT:6> and node . children [ <NUM_LIT:6> ] . value == '<STR_LIT>' : <EOL> if len ( self . lines ) > n : <EOL> self . add_lines ( """<STR_LIT>""" % jsvars ) <EOL> else : <EOL> self . add_lines ( """<STR_LIT>""" % jsvars ) <EOL> indent_level = self . indent ( <NUM_LIT:1> ) <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT>' ) <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT::>' ) <EOL> self . dispatch ( childs . next ( ) ) <EOL> self . assert_dedent ( self . dedent ( <NUM_LIT:1> ) , indent_level ) <EOL> self . add_lines ( """<STR_LIT>""" % jsvars ) <EOL> self . inloop -= <NUM_LIT:1> <EOL> def node_funcdef ( self , node , is_lambda = False ) : <EOL> jsvars = self . jsvars . copy ( ) <EOL> depth = self . get_names_depth ( ) <EOL> childs = node . children . __iter__ ( ) <EOL> if is_lambda : <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT>' ) <EOL> name = "<STR_LIT>" <EOL> assign_name = self . get_tmp_jsname ( '<STR_LIT>' ) <EOL> assign = "<STR_LIT>" % assign_name <EOL> if isinstance ( node . children [ <NUM_LIT:1> ] , Leaf ) and node . children [ <NUM_LIT:1> ] . value == '<STR_LIT::>' : <EOL> params = Parameters ( [ ] , None , None , None ) <EOL> else : <EOL> child = childs . next ( ) <EOL> if isinstance ( child , Leaf ) : <EOL> params = Parameters ( [ child . value ] , None , None , None ) <EOL> else : <EOL> params = self . dispatch ( child ) <EOL> else : <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT>' ) <EOL> name = childs . next ( ) . value <EOL> self . add_name ( name , force = True ) <EOL> assign = '<STR_LIT>' <EOL> params = self . dispatch ( childs . next ( ) ) <EOL> self . assert_instance ( node , params , Parameters ) <EOL> if params . star_args is None : <EOL> star_args = '<STR_LIT:null>' <EOL> else : <EOL> star_args = repr ( params . star_args ) <EOL> if params . dstar_args is None : <EOL> dstar_args = '<STR_LIT:null>' <EOL> else : <EOL> dstar_args = repr ( params . dstar_args ) <EOL> if params . defaults is None : <EOL> defaults = '<STR_LIT:null>' <EOL> else : <EOL> defaults = '<STR_LIT:U+002CU+0020>' . join ( [ str ( self . dispatch ( i ) ) for i in params . defaults ] ) <EOL> defaults = '<STR_LIT>' % defaults <EOL> tfpdef = { } <EOL> jsargs = [ ] <EOL> locals_init = [ ] <EOL> for arg in params . all_args : <EOL> if isinstance ( arg , tuple ) : <EOL> tfpdef_name = self . get_tmp_jsname ( '<STR_LIT>' ) <EOL> jsargs . append ( tfpdef_name ) <EOL> def flatten_arg ( names ) : <EOL> args = [ ] <EOL> for name in names : <EOL> if isinstance ( name , tuple ) : <EOL> name = flatten_arg ( name ) <EOL> else : <EOL> self . add_name ( name ) <EOL> args . append ( name ) <EOL> args = '<STR_LIT:U+002CU+0020>' . join ( args ) <EOL> return "<STR_LIT>" % args <EOL> tfpdef [ tfpdef_name ] = flatten_arg ( arg ) <EOL> else : <EOL> jsargs . append ( '<STR_LIT>' % arg ) <EOL> if is_lambda : <EOL> locals_init . append ( <EOL> "<STR_LIT>" % ( <EOL> jsvars [ '<STR_LIT>' ] , <EOL> arg , <EOL> arg , <EOL> ) <EOL> ) <EOL> else : <EOL> locals_init . append ( "<STR_LIT>" % ( arg , arg ) ) <EOL> args = params . args <EOL> if not jsargs : <EOL> jsargs = '<STR_LIT>' <EOL> else : <EOL> jsargs = '<STR_LIT:U+002CU+0020>' . join ( jsargs ) <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT::>' ) <EOL> type = self . next_func_type [ - <NUM_LIT:1> ] <EOL> lineno = self . track_lineno ( node ) <EOL> jsvars . update ( locals ( ) ) <EOL> self . add_lines ( """<STR_LIT>""" % jsvars ) <EOL> if not is_lambda : <EOL> self . add_lines ( """<STR_LIT>""" % jsvars ) <EOL> else : <EOL> for line in locals_init : <EOL> self . add_lines ( "<STR_LIT>" % line ) <EOL> lines = self . lines <EOL> self . lines = func_lines = [ ] <EOL> names = Names ( ) <EOL> self . names . append ( names ) <EOL> for a in params . all_args : <EOL> self . add_name ( a ) <EOL> if not is_lambda : <EOL> self . collect_locals ( node ) <EOL> indent_level = self . indent ( <NUM_LIT:2> ) <EOL> try : <EOL> lambda_code = None <EOL> while True : <EOL> child = childs . next ( ) <EOL> code = self . dispatch ( child ) <EOL> if is_lambda : <EOL> if lambda_code is None : <EOL> self . add_lines ( <EOL> "<STR_LIT>" % code , self . track_lineno ( child ) , <EOL> ) <EOL> lambda_code = code <EOL> else : <EOL> self . TranslationError ( <EOL> "<STR_LIT>" , <EOL> self . get_lineno ( node ) <EOL> ) <EOL> except StopIteration : <EOL> pass <EOL> self . assert_dedent ( self . dedent ( <NUM_LIT:2> ) , indent_level ) <EOL> self . lines = lines <EOL> assert names is self . names . pop ( ) <EOL> for n in names . itervalues ( ) : <EOL> if n . name not in params . all_args and n . builtin is False and n . to_js is None and n . depth <= depth and n . glob is False : <EOL> scope = '<STR_LIT>' <EOL> src = self . jsvars [ '<STR_LIT>' ] <EOL> src = '<STR_LIT>' % self . jsvars <EOL> src = '<STR_LIT>' % ( self . jsvars [ '<STR_LIT>' ] , n . depth ) <EOL> locals_init . append ( <EOL> "<STR_LIT>" % ( <EOL> n . name , src , n . name , <EOL> ) , <EOL> ) <EOL> locals_init = '<STR_LIT>' . join ( locals_init ) <EOL> locals_init = locals_init . strip ( ) <EOL> jsvars . update ( locals ( ) ) <EOL> if not is_lambda : <EOL> self . add_lines ( """<STR_LIT>""" % jsvars ) <EOL> if tfpdef : <EOL> self . indent ( <NUM_LIT:2> ) <EOL> for k , v in tfpdef . iteritems ( ) : <EOL> n = self . create_assign_stmt ( <EOL> lhs = str ( v ) , <EOL> subst_rhs = Code ( k , self . track_lineno ( node ) ) , <EOL> ) . children [ <NUM_LIT:0> ] <EOL> line = self . node_expr_stmt ( n ) <EOL> self . add_lines ( line ) <EOL> self . dedent ( <NUM_LIT:2> ) <EOL> self . lines += func_lines <EOL> if is_lambda : <EOL> self . add_lines ( """<STR_LIT>""" % jsvars ) <EOL> return assign_name <EOL> if isinstance ( self . names [ - <NUM_LIT:1> ] , ClassNames ) : <EOL> self . add_lines ( """<STR_LIT>""" % jsvars ) <EOL> else : <EOL> self . add_lines ( """<STR_LIT>""" % jsvars ) <EOL> return name <EOL> def node_global_stmt ( self , node ) : <EOL> for child in node . children [ <NUM_LIT:1> : ] : <EOL> if child . value == '<STR_LIT:U+002C>' : <EOL> continue <EOL> self . add_name ( child . value , glob = True ) <EOL> def node_if_stmt ( self , node ) : <EOL> jsvars = self . jsvars . copy ( ) <EOL> childs = node . children . __iter__ ( ) <EOL> try : <EOL> while True : <EOL> stmt = childs . next ( ) <EOL> if stmt . value == '<STR_LIT>' : <EOL> test = self . get_test ( childs . next ( ) ) <EOL> test = "<STR_LIT>" % test <EOL> self . add_lines ( test , self . track_lineno ( stmt ) ) <EOL> elif stmt . value == '<STR_LIT>' : <EOL> test = self . get_test ( childs . next ( ) ) <EOL> test = "<STR_LIT>" % test <EOL> self . add_lines ( test ) <EOL> elif stmt . value == '<STR_LIT>' : <EOL> self . add_lines ( '<STR_LIT>' ) <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT::>' ) <EOL> self . indent ( ) <EOL> self . dispatch ( childs . next ( ) ) <EOL> self . dedent ( ) <EOL> except StopIteration : <EOL> pass <EOL> self . add_lines ( "<STR_LIT:}>" ) <EOL> def node_import_as_name ( self , node ) : <EOL> assert len ( node . children ) == <NUM_LIT:3> <EOL> return node . children [ <NUM_LIT:0> ] . value , node . children [ <NUM_LIT:2> ] . value <EOL> def node_import_as_names ( self , node ) : <EOL> names = [ ] <EOL> for child in node . children : <EOL> if isinstance ( child , Leaf ) : <EOL> if child . value == '<STR_LIT:U+002C>' : <EOL> continue <EOL> names . append ( [ child . value , None ] ) <EOL> else : <EOL> names . append ( self . dispatch ( child ) ) <EOL> return names <EOL> def node_import_from ( self , node ) : <EOL> jsvars = self . jsvars . copy ( ) <EOL> imports = [ ] <EOL> if isinstance ( node . children [ <NUM_LIT:1> ] , Leaf ) : <EOL> modname = node . children [ <NUM_LIT:1> ] . value <EOL> else : <EOL> modname = '<STR_LIT:.>' . join ( self . dispatch ( node . children [ <NUM_LIT:1> ] ) ) <EOL> for child in node . children [ <NUM_LIT:3> : ] : <EOL> if isinstance ( child , Leaf ) : <EOL> if child . value in [ '<STR_LIT:(>' , '<STR_LIT:)>' ] : <EOL> continue <EOL> names = [ [ child . value , None ] ] <EOL> else : <EOL> names = self . dispatch ( child ) <EOL> if not isinstance ( names , list ) : <EOL> names = [ names ] <EOL> if modname == '<STR_LIT>' : <EOL> for name , assname in names : <EOL> if not hasattr ( __Pyjamas__ , name ) : <EOL> raise self . TranslationError ( <EOL> "<STR_LIT>" % ( <EOL> name , modname , <EOL> ) , <EOL> ) <EOL> to_js = getattr ( __Pyjamas__ , name ) ( self ) <EOL> self . add_name ( name , to_js = to_js , force = True ) <EOL> return <EOL> if modname == '<STR_LIT>' : <EOL> for name , assname in names : <EOL> if assname is None : <EOL> assname = name <EOL> self . add_name ( assname , to_js = name , force = True ) <EOL> return <EOL> if modname == '<STR_LIT>' : <EOL> for name , assname in names : <EOL> imp = '<STR_LIT>' % name <EOL> imp = getattr ( self . __future__ , imp , None ) <EOL> if imp is None : <EOL> raise self . TranslationError ( <EOL> "<STR_LIT>" % ( <EOL> name , <EOL> ) , <EOL> ) <EOL> imp ( self ) <EOL> level = modname <EOL> modname = modname . lstrip ( '<STR_LIT:.>' ) <EOL> level = len ( level ) - len ( modname ) <EOL> if level == <NUM_LIT:0> : <EOL> level = '<STR_LIT:null>' <EOL> else : <EOL> modname = '<STR_LIT>' <EOL> c_modname = self . add_const_str ( modname ) <EOL> assnames = [ ] <EOL> fromlist = [ ] <EOL> for name , assname in names : <EOL> c_name = self . add_const_str ( name ) <EOL> fromlist . append ( c_name ) <EOL> if assname is None : <EOL> assnames . append ( [ c_name , '<STR_LIT:null>' ] ) <EOL> assname = name <EOL> else : <EOL> c_assname = self . add_const_str ( assname ) <EOL> assnames . append ( [ c_name , c_assname ] ) <EOL> self . add_name ( assname ) <EOL> self . add_import ( modname , [ name ] ) <EOL> if assnames : <EOL> assnames = "<STR_LIT>" % '<STR_LIT>' . join ( [ "<STR_LIT>" % ( i , j ) for ( i , j ) in assnames ] ) <EOL> else : <EOL> assnames = '<STR_LIT>' <EOL> fromlist = '<STR_LIT:U+002CU+0020>' . join ( fromlist ) <EOL> jsvars . update ( <EOL> modname = c_modname , <EOL> fromlist = fromlist , <EOL> assnames = assnames , <EOL> level = level , <EOL> ) <EOL> imports . append ( """<STR_LIT>""" % jsvars ) <EOL> return "<STR_LIT:\n>" . join ( imports ) <EOL> def node_import_name ( self , node ) : <EOL> jsvars = self . jsvars . copy ( ) <EOL> imports = [ ] <EOL> for child in node . children [ <NUM_LIT:1> : ] : <EOL> if isinstance ( child , Leaf ) : <EOL> imp = child . value <EOL> else : <EOL> imp = self . dispatch ( child ) <EOL> if not isinstance ( imp , list ) : <EOL> imp = [ imp ] <EOL> if not isinstance ( imp [ <NUM_LIT:0> ] , Import ) : <EOL> imp = [ Import ( imp ) ] <EOL> for i in imp : <EOL> self . add_name ( i . assname ) <EOL> self . add_import ( i . modname , None ) <EOL> c_modname = self . add_const_str ( i . modname ) <EOL> c_assname = self . add_const_str ( i . assname ) <EOL> assnames = "<STR_LIT>" % ( c_modname , c_assname ) <EOL> dst = self . get_jsname ( i . assname ) <EOL> jsvars . update ( <EOL> dst = dst , <EOL> modname = c_modname , <EOL> assname = i . assname , <EOL> assnames = assnames , <EOL> ) <EOL> imports . append ( """<STR_LIT>""" % jsvars ) <EOL> return "<STR_LIT:\n>" . join ( imports ) <EOL> def node_import_stmt ( self , node ) : <EOL> self . not_implemented ( node ) <EOL> def node_lambdef ( self , node ) : <EOL> return self . node_funcdef ( node , is_lambda = True ) <EOL> def node_listmaker ( self , node , cls = '<STR_LIT:list>' ) : <EOL> items = [ ] <EOL> for child in node . children : <EOL> if isinstance ( child , Leaf ) : <EOL> if child . value != '<STR_LIT:U+002C>' : <EOL> items . append ( self . dispatch ( child ) ) <EOL> elif type_repr ( child . type ) == '<STR_LIT>' : <EOL> simple_stmt = self . create_call_stmt ( <EOL> '<STR_LIT>' , <EOL> [ node . children [ <NUM_LIT:0> ] ] , <EOL> subst_base = '<STR_LIT>' , <EOL> ) <EOL> childs = child . children . __iter__ ( ) <EOL> base_node = comp_node = child <EOL> comp_node . type = sym_type ( '<STR_LIT>' ) <EOL> comp_node . children = [ ] <EOL> child = childs . next ( ) <EOL> try : <EOL> while True : <EOL> while isinstance ( child , Leaf ) or not type_repr ( child . type ) in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> comp_node . append_child ( child ) <EOL> child = childs . next ( ) <EOL> childs = child . children . __iter__ ( ) <EOL> comp_node . append_child ( Leaf ( <NUM_LIT:11> , '<STR_LIT::>' ) ) <EOL> comp_node . append_child ( child ) <EOL> comp_node = child <EOL> comp_node . children = [ ] <EOL> if type_repr ( comp_node . type ) == '<STR_LIT>' : <EOL> comp_node . type = sym_type ( '<STR_LIT>' ) <EOL> else : <EOL> comp_node . type = sym_type ( '<STR_LIT>' ) <EOL> child = childs . next ( ) <EOL> except StopIteration : <EOL> pass <EOL> comp_node . append_child ( Leaf ( <NUM_LIT:11> , '<STR_LIT::>' ) ) <EOL> comp_node . append_child ( simple_stmt ) <EOL> lines = self . lines <EOL> self . lines = [ ] <EOL> self . node_for_stmt ( base_node ) <EOL> i = self . indent_level - <NUM_LIT:1> <EOL> comp = '<STR_LIT:\n>' . join ( [ "<STR_LIT>" % ( '<STR_LIT:+>' * ( j - i ) , s ) for j , s in self . lines ] ) <EOL> self . lines = lines <EOL> jsvars = self . jsvars . copy ( ) <EOL> jsvars . update ( locals ( ) ) <EOL> retval = '<STR_LIT>' <EOL> if cls != '<STR_LIT:list>' : <EOL> retval = "<STR_LIT>" % jsvars <EOL> jsvars . update ( locals ( ) ) <EOL> return """<STR_LIT>""" % jsvars <EOL> else : <EOL> items . append ( self . dispatch ( child ) ) <EOL> return items <EOL> def node_not_test ( self , node , jsbool = False , name = None ) : <EOL> jsvars = self . jsvars . copy ( ) <EOL> if len ( node . children ) == <NUM_LIT:2> : <EOL> self . assert_value ( node , node . children [ <NUM_LIT:0> ] . value , '<STR_LIT>' ) <EOL> value = self . dispatch ( node . children [ <NUM_LIT:1> ] ) <EOL> jsvars . update ( locals ( ) ) <EOL> if jsbool : <EOL> return "<STR_LIT>" % jsvars <EOL> return "<STR_LIT>" % jsvars <EOL> self . not_implemented ( node ) <EOL> def node_old_lambdef ( self , node ) : <EOL> self . not_implemented ( node ) <EOL> def node_old_test ( self , node ) : <EOL> self . not_implemented ( node ) <EOL> def node_or_test ( self , node , jsbool = False , name = None ) : <EOL> jsvars = self . jsvars . copy ( ) <EOL> args = [ ] <EOL> childs = node . children . __iter__ ( ) <EOL> if jsbool : <EOL> name = None <EOL> elif name is None : <EOL> name = self . get_tmp_jsname ( '<STR_LIT>' ) <EOL> self . add_lines ( "<STR_LIT>" % name ) <EOL> args . append ( self . get_test ( childs . next ( ) , name ) ) <EOL> try : <EOL> while True : <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT>' ) <EOL> args . append ( self . get_test ( childs . next ( ) , name ) ) <EOL> except StopIteration : <EOL> pass <EOL> test = '<STR_LIT>' . join ( args ) <EOL> if jsbool : <EOL> return test <EOL> return '<STR_LIT>' % ( test , name , name ) <EOL> def node_parameters ( self , node ) : <EOL> self . assert_value ( node , node . children [ <NUM_LIT:0> ] . value , '<STR_LIT:(>' ) <EOL> self . assert_value ( node , node . children [ - <NUM_LIT:1> ] . value , '<STR_LIT:)>' ) <EOL> assert len ( node . children ) <= <NUM_LIT:3> <EOL> if len ( node . children ) == <NUM_LIT:2> : <EOL> return Parameters ( [ ] , None , None , None ) <EOL> if isinstance ( node . children [ <NUM_LIT:1> ] , Leaf ) : <EOL> args = node . children [ <NUM_LIT:1> ] . value <EOL> else : <EOL> args = self . dispatch ( node . children [ <NUM_LIT:1> ] ) <EOL> if isinstance ( args , basestring ) : <EOL> return Parameters ( [ args ] , None , None , None ) <EOL> return self . dispatch ( node . children [ <NUM_LIT:1> ] ) <EOL> def node_pass_stmt ( self , node ) : <EOL> self . not_implemented ( node ) <EOL> def node_power ( self , node ) : <EOL> jsvars = self . jsvars . copy ( ) <EOL> childs = node . children . __iter__ ( ) <EOL> left = self . dispatch ( childs . next ( ) ) <EOL> if isinstance ( node . children [ <NUM_LIT:1> ] , Leaf ) : <EOL> if node . children [ <NUM_LIT:1> ] . value == '<STR_LIT>' : <EOL> assert len ( node . children ) == <NUM_LIT:3> <EOL> jsvars . update ( <EOL> left = self . dispatch ( node . children [ <NUM_LIT:0> ] ) , <EOL> right = self . dispatch ( node . children [ <NUM_LIT:2> ] ) , <EOL> ) <EOL> return "<STR_LIT>" % jsvars <EOL> elif isinstance ( node . children [ <NUM_LIT:1> ] , Node ) : <EOL> attrs = [ ] <EOL> lineno = self . track_lineno ( node ) <EOL> for child in node . children [ <NUM_LIT:1> : ] : <EOL> if isinstance ( child , Leaf ) and child . value == '<STR_LIT>' : <EOL> assert child is node . children [ - <NUM_LIT:2> ] <EOL> jsvars . update ( <EOL> left = left , <EOL> right = self . dispatch ( node . children [ - <NUM_LIT:1> ] ) , <EOL> ) <EOL> return "<STR_LIT>" % jsvars <EOL> trailer = self . dispatch ( child ) <EOL> if isinstance ( trailer , Attribute ) : <EOL> assert not isinstance ( left , __Pyjamas__ . __Pyjamas__ ) , node <EOL> attrs . append ( trailer . name ) <EOL> elif isinstance ( trailer , Slice ) : <EOL> assert not isinstance ( left , __Pyjamas__ . __Pyjamas__ ) , node <EOL> assign = self . assign_state is not False and child is node . children [ - <NUM_LIT:1> ] <EOL> if assign is False : <EOL> method = "<STR_LIT>" <EOL> else : <EOL> method = "<STR_LIT>" <EOL> if len ( trailer . items ) == <NUM_LIT:1> : <EOL> if assign is False : <EOL> method = '<STR_LIT>' % jsvars <EOL> else : <EOL> method = "<STR_LIT>" % jsvars <EOL> args = trailer . items [ <NUM_LIT:0> ] <EOL> elif len ( trailer . items ) == <NUM_LIT:2> : <EOL> args = list ( trailer . items ) <EOL> if args [ <NUM_LIT:0> ] == None : <EOL> args [ <NUM_LIT:0> ] = self . add_const_int ( <NUM_LIT:0> ) <EOL> if args [ <NUM_LIT:1> ] == None : <EOL> args [ <NUM_LIT:1> ] = self . add_const_int ( <NUM_LIT> ) <EOL> args = "<STR_LIT>" % '<STR_LIT:U+002CU+0020>' . join ( str ( i ) for i in args ) <EOL> else : <EOL> args = [ ] <EOL> for a in trailer . items : <EOL> if a is None : <EOL> args . append ( jsvars [ '<STR_LIT:None>' ] ) <EOL> else : <EOL> args . append ( a ) <EOL> args = '<STR_LIT:U+002CU+0020>' . join ( args ) <EOL> jsvars . update ( args = args ) <EOL> args = "<STR_LIT>" % jsvars <EOL> if attrs : <EOL> attrs = '<STR_LIT:U+002CU+0020>' . join ( attrs ) <EOL> jsvars . update ( attrs = attrs , left = left ) <EOL> left = """<STR_LIT>""" % jsvars <EOL> attrs = [ ] <EOL> jsvars . update ( locals ( ) ) <EOL> if self . assign_state is False : <EOL> left = """<STR_LIT>""" % jsvars <EOL> else : <EOL> jsvars [ '<STR_LIT>' ] = self . assign_state <EOL> left = """<STR_LIT>""" % jsvars <EOL> elif isinstance ( trailer , Parameters ) : <EOL> params = trailer <EOL> args = '<STR_LIT>' <EOL> if params . args : <EOL> args += '<STR_LIT:U+002CU+0020>' + '<STR_LIT:U+002CU+0020>' . join ( [ str ( a ) for a in params . args ] ) <EOL> star_args = params . star_args <EOL> dstar_args = params . dstar_args <EOL> named_args = params . named_args <EOL> extended = False <EOL> if star_args or dstar_args or named_args : <EOL> extended = True <EOL> if star_args is None : <EOL> star_args = '<STR_LIT:null>' <EOL> if dstar_args is None : <EOL> dstar_args = '<STR_LIT:null>' <EOL> if named_args is None : <EOL> named_args = '<STR_LIT:null>' <EOL> else : <EOL> named_args = '<STR_LIT:U+002CU+0020>' . join ( [ <EOL> '<STR_LIT>' % ( k , v ) for k , v in named_args . iteritems ( ) <EOL> ] ) <EOL> named_args = "<STR_LIT>" % named_args <EOL> if isinstance ( left , __Pyjamas__ . __Pyjamas__ ) : <EOL> if params . named_args is None : <EOL> left = Code ( left . js ( self , * params . args ) , self . get_lineno ( node ) ) <EOL> else : <EOL> left = Code ( left . js ( self , * params . args , ** params . named_args ) , self . get_lineno ( node ) ) <EOL> elif not attrs : <EOL> jsvars . update ( locals ( ) ) <EOL> if star_args is None : <EOL> left = """<STR_LIT>""" % jsvars <EOL> else : <EOL> left = """<STR_LIT>""" % jsvars <EOL> else : <EOL> attrs = '<STR_LIT:U+002CU+0020>' . join ( attrs ) <EOL> jsvars . update ( locals ( ) ) <EOL> if star_args is None : <EOL> left = """<STR_LIT>""" % jsvars <EOL> else : <EOL> left = """<STR_LIT>""" % jsvars <EOL> attrs = [ ] <EOL> if attrs : <EOL> attrs = '<STR_LIT:U+002CU+0020>' . join ( attrs ) <EOL> if self . assign_state is False : <EOL> jsvars . update ( locals ( ) ) <EOL> return Code ( """<STR_LIT>""" % jsvars , self . get_lineno ( node ) ) <EOL> else : <EOL> value = self . assign_state <EOL> jsvars . update ( locals ( ) ) <EOL> return Code ( """<STR_LIT>""" % jsvars , self . get_lineno ( node ) ) <EOL> return Code ( left , self . get_lineno ( node ) ) <EOL> self . not_implemented ( node ) <EOL> def node_print_stmt ( self , node ) : <EOL> if self . options . print_statements is False : <EOL> return <EOL> jsvars = self . jsvars . copy ( ) <EOL> self . assert_value ( node , node . children [ <NUM_LIT:0> ] . value , '<STR_LIT>' ) <EOL> args = [ ] <EOL> for child in node . children [ <NUM_LIT:1> : ] : <EOL> if isinstance ( child , Leaf ) and child . value == '<STR_LIT:U+002C>' : <EOL> continue <EOL> arg = self . dispatch ( child ) <EOL> if child is not None : <EOL> args . append ( arg ) <EOL> newline = '<STR_LIT:true>' <EOL> if isinstance ( node . children [ - <NUM_LIT:1> ] , Leaf ) and node . children [ - <NUM_LIT:1> ] . value == '<STR_LIT:U+002C>' : <EOL> newline = '<STR_LIT:false>' <EOL> args = '<STR_LIT:U+002CU+0020>' . join ( [ str ( i ) for i in args ] ) <EOL> jsvars . update ( locals ( ) ) <EOL> return """<STR_LIT>""" % jsvars <EOL> def node_raise_stmt ( self , node ) : <EOL> jsvars = self . jsvars . copy ( ) <EOL> args = [ ] <EOL> for child in node . children [ <NUM_LIT:1> : ] : <EOL> if isinstance ( child , Leaf ) and child . value == '<STR_LIT:U+002C>' : <EOL> continue <EOL> args . append ( self . dispatch ( child ) ) <EOL> args = '<STR_LIT:U+002CU+0020>' . join ( [ str ( a ) for a in args ] ) <EOL> jsvars . update ( locals ( ) ) <EOL> return "<STR_LIT>" % jsvars <EOL> def node_return_stmt ( self , node ) : <EOL> assert len ( node . children ) == <NUM_LIT:2> <EOL> value = self . dispatch ( node . children [ <NUM_LIT:1> ] ) <EOL> if isinstance ( value , list ) : <EOL> jsvars = self . jsvars . copy ( ) <EOL> items = '<STR_LIT:U+002CU+0020>' . join ( [ str ( i ) for i in value ] ) <EOL> jsvars . update ( locals ( ) ) <EOL> return "<STR_LIT>" % jsvars <EOL> return "<STR_LIT>" % value <EOL> def node_shift_expr ( self , node ) : <EOL> return self . get_bit_expr ( node ) <EOL> def node_simple_stmt ( self , node ) : <EOL> for child in node . children : <EOL> if isinstance ( child , Leaf ) : <EOL> if child . value in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> self . add_lines ( '<STR_LIT>' % child . value , self . get_lineno ( child ) ) <EOL> continue <EOL> code = self . dispatch ( child ) <EOL> if isinstance ( code , Code ) : <EOL> code = code . code <EOL> code = str ( code ) <EOL> if code is not None : <EOL> if code [ - <NUM_LIT:1> ] not in [ '<STR_LIT:;>' , '<STR_LIT:}>' ] : <EOL> code += '<STR_LIT:;>' <EOL> self . add_lines ( code , self . get_lineno ( child ) ) <EOL> def node_single_input ( self , node ) : <EOL> self . not_implemented ( node ) <EOL> def node_sliceop ( self , node ) : <EOL> assert len ( node . children ) == <NUM_LIT:2> <EOL> self . assert_value ( node , node . children [ <NUM_LIT:0> ] . value , '<STR_LIT::>' ) <EOL> return self . dispatch ( node . children [ <NUM_LIT:1> ] ) <EOL> def node_small_stmt ( self , node ) : <EOL> self . not_implemented ( node ) <EOL> def node_star_expr ( self , node ) : <EOL> self . not_implemented ( node ) <EOL> def node_stmt ( self , node ) : <EOL> self . not_implemented ( node ) <EOL> def node_subscript ( self , node ) : <EOL> slice = [ None ] <EOL> for child in node . children : <EOL> if isinstance ( child , Leaf ) and child . value == '<STR_LIT::>' : <EOL> slice . append ( None ) <EOL> continue <EOL> slice [ - <NUM_LIT:1> ] = self . dispatch ( child ) <EOL> return Slice ( tuple ( slice ) ) <EOL> def node_subscriptlist ( self , node ) : <EOL> jsvars = self . jsvars . copy ( ) <EOL> args = [ ] <EOL> childs = node . children . __iter__ ( ) <EOL> args . append ( self . dispatch ( childs . next ( ) ) ) <EOL> try : <EOL> while True : <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT:U+002C>' ) <EOL> args . append ( self . dispatch ( childs . next ( ) ) ) <EOL> except StopIteration : <EOL> pass <EOL> args = '<STR_LIT:U+002CU+0020>' . join ( [ str ( i ) for i in args ] ) <EOL> jsvars . update ( locals ( ) ) <EOL> return "<STR_LIT>" % jsvars <EOL> def node_suite ( self , node ) : <EOL> for child in node . children : <EOL> if isinstance ( child , Node ) : <EOL> self . dispatch ( child ) <EOL> def node_term ( self , node ) : <EOL> jsvars = self . jsvars . copy ( ) <EOL> childs = node . children . __iter__ ( ) <EOL> left = self . dispatch ( childs . next ( ) ) <EOL> try : <EOL> while True : <EOL> op = childs . next ( ) . value <EOL> right = self . dispatch ( childs . next ( ) ) <EOL> op = self . op_names2 . get ( op , None ) <EOL> if op is None : <EOL> self . not_implemented ( node ) <EOL> jsvars . update ( left = left , op = op , right = right ) <EOL> left = "<STR_LIT>" % jsvars <EOL> except StopIteration : <EOL> pass <EOL> return left <EOL> def node_test ( self , node ) : <EOL> jsvars = self . jsvars . copy ( ) <EOL> childs = node . children . __iter__ ( ) <EOL> left = self . dispatch ( childs . next ( ) ) <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT>' ) <EOL> test = self . get_test ( childs . next ( ) ) <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT>' ) <EOL> right = self . dispatch ( childs . next ( ) ) <EOL> return '<STR_LIT>' % ( test , left , right ) <EOL> def node_testlist ( self , node ) : <EOL> items = [ ] <EOL> childs = node . children . __iter__ ( ) <EOL> try : <EOL> while True : <EOL> items . append ( self . dispatch ( childs . next ( ) ) ) <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT:U+002C>' ) <EOL> except StopIteration : <EOL> pass <EOL> return items <EOL> def node_testlist1 ( self , node ) : <EOL> self . not_implemented ( node ) <EOL> def node_testlist_gexp ( self , node ) : <EOL> items = [ ] <EOL> for child in node . children : <EOL> if isinstance ( child , Leaf ) : <EOL> if child . value == '<STR_LIT:U+002C>' : <EOL> continue <EOL> else : <EOL> if type_repr ( child . type ) == '<STR_LIT>' : <EOL> return self . node_listmaker ( node , cls = '<STR_LIT>' ) <EOL> items . append ( self . dispatch ( child ) ) <EOL> return items <EOL> def node_testlist_safe ( self , node ) : <EOL> self . not_implemented ( node ) <EOL> def node_testlist_star_expr ( self , node ) : <EOL> jsvars = self . jsvars . copy ( ) <EOL> childs = node . children . __iter__ ( ) <EOL> items = [ ] <EOL> try : <EOL> while True : <EOL> items . append ( self . dispatch ( childs . next ( ) ) ) <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT:U+002C>' ) <EOL> except StopIteration : <EOL> pass <EOL> cls = '<STR_LIT>' <EOL> items = '<STR_LIT:U+002CU+0020>' . join ( [ str ( i ) for i in items ] ) <EOL> jsvars . update ( locals ( ) ) <EOL> return "<STR_LIT>" % jsvars <EOL> def node_tfpdef ( self , node ) : <EOL> childs = node . children . __iter__ ( ) <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT:(>' ) <EOL> tpl = self . dispatch ( childs . next ( ) ) <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT:)>' ) <EOL> return tuple ( tpl ) <EOL> def node_tfplist ( self , node ) : <EOL> childs = node . children . __iter__ ( ) <EOL> tpl = [ ] <EOL> try : <EOL> while True : <EOL> child = childs . next ( ) <EOL> if isinstance ( child , Leaf ) : <EOL> tpl . append ( child . value ) <EOL> else : <EOL> tpl . append ( self . dispatch ( child ) ) <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT:U+002C>' ) <EOL> except StopIteration : <EOL> pass <EOL> return tpl <EOL> def node_tname ( self , node ) : <EOL> self . not_implemented ( node ) <EOL> def node_trailer ( self , node ) : <EOL> if node . children [ <NUM_LIT:0> ] . value == '<STR_LIT:(>' : <EOL> args = [ ] <EOL> star_args = None <EOL> dstar_args = None <EOL> defaults = None <EOL> childs = node . children . __iter__ ( ) <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT:(>' ) <EOL> while True : <EOL> child = childs . next ( ) <EOL> if isinstance ( child , Leaf ) : <EOL> if child . value == '<STR_LIT:)>' : <EOL> break <EOL> args . append ( self . dispatch ( child ) ) <EOL> else : <EOL> arg = self . dispatch ( child ) <EOL> if isinstance ( arg , list ) : <EOL> args += arg <EOL> else : <EOL> args . append ( arg ) <EOL> return Parameters ( args , star_args , dstar_args , defaults ) <EOL> elif node . children [ <NUM_LIT:0> ] . value == '<STR_LIT:.>' : <EOL> assert len ( node . children ) == <NUM_LIT:2> <EOL> if isinstance ( node . children [ <NUM_LIT:1> ] , Leaf ) : <EOL> return Attribute ( repr ( node . children [ <NUM_LIT:1> ] . value ) ) <EOL> return Attribute ( self . dispatch ( node . children [ <NUM_LIT:1> ] ) ) <EOL> elif node . children [ <NUM_LIT:0> ] . value == '<STR_LIT:[>' : <EOL> slice = [ ] <EOL> assert len ( node . children ) == <NUM_LIT:3> <EOL> if isinstance ( node . children [ <NUM_LIT:1> ] , Leaf ) and node . children [ <NUM_LIT:1> ] . value == '<STR_LIT::>' : <EOL> return Slice ( ( None , None ) ) <EOL> slice = self . dispatch ( node . children [ <NUM_LIT:1> ] ) <EOL> if isinstance ( slice , Slice ) : <EOL> return slice <EOL> return Slice ( ( slice , ) ) <EOL> self . not_implemented ( node ) <EOL> def node_try_stmt ( self , node ) : <EOL> jsvars = self . jsvars . copy ( ) <EOL> childs = node . children . __iter__ ( ) <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT>' ) <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT::>' ) <EOL> _try = childs . next ( ) <EOL> _except_clauses = [ ] <EOL> _except = None <EOL> _else = None <EOL> _finally = None <EOL> try : <EOL> while True : <EOL> child = childs . next ( ) <EOL> if isinstance ( child , Leaf ) : <EOL> if child . value == '<STR_LIT>' : <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT::>' ) <EOL> _except = childs . next ( ) <EOL> elif child . value == '<STR_LIT>' : <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT::>' ) <EOL> _else = childs . next ( ) <EOL> elif child . value == '<STR_LIT>' : <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT::>' ) <EOL> _finally = childs . next ( ) <EOL> else : <EOL> raise NotImplementedError ( repr ( node ) ) <EOL> elif type_repr ( child . type ) == '<STR_LIT>' : <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT::>' ) <EOL> _except_clauses . append ( ( child , childs . next ( ) ) ) <EOL> else : <EOL> raise NotImplementedError ( repr ( node ) ) <EOL> except StopIteration : <EOL> pass <EOL> track_len = self . get_tmp_jsname ( '<STR_LIT>' ) <EOL> self . add_lines ( "<STR_LIT>" % track_len ) <EOL> self . add_lines ( "<STR_LIT>" , node . children [ <NUM_LIT:0> ] . lineno ) <EOL> self . indent ( ) <EOL> self . dispatch ( _try ) <EOL> if _else is not None : <EOL> self . add_lines ( "<STR_LIT>" % jsvars ) <EOL> self . dedent ( ) <EOL> self . add_lines ( "<STR_LIT>" % jsvars ) <EOL> self . indent ( ) <EOL> self . add_lines ( "<STR_LIT>" % jsvars ) <EOL> self . add_lines ( <EOL> "<STR_LIT>" % ( <EOL> track_len , <EOL> ) , <EOL> ) <EOL> elseif = "<STR_LIT>" <EOL> jsvars . update ( locals ( ) ) <EOL> if _else is not None : <EOL> self . add_lines ( "<STR_LIT>" % jsvars ) <EOL> self . indent ( ) <EOL> self . dispatch ( _else ) <EOL> self . dedent ( ) <EOL> elseif = "<STR_LIT>" <EOL> for clause , suite in _except_clauses : <EOL> classes , varname = self . dispatch ( clause ) <EOL> jsvars [ '<STR_LIT>' ] = '<STR_LIT:U+002CU+0020>' . join ( [ str ( i ) for i in classes ] ) <EOL> jsvars [ '<STR_LIT>' ] = elseif <EOL> self . add_lines ( """<STR_LIT>""" % jsvars ) <EOL> self . indent ( ) <EOL> if varname : <EOL> self . add_lines ( "<STR_LIT>" % ( varname , jsvars [ '<STR_LIT>' ] ) ) <EOL> self . dispatch ( suite ) <EOL> self . dedent ( ) <EOL> elseif = "<STR_LIT>" <EOL> if elseif != "<STR_LIT>" : <EOL> self . add_lines ( "<STR_LIT>" ) <EOL> self . indent ( ) <EOL> if _except is None : <EOL> self . add_lines ( "<STR_LIT>" % jsvars ) <EOL> else : <EOL> self . dispatch ( _except ) <EOL> if elseif != "<STR_LIT>" : <EOL> self . dedent ( ) <EOL> self . add_lines ( "<STR_LIT:}>" ) <EOL> if _finally is not None : <EOL> self . dedent ( ) <EOL> self . add_lines ( "<STR_LIT>" ) <EOL> self . indent ( ) <EOL> self . dispatch ( _finally ) <EOL> self . dedent ( ) <EOL> self . add_lines ( "<STR_LIT:}>" ) <EOL> def node_typedargslist ( self , node ) : <EOL> childs = node . children . __iter__ ( ) <EOL> args = [ ] <EOL> star_args = None <EOL> dstar_args = None <EOL> defaults = [ ] <EOL> try : <EOL> while True : <EOL> child = childs . next ( ) <EOL> if isinstance ( child , Leaf ) : <EOL> if child . value == '<STR_LIT:*>' : <EOL> star_args = childs . next ( ) . value <EOL> elif child . value == '<STR_LIT>' : <EOL> dstar_args = childs . next ( ) . value <EOL> elif child . value == '<STR_LIT:=>' : <EOL> defaults . append ( childs . next ( ) ) <EOL> elif child . value != '<STR_LIT:U+002C>' : <EOL> args . append ( child . value ) <EOL> elif type_repr ( child . type ) == '<STR_LIT>' : <EOL> args . append ( self . dispatch ( child ) ) <EOL> else : <EOL> self . not_implemented ( node ) <EOL> except StopIteration : <EOL> pass <EOL> return Parameters ( args , star_args , dstar_args , defaults ) <EOL> def node_varargslist ( self , node ) : <EOL> return self . node_typedargslist ( node ) <EOL> def node_vfpdef ( self , node ) : <EOL> self . not_implemented ( node ) <EOL> def node_vfplist ( self , node ) : <EOL> self . not_implemented ( node ) <EOL> def node_vname ( self , node ) : <EOL> self . not_implemented ( node ) <EOL> def node_while_stmt ( self , node ) : <EOL> jsvars = self . jsvars . copy ( ) <EOL> inloop = self . inloop <EOL> self . inloop += <NUM_LIT:1> <EOL> childs = node . children . __iter__ ( ) <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT>' ) <EOL> test = self . get_test ( childs . next ( ) ) <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT::>' ) <EOL> if len ( node . children ) > <NUM_LIT:4> and node . children [ <NUM_LIT:4> ] . value == '<STR_LIT>' : <EOL> loop = self . get_tmp_jsname ( '<STR_LIT>' ) <EOL> loopdecl = '<STR_LIT>' % loop <EOL> loopass = '<STR_LIT>' % loop <EOL> else : <EOL> loop = None <EOL> loopdecl = loopass = '<STR_LIT>' <EOL> jsvars . update ( locals ( ) ) <EOL> self . add_lines ( """<STR_LIT>""" % jsvars , node . children [ <NUM_LIT:0> ] . lineno ) <EOL> self . indent ( ) <EOL> self . dispatch ( childs . next ( ) ) <EOL> self . dedent ( ) <EOL> self . add_lines ( "<STR_LIT>" ) <EOL> if loop is not None : <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT>' ) <EOL> self . assert_value ( node , childs . next ( ) . value , '<STR_LIT::>' ) <EOL> self . add_lines ( """<STR_LIT>""" % jsvars , ) <EOL> self . indent ( ) <EOL> self . dispatch ( childs . next ( ) ) <EOL> self . dedent ( ) <EOL> self . add_lines ( "<STR_LIT>" ) <EOL> self . inloop -= <NUM_LIT:1> <EOL> def node_with_item ( self , node ) : <EOL> self . not_implemented ( node ) <EOL> def node_with_stmt ( self , node ) : <EOL> self . not_implemented ( node ) <EOL> def node_with_var ( self , node ) : <EOL> self . not_implemented ( node ) <EOL> def node_xor_expr ( self , node ) : <EOL> return self . get_bit_expr ( node ) <EOL> def node_yield_expr ( self , node ) : <EOL> return <EOL> self . not_implemented ( node ) <EOL> def node_yield_stmt ( self , node ) : <EOL> return <EOL> self . not_implemented ( node ) <EOL> def leaftype_name ( self , leaf ) : <EOL> if leaf . value . find ( '<STR_LIT:$>' ) >= <NUM_LIT:0> : <EOL> return leaf . value <EOL> self . add_name ( leaf . value , leaf . lineno , force = False ) <EOL> return self . get_jsname ( leaf . value ) <EOL> def leaftype_number ( self , leaf ) : <EOL> if re_oct_int . match ( leaf . value ) : <EOL> i = str ( int ( leaf . value , <NUM_LIT:8> ) ) <EOL> return self . add_const_int ( i ) <EOL> if re_hex_int . match ( leaf . value ) : <EOL> i = str ( int ( leaf . value , <NUM_LIT:16> ) ) <EOL> return self . add_const_int ( i ) <EOL> if re_int . match ( leaf . value ) : <EOL> return self . add_const_int ( leaf . value ) <EOL> if re_oct_long . match ( leaf . value ) : <EOL> i = str ( long ( leaf . value , <NUM_LIT:16> ) ) <EOL> return self . add_const_long ( i ) <EOL> if re_hex_long . match ( leaf . value ) : <EOL> i = str ( long ( leaf . value , <NUM_LIT:16> ) ) <EOL> return self . add_const_long ( i ) <EOL> if re_long . match ( leaf . value ) : <EOL> return self . add_const_long ( leaf . value ) <EOL> return self . add_const_float ( leaf . value ) <EOL> def substitute_js_chars ( self , m ) : <EOL> c = m . group ( <NUM_LIT:0> ) <EOL> i = ord ( c ) <EOL> if i < <NUM_LIT:32> : <EOL> return '<STR_LIT>' % i <EOL> if c in [ "<STR_LIT:'>" , '<STR_LIT:">' , '<STR_LIT:<>' , '<STR_LIT:>>' , '<STR_LIT:\\>' ] : <EOL> return '<STR_LIT>' % i <EOL> return c <EOL> def leaftype_str ( self , leaf ) : <EOL> s = leaf . value <EOL> return self . add_const_str ( eval ( s ) ) <EOL> def leaf_False ( self , leaf ) : <EOL> return self . jsvars [ '<STR_LIT:False>' ] <EOL> def leaf_None ( self , leaf ) : <EOL> return self . jsvars [ '<STR_LIT:None>' ] <EOL> def leaf_True ( self , leaf ) : <EOL> return self . jsvars [ '<STR_LIT:True>' ] <EOL> def leaf_pass ( self , leaf ) : <EOL> return None <EOL> def leaf_return ( self , leaf ) : <EOL> return '<STR_LIT>' <EOL> from options import ( all_compile_options , add_compile_options , <EOL> get_compile_options , debug_options , speed_options , <EOL> pythonic_options ) <EOL> if os . environ . has_key ( '<STR_LIT>' ) : <EOL> sys . path [ <NUM_LIT:0> : <NUM_LIT:0> ] = [ os . environ [ '<STR_LIT>' ] ] <EOL> import pyjs <EOL> if pyjs . pyjspth is None : <EOL> LIBRARY_PATH = os . path . abspath ( os . path . dirname ( __file__ ) ) <EOL> else : <EOL> LIBRARY_PATH = os . path . join ( pyjs . pyjspth , "<STR_LIT>" , "<STR_LIT:src>" , "<STR_LIT>" ) <EOL> TranslationError = Translator . TranslationError <EOL> def translate ( compiler , sources , output_file , module_name = None , ** kw ) : <EOL> global TranslationError <EOL> kw = dict ( all_compile_options , ** kw ) <EOL> list_imports = kw . get ( '<STR_LIT>' , False ) <EOL> sources = map ( os . path . abspath , sources ) <EOL> output_file = os . path . abspath ( output_file ) <EOL> if not module_name : <EOL> module_name , extension = os . path . splitext ( os . path . basename ( sources [ <NUM_LIT:0> ] ) ) <EOL> translator = Translator ( sources [ <NUM_LIT:0> ] , module_name , kw ) <EOL> TranslationError = translator . TranslationError <EOL> trees = [ ] <EOL> tree = None <EOL> for src in sources : <EOL> current_tree = translator . ast_tree_creator ( src ) <EOL> flags = set ( ) <EOL> f = file ( src ) <EOL> for l in f : <EOL> if l . startswith ( '<STR_LIT>' ) : <EOL> flags . add ( l . strip ( ) [ <NUM_LIT:7> : ] ) <EOL> f . close ( ) <EOL> if tree : <EOL> tree = translator . tree_merge ( tree , current_tree , flags ) <EOL> else : <EOL> tree = current_tree <EOL> f = file ( sources [ <NUM_LIT:0> ] , "<STR_LIT:r>" ) <EOL> src = f . read ( ) <EOL> f . close ( ) <EOL> if list_imports : <EOL> translator . dispatch_file ( tree ) <EOL> return translator . imported_modules . keys ( ) , translator . imported_js . keys ( ) <EOL> v = ImportVisitor ( module_name ) <EOL> compiler . walk ( tree , v ) <EOL> return v . imported_modules , v . imported_js <EOL> translator . dispatch_file ( tree ) <EOL> if output_file == '<STR_LIT:->' : <EOL> output = sys . stdout <EOL> else : <EOL> output = file ( output_file , '<STR_LIT:w>' ) <EOL> output . write ( translator . get_javascript ( ) ) <EOL> output . close ( ) <EOL> return sorted ( translator . imported_modules . keys ( ) ) , sorted ( translator . imported_js . keys ( ) ) <EOL> class ImportVisitor ( object ) : <EOL> def __init__ ( self , module_name ) : <EOL> self . module_name = module_name <EOL> self . imported_modules = [ ] <EOL> self . imported_js = [ ] <EOL> def add_imported_module ( self , importName ) : <EOL> if not importName in self . imported_modules : <EOL> self . imported_modules . append ( importName ) <EOL> def visitModule ( self , node ) : <EOL> self . visit ( node . node ) <EOL> def visitImport ( self , node ) : <EOL> self . _doImport ( node . names ) <EOL> def _doImport ( self , names ) : <EOL> for importName , importAs in names : <EOL> if importName == '<STR_LIT>' : <EOL> continue <EOL> if importName . endswith ( "<STR_LIT>" ) : <EOL> continue <EOL> imp . add_imported_js ( importName ) <EOL> continue <EOL> self . add_imported_module ( importName ) <EOL> def visitFrom ( self , node ) : <EOL> if node . modname == '<STR_LIT>' : <EOL> return <EOL> if node . modname == '<STR_LIT>' : <EOL> return <EOL> absPath = False <EOL> modname = node . modname <EOL> if hasattr ( node , '<STR_LIT>' ) and node . level > <NUM_LIT:0> : <EOL> absPath = True <EOL> modname = self . module_name . split ( '<STR_LIT:.>' ) <EOL> level = node . level <EOL> if len ( modname ) < level : <EOL> raise TranslationError ( <EOL> "<STR_LIT>" , <EOL> node , self . module_name ) <EOL> if node . modname != '<STR_LIT>' : <EOL> level += <NUM_LIT:1> <EOL> if level > <NUM_LIT:1> : <EOL> modname = '<STR_LIT:.>' . join ( modname [ : - ( node . level - <NUM_LIT:1> ) ] ) <EOL> else : <EOL> modname = self . module_name <EOL> if node . modname != '<STR_LIT>' : <EOL> modname += '<STR_LIT:.>' + node . modname <EOL> if modname [ <NUM_LIT:0> ] == '<STR_LIT:.>' : <EOL> modname = modname [ <NUM_LIT:1> : ] <EOL> for name in node . names : <EOL> sub = modname + '<STR_LIT:.>' + name [ <NUM_LIT:0> ] <EOL> ass_name = name [ <NUM_LIT:1> ] or name [ <NUM_LIT:0> ] <EOL> self . _doImport ( ( ( sub , ass_name ) , ) ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> translator = Translator ( sys . argv [ <NUM_LIT:1> ] , None , { } ) <EOL> translator . tree = translator . ast_tree_creator ( ) <EOL> translator . dispatch_file ( translator . tree ) <EOL> print translator . get_javascript ( ) </s>
