-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathquote.lua
More file actions
251 lines (205 loc) · 5.35 KB
/
quote.lua
File metadata and controls
251 lines (205 loc) · 5.35 KB
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
do
local lisp = require "lisp"
local old_string = _ENV.string
local old_tonumber = _ENV.tonumber
local old_error = _ENV.error
local old_type = _ENV.type
local old_table = _ENV.table
local old_print = _ENV.print
local old_tostring = _ENV.tostring
_ENV = {}
_ENV.list = lisp.list
_ENV.append = lisp.append
_ENV.string = old_string
_ENV.tonumber = old_tonumber
_ENV.error = old_error
_ENV.type = old_type
_ENV.table = old_table
_ENV.print = old_print
_ENV.tostring = old_tostring
end
function quote(symbols)
--
-- when *stopper* is nil, eschew all space
-- when *stopper* is not nil, eschew until hit a stopper
--
local latest_linebreak = 1
local line_num = 1
local function eschew(location, stopper)
local function is_stopper(sbyte)
for i = 1, #stopper do
if sbyte == string.byte(stopper[i]) then
return true
end
end
return false
end
local skipped = { " ", "\n", "\t" }
local function is_skipped(sbyte)
for i = 1, #skipped do
if sbyte == string.byte(skipped[i]) then
return true
end
end
return false
end
location = location - 1
repeat
location = location + 1
local current
if location <= string.len(symbols) then
current = string.byte(symbols, location)
if current == string.byte("\n") then
line_num = line_num + 1
latest_linebreak = location
end
if current == string.byte(";") then
while location + 1 <= string.len(symbols) do
current = string.byte(symbols, location + 1)
location = location + 1
if current == string.byte("\n") then
break
end
end
end
else
current = nil
location = nil
end
until location == nil or
(stopper == nil and (not is_skipped(current))) or
(stopper and is_stopper(current))
return location
end
--
-- parse simple *symbol*s
--
local function list_symbol(location)
local stop_loc = eschew(location, {" ", ")", "\n"})
if stop_loc == nil then
stop_loc = string.len(symbols) + 1
end
local symbol = string.sub(symbols, location, stop_loc - 1)
if type(symbol) == 'string' then
if tonumber(symbol) then
symbol = tonumber(symbol)
elseif symbol == 'true' then
symbol = true
elseif symbol == 'false' then
symbol = false
end
end
return list(symbol), eschew(stop_loc)
end
local function forward(expected, location)
if location == nil or
string.byte(symbols, location) ~= string.byte(expected) then
local location_str = "[end]"
if location then
location_str = tostring(location - latest_linebreak + 1)
end
error("Syntax error. Expect \"" .. expected .. "\" at line ".. line_num ..
", location " .. location_str);
else
location = location + 1
location = eschew(location)
end
return location
end
--
-- forward declaration
--
local list_quote
local read_string
local sub_list
--
-- parse list item
--
local function list_item(location)
if string.byte(symbols, location) == string.byte("(") then
return sub_list(location)
elseif string.byte(symbols, location) == string.byte("'") then
return list_quote(location)
elseif string.byte(symbols, location) == string.byte("\"") then
return read_string(location)
elseif string.byte(symbols, location) == string.byte(")") then
local location_str = "[end]"
if location then
location_str = tostring(location - latest_linebreak + 1)
end
error("Syntax error. Unexpected \")\" at line " .. line_num ..
", location " .. location_str)
else
return list_symbol(location)
end
end
--
-- parse sub-list
--
function sub_list(location)
local result_list = list()
location = forward("(", location)
while location and
string.byte(symbols, location) ~= string.byte(")") do
local new_item
new_item, location = list_item(location)
result_list = append(result_list, new_item)
end
location = forward(")", location)
if location then
location = eschew(location)
end
return list(result_list), location
end
--
-- parse quote
--
function list_quote(location)
location = forward("'", location)
local new_item
new_item, location = list_item(location)
local result_list = append(list("quote"), new_item)
return list(result_list), location
end
local escapes =
{
['n'] = "\n"
}
function read_string(location)
local result = {}
local last_escape = location + 1
repeat
location = location + 1
local current = string.byte(symbols, location)
if current == string.byte("\\") then
if location - 1 >= last_escape then
table.insert(result, string.sub(symbols, last_escape, location - 1))
end
last_escape = location + 1
location = location + 1
-- well-known escape
--
current = string.char(string.byte(symbols, location))
if escapes[current] ~= nil then
table.insert(result, escapes[current])
last_escape = last_escape + 1
end
end
until current == string.byte("\"")
table.insert(result, string.sub(symbols, last_escape, location - 1))
result = append(list("quote"), list(table.concat(result)))
return list(result), eschew(location + 1)
end
--
-- main chunk
--
local result_list = list()
local location = eschew(1)
while location ~= nil do
local new_item
new_item, location = list_item(location)
result_list = append(result_list, new_item)
end
return result_list
end
return _ENV