<s> class Keyword : <EOL> def __init__ ( self , s ) : <EOL> self . val = s <EOL> def __repr__ ( self ) : <EOL> return self . val <EOL> def __eq__ ( self , k ) : <EOL> return type ( k ) == type ( self ) and self . val == k . val <EOL> class Symbol : <EOL> def __init__ ( self , s ) : <EOL> self . val = s <EOL> def __repr__ ( self ) : <EOL> return self . val <EOL> def __eq__ ( self , k ) : <EOL> return type ( k ) == type ( self ) and self . val == k . val <EOL> def sexp_to_key_map ( sexp ) : <EOL> try : <EOL> key_type = type ( key ( "<STR_LIT>" ) ) <EOL> result = { } <EOL> for i in range ( <NUM_LIT:0> , len ( sexp ) , <NUM_LIT:2> ) : <EOL> k , val = sexp [ i ] , sexp [ i + <NUM_LIT:1> ] <EOL> if type ( k ) == key_type : <EOL> result [ str ( k ) ] = val <EOL> return result <EOL> except : <EOL> raise Exception ( "<STR_LIT>" % sexp ) <EOL> def key ( s ) : <EOL> return Keyword ( s ) <EOL> def sym ( s ) : <EOL> return Symbol ( s ) <EOL> def read ( s ) : <EOL> """<STR_LIT>""" <EOL> return read_form ( s ) [ <NUM_LIT:0> ] <EOL> def read_relaxed ( s ) : <EOL> """<STR_LIT>""" <EOL> lines = s . splitlines ( ) <EOL> lines = [ line . strip ( ) for line in lines ] <EOL> lines = [ line for line in lines if line ] <EOL> lines = [ line for line in lines if not line . startswith ( "<STR_LIT:;>" ) ] <EOL> s = '<STR_LIT:\n>' . join ( lines ) <EOL> return read_form ( s ) [ <NUM_LIT:0> ] <EOL> def read_form ( form ) : <EOL> """<STR_LIT>""" <EOL> if len ( form ) == <NUM_LIT:0> : <EOL> raise SyntaxError ( '<STR_LIT>' ) <EOL> ch = form [ <NUM_LIT:0> ] <EOL> if ch . isspace ( ) : <EOL> raise SyntaxError ( '<STR_LIT>' ) <EOL> elif ch == '<STR_LIT:(>' : <EOL> return read_list ( form ) <EOL> elif ch == '<STR_LIT:">' : <EOL> return read_string ( form ) <EOL> elif ch == '<STR_LIT::>' : <EOL> return read_keyword ( form ) <EOL> elif ch . isdigit ( ) or ch == "<STR_LIT:->" : <EOL> return read_int ( form ) <EOL> elif ch . isalpha ( ) : <EOL> return read_symbol ( form ) <EOL> elif ch == '<STR_LIT>' : <EOL> return read_atom ( form ) <EOL> else : <EOL> raise SyntaxError ( '<STR_LIT>' + ch ) <EOL> def read_list ( form ) : <EOL> """<STR_LIT>""" <EOL> if len ( form ) == <NUM_LIT:0> : <EOL> raise SyntaxError ( '<STR_LIT>' ) <EOL> if form [ <NUM_LIT:0> ] != '<STR_LIT:(>' : <EOL> raise SyntaxError ( '<STR_LIT>' + form ) <EOL> form = form [ <NUM_LIT:1> : ] <EOL> lst = [ ] <EOL> while len ( form ) > <NUM_LIT:0> : <EOL> ch = form [ <NUM_LIT:0> ] <EOL> if ch . isspace ( ) : <EOL> form = form [ <NUM_LIT:1> : ] <EOL> continue <EOL> elif ch == '<STR_LIT:)>' : <EOL> return ( lst , form [ <NUM_LIT:1> : ] ) <EOL> else : <EOL> val , remain = read_form ( form ) <EOL> lst . append ( val ) <EOL> form = remain <EOL> raise SyntaxError ( '<STR_LIT>' ) <EOL> def read_string ( form ) : <EOL> """<STR_LIT>""" <EOL> if len ( form ) == <NUM_LIT:0> : <EOL> raise SyntaxError ( '<STR_LIT>' ) <EOL> if form [ <NUM_LIT:0> ] != '<STR_LIT:">' : <EOL> raise SyntaxError ( '<STR_LIT>' + form ) <EOL> form = form [ <NUM_LIT:1> : ] <EOL> s = "<STR_LIT>" <EOL> escaped = False <EOL> while len ( form ) > <NUM_LIT:0> : <EOL> ch = form [ <NUM_LIT:0> ] <EOL> if ch == '<STR_LIT:">' and not escaped : <EOL> return ( s , form [ <NUM_LIT:1> : ] ) <EOL> elif escaped : <EOL> escaped = False <EOL> s = s [ : - <NUM_LIT:1> ] <EOL> elif ch == "<STR_LIT:\\>" : <EOL> escaped = True <EOL> s = s + ch <EOL> form = form [ <NUM_LIT:1> : ] <EOL> raise SyntaxError ( '<STR_LIT>' ) <EOL> def read_atom ( form ) : <EOL> """<STR_LIT>""" <EOL> if len ( form ) == <NUM_LIT:0> : <EOL> raise SyntaxError ( '<STR_LIT>' ) <EOL> if form [ <NUM_LIT:0> ] != '<STR_LIT>' : <EOL> raise SyntaxError ( '<STR_LIT>' + form ) <EOL> form = form [ <NUM_LIT:1> : ] <EOL> s = "<STR_LIT>" <EOL> while len ( form ) > <NUM_LIT:0> : <EOL> ch = form [ <NUM_LIT:0> ] <EOL> if ch . isspace ( ) : <EOL> return ( s , form [ <NUM_LIT:1> : ] ) <EOL> s = s + ch <EOL> form = form [ <NUM_LIT:1> : ] <EOL> raise SyntaxError ( '<STR_LIT>' ) <EOL> def read_keyword ( form ) : <EOL> """<STR_LIT>""" <EOL> if len ( form ) == <NUM_LIT:0> : <EOL> raise SyntaxError ( '<STR_LIT>' ) <EOL> if form [ <NUM_LIT:0> ] != '<STR_LIT::>' : <EOL> raise SyntaxError ( '<STR_LIT>' ) <EOL> form = form [ <NUM_LIT:1> : ] <EOL> s = "<STR_LIT>" <EOL> while len ( form ) > <NUM_LIT:0> : <EOL> ch = form [ <NUM_LIT:0> ] <EOL> if not ( ch . isalpha ( ) or ch . isdigit ( ) or ch == '<STR_LIT:->' or ch == '<STR_LIT:.>' ) : <EOL> return ( Keyword ( "<STR_LIT::>" + s ) , form ) <EOL> else : <EOL> s = s + ch <EOL> form = form [ <NUM_LIT:1> : ] <EOL> if len ( s ) > <NUM_LIT:1> : <EOL> return ( Keyword ( "<STR_LIT::>" + s ) , form ) <EOL> else : <EOL> raise SyntaxError ( '<STR_LIT>' ) <EOL> def read_symbol ( form ) : <EOL> """<STR_LIT>""" <EOL> if len ( form ) == <NUM_LIT:0> : <EOL> raise SyntaxError ( '<STR_LIT>' ) <EOL> if not form [ <NUM_LIT:0> ] . isalpha ( ) : <EOL> raise SyntaxError ( '<STR_LIT>' ) <EOL> s = "<STR_LIT>" <EOL> while len ( form ) > <NUM_LIT:0> : <EOL> ch = form [ <NUM_LIT:0> ] <EOL> if not ( ch . isalpha ( ) or ch . isdigit ( ) or ch == '<STR_LIT:->' or ch == "<STR_LIT::>" ) : <EOL> if s == "<STR_LIT:t>" : <EOL> return ( True , form ) <EOL> elif s == "<STR_LIT>" : <EOL> return ( False , form ) <EOL> else : <EOL> return ( Symbol ( s ) , form ) <EOL> else : <EOL> s = s + ch <EOL> form = form [ <NUM_LIT:1> : ] <EOL> if len ( s ) > <NUM_LIT:0> : <EOL> return ( Symbol ( s ) , form ) <EOL> else : <EOL> raise SyntaxError ( '<STR_LIT>' ) <EOL> def read_int ( form ) : <EOL> """<STR_LIT>""" <EOL> if len ( form ) == <NUM_LIT:0> : <EOL> raise SyntaxError ( '<STR_LIT>' ) <EOL> s = "<STR_LIT>" <EOL> while ( len ( form ) > <NUM_LIT:0> ) : <EOL> ch = form [ <NUM_LIT:0> ] <EOL> if not ( ch . isdigit ( ) or ch == '<STR_LIT:->' ) : <EOL> return ( int ( s ) , form ) <EOL> else : <EOL> s = s + ch <EOL> form = form [ <NUM_LIT:1> : ] <EOL> if len ( s ) > <NUM_LIT:0> : <EOL> return ( int ( s ) , form ) <EOL> else : <EOL> raise SyntaxError ( '<STR_LIT>' ) <EOL> def to_string ( exp ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( exp , list ) : <EOL> return '<STR_LIT:(>' + '<STR_LIT:U+0020>' . join ( map ( to_string , exp ) ) + '<STR_LIT:)>' <EOL> else : <EOL> return atom_to_str ( exp ) <EOL> def atom_to_str ( exp ) : <EOL> if exp and ( type ( exp ) == type ( True ) ) : <EOL> return "<STR_LIT:t>" <EOL> elif ( not exp ) and ( type ( exp ) == type ( False ) ) : <EOL> return "<STR_LIT>" <EOL> elif type ( exp ) == Symbol : <EOL> return exp . val <EOL> elif isinstance ( exp , str ) : <EOL> return "<STR_LIT>" + exp . replace ( "<STR_LIT:\\>" , "<STR_LIT>" ) . replace ( "<STR_LIT>" , "<STR_LIT>" ) + "<STR_LIT>" <EOL> else : <EOL> return str ( exp ) </s>
