<s> """<STR_LIT>""" <EOL> __author__ = "<STR_LIT>" <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> import codecs <EOL> import os <EOL> import logging <EOL> import StringIO <EOL> import sys <EOL> from . import grammar , parse , token , tokenize , pgen <EOL> class Driver ( object ) : <EOL> def __init__ ( self , grammar , convert = None , logger = None ) : <EOL> self . grammar = grammar <EOL> if logger is None : <EOL> logger = logging . getLogger ( ) <EOL> self . logger = logger <EOL> self . convert = convert <EOL> def parse_tokens ( self , tokens , debug = False ) : <EOL> """<STR_LIT>""" <EOL> p = parse . Parser ( self . grammar , self . convert ) <EOL> p . setup ( ) <EOL> lineno = <NUM_LIT:1> <EOL> column = <NUM_LIT:0> <EOL> type = value = start = end = line_text = None <EOL> prefix = u"<STR_LIT>" <EOL> for quintuple in tokens : <EOL> type , value , start , end , line_text = quintuple <EOL> if start != ( lineno , column ) : <EOL> assert ( lineno , column ) <= start , ( ( lineno , column ) , start ) <EOL> s_lineno , s_column = start <EOL> if lineno < s_lineno : <EOL> prefix += "<STR_LIT:\n>" * ( s_lineno - lineno ) <EOL> lineno = s_lineno <EOL> column = <NUM_LIT:0> <EOL> if column < s_column : <EOL> prefix += line_text [ column : s_column ] <EOL> column = s_column <EOL> if type in ( tokenize . COMMENT , tokenize . NL ) : <EOL> prefix += value <EOL> lineno , column = end <EOL> if value . endswith ( "<STR_LIT:\n>" ) : <EOL> lineno += <NUM_LIT:1> <EOL> column = <NUM_LIT:0> <EOL> continue <EOL> if type == token . OP : <EOL> type = grammar . opmap [ value ] <EOL> if debug : <EOL> self . logger . debug ( "<STR_LIT>" , <EOL> token . tok_name [ type ] , value , prefix ) <EOL> if p . addtoken ( type , value , ( prefix , start ) ) : <EOL> if debug : <EOL> self . logger . debug ( "<STR_LIT>" ) <EOL> break <EOL> prefix = "<STR_LIT>" <EOL> lineno , column = end <EOL> if value . endswith ( "<STR_LIT:\n>" ) : <EOL> lineno += <NUM_LIT:1> <EOL> column = <NUM_LIT:0> <EOL> else : <EOL> raise parse . ParseError ( "<STR_LIT>" , <EOL> type , value , ( prefix , start ) ) <EOL> return p . rootnode <EOL> def parse_stream_raw ( self , stream , debug = False ) : <EOL> """<STR_LIT>""" <EOL> tokens = tokenize . generate_tokens ( stream . readline ) <EOL> return self . parse_tokens ( tokens , debug ) <EOL> def parse_stream ( self , stream , debug = False ) : <EOL> """<STR_LIT>""" <EOL> return self . parse_stream_raw ( stream , debug ) <EOL> def parse_file ( self , filename , encoding = None , debug = False ) : <EOL> """<STR_LIT>""" <EOL> stream = codecs . open ( filename , "<STR_LIT:r>" , encoding ) <EOL> try : <EOL> return self . parse_stream ( stream , debug ) <EOL> finally : <EOL> stream . close ( ) <EOL> def parse_string ( self , text , debug = False ) : <EOL> """<STR_LIT>""" <EOL> tokens = tokenize . generate_tokens ( StringIO . StringIO ( text ) . readline ) <EOL> return self . parse_tokens ( tokens , debug ) <EOL> def load_grammar ( gt = "<STR_LIT>" , gp = None , <EOL> save = True , force = False , logger = None ) : <EOL> """<STR_LIT>""" <EOL> if logger is None : <EOL> logger = logging . getLogger ( ) <EOL> if gp is None : <EOL> head , tail = os . path . splitext ( gt ) <EOL> if tail == "<STR_LIT>" : <EOL> tail = "<STR_LIT>" <EOL> gp = head + tail + "<STR_LIT:.>" . join ( map ( str , sys . version_info ) ) + "<STR_LIT>" <EOL> if force or not _newer ( gp , gt ) : <EOL> logger . info ( "<STR_LIT>" , gt ) <EOL> g = pgen . generate_grammar ( gt ) <EOL> if save : <EOL> logger . info ( "<STR_LIT>" , gp ) <EOL> try : <EOL> g . dump ( gp ) <EOL> except IOError , e : <EOL> logger . info ( "<STR_LIT>" + str ( e ) ) <EOL> else : <EOL> g = grammar . Grammar ( ) <EOL> g . load ( gp ) <EOL> return g <EOL> def _newer ( a , b ) : <EOL> """<STR_LIT>""" <EOL> if not os . path . exists ( a ) : <EOL> return False <EOL> if not os . path . exists ( b ) : <EOL> return True <EOL> return os . path . getmtime ( a ) >= os . path . getmtime ( b ) <EOL> def main ( * args ) : <EOL> """<STR_LIT>""" <EOL> if not args : <EOL> args = sys . argv [ <NUM_LIT:1> : ] <EOL> logging . basicConfig ( level = logging . INFO , stream = sys . stdout , <EOL> format = '<STR_LIT>' ) <EOL> for gt in args : <EOL> load_grammar ( gt , save = True , force = True ) <EOL> return True <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> sys . exit ( int ( not main ( ) ) ) </s>
