<s> """<STR_LIT>""" <EOL> import sys <EOL> import re <EOL> COMMENT_RE = re . compile ( r'<STR_LIT>' ) <EOL> QUOTE_RE_STR = r'<STR_LIT>' <EOL> QUOTE_RE = re . compile ( QUOTE_RE_STR ) <EOL> def comment_replace ( matchobj ) : <EOL> return matchobj . group ( <NUM_LIT:1> ) + matchobj . group ( <NUM_LIT:2> ) + '<STR_LIT:#>' * len ( matchobj . group ( <NUM_LIT:3> ) ) <EOL> def mask_comments ( input ) : <EOL> """<STR_LIT>""" <EOL> search_re = re . compile ( r'<STR_LIT>' ) <EOL> return [ search_re . sub ( comment_replace , line ) for line in input ] <EOL> def quote_replace ( matchobj ) : <EOL> return "<STR_LIT>" % ( matchobj . group ( <NUM_LIT:1> ) , <EOL> matchobj . group ( <NUM_LIT:2> ) , <EOL> '<STR_LIT:x>' * len ( matchobj . group ( <NUM_LIT:3> ) ) , <EOL> matchobj . group ( <NUM_LIT:2> ) ) <EOL> def mask_quotes ( input ) : <EOL> """<STR_LIT>""" <EOL> search_re = re . compile ( r'<STR_LIT>' + QUOTE_RE_STR ) <EOL> return [ search_re . sub ( quote_replace , line ) for line in input ] <EOL> def do_split ( input , masked_input , search_re ) : <EOL> output = [ ] <EOL> mask_output = [ ] <EOL> for ( line , masked_line ) in zip ( input , masked_input ) : <EOL> m = search_re . match ( masked_line ) <EOL> while m : <EOL> split = len ( m . group ( <NUM_LIT:1> ) ) <EOL> line = line [ : split ] + r'<STR_LIT:\n>' + line [ split : ] <EOL> masked_line = masked_line [ : split ] + r'<STR_LIT:\n>' + masked_line [ split : ] <EOL> m = search_re . match ( masked_line ) <EOL> output . extend ( line . split ( r'<STR_LIT:\n>' ) ) <EOL> mask_output . extend ( masked_line . split ( r'<STR_LIT:\n>' ) ) <EOL> return ( output , mask_output ) <EOL> def split_double_braces ( input ) : <EOL> """<STR_LIT>""" <EOL> double_open_brace_re = re . compile ( r'<STR_LIT>' ) <EOL> double_close_brace_re = re . compile ( r'<STR_LIT>' ) <EOL> masked_input = mask_quotes ( input ) <EOL> masked_input = mask_comments ( masked_input ) <EOL> ( output , mask_output ) = do_split ( input , masked_input , double_open_brace_re ) <EOL> ( output , mask_output ) = do_split ( output , mask_output , double_close_brace_re ) <EOL> return output <EOL> def count_braces ( line ) : <EOL> """<STR_LIT>""" <EOL> open_braces = [ '<STR_LIT:[>' , '<STR_LIT:(>' , '<STR_LIT:{>' ] <EOL> close_braces = [ '<STR_LIT:]>' , '<STR_LIT:)>' , '<STR_LIT:}>' ] <EOL> closing_prefix_re = re . compile ( r'<STR_LIT>' ) <EOL> cnt = <NUM_LIT:0> <EOL> stripline = COMMENT_RE . sub ( r'<STR_LIT>' , line ) <EOL> stripline = QUOTE_RE . sub ( r"<STR_LIT>" , stripline ) <EOL> for char in stripline : <EOL> for brace in open_braces : <EOL> if char == brace : <EOL> cnt += <NUM_LIT:1> <EOL> for brace in close_braces : <EOL> if char == brace : <EOL> cnt -= <NUM_LIT:1> <EOL> after = False <EOL> if cnt > <NUM_LIT:0> : <EOL> after = True <EOL> if cnt < <NUM_LIT:0> and closing_prefix_re . match ( stripline ) : <EOL> after = True <EOL> return ( cnt , after ) <EOL> def prettyprint_input ( lines ) : <EOL> """<STR_LIT>""" <EOL> indent = <NUM_LIT:0> <EOL> basic_offset = <NUM_LIT:2> <EOL> last_line = "<STR_LIT>" <EOL> for line in lines : <EOL> if COMMENT_RE . match ( line ) : <EOL> print line <EOL> else : <EOL> line = line . strip ( '<STR_LIT>' ) <EOL> if len ( line ) > <NUM_LIT:0> : <EOL> ( brace_diff , after ) = count_braces ( line ) <EOL> if brace_diff != <NUM_LIT:0> : <EOL> if after : <EOL> print "<STR_LIT:U+0020>" * ( basic_offset * indent ) + line <EOL> indent += brace_diff <EOL> else : <EOL> indent += brace_diff <EOL> print "<STR_LIT:U+0020>" * ( basic_offset * indent ) + line <EOL> else : <EOL> print "<STR_LIT:U+0020>" * ( basic_offset * indent ) + line <EOL> else : <EOL> print "<STR_LIT>" <EOL> last_line = line <EOL> def main ( ) : <EOL> if len ( sys . argv ) > <NUM_LIT:1> : <EOL> data = open ( sys . argv [ <NUM_LIT:1> ] ) . read ( ) . splitlines ( ) <EOL> else : <EOL> data = sys . stdin . read ( ) . splitlines ( ) <EOL> lines = split_double_braces ( data ) <EOL> prettyprint_input ( lines ) <EOL> return <NUM_LIT:0> <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> sys . exit ( main ( ) ) </s>
