diff options
author | Li Jin <dragon-fly@qq.com> | 2020-03-12 11:46:29 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2020-03-12 11:46:29 +0800 |
commit | d0adaf086d91f600f497c6e267a3623f3cb9012e (patch) | |
tree | 53379de1fcb833eacdf7e183257ef5cf0cb30f77 | |
parent | b2cdbc975526b710d23c41af18978afbac516240 (diff) | |
download | yuescript-d0adaf086d91f600f497c6e267a3623f3cb9012e.tar.gz yuescript-d0adaf086d91f600f497c6e267a3623f3cb9012e.tar.bz2 yuescript-d0adaf086d91f600f497c6e267a3623f3cb9012e.zip |
fix line break issue in macro, disable macro declaration outside root scope.
-rw-r--r-- | spec/inputs/macro.moon | 2 | ||||
-rw-r--r-- | src/MoonP/moon_compiler.cpp | 14 |
2 files changed, 10 insertions, 6 deletions
diff --git a/spec/inputs/macro.moon b/spec/inputs/macro.moon index 07ac7b3..9083449 100644 --- a/spec/inputs/macro.moon +++ b/spec/inputs/macro.moon | |||
@@ -77,7 +77,7 @@ macro expr curry = (...)-> | |||
77 | len = #args | 77 | len = #args |
78 | body = args[len] | 78 | body = args[len] |
79 | def = table.concat ["(#{args[i]})->" for i = 1, len - 1] | 79 | def = table.concat ["(#{args[i]})->" for i = 1, len - 1] |
80 | "#{def}\n#{body\gsub "^do\n",""}" | 80 | "#{def}\n#{body\gsub "^do%s*\n",""}" |
81 | 81 | ||
82 | f = $curry x,y,z,do | 82 | f = $curry x,y,z,do |
83 | print x,y,z | 83 | print x,y,z |
diff --git a/src/MoonP/moon_compiler.cpp b/src/MoonP/moon_compiler.cpp index 3a326b1..28db051 100644 --- a/src/MoonP/moon_compiler.cpp +++ b/src/MoonP/moon_compiler.cpp | |||
@@ -40,7 +40,7 @@ inline std::string s(std::string_view sv) { | |||
40 | } | 40 | } |
41 | 41 | ||
42 | const char* moonScriptVersion() { | 42 | const char* moonScriptVersion() { |
43 | return "0.5.0-r0.3.0"; | 43 | return "0.5.0-r0.3.2"; |
44 | } | 44 | } |
45 | 45 | ||
46 | // name of table stored in lua registry | 46 | // name of table stored in lua registry |
@@ -2155,6 +2155,9 @@ private: | |||
2155 | } | 2155 | } |
2156 | 2156 | ||
2157 | void transformMacro(Macro_t* macro, str_list& out, bool exporting) { | 2157 | void transformMacro(Macro_t* macro, str_list& out, bool exporting) { |
2158 | if (_scopes.size() > 1) { | ||
2159 | throw std::logic_error(_info.errorMessage("can not define macro outside the root block"sv, macro)); | ||
2160 | } | ||
2158 | auto type = _parser.toString(macro->type); | 2161 | auto type = _parser.toString(macro->type); |
2159 | auto macroName = _parser.toString(macro->name); | 2162 | auto macroName = _parser.toString(macro->name); |
2160 | auto argsDef = macro->macroLit->argsDef.get(); | 2163 | auto argsDef = macro->macroLit->argsDef.get(); |
@@ -2985,6 +2988,7 @@ private: | |||
2985 | } | 2988 | } |
2986 | } else str = _parser.toString(arg); | 2989 | } else str = _parser.toString(arg); |
2987 | Utils::trim(str); | 2990 | Utils::trim(str); |
2991 | Utils::replace(str, "\r\n"sv, "\n"); | ||
2988 | lua_pushlstring(L, str.c_str(), str.size()); | 2992 | lua_pushlstring(L, str.c_str(), str.size()); |
2989 | } // cur macro pcall func args... | 2993 | } // cur macro pcall func args... |
2990 | hideStackTrace(true); | 2994 | hideStackTrace(true); |
@@ -3717,14 +3721,14 @@ private: | |||
3717 | 3721 | ||
3718 | void transformLuaString(LuaString_t* luaString, str_list& out) { | 3722 | void transformLuaString(LuaString_t* luaString, str_list& out) { |
3719 | auto content = _parser.toString(luaString->content); | 3723 | auto content = _parser.toString(luaString->content); |
3720 | Utils::replace(content, "\r"sv, ""); | 3724 | Utils::replace(content, "\r\n"sv, "\n"); |
3721 | if (content[0] == '\n') content.erase(content.begin()); | 3725 | if (content[0] == '\n') content.erase(content.begin()); |
3722 | out.push_back(_parser.toString(luaString->open) + content + _parser.toString(luaString->close)); | 3726 | out.push_back(_parser.toString(luaString->open) + content + _parser.toString(luaString->close)); |
3723 | } | 3727 | } |
3724 | 3728 | ||
3725 | void transformSingleString(SingleString_t* singleString, str_list& out) { | 3729 | void transformSingleString(SingleString_t* singleString, str_list& out) { |
3726 | auto str = _parser.toString(singleString); | 3730 | auto str = _parser.toString(singleString); |
3727 | Utils::replace(str, "\r"sv, ""); | 3731 | Utils::replace(str, "\r\n"sv, "\n"); |
3728 | Utils::replace(str, "\n"sv, "\\n"sv); | 3732 | Utils::replace(str, "\n"sv, "\\n"sv); |
3729 | out.push_back(str); | 3733 | out.push_back(str); |
3730 | } | 3734 | } |
@@ -3737,7 +3741,7 @@ private: | |||
3737 | switch (content->getId()) { | 3741 | switch (content->getId()) { |
3738 | case id<double_string_inner_t>(): { | 3742 | case id<double_string_inner_t>(): { |
3739 | auto str = _parser.toString(content); | 3743 | auto str = _parser.toString(content); |
3740 | Utils::replace(str, "\r"sv, ""); | 3744 | Utils::replace(str, "\r\n"sv, "\n"); |
3741 | Utils::replace(str, "\n"sv, "\\n"sv); | 3745 | Utils::replace(str, "\n"sv, "\\n"sv); |
3742 | temp.push_back(s("\""sv) + str + s("\""sv)); | 3746 | temp.push_back(s("\""sv) + str + s("\""sv)); |
3743 | break; | 3747 | break; |
@@ -4346,7 +4350,7 @@ private: | |||
4346 | void transformExport(Export_t* exportNode, str_list& out) { | 4350 | void transformExport(Export_t* exportNode, str_list& out) { |
4347 | auto x = exportNode; | 4351 | auto x = exportNode; |
4348 | if (_scopes.size() > 1) { | 4352 | if (_scopes.size() > 1) { |
4349 | throw std::logic_error(_info.errorMessage("can not do module export outside root block"sv, x)); | 4353 | throw std::logic_error(_info.errorMessage("can not do module export outside the root block"sv, exportNode)); |
4350 | } | 4354 | } |
4351 | if (exportNode->assign) { | 4355 | if (exportNode->assign) { |
4352 | auto expList = exportNode->target.to<ExpList_t>(); | 4356 | auto expList = exportNode->target.to<ExpList_t>(); |