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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
-- Copyright (c) 2011-2015 Rob Hoelz <rob@hoelz.ro>
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy of
-- this software and associated documentation files (the "Software"), to deal in
-- the Software without restriction, including without limitation the rights to
-- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
-- the Software, and to permit persons to whom the Software is furnished to do so,
-- subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
-- FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
-- COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
-- IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
-- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-- @class repl
--- This module implements the core functionality of a REPL.
local plugins_lookup_meta = { __mode = 'k' }
local repl = { _buffer = '', _plugins = setmetatable({}, plugins_lookup_meta), _features = {}, _ifplugin = {}, _iffeature = {}, VERSION = 0.10 }
local compat = require 'repl.compat'
local select = select
local dtraceback = debug.traceback
local setmetatable = setmetatable
local sformat = string.format
local smatch = string.match
local error = error
local setfenv = require('repl.utils').setfenv
local function gather_results(success, ...)
local n = select('#', ...)
return success, { n = n, ... }
end
local function tcopy(t, copy)
copy = copy or {}
for k, v in pairs(t) do
copy[k] = v
end
return copy
end
--- Returns the prompt for a given level.
-- @param level The prompt level. Either 1 or 2.
function repl:getprompt(level)
return level == 1 and '>' or '>>'
end
--- Displays a prompt for the given prompt level.
-- @param level The prompt level. Either 1 or 2.
function repl:prompt(level)
self:showprompt(self:getprompt(level))
end
--- Returns the name of the REPL. For usage in chunk compilation.
-- @return The REPL's name.
-- @see load
function repl:name()
return 'REPL'
end
--- Gets a traceback for an error.
-- @param ... All of the stuff that xpcall passes to error functions.
-- @see xpcall
-- @return A stack trace. The default implementation returns a simple string-based trace.
function repl:traceback(...)
return dtraceback(...)
end
--- Uses the compilation error to determine whether or not further input
--- is pending after the last line. You shouldn't have to override this
--- unless you use an implementation of Lua that varies in its error
--- messages.
-- @param err The compilation error from Lua.
-- @return Whether or not the input should continue after this line.
function repl:detectcontinue(err)
return smatch(err, "'<eof>'$") or smatch(err, "<eof>$")
end
function repl:compilechunk(chunk)
return compat.loadstring(chunk, self:name())
end
--- Evaluates a line of input, and displays return value(s).
-- @param line The line to evaluate
-- @return The prompt level (1 or 2)
function repl:handleline(line)
local chunk = self._buffer .. line
local f, err = self:compilechunk(chunk)
if f then
self._buffer = ''
setfenv(f, self:getcontext())
local success, results = gather_results(xpcall(f, function(...) return self:traceback(...) end))
if success then
self:displayresults(results)
else
self:displayerror(results[1])
end
else
if self:detectcontinue(err) then
self._buffer = chunk .. '\n'
return 2
else
self:displayerror(err)
self._buffer = ''
end
end
return 1
end
--- Creates a new REPL object, so you can override methods without fear.
-- @return A REPL clone.
function repl:clone()
local plugins_copy = tcopy(self._plugins, setmetatable({}, plugins_lookup_meta))
local features_copy = tcopy(self._features)
local ifplugin_copy = {}
local iffeature_copy = {}
for k, v in pairs(self._ifplugin) do
ifplugin_copy[k] = tcopy(v)
end
for k, v in pairs(self._iffeature) do
iffeature_copy[k] = tcopy(v)
end
return setmetatable({
_buffer = '',
_plugins = plugins_copy,
_features = features_copy,
_ifplugin = ifplugin_copy,
_iffeature = iffeature_copy,
}, { __index = self })
end
--- Displays the given prompt to the user. Must be overriden.
-- @param prompt The prompt to display.
function repl:showprompt(prompt)
error 'You must implement the showprompt method'
end
--- Displays the results from evaluate(). Must be overriden.
-- @param results The results to display. The results are a table, with the integer keys containing the results, and the 'n' key containing the highest integer key.
function repl:displayresults(results)
error 'You must implement the displayresults method'
end
--- Displays errors from evaluate(). Must be overriden.
-- @param err The error value returned from repl:traceback().
-- @see repl:traceback
function repl:displayerror(err)
error 'You must implement the displayerror method'
end
--- Checks whether this REPL object has loaded the given plugin.
-- @param plugin The plugin that the REPL may have loaded.
function repl:hasplugin(plugin)
return self._plugins[plugin]
end
function repl:hasfeature(feature)
return self._features[feature]
end
function repl:requirefeature(feature)
if not self:hasfeature(feature) then
error(sformat('required feature %q not present', feature), 2)
end
end
function repl:ifplugin(plugin, action)
if self:hasplugin(plugin) then
action()
else
local pending_actions = self._ifplugin[plugin]
if not pending_actions then
pending_actions = {}
self._ifplugin[plugin] = pending_actions
end
pending_actions[#pending_actions + 1] = action
end
end
--- If the given feature has been loaded, call `action`. Otherwise, if the
-- feature is ever loaded in the future, call `action` after that loading occurs.
function repl:iffeature(feature, action)
if self:hasfeature(feature) then
action()
else
local pending_features = self._iffeature[feature]
if not pending_features then
pending_features = {}
self._iffeature[feature] = pending_features
end
pending_features[#pending_features + 1] = action
end
end
local function setup_before(repl)
local mt = {}
function mt:__newindex(key, value)
if type(value) ~= 'function' then
error(tostring(value) .. " is not a function", 2)
end
local old_value = repl[key]
if old_value == nil then
error(sformat("The '%s' method does not exist", key), 2)
end
repl[key] = function(...)
value(...)
return old_value(...)
end
end
return setmetatable({}, mt)
end
local function setup_after(repl)
local mt = {}
function mt:__newindex(key, value)
if type(value) ~= 'function' then
error(tostring(value) .. " is not a function", 2)
end
local old_value = repl[key]
if old_value == nil then
error(sformat("The '%s' method does not exist", key), 2)
end
repl[key] = function(...)
local _, results = gather_results(true, old_value(...))
value(...)
return compat.unpack(results, 1, results.n)
end
end
return setmetatable({}, mt)
end
local function setup_around(repl)
local mt = {}
function mt:__newindex(key, value)
if type(value) ~= 'function' then
error(tostring(value) .. " is not a function", 2)
end
local old_value = repl[key]
if old_value == nil then
error(sformat("The '%s' method does not exist", key), 2)
end
repl[key] = function(self, ...)
return value(self, old_value, ...)
end
end
return setmetatable({}, mt)
end
local function setup_override(repl)
local mt = {}
function mt:__newindex(key, value)
if type(value) ~= 'function' then
error(tostring(value) .. " is not a function", 2)
end
local old_value = repl[key]
if old_value == nil then
error(sformat("The '%s' method does not exist", key), 2)
end
repl[key] = value
end
return setmetatable({}, mt)
end
local function setup_repl(repl)
local mt = {}
function mt:__newindex(key, value)
local old_value = repl[key]
if old_value ~= nil then
error(sformat("The '%s' method already exists", key), 2)
end
repl[key] = value
end
function mt:__index(key)
local value = repl[key]
if type(value) == 'function' then
-- XXX cache this?
return function(_, ...)
return value(repl, ...)
end
end
return value
end
return setmetatable({}, mt)
end
-- TODO use lua-procure for this (eventually)
local function findchunk(name)
for _, loader in pairs(compat.package.searchers) do
local chunk = loader(name)
if type(chunk) == 'function' then
return chunk
end
end
error('unable to locate plugin', 3)
end
function repl:loadplugin(chunk)
if self:hasplugin(chunk) then
error(sformat('plugin %q has already been loaded', tostring(chunk)), 2)
end
self._plugins[chunk] = true
local plugin_actions = self._ifplugin[chunk]
self._ifplugin[chunk] = nil
if type(chunk) == 'string' then
chunk = findchunk('repl.plugins.' .. chunk)
end
local plugin_env = {
repl = setup_repl(self),
before = setup_before(self),
after = setup_after(self),
around = setup_around(self),
override = setup_override(self),
init = function() end,
}
local function ro_globals(_, key, _)
error(sformat('global environment is read-only (key = %q)', key), 2)
end
plugin_env._G = plugin_env
plugin_env.features = {}
setmetatable(plugin_env, { __index = _G, __newindex = ro_globals })
setfenv(chunk, plugin_env)
local _, results = gather_results(nil, chunk())
local features = plugin_env.features or {}
if type(features) == 'string' then
features = { features }
end
for _, feature in ipairs(features) do
if self._features[feature] then
error(sformat('feature %q already present', feature), 2)
end
self._features[feature] = true
local feature_actions = self._iffeature[feature]
self._iffeature[feature] = nil
if feature_actions then
for _, action in ipairs(feature_actions) do
action()
end
end
end
if plugin_actions then
for _, action in ipairs(plugin_actions) do
action()
end
end
return compat.unpack(results, 1, results.n)
end
-- XXX how to guarantee this gets called?
function repl:shutdown()
end
function repl:getcontext()
return _G
end
return repl
|