From 3bb3036f12247bd87afdf81dfef7e695c8fe3528 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Sat, 21 Mar 2020 12:13:30 +0800 Subject: clean up. --- src/MoonP/moon_compiler.cpp | 59 +++++++++++++++++---------------------------- src/MoonP/stacktraceplus.h | 44 ++++++--------------------------- 2 files changed, 29 insertions(+), 74 deletions(-) diff --git a/src/MoonP/moon_compiler.cpp b/src/MoonP/moon_compiler.cpp index 28db051..00b897d 100644 --- a/src/MoonP/moon_compiler.cpp +++ b/src/MoonP/moon_compiler.cpp @@ -33,6 +33,9 @@ using namespace parserlib; #define BLOCK_END } while (false); #define BREAK_IF(cond) if (cond) break +#define _DEFER(code,line) std::shared_ptr _defer_##line(nullptr, [&](auto){code;}) +#define DEFER(code) _DEFER(code,__LINE__) + typedef std::list str_list; inline std::string s(std::string_view sv) { @@ -40,7 +43,7 @@ inline std::string s(std::string_view sv) { } const char* moonScriptVersion() { - return "0.5.0-r0.3.2"; + return "0.5.0-r0.3.3"; } // name of table stored in lua registry @@ -82,6 +85,7 @@ public: _config = config; _info = _parser.parse(codes); GlobalVars globals; + DEFER(clear()); if (_info.node) { try { str_list out; @@ -99,15 +103,12 @@ public: globals->push_back({var.first, line, col}); } } - clear(); return {std::move(out.back()), Empty, std::move(globals)}; } catch (const std::logic_error& error) { - clear(); return {Empty, error.what(), std::move(globals)}; } } else { - clear(); - return {Empty, _info.error, std::move(globals)}; + return {Empty, std::move(_info.error), std::move(globals)}; } } @@ -115,15 +116,14 @@ public: _indentOffset = 0; _scopes.clear(); _codeCache.clear(); - std::stack emptyWith; - _withVars.swap(emptyWith); - std::stack emptyContinue; - _continueVars.swap(emptyContinue); _buf.str(""); _buf.clear(); _joinBuf.str(""); _joinBuf.clear(); _globals.clear(); + _info = {}; + _withVars = {}; + _continueVars = {}; if (_useModule) { _useModule = false; if (!_sameModule) { @@ -626,7 +626,7 @@ private: BREAK_IF(!callable->item.is()); if (chainList.size() == 1 || !ast_is(*(++chainList.begin()))) { - throw std::logic_error(_info.errorMessage("macro expression must be followed by argument list"sv, callable)); + throw std::logic_error(_info.errorMessage("macro expression must be followed by arguments list"sv, callable)); } return true; BLOCK_END @@ -2093,19 +2093,6 @@ private: lua_pop(L, 3); // item } - void hideStackTrace(bool hide) { - lua_getglobal(L, "package"); // package - lua_getfield(L, -1, "loaded"); // package loaded - lua_getfield(L, -1, "moonp"); // package loaded moonp - if (hide) { - lua_pushboolean(L, 1); // package loaded moonp true - } else { - lua_pushnil(L); // package loaded moonp nil - } - lua_setfield(L, -2, "_hide_stacktrace_"); - lua_pop(L, 3); // empty - } - bool isModuleLoaded(std::string_view name) { int top = lua_gettop(L); lua_pushliteral(L, MOONP_MODULE); // MOONP_MODULE @@ -2941,10 +2928,10 @@ private: std::pair expandMacroStr(ChainValue_t* chainValue) { const auto& chainList = chainValue->items.objects(); - auto callable = ast_cast(chainList.front()); - auto macroName = _parser.toString(callable->item.to()->name); + auto x = ast_to(chainList.front())->item.to(); + auto macroName = _parser.toString(x->name); if (!_useModule) { - throw std::logic_error(_info.errorMessage("can not resolve macro", callable->item)); + throw std::logic_error(_info.errorMessage("can not resolve macro"sv, x)); } pushCurrentModule(); // cur int top = lua_gettop(L) - 1; @@ -2952,7 +2939,7 @@ private: lua_rawget(L, -2); // cur[macroName], cur macro if (lua_istable(L, -1) == 0) { lua_settop(L, top); - throw std::logic_error(_info.errorMessage("can not resolve macro", callable->item)); + throw std::logic_error(_info.errorMessage("can not resolve macro"sv, x)); } lua_rawgeti(L, -1, 1); // cur macro func pushMoonp("pcall"sv); // cur macro func pcall @@ -2988,26 +2975,24 @@ private: } } else str = _parser.toString(arg); Utils::trim(str); - Utils::replace(str, "\r\n"sv, "\n"); + Utils::replace(str, "\r\n"sv, "\n"sv); lua_pushlstring(L, str.c_str(), str.size()); } // cur macro pcall func args... - hideStackTrace(true); bool success = lua_pcall(L, static_cast(args->size()) + 1, 2, 0) == 0; if (!success) { // cur macro err std::string err = lua_tostring(L, -1); lua_settop(L, top); - throw std::logic_error(_info.errorMessage(s("fail to expand macro\n"sv) + err, callable)); + throw std::logic_error(_info.errorMessage(s("fail to expand macro: "sv) + err, x)); } // cur macro success res - hideStackTrace(false); if (lua_toboolean(L, -2) == 0) { std::string err = lua_tostring(L, -1); lua_settop(L, top); - throw std::logic_error(_info.errorMessage(s("fail to expand macro\n"sv) + err, callable)); + throw std::logic_error(_info.errorMessage(s("fail to expand macro: "sv) + err, x)); } lua_remove(L, -2); // cur macro res if (lua_isstring(L, -1) == 0) { lua_settop(L, top); - throw std::logic_error(_info.errorMessage(s("macro function must return string with expanded codes"sv), callable)); + throw std::logic_error(_info.errorMessage(s("macro function must return string with expanded codes"sv), x)); } // cur macro codes lua_rawgeti(L, -2, 2); // cur macro codes type std::string type = lua_tostring(L, -1); @@ -3017,13 +3002,13 @@ private: } std::pair, std::unique_ptr> expandMacro(ChainValue_t* chainValue, ExpUsage usage) { - auto x = chainValue; + auto x = ast_to(chainValue->items.front())->item.to(); const auto& chainList = chainValue->items.objects(); std::string type, codes; std::tie(type, codes) = expandMacroStr(chainValue); std::string targetType(usage != ExpUsage::Common || chainList.size() > 2 ? "expr"sv : "block"sv); if (type != targetType) { - throw std::logic_error(_info.errorMessage(s("macro type mismatch, "sv) + targetType + s(" expected, got "sv) + type + '.', x)); + throw std::logic_error(_info.errorMessage(s("macro type mismatch, "sv) + targetType + s(" expected, got "sv) + type, x)); } ParseInfo info; if (usage == ExpUsage::Common) { @@ -3039,7 +3024,8 @@ private: info = _parser.parse(codes); } if (!info.node) { - throw std::logic_error(_info.errorMessage("fail to expand macro: " + info.error, x)); + info.error = info.error.substr(info.error.find(':') + 2); + throw std::logic_error(_info.errorMessage("fail to parse expanded codes: " + info.error, x)); } int line = x->m_begin.m_line; int col = x->m_begin.m_col; @@ -5002,4 +4988,3 @@ std::tuple MoonCompiler::compile(std::string } } // namespace MoonP - diff --git a/src/MoonP/stacktraceplus.h b/src/MoonP/stacktraceplus.h index a309578..fcd887a 100644 --- a/src/MoonP/stacktraceplus.h +++ b/src/MoonP/stacktraceplus.h @@ -358,7 +358,7 @@ end -- @param message An optional error string or object. -- @param level An optional number telling at which level to start the traceback (default is 1) -- --- Returns a string with the stack trace and a string with the original error. +-- Returns a string with the stack trace. -- function _M.stacktrace(thread, message, level) if type(thread) ~= "thread" then @@ -389,26 +389,16 @@ function _M.stacktrace(thread, message, level) dumper:add("\r\n}") elseif type(message) == "string" then local fname, line, msg = message:match('(.+):(%d+): (.*)$') - local nfname, nline, nmsg = fname:match('(.+):(%d+): (.*)$') - if nfname then - fname = nmsg + if fname then + local nfname, nline, nmsg = fname:match('(.+):(%d+): (.*)$') + if nfname then + fname = nmsg + end end if fname then fname = fname:gsub("%[string \"", "") fname = fname:gsub("\"%]", "") fname = fname:gsub("^%s*(.-)%s*$", "%1") - local extension = fname:match("%.([^%.\\/]*)$") - if not extension then - local fext = fname .. ".lua" - if moonp.file_exist(fext) then - fname = fext - else - fext = fname .. ".moon" - if moonp.file_exist(fext) then - fname = fext - end - end - end fname, line = getMoonLineNumber(fname, line) if _M.simplified then message = table.concat({ @@ -425,13 +415,6 @@ function _M.stacktrace(thread, message, level) dumper:add(message) end - local moonp = require("moonp") - if moonp._hide_stacktrace_ then - local msg = dumper:concat_lines() - moonp._hide_stacktrace_ = nil - return message - end - dumper:add("\r\n") dumper:add[[ Stack Traceback @@ -448,20 +431,7 @@ Stack Traceback elseif info.what == "main" or info.what == "Lua" then info.source = info.source end - local fname = info.source - local extension = fname:match("%.([^%.\\/]*)$") - if not extension then - local fext = fname .. ".lua" - if moonp.file_exist(fext) then - fname = fext - else - fext = fname .. ".moon" - if moonp.file_exist(fext) then - fname = fext - end - end - end - info.source, info.currentline = getMoonLineNumber(fname, info.currentline) + info.source, info.currentline = getMoonLineNumber(info.source, info.currentline) if info.what == "main" then if _M.simplified then dumper:add_f("(%d) '%s':%d\r\n", level_to_show, info.source, info.currentline) -- cgit v1.2.3-55-g6feb