From 70d8bc6d5396799c71ee97a7f98791779c8f1a37 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Fri, 29 Apr 2022 08:57:42 +0800 Subject: prevent text mode macro from inserting line numbers in generated codes. --- doc/docs/doc/README.md | 6 +++--- src/yuescript/yue_compiler.cpp | 21 ++++++++++++--------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/doc/docs/doc/README.md b/doc/docs/doc/README.md index cc98538..abff593 100755 --- a/doc/docs/doc/README.md +++ b/doc/docs/doc/README.md @@ -417,7 +417,7 @@ merge = {...a, ...b} The **#** operator can be used as a shortcut for metatable manipulation. -* **Metatable Creation** +* **Metatable Creation** Create normal table with key **#** or metamethod key that ends with **#**. ```moonscript @@ -453,7 +453,7 @@ close _ = close#: -> print "out of scope" -* **Metatable Accessing** +* **Metatable Accessing** Accessing metatable with key **#** or metamethod key that ends with **#**. ```moonscript @@ -476,7 +476,7 @@ print tb.item -* **Metatable Destructure** +* **Metatable Destructure** Destruct metatable with metamethod key that ends with **#**. ```moonscript diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 476494e..1546b85 100755 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -60,7 +60,7 @@ using namespace parserlib; typedef std::list str_list; -const std::string_view version = "0.10.15"sv; +const std::string_view version = "0.10.16"sv; const std::string_view extension = "yue"sv; class YueCompilerImpl { @@ -4244,6 +4244,8 @@ private: } else { codes = lua_tostring(L, -1); } + Utils::trim(codes); + Utils::replace(codes, "\r\n"sv, "\n"sv); return {type, codes, std::move(localVars)}; } @@ -4266,11 +4268,20 @@ private: std::string err = lua_tostring(L, -1); throw std::logic_error(_info.errorMessage(err, x)); } + if (!codes.empty()) { + if (_config.reserveLineNumber) { + codes.insert(0, nll(chainValue).substr(1)); + } + codes.append(nlr(chainValue)); + } return {nullptr, nullptr, std::move(codes), std::move(localVars)}; } else if (type == "text"sv) { if (!isBlock) { throw std::logic_error(_info.errorMessage("text macro can only be placed where block macro is allowed"sv, x)); } + if (!codes.empty()) { + codes.append(_newLine); + } return {nullptr, nullptr, std::move(codes), std::move(localVars)}; } else { if (!codes.empty()) { @@ -4370,15 +4381,7 @@ private: std::string luaCodes; str_list localVars; std::tie(node, codes, luaCodes, localVars) = expandMacro(chainValue, usage, allowBlockMacroReturn); - Utils::replace(luaCodes, "\r\n"sv, "\n"sv); - Utils::trim(luaCodes); if (!node) { - if (!luaCodes.empty()) { - if (_config.reserveLineNumber) { - luaCodes.insert(0, nll(chainValue).substr(1)); - } - luaCodes.append(nlr(chainValue)); - } out.push_back(luaCodes); if (!localVars.empty()) { for (const auto& var : localVars) { -- cgit v1.2.3-55-g6feb