From c344b8ca3a82d84b886577c420b31d533c6792da Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 4 Nov 2011 18:10:16 -0700 Subject: [PATCH] parse/compile errors have better error messages --- bin/moon | 11 ++++++++++- moonscript/init.lua | 9 +++++++-- moonscript/init.moon | 7 +++++-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/bin/moon b/bin/moon index 9fe6b3b..253859c 100755 --- a/bin/moon +++ b/bin/moon @@ -41,7 +41,16 @@ local new_arg = { select(ind + 1, unpack(arg)) } -local chunk = moonscript.loadfile(script_fname) +local chunk +local passed, err = pcall(function() + chunk = moonscript.loadfile(script_fname) +end) + +if not passed then + print(err) + os.exit() +end + getfenv(chunk).arg = new_arg local runner = coroutine.create(chunk) diff --git a/moonscript/init.lua b/moonscript/init.lua index c3a41b3..3e05de5 100644 --- a/moonscript/init.lua +++ b/moonscript/init.lua @@ -27,7 +27,7 @@ to_lua = function(text) end local tree, err = parse.string(text) if not tree then - error("Parse error: " .. err, 2) + error(err, 2) end local code, ltable, pos = compile.tree(tree) if not code then @@ -65,7 +65,12 @@ if not _G.moon_no_loader then init_loader() end loadstring = function(str, chunk_name) - local code, ltable = to_lua(str) + local passed, code, ltable = pcall(function() + return to_lua(str) + end) + if not passed then + error(chunk_name .. ": " .. code, 2) + end if chunk_name then line_tables[chunk_name] = ltable end diff --git a/moonscript/init.moon b/moonscript/init.moon index ccdf8ec..2fb95e8 100644 --- a/moonscript/init.moon +++ b/moonscript/init.moon @@ -31,7 +31,7 @@ to_lua = (text) -> tree, err = parse.string text if not tree - error "Parse error: " .. err, 2 + error err, 2 code, ltable, pos = compile.tree tree if not code @@ -63,7 +63,10 @@ init_loader = -> init_loader! if not _G.moon_no_loader loadstring = (str, chunk_name) -> - code, ltable = to_lua str + passed, code, ltable = pcall -> to_lua str + if not passed + error chunk_name .. ": " .. code, 2 + line_tables[chunk_name] = ltable if chunk_name lua.loadstring code, chunk_name or "=(moonscript.loadstring)"