Improved error reporting for '?' and '>' queries. Also improved source

code visualization of functions defined in strings.
This commit is contained in:
Bruce Hill 2018-04-08 18:14:39 -07:00
parent bff12f4742
commit 57315ef632
2 changed files with 51 additions and 20 deletions

39
ldt.lua
View File

@ -1018,6 +1018,7 @@ ldb = {
pads.err._frame:attrset(Color("red"))
pads.err:refresh()
end
local stack_sources = { }
local stack_locations = { }
local err_lines = { }
do
@ -1029,7 +1030,14 @@ ldb = {
if not info then
break
end
local fn_name = info.name or "<unnamed function>"
local fn_name = info.name
if not (fn_name) then
if info.istailcall then
fn_name = "<tail call>"
else
fn_name = "<anonymous>"
end
end
table.insert(stack_names, fn_name)
local line
if info.short_src then
@ -1037,7 +1045,7 @@ ldb = {
if line_table then
local char = line_table[info.currentline]
local line_num = 1
local file = file_cache[info.short_src]
local file = file_cache[info.short_src] or info.source
for _ in file:sub(1, char):gmatch("\n") do
line_num = line_num + 1
end
@ -1050,6 +1058,7 @@ ldb = {
end
err_lines[line] = true
table.insert(stack_locations, line)
table.insert(stack_sources, info.source)
max_filename = math.max(max_filename, #line)
max_fn_name = math.max(max_fn_name, #fn_name)
end
@ -1067,7 +1076,10 @@ ldb = {
end))
end
local show_src
show_src = function(filename, line_no)
show_src = function(filename, line_no, file_contents)
if file_contents == nil then
file_contents = nil
end
if pads.src then
if pads.src.filename == filename then
pads.src:select(line_no)
@ -1096,10 +1108,10 @@ ldb = {
pads.src:erase()
end
end
local file = file_cache[filename]
if file then
file_contents = file_contents or file_cache[filename]
if file_contents then
local src_lines = { }
for line in (file .. '\n'):gmatch("([^\n]*)\n") do
for line in (file_contents .. '\n'):gmatch("([^\n]*)\n") do
table.insert(src_lines, line)
end
pads.src = NumberedPad("(S)ource Code", pads.err.height, 0, pads.stack.height, pads.stack.x, src_lines, function(self, i)
@ -1174,9 +1186,9 @@ ldb = {
return pads.vars:select(1)
end
pads.stack.on_select = function(self, stack_index)
local filename, line_no = pads.stack.columns[2][stack_index]:match("([^:]*):(%d*).*")
local filename, line_no = pads.stack.columns[2][stack_index]:match("^(.*):(%d*)$")
line_no = tonumber(line_no)
show_src(filename, line_no)
show_src(filename, line_no, stack_sources[stack_index])
return show_vars(stack_index)
end
pads.stack:select(1)
@ -1250,10 +1262,17 @@ ldb = {
local run_fn
run_fn, err_msg = load(code, 'user input', 't', stack_env)
if not run_fn then
stdscr:attrset(Color('red bold'))
stdscr:addstr(err_msg)
stdscr:attrset(Color())
else
local ret = run_fn()
if ret ~= nil or print_nil then
local ret
ok, ret = pcall(run_fn)
if not ok then
stdscr:attrset(Color('red bold'))
stdscr:addstr(ret)
stdscr:attrset(Color())
elseif ret ~= nil or print_nil then
local value_bits = {
'= ',
Color('yellow'),

View File

@ -660,6 +660,7 @@ ldb = {
pads.err._frame\attrset(Color("red"))
pads.err\refresh!
stack_sources = {}
stack_locations = {}
err_lines = {}
do -- Stack pad
@ -669,14 +670,18 @@ ldb = {
for i=stack_min,stack_max
info = debug.getinfo(i)
if not info then break
fn_name = info.name or "<unnamed function>"
fn_name = info.name
unless fn_name
fn_name = if info.istailcall then "<tail call>"
else "<anonymous>"
table.insert(stack_names, fn_name)
line = if info.short_src
line_table = line_tables[info.short_src]
if line_table
char = line_table[info.currentline]
line_num = 1
file = file_cache[info.short_src]
file = file_cache[info.short_src] or info.source
for _ in file\sub(1,char)\gmatch("\n") do line_num += 1
"#{info.short_src}:#{line_num}"
else
@ -685,6 +690,7 @@ ldb = {
"???"
err_lines[line] = true
table.insert(stack_locations, line)
table.insert(stack_sources, info.source)
max_filename = math.max(max_filename, #line)
max_fn_name = math.max(max_fn_name, #fn_name)
max_fn_name, max_filename = 0, 0
@ -698,7 +704,7 @@ ldb = {
stack_names, ((i)=> (i == @selected) and Color("black on green") or Color("green bold")),
stack_locations, ((i)=> (i == @selected) and Color("black on cyan") or Color("cyan bold"))
show_src = (filename, line_no)->
show_src = (filename, line_no, file_contents=nil)->
if pads.src
if pads.src.filename == filename
pads.src\select(line_no)
@ -716,10 +722,10 @@ ldb = {
return
else
pads.src\erase!
file = file_cache[filename]
if file
file_contents = file_contents or file_cache[filename]
if file_contents
src_lines = {}
for line in (file..'\n')\gmatch("([^\n]*)\n")
for line in (file_contents..'\n')\gmatch("([^\n]*)\n")
table.insert src_lines, line
pads.src = NumberedPad "(S)ource Code", pads.err.height,0,
pads.stack.height,pads.stack.x, src_lines, (i)=>
@ -774,10 +780,10 @@ ldb = {
pads.vars\select(1)
pads.stack.on_select = (stack_index)=>
filename,line_no = pads.stack.columns[2][stack_index]\match("([^:]*):(%d*).*")
filename,line_no = pads.stack.columns[2][stack_index]\match("^(.*):(%d*)$")
--filename, line_no = pads.stack.lines[stack_index]\match("[^|]*| ([^:]*):(%d*).*")
line_no = tonumber(line_no)
show_src(filename, line_no)
show_src(filename, line_no, stack_sources[stack_index])
show_vars(stack_index)
pads.stack\select(1)
@ -839,10 +845,16 @@ ldb = {
run_fn, err_msg = load(code, 'user input', 't', stack_env)
if not run_fn
stdscr\attrset(Color('red bold'))
stdscr\addstr(err_msg)
stdscr\attrset(Color!)
else
ret = run_fn!
if ret != nil or print_nil
ok, ret = pcall(run_fn)
if not ok
stdscr\attrset(Color('red bold'))
stdscr\addstr(ret)
stdscr\attrset(Color!)
elseif ret != nil or print_nil
value_bits = {'= ', Color('yellow'), unpack(colored_repr(ret, SCREEN_W-2, 4))}
numlines = 1
for i=1,#value_bits-1,2