<s> import re <EOL> from . ast import is_boolean , is_list <EOL> from . types import LispError , String <EOL> """<STR_LIT>""" <EOL> def parse ( source ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def remove_comments ( source ) : <EOL> """<STR_LIT>""" <EOL> return re . sub ( r"<STR_LIT>" , "<STR_LIT:\n>" , source ) <EOL> def find_matching_paren ( source , start = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> assert source [ start ] == '<STR_LIT:(>' <EOL> pos = start <EOL> open_brackets = <NUM_LIT:1> <EOL> while open_brackets > <NUM_LIT:0> : <EOL> pos += <NUM_LIT:1> <EOL> if len ( source ) == pos : <EOL> raise LispError ( "<STR_LIT>" % source [ start : ] ) <EOL> if source [ pos ] == '<STR_LIT:(>' : <EOL> open_brackets += <NUM_LIT:1> <EOL> if source [ pos ] == '<STR_LIT:)>' : <EOL> open_brackets -= <NUM_LIT:1> <EOL> return pos <EOL> def split_exps ( source ) : <EOL> """<STR_LIT>""" <EOL> rest = source . strip ( ) <EOL> exps = [ ] <EOL> while rest : <EOL> exp , rest = first_expression ( rest ) <EOL> exps . append ( exp ) <EOL> return exps <EOL> def first_expression ( source ) : <EOL> """<STR_LIT>""" <EOL> source = source . strip ( ) <EOL> if source [ <NUM_LIT:0> ] == "<STR_LIT:'>" : <EOL> exp , rest = first_expression ( source [ <NUM_LIT:1> : ] ) <EOL> return source [ <NUM_LIT:0> ] + exp , rest <EOL> elif source [ <NUM_LIT:0> ] == "<STR_LIT:(>" : <EOL> last = find_matching_paren ( source ) <EOL> return source [ : last + <NUM_LIT:1> ] , source [ last + <NUM_LIT:1> : ] <EOL> else : <EOL> match = re . match ( r"<STR_LIT>" , source ) <EOL> end = match . end ( ) <EOL> atom = source [ : end ] <EOL> return atom , source [ end : ] <EOL> def parse_multiple ( source ) : <EOL> """<STR_LIT>""" <EOL> source = remove_comments ( source ) <EOL> return [ parse ( exp ) for exp in split_exps ( source ) ] <EOL> def unparse ( ast ) : <EOL> """<STR_LIT>""" <EOL> if is_boolean ( ast ) : <EOL> return "<STR_LIT>" if ast else "<STR_LIT>" <EOL> elif is_list ( ast ) : <EOL> if len ( ast ) > <NUM_LIT:0> and ast [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> return "<STR_LIT>" % unparse ( ast [ <NUM_LIT:1> ] ) <EOL> else : <EOL> return "<STR_LIT>" % "<STR_LIT:U+0020>" . join ( [ unparse ( x ) for x in ast ] ) <EOL> else : <EOL> return str ( ast ) </s>
