about summary refs log tree commit diff stats
path: root/read.lua
blob: 5acb5c572931cc2408b11d88e667e267f8e25743 (plain)
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
--- lam.read

local m = {}
local type = require("type")
local eof, input_port = type.eof, type.input_port
local util = require("util")
local error, pop = util.error, util.pop
local utf8_char = require("utf8").char

local token_separators = "[%s#()\"'`,@;]"

local function consume_token (cs)
	local tok = {}
	while #cs > 0 and not cs[1]:match(token_separators) do
		local c = pop(cs)
		table.insert(tok, c)
	end
	return table.concat(tok), cs
end

---[[ READ TABLE ]]---

--- helper functions

local function consume_unquote (cs)
	pop(cs) -- remove ','
	if cs[1] == "@" then
		pop(cs) -- remove '@'
		return ",@", "quote", cs
	else
		return ",", "quote", cs
	end
end

local function consume_comment (cs)
	local comment = {}
	while #cs > 0 and not cs[1]:match("\n") do
		table.insert(comment, pop(cs))
	end
	return table.concat(comment), "comment", cs
end

local function consume_literal (cs)
	local tok

	-- read '#\ ' and such correctly
	if cs[2] == "\\" then
		if not cs[3] then error("bad literal", "#\\") end
		if cs[3]:match(token_separators) then
			pop(cs) -- remove '\'
			pop(cs) -- remove next character
			return type.character(cs[1])
		end
	end

	pop(cs) -- discard '#' ...
	tok, cs = consume_token(cs)
	tok = "#" .. tok -- ... then put it back

	print(tok)

	local val
	if m.readtable.literals.lit[tok] ~= nil then
		val = m.readtable.literals.lit[tok]
	else
		for re, fn in pairs(m.readtable.literals.regex)
		do
			if tok:match(re) then
				val = fn(tok)
			end
		end
	end

	if val == nil then
		error("bad literal", tok)
	end
	return val, "literal", cs
end

local function consume_whitespace (cs)
	while #cs > 0 and cs[1]:match("%s") do
		pop(cs)
	end
	return false, nil, cs
end

local function consume_number_etc (cs)
	-- Since numbers can start with +, -, and ., those symbols and ... are
	-- handled along with numbers.
	local tok
	tok, cs = consume_token(cs)
	if tok:match("^[-+]$") or tok == "..." then
		return tok, "symbol", cs
	elseif tok == "." then
		return tok, "dot", cs
	else -- number
		local n = tonumber(tok)
		if not n then
			error("bad number", n)
		end
		return n, "number", cs
	end
end

-- strings

local function consume_string_whitespace (cs)
	-- \<intraline ws>*<line ending> <intraline ws>* : nothing
	local s = {"\\"}
	while cs[1]:match("[ \t]") do
		table.insert(s, pop(cs))
	end
	if cs[1] ~= "\n" then
		table.insert(s, cs[1])
		return table.concat(s), cs
	end
	while cs[1]:match("%s") do
		pop(cs)
	end
	return cs[1], cs
end

local function consume_string_hexvalue (cs)
	-- \x<hex scalar value>; : specified character
	local u8ch = {}
	repeat
		local c = pop(cs)
		table.insert(u8ch, c)
	until c == ";"
	table.remove(u8ch) -- discard ';'
	return utf8_char(tonumber(table.concat(u8ch), 16)), cs
end

local function consume_string (cs)
	local str = {}
	local escapes = {
		a = "\a",
		b = "\b",
		t = "\t",
		n = "\n",
		r = "\r",
		["\""] = "\"",
		["\\"] = "\\",
		["|"] = "|",
		[" "] = consume_string_whitespace,
		["\t"] = consume_string_whitespace,
		["\n"] = consume_string_whitespace,
		x = consume_string_hexvalue,
	}
	pop(cs) -- discard '"'
	repeat
		local c = pop(cs)
		if c == "\\" then
			c = cs[1]
			if escapes[c] then
				if type.luatype(escapes[c]) == "function" then
					c, cs = escapes[c](cs)
					table.insert(str, c)
				else
					table.insert(str, escapes[c])
				end
			else
				table.insert(str, "\\"..c)
			end
			pop(cs)
		elseif c == "\"" then
			break
		else
			table.insert(str, c)
		end
	until #cs == 0
	return type.string(str), "string", cs
end

local function consume_char_as (token_type)
	-- return a function that pops a character and returns it with
	-- TOKEN_TYPE
	return function (cs) return pop(cs), token_type, cs end
end

-- each function should take a list of characters and return the token, its
-- type, and the rest of the characters
m.readtable = {}
m.readtable.chars = {
	["("] = consume_char_as("open"),
	[")"] = consume_char_as("close"),
	["'"] = consume_char_as("quote"),
	["`"] = consume_char_as("quote"),
	[","] = consume_unquote,
	["\""] = consume_string,
	[";"] = consume_comment,
	["#"] = consume_literal,
}
m.readtable.regex = {
	["%s"] = consume_whitespace,
	["[%d.+-]"] = consume_number_etc,
}
m.readtable.default = -- default action if nothing else matches
	function (cs)
		local tok, cs = consume_token(cs)
		return tok, "symbol", cs
	end

-- convenience function to make writing the regexen rules easier below
local function based_num (base)
	return function (token)
		local n = tonumber(token:sub(3), base)
		if not n then
			error("bad number", token)
		end
		return n
	end
end

m.readtable.literals = {
	lit = {
		["#t"] = true,
		["#true"] = true,
		["#f"] = false,
		["#false"] = false,
	},
	regex = {
		["^#b"] = based_num(2),
		["^#o"] = based_num(8),
		["^#d"] = based_num(10),
		["^#x"] = based_num(16),
		["^#\\."] =
			function (tok)
				return type.character(tok:sub(3))
			end,
	},
}
-- add named characters
for char, name in pairs(type.character_names) do
	m.readtable.literals.lit["#\\"..name] = type.character(char)
end

---[[ READER MACROS ]]---
-- Each of these are named after the type of the token read and contain
-- function taking (TOKEN, TYPE, PORT) and returning a lisp object

m.readmacros = {
	close =
		function (token, _, _)
			error("unexpected", token)
		end,
	quote =
		function (token, _, port)
			local qs = {
				["'"] = "quote",
				["`"] = "quasiquote",
				[","] = "unquote",
				[",@"] = "unquote-splicing",
			}
			if not qs[token] then
				error("bad quote", token)
			end
			local Q = {qs[token]}
			table.insert(Q, m.read(port))
			return type.list(Q)
		end,
	comment = function (_,_,_) return nil end,
}

---[[ READ ]]---

function m.read (port)
	local function read_ahead(token, token_type)
		if token == eof then error("unexpected", token) end
		if token_type == "open" then
			-- this must be defined here because it calls read_ahead
			-- recursively.
			local L = {}
			repeat
				token, token_type = port:next(m.readtable)
				if token_type == "close" then
					return type.list(L)
				elseif token_type == "dot" then
					local fin = m.read(port)
					port:next(m.readtable) -- discard ')'
					return type.list(L, fin)
				else
					table.insert(L,
						read_ahead(token, token_type))
				end
			until nil
		elseif m.readmacros[token_type] then
			return m.readmacros[token_type](token, token_type, port)
		else
			return token
		end
	end
	---
	local token1, type1 = port:next(m.readtable)
	if token1 == eof then
		return eof
	else
		return read_ahead(token1, type1)
	end
end

function m.read_string (str)
	return m.read(input_port(str, "string"))
end

--------
return m