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

local read = {}
local type = require "type"
local utf8 = require "utf8"
local util = require "util"
local pop = util.pop
local unpack = table.unpack or unpack

local function program_characters (program)
	local chars = {}
	for pos, code in utf8.codes(program) do
		table.insert(chars, code)
	end
	return chars
end

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

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

local function consume_string (chars)
	local str = {}
	local backslash = {
		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(chars) -- throw initial " away
	repeat
		local c = pop(chars)
		if c == [[\]] then
			c = chars[1]
			if backlash[c] then
				if type(backslash[c]) == "function" then
					c, chars = backslash[c](chars)
					table.insert(str, c)
				else
					table.insert(str, backlash[c])
				end
			else
				table.insert(str, "\\"..c)
			end
			pop(chars)
		elseif c == [["]] then
			break
		else
			table.insert(str, c)
		end
	until #chars == 0
	return table.concat(str), "string", chars
end

local function consume_token (chars)
	local tok = {}
	while #chars>0 and chars[1]:match("[^%s()\"#'`,@;]") do
		table.insert(tok, pop(chars))
	end
	return table.concat(tok), chars
end

local consume_symbol = consume_token

local function consume_number (chars)
	local digits, chars = consume_token(chars)
	local num = tonumber(digits)
	if num == nil then error("Bad number: " .. num) end
	return num, chars
end

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

local function consume_comment (chars)
	local comment = {}
	repeat
		table.insert(comment, pop(chars))
	until #chars == 0 or chars[1]:match("\n")
	return table.concat(comment), "comment", chars
end

--- API

read.readtable = {
	["("] = function(chars) return pop(chars), "open", chars end,
	[")"] = function(chars) return pop(chars), "close", chars end,
	["'"] = function(chars) return pop(chars), "quote", chars end,
	["`"] = function(chars) return pop(chars), "quote", chars end,
	[","] = function(chars) return pop(chars), "quote", chars end,
	["\""] = consume_string,
	[";"] = consume_comment,
	-- ["#"] =
}

function read.scan (chars)
	local chars = chars
	return function()
		if #chars == 0 then return nil end
		local token, toktype = "", nil
		while true do
			if read.readtable[chars[1]] then
				token, toktype, chars =
					read.readtable[chars[1]](chars)
				return token, toktype
			elseif chars[1]:match("%s") then
				chars = consume_whitespace(chars)
			elseif chars[1]:match("%d") then
				token, chars = consume_number(chars)
				return token, "number"
			else
				token, chars = consume_symbol(chars)
				return token, "symbol"
			end
		end
	end
end

function read.tokenize (program)
	if not program or #program == 0 then return nil end
	local tokens = {}
	for token, toktype in read.scan(program_characters(program)) do
		table.insert(tokens, {type = toktype, value = token})
	end
	return tokens
end

read.readmacros = {
	open =
		function (token, tokens)
			local L = {}
			while tokens[1].type ~= "close" do
				table.insert(L, read.parse(tokens))
			end
			pop(tokens) -- remove final ")"
			return type.List(L)
		end,
	close =
		function (token, tokens)
			error ("Unexpected '" .. token.value .. "'")
		end,
	quote =
		function (token, tokens)
			local Q
			if token.value == "'" then
				Q = {"quote"}
			elseif token.value == "`" then
				Q = {"quasiquote"}
			elseif token.value == "," then
				Q = {"unquote"}
			end
			table.insert(Q, read.parse(tokens))
			return type.List(Q)
		end,
}

function read.parse (tokens)
	if not next(tokens) then return nil end
	local token = pop(tokens)
	if read.readmacros[token.type] then
		return read.readmacros[token.type](token, tokens)
	else
		return token.value
	end
end

function read.read (program)
	return read.parse(read.tokenize(program))
end

---
return read