-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.py
67 lines (49 loc) · 1.15 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
import re
class Lexer:
def __init__(self, src):
self._src = src
def __next__(self):
tokens = [
r'\s*([()\\;])',
r'\s*(:)',
r'\s*([^()\\;:\s]+)',
]
for r in tokens:
m = re.match(r, self._src)
if m:
self._src = self._src[m.end():]
return m.group(1)
raise StopIteration
def __iter__(self):
return self
def parse(text):
lexer = Lexer(text)
return parse_exp(lexer, ';')
def parse_exp(lexer, term):
exps = []
try:
while True:
token = next(lexer)
if token == '\\':
internal = parse_lambda(lexer)
exps.append(internal)
elif token == '(':
internal = parse_exp(lexer, ')')
exps.append(internal)
elif token is term:
return exps[0] if len(exps) == 1 else exps
else:
exps.append(('symbol', token))
except StopIteration:
return exps[0] if len(exps) == 1 else exps
def parse_lambda(lexer):
token = next(lexer)
args = []
while token != ':':
args.append(token)
token = next(lexer)
body = parse_exp(lexer, ')')
return ('lambda', args, body)
a = parse('\\a:(\\t:t a) a')
# a = parse('x ((y z) a) (b c)')
print(a)