about summary refs log tree commit diff stats
path: root/read.lua
blob: 6d55e2341956ccc1e6fbedaed58286e0acc673d1 (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
--- lam.read

local m = {}
local type = require("type")
local port = require("port")
local eof, input_port = port.eof, port.input_port
local util = require("util")
local constantly, error, pop = util.constantly, util.error, util.pop

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 ]]---

-- 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 = {
	["("] = function (cs) return pop(cs), "open", cs end,
	[")"] = function (cs) return pop(cs), "close", cs end,
	["'"] = function (cs) return pop(cs), "quote", cs end,
	["`"] = function (cs) return pop(cs), "quote", cs end,
	[","] = -- unquote
		function (cs)
			pop(cs) -- remove ','
			if cs[1] == "@" then
				pop(cs) -- remove '@'
				return ",@", "quote", cs
			else
				return ",", "quote", cs
			end
		end,
	[";"] = -- comment
		function (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,
	["#"] = -- literal
		function (cs)
			local tok
			-- bail on just '#\'
			if not (cs[2] and cs[3]) then
				cs = {}
				error("bad literal", "#\\")
			end

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

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

			local val
			if m.readtable.literals.lit[tok] 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,
}
m.readtable.regex = {
	["%s"] = -- whitespace
		function (cs)
			while #cs > 0 and cs[1]:match("%s") do
				pop(cs)
			end
			return false, nil, cs
		end,
	["[%d.+-]"] = -- numbers and symbols +, -, ., and ...
		function (cs)
			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,
}
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 = constantly(nil), -- throw comments away
}

---[[ 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