1
0
Fork 0
mirror of https://github.com/Reuh/candran.git synced 2025-10-27 09:59:29 +00:00

Handle static import errors in can

This commit is contained in:
Étienne Fildadut 2021-06-18 14:23:45 +02:00
parent 008e7732bc
commit d4102f1af6
3 changed files with 97 additions and 81 deletions

24
bin/can
View file

@ -118,6 +118,20 @@ else
print("Candran " .. candran.VERSION .. ", targeting " .. candran.default.target) print("Candran " .. candran.VERSION .. ", targeting " .. candran.default.target)
candran.setup() candran.setup()
-- only perform static imports once, on startup
do
local r, e = candran.load("local _", "stdin")
if not r then
print("In static import: "..e)
else
r, e = pcall(r)
if not r then
print("In static import: "..e)
end
end
candran.default.import = {}
end
-- REPL loop -- REPL loop
local multiline = false -- true if wait for another line local multiline = false -- true if wait for another line
local buffer local buffer
@ -157,15 +171,15 @@ else
end end
-- exec -- exec
local t = { pcall(candran.load, buffer, "stdin") } local r, e = candran.load(buffer, "stdin")
if t[1] == false then if not r then
if t[2]:match("expected '[end})]+' to close") then if e:match("expected '[end})]+' to close") then
multiline = true multiline = true
else else
print(t[2]) print(e)
end end
else else
t = { pcall(t[2]) } local t = { pcall(r) }
if t[1] == false then if t[1] == false then
print(t[2]) print(t[2])
elseif #t > 1 then elseif #t > 1 then

View file

@ -320,6 +320,7 @@ end
--- Candran error message handler. --- Candran error message handler.
-- Use it in xpcall to rewrite stacktraces to display Candran source file lines instead of compiled Lua lines. -- Use it in xpcall to rewrite stacktraces to display Candran source file lines instead of compiled Lua lines.
function candran.messageHandler(message, noTraceback) function candran.messageHandler(message, noTraceback)
message = tostring(message)
if not noTraceback and not message:match("\nstack traceback:\n") then if not noTraceback and not message:match("\nstack traceback:\n") then
message = debug.traceback(message, 2) message = debug.traceback(message, 2)
end end

View file

@ -7251,88 +7251,89 @@ return f() -- candran.can:316
end -- candran.can:316 end -- candran.can:316
end -- candran.can:316 end -- candran.can:316
candran["messageHandler"] = function(message, noTraceback) -- candran.can:322 candran["messageHandler"] = function(message, noTraceback) -- candran.can:322
message = tostring(message) -- candran.can:323
if not noTraceback and not message:match("\ if not noTraceback and not message:match("\
stack traceback:\ stack traceback:\
") then -- candran.can:323 ") then -- candran.can:324
message = debug["traceback"](message, 2) -- candran.can:324 message = debug["traceback"](message, 2) -- candran.can:325
end -- candran.can:324 end -- candran.can:325
return message:gsub("(\ return message:gsub("(\
?%s*)([^\ ?%s*)([^\
]-)%:(%d+)%:", function(indentation, source, line) -- candran.can:326 ]-)%:(%d+)%:", function(indentation, source, line) -- candran.can:327
line = tonumber(line) -- candran.can:327 line = tonumber(line) -- candran.can:328
local originalFile -- candran.can:329 local originalFile -- candran.can:330
local strName = source:match("^(.-)%(compiled candran%)$") -- candran.can:330 local strName = source:match("^(.-)%(compiled candran%)$") -- candran.can:331
if strName then -- candran.can:331 if strName then -- candran.can:332
if codeCache[strName] then -- candran.can:332 if codeCache[strName] then -- candran.can:333
originalFile = codeCache[strName] -- candran.can:333 originalFile = codeCache[strName] -- candran.can:334
source = strName -- candran.can:334 source = strName -- candran.can:335
end -- candran.can:334 end -- candran.can:335
else -- candran.can:334 else -- candran.can:335
do -- candran.can:337 do -- candran.can:338
local fi -- candran.can:337 local fi -- candran.can:338
fi = io["open"](source, "r") -- candran.can:337 fi = io["open"](source, "r") -- candran.can:338
if fi then -- candran.can:337 if fi then -- candran.can:338
originalFile = fi:read("*a") -- candran.can:338 originalFile = fi:read("*a") -- candran.can:339
fi:close() -- candran.can:339 fi:close() -- candran.can:340
end -- candran.can:339 end -- candran.can:340
end -- candran.can:339 end -- candran.can:340
end -- candran.can:339 end -- candran.can:340
if originalFile then -- candran.can:343 if originalFile then -- candran.can:344
local i = 0 -- candran.can:344 local i = 0 -- candran.can:345
for l in (originalFile .. "\ for l in (originalFile .. "\
"):gmatch("([^\ "):gmatch("([^\
]*)\ ]*)\
") do -- candran.can:345 ") do -- candran.can:346
i = i + 1 -- candran.can:346 i = i + 1 -- candran.can:347
if i == line then -- candran.can:347 if i == line then -- candran.can:348
local extSource, lineMap = l:match(".*%-%- (.-)%:(%d+)$") -- candran.can:348 local extSource, lineMap = l:match(".*%-%- (.-)%:(%d+)$") -- candran.can:349
if lineMap then -- candran.can:349 if lineMap then -- candran.can:350
if extSource ~= source then -- candran.can:350 if extSource ~= source then -- candran.can:351
return indentation .. extSource .. ":" .. lineMap .. "(" .. extSource .. ":" .. line .. "):" -- candran.can:351 return indentation .. extSource .. ":" .. lineMap .. "(" .. extSource .. ":" .. line .. "):" -- candran.can:352
else -- candran.can:351 else -- candran.can:352
return indentation .. extSource .. ":" .. lineMap .. "(" .. line .. "):" -- candran.can:353 return indentation .. extSource .. ":" .. lineMap .. "(" .. line .. "):" -- candran.can:354
end -- candran.can:353 end -- candran.can:354
end -- candran.can:353 end -- candran.can:354
break -- candran.can:356 break -- candran.can:357
end -- candran.can:356 end -- candran.can:357
end -- candran.can:356 end -- candran.can:357
end -- candran.can:356 end -- candran.can:357
end) -- candran.can:356 end) -- candran.can:357
end -- candran.can:356 end -- candran.can:357
candran["searcher"] = function(modpath) -- candran.can:364 candran["searcher"] = function(modpath) -- candran.can:365
local filepath = util["search"](modpath, { "can" }) -- candran.can:365 local filepath = util["search"](modpath, { "can" }) -- candran.can:366
if not filepath then -- candran.can:366 if not filepath then -- candran.can:367
if _VERSION == "Lua 5.4" then -- candran.can:367 if _VERSION == "Lua 5.4" then -- candran.can:368
return "no candran file in package.path" -- candran.can:368 return "no candran file in package.path" -- candran.can:369
else -- candran.can:368 else -- candran.can:369
return "\ return "\
\9no candran file in package.path" -- candran.can:370 \9no candran file in package.path" -- candran.can:371
end -- candran.can:370 end -- candran.can:371
end -- candran.can:370 end -- candran.can:371
return function(modpath) -- candran.can:373 return function(modpath) -- candran.can:374
local r, s = candran["loadfile"](filepath) -- candran.can:374 local r, s = candran["loadfile"](filepath) -- candran.can:375
if r then -- candran.can:375 if r then -- candran.can:376
return r(modpath, filepath) -- candran.can:376 return r(modpath, filepath) -- candran.can:377
else -- candran.can:376 else -- candran.can:377
error(("error loading candran module '%s' from file '%s':\ error(("error loading candran module '%s' from file '%s':\
\9%s"):format(modpath, filepath, tostring(s)), 0) -- candran.can:378 \9%s"):format(modpath, filepath, tostring(s)), 0) -- candran.can:379
end -- candran.can:378 end -- candran.can:379
end, filepath -- candran.can:380 end, filepath -- candran.can:381
end -- candran.can:380 end -- candran.can:381
candran["setup"] = function() -- candran.can:384 candran["setup"] = function() -- candran.can:385
local searchers = (function() -- candran.can:385 local searchers = (function() -- candran.can:386
if _VERSION == "Lua 5.1" then -- candran.can:385 if _VERSION == "Lua 5.1" then -- candran.can:386
return package["loaders"] -- candran.can:386 return package["loaders"] -- candran.can:387
else -- candran.can:386 else -- candran.can:387
return package["searchers"] -- candran.can:388 return package["searchers"] -- candran.can:389
end -- candran.can:388 end -- candran.can:389
end)() -- candran.can:388 end)() -- candran.can:389
for _, s in ipairs(searchers) do -- candran.can:391 for _, s in ipairs(searchers) do -- candran.can:392
if s == candran["searcher"] then -- candran.can:392 if s == candran["searcher"] then -- candran.can:393
return candran -- candran.can:393 return candran -- candran.can:394
end -- candran.can:393 end -- candran.can:394
end -- candran.can:393 end -- candran.can:394
table["insert"](searchers, 1, candran["searcher"]) -- candran.can:397 table["insert"](searchers, 1, candran["searcher"]) -- candran.can:398
return candran -- candran.can:398 return candran -- candran.can:399
end -- candran.can:398 end -- candran.can:399
return candran -- candran.can:401 return candran -- candran.can:402