aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--error_handling.lua60
-rw-r--r--error_handling.moon43
-rw-r--r--nomsu.lua7
-rwxr-xr-xnomsu.moon6
-rw-r--r--nomsu_compiler.lua2
-rw-r--r--nomsu_compiler.moon2
-rw-r--r--parser.lua3
-rw-r--r--parser.moon3
8 files changed, 58 insertions, 68 deletions
diff --git a/error_handling.lua b/error_handling.lua
index f5bd10b..5cf54e5 100644
--- a/error_handling.lua
+++ b/error_handling.lua
@@ -3,16 +3,15 @@ local ok, to_lua = pcall(function()
return require('moonscript.base').to_lua
end)
if not ok then
- to_lua = nil
+ to_lua = function()
+ return nil
+ end
end
-local moonscript_line_tables = setmetatable({ }, {
- __index = function(self, filename)
- if not (to_lua) then
- return nil
- end
- local _, line_table = to_lua(FILE_CACHE[filename])
- self[filename] = line_table
- return line_table
+local MOON_SOURCE_MAP = setmetatable({ }, {
+ __index = function(self, file)
+ local _, line_table = to_lua(file)
+ self[file] = line_table or false
+ return line_table or false
end
})
debug.getinfo = function(thread, f, what)
@@ -57,21 +56,6 @@ print_err_msg = function(error_message, stack_offset)
end
io.stderr:write(tostring(colored.red("ERROR:")) .. " " .. tostring(colored.bright(colored.red((error_message or "")))) .. "\n")
io.stderr:write("stack traceback:\n")
- ok, to_lua = pcall(function()
- return require('moonscript.base').to_lua
- end)
- if not ok then
- to_lua = function()
- return nil
- end
- end
- local LINE_TABLES = setmetatable({ }, {
- __index = function(self, file)
- local _, line_table = to_lua(file)
- self[file] = line_table or false
- return line_table or false
- end
- })
local get_line
get_line = function(file, line_no)
local start = LINE_STARTS[file][line_no] or 1
@@ -116,7 +100,20 @@ print_err_msg = function(error_message, stack_offset)
local file = FILE_CACHE[filename]:sub(tonumber(start), tonumber(stop))
local err_line = get_line(file, calling_fn.currentline):sub(1, -2)
local offending_statement = colored.bright(colored.red(err_line:match("^[ ]*(.*)")))
- name = "action '" .. tostring(calling_fn.name) .. "'"
+ if calling_fn.name then
+ do
+ local tmp = calling_fn.name:match("^A_([a-zA-Z0-9_]*)$")
+ if tmp then
+ name = "action '" .. tostring(tmp:gsub("_", " "):gsub("x([0-9A-F][0-9A-F])", function(self)
+ return string.char(tonumber(self, 16))
+ end)) .. "'"
+ else
+ name = "action '" .. tostring(calling_fn.name) .. "'"
+ end
+ end
+ else
+ name = "main chunk"
+ end
line = colored.yellow(tostring(filename) .. ":" .. tostring(calling_fn.currentline) .. " in " .. tostring(name) .. "\n " .. tostring(offending_statement))
else
local file
@@ -163,8 +160,8 @@ print_err_msg = function(error_message, stack_offset)
end
end
end
- if file and calling_fn.short_src:match(".moon$") and LINE_TABLES[file] then
- local char = LINE_TABLES[file][calling_fn.currentline]
+ if file and calling_fn.short_src:match("%.moon$") and type(MOON_SOURCE_MAP[file]) == 'table' then
+ local char = MOON_SOURCE_MAP[file][calling_fn.currentline]
line_num = 1
for _ in file:sub(1, char):gmatch("\n") do
line_num = line_num + 1
@@ -202,13 +199,8 @@ err_hand = function(error_message)
print_err_msg(error_message)
return os.exit(false, true)
end
-local has_ldt, ldt = pcall(require, 'ldt')
local safe_run
-if has_ldt then
- safe_run = ldt.guard
-else
- safe_run = function(fn)
- return xpcall(fn, err_hand)
- end
+safe_run = function(fn)
+ return xpcall(fn, err_hand)
end
return safe_run
diff --git a/error_handling.moon b/error_handling.moon
index 101c11e..d932b91 100644
--- a/error_handling.moon
+++ b/error_handling.moon
@@ -3,14 +3,12 @@ debug_getinfo = debug.getinfo
export SOURCE_MAP
ok, to_lua = pcall -> require('moonscript.base').to_lua
-if not ok then to_lua = nil
-moonscript_line_tables = setmetatable {}, {
- __index: (filename)=>
- return nil unless to_lua
- _, line_table = to_lua(FILE_CACHE[filename])
- self[filename] = line_table
- return line_table
-}
+if not ok then to_lua = -> nil
+MOON_SOURCE_MAP = setmetatable {},
+ __index: (file)=>
+ _, line_table = to_lua(file)
+ self[file] = line_table or false
+ return line_table or false
-- Make a better version of debug.getinfo that provides info about the original source
-- where the error came from, even if that's in another language.
@@ -38,15 +36,6 @@ print_err_msg = (error_message, stack_offset=3)->
io.stderr\write("#{colored.red "ERROR:"} #{colored.bright colored.red (error_message or "")}\n")
io.stderr\write("stack traceback:\n")
- -- TODO: properly print out the calling site of nomsu code, not just the *called* code
- ok, to_lua = pcall -> require('moonscript.base').to_lua
- if not ok then to_lua = -> nil
- LINE_TABLES = setmetatable {},
- __index: (file)=>
- _, line_table = to_lua(file)
- self[file] = line_table or false
- return line_table or false
-
get_line = (file, line_no)->
start = LINE_STARTS[file][line_no] or 1
stop = (LINE_STARTS[file][line_no+1] or 0) - 1
@@ -77,7 +66,11 @@ print_err_msg = (error_message, stack_offset=3)->
err_line = get_line(file, calling_fn.currentline)\sub(1,-2)
offending_statement = colored.bright(colored.red(err_line\match("^[ ]*(.*)")))
-- TODO: get name properly
- name = "action '#{calling_fn.name}'"
+ name = if calling_fn.name
+ if tmp = calling_fn.name\match("^A_([a-zA-Z0-9_]*)$")
+ "action '#{tmp\gsub("_"," ")\gsub("x([0-9A-F][0-9A-F])", => string.char(tonumber(@, 16)))}'"
+ else "action '#{calling_fn.name}'"
+ else "main chunk"
line = colored.yellow("#{filename}:#{calling_fn.currentline} in #{name}\n #{offending_statement}")
else
ok, file = pcall ->FILE_CACHE[calling_fn.short_src]
@@ -105,8 +98,8 @@ print_err_msg = (error_message, stack_offset=3)->
name = "upvalue '#{varname}'"
if not varname\match("%(")
break
- if file and calling_fn.short_src\match(".moon$") and LINE_TABLES[file]
- char = LINE_TABLES[file][calling_fn.currentline]
+ if file and calling_fn.short_src\match("%.moon$") and type(MOON_SOURCE_MAP[file]) == 'table'
+ char = MOON_SOURCE_MAP[file][calling_fn.currentline]
line_num = 1
for _ in file\sub(1,char)\gmatch("\n") do line_num += 1
line = colored.cyan("#{calling_fn.short_src}:#{line_num} in #{name or '?'}")
@@ -131,13 +124,7 @@ err_hand = (error_message)->
print_err_msg error_message
os.exit(false, true)
--- Note: xpcall has a slightly different API in Lua <=5.1 vs. >=5.2, but this works
--- for both APIs
-has_ldt, ldt = pcall(require,'ldt')
-safe_run = if has_ldt
- ldt.guard
-else
- (fn)->
- xpcall(fn, err_hand)
+safe_run = (fn)->
+ xpcall(fn, err_hand)
return safe_run
diff --git a/nomsu.lua b/nomsu.lua
index b500cfa..0b26ac0 100644
--- a/nomsu.lua
+++ b/nomsu.lua
@@ -187,4 +187,9 @@ run = function()
end
end
end
-return run_safely(run)
+local has_ldt, ldt = pcall(require, 'ldt')
+if has_ldt then
+ return ldt.guard(run)
+else
+ return run_safely(run)
+end
diff --git a/nomsu.moon b/nomsu.moon
index 9dc256e..3aa2c60 100755
--- a/nomsu.moon
+++ b/nomsu.moon
@@ -157,4 +157,8 @@ run = ->
elseif not ok
print_err_msg ret
-run_safely(run)
+has_ldt, ldt = pcall(require,'ldt')
+if has_ldt
+ ldt.guard(run)
+else
+ run_safely(run)
diff --git a/nomsu_compiler.lua b/nomsu_compiler.lua
index d093ff5..5c7fb91 100644
--- a/nomsu_compiler.lua
+++ b/nomsu_compiler.lua
@@ -501,7 +501,7 @@ do
source = nil
end
local lua_string = tostring(lua)
- local run_lua_fn, err = load(lua_string, nil and tostring(source or lua.source), "t", self)
+ local run_lua_fn, err = load(lua_string, tostring(source or lua.source), "t", self)
if not run_lua_fn then
local line_numbered_lua = concat((function()
local _accum_0 = { }
diff --git a/nomsu_compiler.moon b/nomsu_compiler.moon
index 8226db4..4d7b4cf 100644
--- a/nomsu_compiler.moon
+++ b/nomsu_compiler.moon
@@ -339,7 +339,7 @@ with NomsuCompiler
.run_lua = (lua, source=nil)=>
lua_string = tostring(lua)
- run_lua_fn, err = load(lua_string, nil and tostring(source or lua.source), "t", self)
+ run_lua_fn, err = load(lua_string, tostring(source or lua.source), "t", self)
if not run_lua_fn
line_numbered_lua = concat(
[format("%3d|%s",i,line) for i, line in ipairs get_lines\match(lua_string)],
diff --git a/parser.lua b/parser.lua
index 863e17e..a94418f 100644
--- a/parser.lua
+++ b/parser.lua
@@ -162,7 +162,8 @@ parse = function(nomsu_code, source)
end
errors = _accum_0
end
- error(table.concat(errors, "\n\n"), 0)
+ io.stderr:write("Errors occurred while parsing:\n\n", table.concat(errors, "\n\n"), '\n')
+ os.exit(1)
end
return tree
end
diff --git a/parser.moon b/parser.moon
index 4de1fe2..a713e85 100644
--- a/parser.moon
+++ b/parser.moon
@@ -116,7 +116,8 @@ parse = (nomsu_code, source=nil)->
keys = utils.keys(userdata.errors)
table.sort(keys)
errors = [userdata.errors[k] for k in *keys]
- error(table.concat(errors, "\n\n"), 0)
+ io.stderr\write("Errors occurred while parsing:\n\n", table.concat(errors, "\n\n"), '\n')
+ os.exit(1)
return tree