/
parse.py
198 lines (165 loc) · 5.67 KB
/
parse.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
import re
import string
from datatypes import nil, true, false, mksym, cons, from_list, LispString, LispInteger
class iterator_undo(object): # undo-able iterator wrapper
def __init__(self, iterable):
super(iterator_undo, self).__init__()
self.iterator = iter(iterable)
self.stack = []
def __iter__(self):
return self
def next(self):
if self.stack:
# print "next: stack", self.stack
return self.stack.pop()
return self.iterator.next() # Raises StopIteration eventually
def undo(self, item):
# print "undo:", item
self.stack.append(item)
def peek(self):
item = self.next()
self.undo(item)
return item
def skip(self, num=1):
for i in range(num):
x = self.next()
def test_iterator_undo():
it = iterator_undo([1,2,3,4,5])
assert list(it) == [1,2,3,4,5]
it = iterator_undo([1,2,3,4,5])
assert 1 == it.next()
assert it.next() == 2
assert it.peek() == 3
assert it.peek() == 3
assert it.next() == 3
assert it.next() == 4
assert it.next() == 5
test_iterator_undo()
# -----------------------------------------------------------------------------
re_num = re.compile(r"^[-+]?\d+$")
normalchars = set(string.ascii_letters + string.digits + "-_<>%=!^&?*+/")
def isnumber(str):
return re_num.match(str) != None
def test_isnumber():
print "testing: isnumber"
assert isnumber("0")
assert isnumber("1")
assert isnumber("10")
assert isnumber("0001")
assert isnumber("01234567890")
assert isnumber("+0")
assert isnumber("+1")
assert isnumber("+10")
assert isnumber("+0001")
assert isnumber("+01234567890")
assert isnumber("-0")
assert isnumber("-1")
assert isnumber("-10")
assert isnumber("-0001")
assert isnumber("-01234567890")
assert not isnumber("")
assert not isnumber("--1")
assert not isnumber("_5")
assert not isnumber("asdf")
assert not isnumber("5a")
assert not isnumber("a5")
assert not isnumber("5a5")
assert not isnumber("a5a")
assert not isnumber("+a1")
assert not isnumber("+9a")
assert not isnumber("-9a")
assert not isnumber("-a3")
assert not isnumber("-10+")
test_isnumber()
# -----------------------------------------------------------------------------
quote = mksym("quote")
quasiquote = mksym("quasiquote")
unquote = mksym("unquote")
def valid_symbol_name(text):
return set(text).issubset(normalchars)
def inner_parse(tokens):
"""take the token list from the lexer and make it into an object"""
tok = tokens.next()
assert len(tok) != 0, "zero sized token"
assert tok != ".", "found '.' outside of pair"
assert tok != ")", "found ')' mismatched bracket"
if tok == "'":
return from_list([quote, inner_parse(tokens), nil])
elif tok[0] == '`':
return from_list([quasiquote, inner_parse(tokens), nil])
elif tok[0] == ',':
return from_list([unquote, inner_parse(tokens), nil])
elif tok[0] == '"':
return LispString(tok[1:-1])
elif tok == "(":
tok = tokens.next()
if tok == ")": return nil
tokens.undo(tok)
tok_list = [inner_parse(tokens)]
while (tokens.peek() not in [")", "."]):
tok_list.append(inner_parse(tokens))
tok = tokens.next()
if tok == ".":
tok_list.append(inner_parse(tokens))
assert tokens.next() == ")", "expected one sexp after a fullstop"
else:
tok_list.append(nil)
assert tok == ")", "missing close bracket of pair"
return from_list(tok_list)
elif isnumber(tok):
return LispInteger(int(tok))
else:
# we have a symbol
assert valid_symbol_name(tok), "invalid atom in symbol '%s'" % tok
if tok == "nil": return nil
if tok == "true": return true
if tok == "false": return false
return mksym(tok)
def kill_comments(iterable):
for x in iterable:
if x[0] != ";":
yield x
def parse(tokens):
tokens = iterator_undo(kill_comments(tokens))
while (True):
yield inner_parse(tokens)
def test():
print "testing: parse"
from lex import tokenize
to_process = [
("()" , "nil"),
("'()" , "( quote nil )"),
("'a" , "( quote a )"),
("'( a b)" , "( quote ( a b ) )"),
("()" , "nil"),
("(a)" , "( a )"),
("('a)" , "( ( quote a ) )"),
("(a b)" , "( a b )"),
("(a b c)" , "( a b c )"),
("(a b c d e f (g) h (i j) k (l m n) o (p q (r s) t) u (v (w (x (y (z))))))",
"( a b c d e f ( g ) h ( i j ) k ( l m n ) o ( p q ( r s ) t ) u ( v ( w ( x ( y ( z ) ) ) ) ) )"),
("(a . b)" , "( a . b )"),
("(a b . c)" , "( a b . c )"),
('"asdf"' , '"asdf"'),
('("asdf")' , '( "asdf" )'),
('(4"asdf")' , '( 4 "asdf" )'),
('(4."asdf")' , '( 4 . "asdf" )'),
('"as df"' , '"as df"'),
('as ;' , 'as'),
('as ;abcdef12345' , 'as'),
('(as ";")' , '( as ";" )'),
('(as "; hello")' , '( as "; hello" )'),
('as ;;;;;;' , 'as'),
('as ; !" %$ 86842 "$^P~>?:@:~' , 'as'),
("(() (()()))", "( nil ( nil nil ) )")]
for test, expected in to_process:
tokens = tokenize([test])
result = list(parse(tokens))
assert len(result) == 1
res = str(result[0])
if res != expected:
print "Mismatch"
print "test :", test
print "result :", res
print "expected:", expected
test()