forked from sublimescala/sublime-ensime
-
Notifications
You must be signed in to change notification settings - Fork 2
/
sexp.py
218 lines (193 loc) · 5.52 KB
/
sexp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import re
class Keyword:
def __init__(self, s):
self.val = s
def __repr__(self):
return self.val
def __eq__(self, k):
return type(k) == type(self) and self.val == k.val
class Symbol:
def __init__(self, s):
self.val = s
def __repr__(self):
return self.val
def __eq__(self, k):
return type(k) == type(self) and self.val == k.val
def sexp_to_key_map(sexp):
try:
key_type = type(key(":key"))
result = {}
for i in xrange(0, len(sexp), 2):
k,val = sexp[i],sexp[i+1]
if type(k) == key_type:
result[str(k)] = val
return result
except:
raise Exception("not a sexp: %s" % sexp)
def key(s):
return Keyword(s)
def sym(s):
return Symbol(s)
def read(s):
"Read a sexp expression from a string."
return read_form(s)[0]
def read_relaxed(s):
"""Read a sexp expression from a string.
Unlike `read` this function allows ; comments
and is more forgiving w.r.t whitespaces."""
lines = s.splitlines()
lines = map(lambda line: line.strip(), lines)
lines = filter(lambda line: line, lines)
lines = filter(lambda line: not line.startswith(";"), lines)
s = '\n'.join(lines)
return read_form(s)[0]
def read_form(str):
"Read a form."
if len(str) == 0:
raise SyntaxError('unexpected EOF while reading form')
ch = str[0]
if ch.isspace():
raise SyntaxError('unexpected whitespace while reading form')
elif ch == '(':
return read_list(str)
elif ch == '"':
return read_string(str)
elif ch == ':':
return read_keyword(str)
elif ch.isdigit() or ch == "-":
return read_int(str)
elif ch.isalpha():
return read_symbol(str)
else:
raise SyntaxError('unexpected character in read_form: ' + ch)
def read_list(str):
"Read a list from a string."
if len(str) == 0:
raise SyntaxError('unexpected EOF while reading list')
if str[0] != '(':
raise SyntaxError('expected ( as first char of list: ' + str)
str = str[1:]
lst = []
while(len(str) > 0):
ch = str[0]
if ch.isspace():
str = str[1:]
continue
elif ch == ')':
return (lst,str[1:])
else:
val,remain = read_form(str)
lst.append(val)
str = remain
raise SyntaxError('EOF while reading list')
def read_string(str):
"Read a string."
if len(str) == 0:
raise SyntaxError('unexpected EOF while reading string')
if str[0] != '"':
raise SyntaxError('expected ( as first char of string: ' + str)
str = str[1:]
s = ""
escaped = False
while(len(str) > 0):
ch = str[0]
if ch == '"' and not escaped:
return (s.replace("\\\\", "\\"),str[1:])
elif escaped:
escaped = False
elif ch == "\\":
escaped = True
s = s + ch
str = str[1:]
raise SyntaxError('EOF while reading string')
def read_keyword(str):
"Read a keyword."
if len(str) == 0:
raise SyntaxError('unexpected EOF while reading keyword')
if str[0] != ':':
raise SyntaxError('expected : as first char of keyword')
str = str[1:]
s = ""
while(len(str) > 0):
ch = str[0]
if not (ch.isalpha() or ch.isdigit() or ch == '-'):
return (Keyword(":" + s),str)
else:
s = s + ch
str = str[1:]
if len(s) > 1:
return (Keyword(":" + s),str)
else:
raise SyntaxError('EOF while reading keyword')
def read_symbol(str):
"Read a symbol."
if len(str) == 0:
raise SyntaxError('unexpected EOF while reading symbol')
if not str[0].isalpha():
raise SyntaxError('expected alpha char as first char of symbol')
s = ""
while(len(str) > 0):
ch = str[0]
if not (ch.isalpha() or ch.isdigit() or ch == '-' or ch == ":"):
if s == "t":
return (True,str)
elif s == "nil":
return (False,str)
else:
return (Symbol(s),str)
else:
s = s + ch
str = str[1:]
if len(s) > 0:
return (Symbol(s),str)
else:
raise SyntaxError('EOF while reading symbol')
def read_int(str):
"Read an integer."
if len(str) == 0:
raise SyntaxError('unexpected EOF while reading int')
s = ""
while(len(str) > 0):
ch = str[0]
if not (ch.isdigit() or ch == '-'):
return (int(s),str)
else:
s = s + ch
str = str[1:]
if len(s) > 0:
return (int(s),str)
else:
raise SyntaxError('EOF while reading int')
def to_string(exp):
"Convert a Python object back into a Lisp-readable string."
if isinstance(exp, list):
return '(' + ' '.join(map(to_string, exp)) + ')'
else:
return atom_to_str(exp)
def atom_to_str(exp):
if exp and (type(exp) == type(True)):
return "t"
elif (not exp) and (type(exp) == type(False)):
return "nil"
elif isinstance(exp, basestring):
return "\"" + exp.replace("\\", "\\\\").replace("\"", "\\\"") + "\""
else:
return str(exp)
def repl(prompt='lis.py> '):
"A prompt-read-eval-print loop."
while True:
val = eval(parse(raw_input(prompt)))
if val is not None: print to_string(val)
if __name__ == "__main__":
print(str(read("nil")))
print(str(read("(\"a b c\")")))
print(str(read("(a b c)")))
print(str(read("(:notes (:notes ((:file \"/Users/aemon/projects/cutey_ape/googleclient/experimental/qt_ape/src/apeoutlinemodel.cpp\" :line 37 :col 100 :beg nil :end nil :severity error :msg \"expected ')'\"))))")))
print(str(read("-4342323")))
print(str(read(":dude")))
print(str(read("ape")))
print(str(read("((((((nil))))))")))
print(str(read("\"hello \\face\"")))
print(str(read("\"hello \\fa\\\"ce\"")))
print(str(read("(:swank-rpc (swank:connection-info) 1)")))
print(to_string([7147L, [['+', 6227, u'a\n an'], ['-', 7137, 7138]]]))