This repository has been archived by the owner on Sep 15, 2018. It is now read-only.
/
parser.py
268 lines (219 loc) · 8.74 KB
/
parser.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# -*- coding: utf-8 -*-
import re
from trampoline import pogo_stick, bounce
from scmlib import *
from tsymbol import tsym
from errors import *
# Delimiter characters. If we see these, we have found
# a token and need to add it to the token list.
DELIMS = '"\'\n\t;( )'
TOKEN_TYPE_STRING = 0
TOKEN_TYPE_INTEGER = 1
TOKEN_TYPE_FLOAT = 2
TOKEN_TYPE_FRACTION = 3
TOKEN_TYPE_COMPLEX = 4
TOKEN_TYPE_BOOLEAN = 5
TOKEN_TYPE_LPAREN = 6
TOKEN_TYPE_RPAREN = 7
TOKEN_TYPE_SINGLE_QUOTE = 8
TOKEN_TYPE_DOT = 9
TOKEN_TYPE_SYMBOL = 10
token_patterns = (
(TOKEN_TYPE_STRING, re.compile(r'^".*"$', flags=re.DOTALL)), \
(TOKEN_TYPE_INTEGER, re.compile(r'^[+-]?\d+$')), \
(TOKEN_TYPE_FLOAT, re.compile(r'^[+-]?(\d+\.\d*|\.\d+)$')), \
(TOKEN_TYPE_FRACTION, re.compile(r'^[+-]?\d+/\d+$')), \
(TOKEN_TYPE_COMPLEX, re.compile(r'''(^(([+-]?\d+|[+-]?(\d+\.\d*|\.\d+))
([+-]|[+-]\d+|[+-](\d+\.\d*|\.\d+))i$)
|
^([+-]|[+-]?\d+|[+-]?(\d+\.\d*|\.\d+))i$
)
''', flags=re.VERBOSE)), \
(TOKEN_TYPE_BOOLEAN, re.compile(r'^#[tf]$')), \
(TOKEN_TYPE_LPAREN, re.compile(r'^\($')), \
(TOKEN_TYPE_RPAREN, re.compile(r'^\)$')), \
(TOKEN_TYPE_SINGLE_QUOTE, re.compile(r"^'$")), \
(TOKEN_TYPE_DOT, re.compile(r'^\.$')), \
(TOKEN_TYPE_SYMBOL, re.compile(r'^[\w!$%&*+-./:<=>?@^_~]+$'))
)
def get_token_type(tok):
for t in token_patterns:
mobj = t[1].match(tok)
if mobj is not None:
return (tok, t[0])
raise SchemeError('unknown token ' + tok)
class Tokenizer(object):
def __init__(self):
self.reset()
def reset(self):
self.tokens = []
self.cur_token = ''
self.string_not_end = False
self.quote_not_end = False
self.comment_not_end = False
self.paren_count = 0;
self.lineno = 0
def need_more_code(self):
"""Check if current code is incomplete.
"""
return self.paren_count > 0 or self.string_not_end or \
self.quote_not_end or self.tokens == []
def get_tokens(self):
"""Return the tokens found, get ready for the next round,
should only be called when need_more_code() returns False.
"""
tokens = self.tokens
self.tokens = []
self.cur_token = ''
return [get_token_type(tok) for tok in tokens]
def tokenize(self, code):
self.tokens = []
self.cur_token = ''
self.tokenize_piece(code)
if self.need_more_code():
raise SchemeError('bad single line expression ' + code)
return self.get_tokens()
def tokenize_piece(self, code):
"""Tokenize a given piece of code.
"""
for char in code:
if self.string_not_end:
# take everything inside a string
self.cur_token += char
if char != '"':
continue
if self.comment_not_end:
# ignore everything inside a comment
if char != '\n':
continue
if char in DELIMS:
# meet a delimiter, save the token seen
if self.cur_token != '':
self.tokens.append(self.cur_token)
self.cur_token = ''
self.quote_not_end = False
if char == '"':
if not self.string_not_end:
self.cur_token += char
self.string_not_end = not self.string_not_end
elif char == "'":
self.tokens.append(char)
self.quote_not_end = True
elif char == ';':
self.comment_not_end = True
elif char == '\n':
self.comment_not_end = False
elif char == '(':
self.quote_not_end = False
self.paren_count += 1
self.tokens.append(char)
elif char == ')':
if self.paren_count - 1 < 0:
raise SchemeError('unexpected ): ' + code.strip())
else:
self.paren_count -= 1
self.tokens.append(char)
else:
self.cur_token += char
class Parser(object):
def __init__(self):
self.reset()
#self.sexps = []
def reset(self):
self.sexps = []
def consume(self, tokens, exp_type):
"""Pop the first token from the token list with
the expected token type.
"""
if len(tokens) == 0:
raise SchemeError('meet empty token list')
if tokens[0][1] != exp_type:
raise SchemeError('expect %s, given %s' % (exp_type, tokens[0][0]))
return tokens.pop(0)
def parse_lexeme_datum(self, tokens, cont):
token = tokens[0]
token_type = token[1]
if token_type == TOKEN_TYPE_BOOLEAN:
self.consume(tokens, TOKEN_TYPE_BOOLEAN)
if token[0] == '#t':
return bounce(cont, True)
else:
return bounce(cont, False)
elif token_type == TOKEN_TYPE_STRING:
self.consume(tokens, TOKEN_TYPE_STRING)
return bounce(cont, token[0][1:-1])
elif token_type == TOKEN_TYPE_SYMBOL:
self.consume(tokens, TOKEN_TYPE_SYMBOL)
return bounce(cont, tsym(token[0]))
elif token_type == TOKEN_TYPE_INTEGER:
self.consume(tokens, TOKEN_TYPE_INTEGER)
return bounce(cont, int(token[0]))
elif token_type == TOKEN_TYPE_FLOAT:
self.consume(tokens, TOKEN_TYPE_FLOAT)
return bounce(cont, float(token[0]))
elif token_type == TOKEN_TYPE_FRACTION:
self.consume(tokens, TOKEN_TYPE_FRACTION)
numer, denom = token[0].split('/')
return bounce(cont, float(numer) / float(denom))
elif token_type == TOKEN_TYPE_COMPLEX:
self.consume(tokens, TOKEN_TYPE_COMPLEX)
return bounce(cont, complex(token[0].replace('i', 'j')))
else:
raise SchemeError(token, 'is not a lexeme datum')
def parse_rest_sexps(self, tokens, cont):
"""Parse the S-expressions in the list. The list may
be a Scheme list or a dotted partial list.
"""
token_type = tokens[0][1]
if token_type == TOKEN_TYPE_DOT:
self.consume(tokens, TOKEN_TYPE_DOT)
return bounce(self.parse_sexp, tokens, cont)
elif token_type != TOKEN_TYPE_RPAREN:
def done_first(first):
nonlocal saved_first_exp
saved_first_exp = first
return bounce(self.parse_rest_sexps, tokens, done_rest)
def done_rest(rest):
return bounce(cont, cons(saved_first_exp, rest))
saved_first_exp = None
return bounce(self.parse_sexp, tokens, done_first)
else:
return bounce(cont, NIL)
def parse_list(self, tokens, cont):
"""Parse a Scheme list.
"""
def done_rest(rest):
self.consume(tokens, TOKEN_TYPE_RPAREN)
return bounce(cont, rest)
self.consume(tokens, TOKEN_TYPE_LPAREN)
return bounce(self.parse_rest_sexps, tokens, done_rest)
def parse_sexp(self, tokens, cont):
"""Parse a single S-expression.
"""
token_type = tokens[0][1]
if token_type in (TOKEN_TYPE_BOOLEAN,
TOKEN_TYPE_INTEGER,
TOKEN_TYPE_FLOAT,
TOKEN_TYPE_FRACTION,
TOKEN_TYPE_COMPLEX,
TOKEN_TYPE_STRING,
TOKEN_TYPE_SYMBOL,):
return bounce(self.parse_lexeme_datum, tokens, cont)
elif token_type == TOKEN_TYPE_SINGLE_QUOTE:
def make_quote(word):
return bounce(cont, lib_list(tsym('quote'), word))
self.consume(tokens, TOKEN_TYPE_SINGLE_QUOTE)
return bounce(self.parse_sexp, tokens, make_quote)
elif token_type == TOKEN_TYPE_LPAREN:
return bounce(self.parse_list, tokens, cont)
else:
raise SchemeError('bad expression syntax')
def parse(self, tokens):
"""The interface. Returns the list of S-expressions
generated from the given token list.
"""
self.sexps = []
while True:
if len(tokens) == 0:
return self.sexps
self.sexps.append(pogo_stick(bounce(self.parse_sexp, tokens, lambda d:d)))