-
Notifications
You must be signed in to change notification settings - Fork 0
/
sexpr.py
89 lines (66 loc) · 1.49 KB
/
sexpr.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
# most awfullest s-expr parser in the history of mankind.
# do not use!!
# note: this requires the top-level expression to be a list
def load(f):
# we read the entire thing into a string because we need to look ahead 1 byte
# (like that's any excuse lol)
s = f.read()
p = 0
# skip white space
while s[p].isspace(): p += 1
list, p = parse_list(s, p)
# skip white space
while p < len(s) and s[p].isspace(): p += 1
assert p == len(s)
return list
def parse_atom(s, p):
if s[p] == '"':
p += 1
end = s.find('"', p)
z = s[p:end]
p = end + 1
else:
assert s[p].isalnum() or s[p] == '-'
z = s[p]
p += 1
while s[p].isalnum() or s[p] == '-':
z += s[p]
p += 1
#print('ATOM', z)
return z, p
# note: this requires the first item of each expression be an atom
def parse_list(s, p):
# expect (
assert s[p] == '('
p += 1
# skip white space
while s[p].isspace(): p += 1
# expect atom
atom, p = parse_atom(s, p)
self = [atom]
while True:
# skip white space
while s[p].isspace(): p += 1
# accept ) or atom or nested list
if s[p] == ')':
p += 1
break
if s[p] == '(':
list, p = parse_list(s, p)
self += [list]
else:
# expect atom
atom, p = parse_atom(s, p)
self += [atom]
#print('LIST', self)
return self, p
# helper to convert a loaded list to a dictionary
def as_dict(list):
dict = {}
for entry in list:
assert isinstance(entry[0], str)
if len(entry) > 2:
dict[entry[0]] = entry[1:]
else:
dict[entry[0]] = entry[1]
return dict