-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parse.py
127 lines (96 loc) · 2.83 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
import Prefix
class ParseError(Exception):
pass
def _getInt(s):
try:
ret = int(s)
except ValueError:
return None
return ret
def _getFloat(s):
try:
ret = float(s)
except ValueError:
return None
return ret
def _getString(s):
return KlipStr(bytes(s[1:-1], 'utf-8').decode('unicode_escape'))
class Parser(object):
def inc(self, fail):
try:
self.cur = next(self.it)
except StopIteration:
if fail:
raise ParseError(fail)
else:
self.cur = None
def parseProclitic(self, procliticName, realName):
fname = self.cur.fname
line = self.cur.line
self.inc('Dangling %s. %s line %d' % (procliticName, fname, line))
temp = self.parseTerminal()
return KlipList([Sym(realName), temp])
def parseTerminal(self):
if self.cur.value == '(':
complaintParms = self.cur.fname, self.cur.line
temp = KlipList()
self.inc('Unmatched open parenthesis. %s line %d' % complaintParms)
while True:
if self.cur.value == ')':
return temp
temp.append(self.parseTerminal())
self.inc('Unmatched open parenthesis. %s line %d' % complaintParms)
if self.cur.value == '{':
complaintParms = self.cur.fname, self.cur.line
ret = KlipHash()
self.inc('Unmatched open brace. %s line %d' % complaintParms)
while True:
if self.cur.value == '}':
return ret
k = self.parseTerminal()
if k == nil:
k.doRaise(ParseError, "nil can't be a key in a hash.")
self.inc('Unmatched open brace. %s line %d' % complaintParms)
if self.cur.value == '}':
raise ParseError('Key with no value in literal hash. %s line %d' % complaintParms)
v = self.parseTerminal()
if v == nil:
v.doRaise(ParseError, "hashes can't contain nil.")
self.inc('Unmatched open brace. %s line %d' % complaintParms)
ret[k] = v
if self.cur.value == '~':
return self.parseProclitic('~', 'quote')
if self.cur.value == '`':
return self.parseProclitic('`', 'quasiquote')
if self.cur.value == ',':
return self.parseProclitic(',', 'unquote')
if self.cur.value == ';':
return self.parseProclitic(';', 'unquotesplicing')
if self.cur.value.startswith("'") or self.cur.value.startswith('"'):
return _getString(self.cur.value)
i = _getInt(self.cur.value)
if i != None:
return i
f = _getFloat(self.cur.value)
if f != None:
return f
return Sym(self.cur.value)
def __call__(self, tokens, fname):
self.fname = fname
self.input = tokens
self.it = iter(tokens)
self.inc(None)
temp = KlipList()
while self.cur:
temp.append(self.parseTerminal())
self.inc(None)
return temp
parse = Parser()
if __name__ == '__main__':
from Preprocess import preprocess
from Tokenize import tokenize
import sys, pprint
fin = open(sys.argv[1], 'r')
tree = parse(tokenize(preprocess(fin.read()), sys.argv[1]), sys.argv[1])
fin.close()
print(tree)