<s> import re <EOL> class BeautifierOptions : <EOL> def __init__ ( self ) : <EOL> self . indent_size = <NUM_LIT:2> <EOL> self . indent_char = '<STR_LIT:U+0020>' <EOL> self . indent_base = '<STR_LIT>' <EOL> def default_options ( ) : <EOL> return BeautifierOptions ( ) <EOL> def beautify ( string , opts = default_options ( ) ) : <EOL> b = Beautifier ( ) <EOL> return b . beautify ( string , opts ) <EOL> class Beautifier : <EOL> def __init__ ( self , opts = default_options ( ) ) : <EOL> self . opts = opts <EOL> self . blank_state ( ) <EOL> def blank_state ( self ) : <EOL> self . wanted_newline = False <EOL> self . just_added_newline = False <EOL> self . indent_string = self . opts . indent_char * self . opts . indent_size <EOL> self . preindent_string = '<STR_LIT>' <EOL> self . last_word = '<STR_LIT>' <EOL> self . last_type = '<STR_LIT>' <EOL> self . last_text = '<STR_LIT>' <EOL> self . last_last_text = '<STR_LIT>' <EOL> self . input = None <EOL> self . output = [ ] <EOL> self . whitespace = [ "<STR_LIT:\n>" , "<STR_LIT:\r>" , "<STR_LIT:\t>" , "<STR_LIT:U+0020>" ] <EOL> self . wordchar = '<STR_LIT>' <EOL> self . digits = '<STR_LIT>' <EOL> self . punct = '<STR_LIT>' <EOL> self . punct = self . punct . split ( '<STR_LIT:U+0020>' ) <EOL> global parser_pos <EOL> parser_pos = <NUM_LIT:0> <EOL> def beautify ( self , s , opts = None ) : <EOL> if opts != None : <EOL> self . opts = opts <EOL> self . blank_state ( ) <EOL> while s and s [ <NUM_LIT:0> ] in [ '<STR_LIT:U+0020>' , '<STR_LIT:\t>' ] : <EOL> self . preindent_string += s [ <NUM_LIT:0> ] <EOL> s = s [ <NUM_LIT:1> : ] <EOL> self . input = s <EOL> parser_pos = <NUM_LIT:0> <EOL> while True : <EOL> token_text , token_type = self . get_next_token ( ) <EOL> if token_type == '<STR_LIT>' : <EOL> break <EOL> handlers = { <EOL> '<STR_LIT>' : self . handle_end_line , <EOL> '<STR_LIT>' : self . handle_inline_comment , <EOL> '<STR_LIT>' : self . handle_unknown , <EOL> } <EOL> handlers [ token_type ] ( token_text ) <EOL> self . last_last_text = self . last_text <EOL> self . last_type = token_type <EOL> self . last_text = token_text <EOL> sweet_code = self . preindent_string + re . sub ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' . join ( self . output ) ) <EOL> return sweet_code <EOL> def append ( self , s ) : <EOL> self . output . append ( s ) <EOL> def get_next_token ( self ) : <EOL> global parser_pos <EOL> if parser_pos >= len ( self . input ) : <EOL> return '<STR_LIT>' , '<STR_LIT>' <EOL> c = self . input [ parser_pos ] <EOL> parser_pos += <NUM_LIT:1> <EOL> if c == '<STR_LIT:\n>' : <EOL> return c , '<STR_LIT>' <EOL> if c == '<STR_LIT:#>' : <EOL> while self . input [ parser_pos ] != '<STR_LIT:\n>' : <EOL> c = c + self . input [ parser_pos ] <EOL> parser_pos += <NUM_LIT:1> <EOL> if parser_pos == len ( self . input ) : <EOL> break <EOL> return c , '<STR_LIT>' <EOL> if c != "<STR_LIT:\n>" : <EOL> while self . input [ parser_pos ] != "<STR_LIT:\n>" : <EOL> c = c + self . input [ parser_pos ] <EOL> parser_pos += <NUM_LIT:1> <EOL> if parser_pos == len ( self . input ) : <EOL> break <EOL> return c , '<STR_LIT>' <EOL> def handle_end_line ( self , token_text ) : <EOL> self . append ( token_text ) <EOL> def handle_unknown ( self , token_text ) : <EOL> self . append ( token_text ) <EOL> def handle_inline_comment ( self , token_text ) : <EOL> self . append ( token_text + '<STR_LIT>' ) </s>
