<s> __revision__ = "<STR_LIT>" <EOL> __doc__ = """<STR_LIT>""" <EOL> import SCons . compat <EOL> import os <EOL> import re <EOL> cpp_lines_dict = { <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , ) <EOL> : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , ) <EOL> : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , ) : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , ) : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , ) : '<STR_LIT>' , <EOL> } <EOL> Table = { } <EOL> for op_list , expr in cpp_lines_dict . items ( ) : <EOL> e = re . compile ( expr ) <EOL> for op in op_list : <EOL> Table [ op ] = e <EOL> del e <EOL> del op <EOL> del op_list <EOL> override = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> l = [ override . get ( x , x ) for x in Table . keys ( ) ] <EOL> e = '<STR_LIT>' + '<STR_LIT:|>' . join ( l ) + '<STR_LIT>' <EOL> CPP_Expression = re . compile ( e , re . M ) <EOL> CPP_to_Python_Ops_Dict = { <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:\r>' : '<STR_LIT>' , <EOL> } <EOL> CPP_to_Python_Ops_Sub = lambda m : CPP_to_Python_Ops_Dict [ m . group ( <NUM_LIT:0> ) ] <EOL> l = sorted ( CPP_to_Python_Ops_Dict . keys ( ) , key = lambda a : len ( a ) , reverse = True ) <EOL> expr = '<STR_LIT:|>' . join ( map ( re . escape , l ) ) <EOL> CPP_to_Python_Ops_Expression = re . compile ( expr ) <EOL> CPP_to_Python_Eval_List = [ <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> for l in CPP_to_Python_Eval_List : <EOL> l [ <NUM_LIT:0> ] = re . compile ( l [ <NUM_LIT:0> ] ) <EOL> def CPP_to_Python ( s ) : <EOL> """<STR_LIT>""" <EOL> s = CPP_to_Python_Ops_Expression . sub ( CPP_to_Python_Ops_Sub , s ) <EOL> for expr , repl in CPP_to_Python_Eval_List : <EOL> s = expr . sub ( repl , s ) <EOL> return s <EOL> del expr <EOL> del l <EOL> del override <EOL> class FunctionEvaluator ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , args , expansion ) : <EOL> """<STR_LIT>""" <EOL> self . name = name <EOL> self . args = function_arg_separator . split ( args ) <EOL> try : <EOL> expansion = expansion . split ( '<STR_LIT>' ) <EOL> except AttributeError : <EOL> pass <EOL> self . expansion = expansion <EOL> def __call__ ( self , * values ) : <EOL> """<STR_LIT>""" <EOL> if len ( self . args ) != len ( values ) : <EOL> raise ValueError ( "<STR_LIT>" % self . name ) <EOL> locals = { } <EOL> for k , v in zip ( self . args , values ) : <EOL> locals [ k ] = v <EOL> parts = [ ] <EOL> for s in self . expansion : <EOL> if not s in self . args : <EOL> s = repr ( s ) <EOL> parts . append ( s ) <EOL> statement = '<STR_LIT>' . join ( parts ) <EOL> return eval ( statement , globals ( ) , locals ) <EOL> line_continuations = re . compile ( '<STR_LIT>' ) <EOL> function_name = re . compile ( '<STR_LIT>' ) <EOL> function_arg_separator = re . compile ( '<STR_LIT>' ) <EOL> class PreProcessor ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , current = os . curdir , cpppath = ( ) , dict = { } , all = <NUM_LIT:0> ) : <EOL> global Table <EOL> cpppath = tuple ( cpppath ) <EOL> self . searchpath = { <EOL> '<STR_LIT:">' : ( current , ) + cpppath , <EOL> '<STR_LIT:<>' : cpppath + ( current , ) , <EOL> } <EOL> self . cpp_namespace = dict . copy ( ) <EOL> self . cpp_namespace [ '<STR_LIT>' ] = self . cpp_namespace <EOL> if all : <EOL> self . do_include = self . all_include <EOL> d = { <EOL> '<STR_LIT>' : self . scons_current_file <EOL> } <EOL> for op in Table . keys ( ) : <EOL> d [ op ] = getattr ( self , '<STR_LIT>' + op ) <EOL> self . default_table = d <EOL> def tupleize ( self , contents ) : <EOL> """<STR_LIT>""" <EOL> global CPP_Expression , Table <EOL> contents = line_continuations . sub ( '<STR_LIT>' , contents ) <EOL> cpp_tuples = CPP_Expression . findall ( contents ) <EOL> return [ ( m [ <NUM_LIT:0> ] , ) + Table [ m [ <NUM_LIT:0> ] ] . match ( m [ <NUM_LIT:1> ] ) . groups ( ) for m in cpp_tuples ] <EOL> def __call__ ( self , file ) : <EOL> """<STR_LIT>""" <EOL> self . current_file = file <EOL> return self . process_contents ( self . read_file ( file ) , file ) <EOL> def process_contents ( self , contents , fname = None ) : <EOL> """<STR_LIT>""" <EOL> self . stack = [ ] <EOL> self . dispatch_table = self . default_table . copy ( ) <EOL> self . current_file = fname <EOL> self . tuples = self . tupleize ( contents ) <EOL> self . initialize_result ( fname ) <EOL> while self . tuples : <EOL> t = self . tuples . pop ( <NUM_LIT:0> ) <EOL> self . dispatch_table [ t [ <NUM_LIT:0> ] ] ( t ) <EOL> return self . finalize_result ( fname ) <EOL> def save ( self ) : <EOL> """<STR_LIT>""" <EOL> self . stack . append ( self . dispatch_table ) <EOL> self . dispatch_table = self . default_table . copy ( ) <EOL> def restore ( self ) : <EOL> """<STR_LIT>""" <EOL> try : self . dispatch_table = self . stack . pop ( ) <EOL> except IndexError : pass <EOL> def do_nothing ( self , t ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def scons_current_file ( self , t ) : <EOL> self . current_file = t [ <NUM_LIT:1> ] <EOL> def eval_expression ( self , t ) : <EOL> """<STR_LIT>""" <EOL> t = CPP_to_Python ( '<STR_LIT:U+0020>' . join ( t [ <NUM_LIT:1> : ] ) ) <EOL> try : return eval ( t , self . cpp_namespace ) <EOL> except ( NameError , TypeError ) : return <NUM_LIT:0> <EOL> def initialize_result ( self , fname ) : <EOL> self . result = [ fname ] <EOL> def finalize_result ( self , fname ) : <EOL> return self . result [ <NUM_LIT:1> : ] <EOL> def find_include_file ( self , t ) : <EOL> """<STR_LIT>""" <EOL> fname = t [ <NUM_LIT:2> ] <EOL> for d in self . searchpath [ t [ <NUM_LIT:1> ] ] : <EOL> if d == os . curdir : <EOL> f = fname <EOL> else : <EOL> f = os . path . join ( d , fname ) <EOL> if os . path . isfile ( f ) : <EOL> return f <EOL> return None <EOL> def read_file ( self , file ) : <EOL> return open ( file ) . read ( ) <EOL> def start_handling_includes ( self , t = None ) : <EOL> """<STR_LIT>""" <EOL> d = self . dispatch_table <EOL> p = self . stack [ - <NUM_LIT:1> ] if self . stack else self . default_table <EOL> for k in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> d [ k ] = p [ k ] <EOL> def stop_handling_includes ( self , t = None ) : <EOL> """<STR_LIT>""" <EOL> d = self . dispatch_table <EOL> d [ '<STR_LIT>' ] = self . do_nothing <EOL> d [ '<STR_LIT>' ] = self . do_nothing <EOL> d [ '<STR_LIT>' ] = self . do_nothing <EOL> def _do_if_else_condition ( self , condition ) : <EOL> """<STR_LIT>""" <EOL> self . save ( ) <EOL> d = self . dispatch_table <EOL> if condition : <EOL> self . start_handling_includes ( ) <EOL> d [ '<STR_LIT>' ] = self . stop_handling_includes <EOL> d [ '<STR_LIT>' ] = self . stop_handling_includes <EOL> else : <EOL> self . stop_handling_includes ( ) <EOL> d [ '<STR_LIT>' ] = self . do_elif <EOL> d [ '<STR_LIT>' ] = self . start_handling_includes <EOL> def do_ifdef ( self , t ) : <EOL> """<STR_LIT>""" <EOL> self . _do_if_else_condition ( t [ <NUM_LIT:1> ] in self . cpp_namespace ) <EOL> def do_ifndef ( self , t ) : <EOL> """<STR_LIT>""" <EOL> self . _do_if_else_condition ( t [ <NUM_LIT:1> ] not in self . cpp_namespace ) <EOL> def do_if ( self , t ) : <EOL> """<STR_LIT>""" <EOL> self . _do_if_else_condition ( self . eval_expression ( t ) ) <EOL> def do_elif ( self , t ) : <EOL> """<STR_LIT>""" <EOL> d = self . dispatch_table <EOL> if self . eval_expression ( t ) : <EOL> self . start_handling_includes ( ) <EOL> d [ '<STR_LIT>' ] = self . stop_handling_includes <EOL> d [ '<STR_LIT>' ] = self . stop_handling_includes <EOL> def do_else ( self , t ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def do_endif ( self , t ) : <EOL> """<STR_LIT>""" <EOL> self . restore ( ) <EOL> def do_define ( self , t ) : <EOL> """<STR_LIT>""" <EOL> _ , name , args , expansion = t <EOL> try : <EOL> expansion = int ( expansion ) <EOL> except ( TypeError , ValueError ) : <EOL> pass <EOL> if args : <EOL> evaluator = FunctionEvaluator ( name , args [ <NUM_LIT:1> : - <NUM_LIT:1> ] , expansion ) <EOL> self . cpp_namespace [ name ] = evaluator <EOL> else : <EOL> self . cpp_namespace [ name ] = expansion <EOL> def do_undef ( self , t ) : <EOL> """<STR_LIT>""" <EOL> try : del self . cpp_namespace [ t [ <NUM_LIT:1> ] ] <EOL> except KeyError : pass <EOL> def do_import ( self , t ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def do_include ( self , t ) : <EOL> """<STR_LIT>""" <EOL> t = self . resolve_include ( t ) <EOL> include_file = self . find_include_file ( t ) <EOL> if include_file : <EOL> self . result . append ( include_file ) <EOL> contents = self . read_file ( include_file ) <EOL> new_tuples = [ ( '<STR_LIT>' , include_file ) ] + self . tupleize ( contents ) + [ ( '<STR_LIT>' , self . current_file ) ] <EOL> self . tuples [ : ] = new_tuples + self . tuples <EOL> do_include_next = do_include <EOL> def resolve_include ( self , t ) : <EOL> """<STR_LIT>""" <EOL> s = t [ <NUM_LIT:1> ] <EOL> while not s [ <NUM_LIT:0> ] in '<STR_LIT>' : <EOL> try : <EOL> s = self . cpp_namespace [ s ] <EOL> except KeyError : <EOL> m = function_name . search ( s ) <EOL> s = self . cpp_namespace [ m . group ( <NUM_LIT:1> ) ] <EOL> if callable ( s ) : <EOL> args = function_arg_separator . split ( m . group ( <NUM_LIT:2> ) ) <EOL> s = s ( * args ) <EOL> if not s : <EOL> return None <EOL> return ( t [ <NUM_LIT:0> ] , s [ <NUM_LIT:0> ] , s [ <NUM_LIT:1> : - <NUM_LIT:1> ] ) <EOL> def all_include ( self , t ) : <EOL> """<STR_LIT:U+0020>""" <EOL> self . result . append ( self . resolve_include ( t ) ) <EOL> class DumbPreProcessor ( PreProcessor ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kw ) : <EOL> PreProcessor . __init__ ( self , * args , ** kw ) <EOL> d = self . default_table <EOL> for func in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> d [ func ] = d [ func ] = self . do_nothing <EOL> del __revision__ </s>
