-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathread.lua
290 lines (246 loc) · 5.26 KB
/
read.lua
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
---
-- The Lemma Reader
---
require 'type'
require 'env'
require 'class/List'
require 'class/Vector'
require 'class/PreHashMap'
require 'class/Symbol'
require 'class/Nil' -- used to signal that a comment has been read
require 'class/Number'
local symbol = '(.+)' -- this is perhaps a little too permissive
function symbol_patterns()
return {
full = symbol,
table = '([^%.]*)'
}
end
local keywords = {['true'] = true, ['false'] = false, ['nil'] = nil}
local function tovalue(x, co)
return keywords[x]
end
local function handle_number(n, co)
if not tonumber(n) then
return error('lexical error on token: '..n)
end
if co then
return Number(n)
else
return tonumber(n)
end
end
-- these are tried in order (make them specific!)
local atoms = {
'^([%+%-]?0x%x+)$', handle_number, -- hexadecimal
'^([%+%-]?%d+%.?%d+)$', handle_number, -- with decimal point
'^([%+%-]?%d+)$', handle_number, -- without decimal point
'^(true)$', tovalue,
'^(false)$', tovalue,
'^(nil)$', tovalue,
'^'..symbol, Symbol
}
local number = {}
for i = 0, 9 do
number[i] = true
end
local whitespace = {
[' '] = true,
['\t'] = true,
['\n'] = true,
[','] = true
}
local delim = {
['('] = true,
[')'] = true,
['['] = true,
[']'] = true,
['{'] = true,
['}'] = true
}
local function read_seq(eos, func)
return function(f, co)
local list = {}
local n = 0
while true do
local c = f:get()
if not c then return Error'eof' end
while whitespace[c] do
c = f:get()
if not c then return Error'eof' end
end
if c == eos then
return func(table.unpack(list, 1, n))
else
f:unget(c)
local form = read(f, co, true)
if form == Error'eof' then
return Error'eof'
elseif form ~= Nil then
n = n + 1
list[n] = form
end
end
end
end
end
local function read_delimed(delim, constr)
return function(f, co)
local str = {}
local escape = false
while true do
local c = f:get()
if not c then return Error'eof' end
if c == delim and not escape then
local str = table.concat(str)
if constr then
return constr(str)
else
return str
end
elseif c == 'n' and escape then
c = '\n'
escape = false
elseif c == '\\' then
if not escape then
escape = true
else
c = '\\'
escape = false
end
else
escape = false
end
if not escape then
table.insert(str, c)
end
end
end
end
local function read_keyword(f, co)
local str = {}
while true do
local c = f:get()
if not c then return Error'eof' end
if delim[c] or whitespace[c] then
f:unget(c)
return table.concat(str)
end
table.insert(str, c)
end
end
local function read_comment(f, co)
local c
repeat
c = f:get()
if not c then return Error'eof' end
until c == '\n'
return Nil
end
local function read_multicomment(f, co)
local last, c
local level = 1
while true do
if level == 0 then
return Nil
end
last = c
c = f:get()
if not c then return Error'eof' end
if last == '#' and c == '|' then
level = level + 1
elseif last == '|' and c == '#' then
level = level - 1
end
end
end
local function read_datumcomment(f, c)
read(f, c)
return Nil
end
local function read_quote(sym)
return function(f, c)
local q = List()
return q:cons(read(f, c)):cons(Symbol(sym))
end
end
local function table_idx(func)
return function(f, c)
local k = read(f, c)
if lemma.type(k) ~= 'Symbol' then
return error'read: dot syntax requires symbol'
end
return List():cons(k:string()):cons(Symbol(func))
end
end
local reader_macros = {
['('] = read_seq(')', List),
['['] = read_seq(']', Vector),
['{'] = read_seq('}', PreHashMap),
['"'] = read_delimed('"'),
['|'] = read_delimed('|', Symbol),
['.'] = table_idx('method'),
[':'] = read_keyword,
["'"] = read_quote('quote'),
['`'] = read_quote('quasiquote'),
['~'] = read_quote('unquote'),
['@'] = read_quote('splice'),
[';'] = read_comment,
['#'] = {
['|'] = read_multicomment,
[';'] = read_datumcomment,
['!'] = read_comment
}
}
---
-- Read the next form from stream f. (Set compiling when... compiling.)
---
function read(f, compiling, waiting)
local form = nil
---
-- If it's not whitespace, and it's not a reader macro, then
-- it's either a symbol or number.
---
local c = f:get()
if not c then return Error'eof' end
while whitespace[c] do
c = f:get()
if not c then return Error'eof' end
end
local macro = reader_macros[c]
while lemma.type(macro) == 'table' do
c = f:get()
if c == Error'eof' then return Error'eof' end
macro = macro[c]
end
if lemma.type(macro) == 'function' then
form = macro(f, compiling)
if form == Nil then
if waiting then
return Nil
end
return read(f, compiling)
end
else
local str = {}
while not delim[c] and not whitespace[c] do
table.insert(str, c)
c = f:get()
if not c then return Error'eof' end
end
f:unget(c)
str = table.concat(str)
-- Do a pattern match on str to identify type of atom
-- if no matches, lexical error
for i = 1, #atoms, 2 do
if string.find(str, atoms[i]) then
form = atoms[i+1](str, compiling)
return form
end
end
if not form then
return error('lexical error on token: '..f:get()..str)
end
end
return form
end