<s> """<STR_LIT>""" <EOL> import re , sys <EOL> import TextTools <EOL> entityRE = re . compile ( '<STR_LIT>' ) <EOL> def parse ( text , pos = <NUM_LIT:0> , endpos = None ) : <EOL> pos = <NUM_LIT:0> <EOL> if endpos is None : <EOL> endpos = len ( text ) <EOL> d = { } <EOL> while <NUM_LIT:1> : <EOL> m = entityRE . search ( text , pos , endpos ) <EOL> if not m : <EOL> break <EOL> name , charcode , comment = m . groups ( ) <EOL> d [ name ] = charcode , comment <EOL> pos = m . end ( ) <EOL> return d <EOL> def writefile ( f , defs ) : <EOL> f . write ( "<STR_LIT>" ) <EOL> items = defs . items ( ) <EOL> items . sort ( ) <EOL> for name , ( charcode , comment ) in items : <EOL> if charcode [ : <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> code = int ( charcode [ <NUM_LIT:2> : - <NUM_LIT:1> ] ) <EOL> if code < <NUM_LIT> : <EOL> charcode = "<STR_LIT>" % code <EOL> else : <EOL> charcode = repr ( charcode ) <EOL> else : <EOL> charcode = repr ( charcode ) <EOL> comment = TextTools . collapse ( comment ) <EOL> f . write ( "<STR_LIT>" % ( name , charcode , comment ) ) <EOL> f . write ( '<STR_LIT>' ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> if len ( sys . argv ) > <NUM_LIT:1> : <EOL> infile = open ( sys . argv [ <NUM_LIT:1> ] ) <EOL> else : <EOL> infile = sys . stdin <EOL> if len ( sys . argv ) > <NUM_LIT:2> : <EOL> outfile = open ( sys . argv [ <NUM_LIT:2> ] , '<STR_LIT:w>' ) <EOL> else : <EOL> outfile = sys . stdout <EOL> text = infile . read ( ) <EOL> defs = parse ( text ) <EOL> writefile ( outfile , defs ) </s>
