diff options
author | Li Jin <dragon-fly@qq.com> | 2023-11-15 16:08:12 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2023-11-15 16:08:12 +0800 |
commit | 9dedf0f5172dd653c1d3f34b303d8bd9f5d427bd (patch) | |
tree | 0147ad4792c6e3601bf70046faa8400119c25c74 /src/yuescript/yue_compiler.cpp | |
parent | 0ca14d8e6c37f5d1360178034851f36c636a7a43 (diff) | |
download | yuescript-9dedf0f5172dd653c1d3f34b303d8bd9f5d427bd.tar.gz yuescript-9dedf0f5172dd653c1d3f34b303d8bd9f5d427bd.tar.bz2 yuescript-9dedf0f5172dd653c1d3f34b303d8bd9f5d427bd.zip |
fix a crash case for import statement.
Diffstat (limited to '')
-rw-r--r-- | src/yuescript/yue_compiler.cpp | 35 |
1 files changed, 21 insertions, 14 deletions
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 7fdf45e..8c4afce 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp | |||
@@ -75,7 +75,7 @@ static std::unordered_set<std::string> Metamethods = { | |||
75 | "close"s // Lua 5.4 | 75 | "close"s // Lua 5.4 |
76 | }; | 76 | }; |
77 | 77 | ||
78 | const std::string_view version = "0.20.5"sv; | 78 | const std::string_view version = "0.20.6"sv; |
79 | const std::string_view extension = "yue"sv; | 79 | const std::string_view extension = "yue"sv; |
80 | 80 | ||
81 | class CompileError : public std::logic_error { | 81 | class CompileError : public std::logic_error { |
@@ -950,6 +950,9 @@ private: | |||
950 | template <class T> | 950 | template <class T> |
951 | ast_ptr<false, T> toAst(std::string_view codes, ast_node* parent) { | 951 | ast_ptr<false, T> toAst(std::string_view codes, ast_node* parent) { |
952 | auto res = _parser.parse<T>(std::string(codes)); | 952 | auto res = _parser.parse<T>(std::string(codes)); |
953 | if (res.error) { | ||
954 | throw CompileError(res.error.value().msg, parent); | ||
955 | } | ||
953 | res.node->traverse([&](ast_node* node) { | 956 | res.node->traverse([&](ast_node* node) { |
954 | node->m_begin.m_line = parent->m_begin.m_line; | 957 | node->m_begin.m_line = parent->m_begin.m_line; |
955 | node->m_begin.m_col = parent->m_begin.m_col; | 958 | node->m_begin.m_col = parent->m_begin.m_col; |
@@ -8808,18 +8811,22 @@ private: | |||
8808 | return name; | 8811 | return name; |
8809 | } | 8812 | } |
8810 | 8813 | ||
8811 | void transformImportAs(ImportAs_t* import, str_list& out) { | 8814 | void transformImportAs(ImportAs_t* importNode, str_list& out) { |
8812 | ast_node* x = import; | 8815 | ast_node* x = importNode; |
8813 | if (!import->target) { | 8816 | if (!importNode->target) { |
8814 | auto name = moduleNameFrom(import->literal); | 8817 | auto name = moduleNameFrom(importNode->literal); |
8815 | import->target.set(toAst<Variable_t>(name, x)); | 8818 | if (_parser.match<Variable_t>(name)) { |
8819 | importNode->target.set(toAst<Variable_t>(name, x)); | ||
8820 | } else { | ||
8821 | throw CompileError("import module name can not be used as a variable name, try renaming it with \"as newName\" clause"sv, importNode->literal); | ||
8822 | } | ||
8816 | } | 8823 | } |
8817 | if (ast_is<ImportAllMacro_t, ImportTabLit_t>(import->target)) { | 8824 | if (ast_is<ImportAllMacro_t, ImportTabLit_t>(importNode->target)) { |
8818 | x = import->target.get(); | 8825 | x = importNode->target.get(); |
8819 | bool importAllMacro = import->target.is<ImportAllMacro_t>(); | 8826 | bool importAllMacro = importNode->target.is<ImportAllMacro_t>(); |
8820 | std::list<std::pair<std::string, std::string>> macroPairs; | 8827 | std::list<std::pair<std::string, std::string>> macroPairs; |
8821 | auto newTab = x->new_ptr<ImportTabLit_t>(); | 8828 | auto newTab = x->new_ptr<ImportTabLit_t>(); |
8822 | if (auto tabLit = import->target.as<ImportTabLit_t>()) { | 8829 | if (auto tabLit = importNode->target.as<ImportTabLit_t>()) { |
8823 | for (auto item : tabLit->items.objects()) { | 8830 | for (auto item : tabLit->items.objects()) { |
8824 | switch (item->get_id()) { | 8831 | switch (item->get_id()) { |
8825 | #ifdef YUE_NO_MACRO | 8832 | #ifdef YUE_NO_MACRO |
@@ -8859,7 +8866,7 @@ private: | |||
8859 | } | 8866 | } |
8860 | #ifndef YUE_NO_MACRO | 8867 | #ifndef YUE_NO_MACRO |
8861 | if (importAllMacro || !macroPairs.empty()) { | 8868 | if (importAllMacro || !macroPairs.empty()) { |
8862 | auto moduleName = _parser.toString(import->literal); | 8869 | auto moduleName = _parser.toString(importNode->literal); |
8863 | Utils::replace(moduleName, "'"sv, ""sv); | 8870 | Utils::replace(moduleName, "'"sv, ""sv); |
8864 | Utils::replace(moduleName, "\""sv, ""sv); | 8871 | Utils::replace(moduleName, "\""sv, ""sv); |
8865 | Utils::trim(moduleName); | 8872 | Utils::trim(moduleName); |
@@ -8935,10 +8942,10 @@ private: | |||
8935 | out.push_back(Empty); | 8942 | out.push_back(Empty); |
8936 | return; | 8943 | return; |
8937 | } else { | 8944 | } else { |
8938 | import->target.set(newTab); | 8945 | importNode->target.set(newTab); |
8939 | } | 8946 | } |
8940 | } | 8947 | } |
8941 | auto target = import->target.get(); | 8948 | auto target = importNode->target.get(); |
8942 | x = target; | 8949 | x = target; |
8943 | auto value = x->new_ptr<Value_t>(); | 8950 | auto value = x->new_ptr<Value_t>(); |
8944 | if (auto var = ast_cast<Variable_t>(target)) { | 8951 | if (auto var = ast_cast<Variable_t>(target)) { |
@@ -8994,7 +9001,7 @@ private: | |||
8994 | auto assignList = x->new_ptr<ExpList_t>(); | 9001 | auto assignList = x->new_ptr<ExpList_t>(); |
8995 | assignList->exprs.push_back(exp); | 9002 | assignList->exprs.push_back(exp); |
8996 | auto assign = x->new_ptr<Assign_t>(); | 9003 | auto assign = x->new_ptr<Assign_t>(); |
8997 | assign->values.push_back(toAst<Exp_t>("require "s + _parser.toString(import->literal), x)); | 9004 | assign->values.push_back(toAst<Exp_t>("require "s + _parser.toString(importNode->literal), x)); |
8998 | auto assignment = x->new_ptr<ExpListAssign_t>(); | 9005 | auto assignment = x->new_ptr<ExpListAssign_t>(); |
8999 | assignment->expList.set(assignList); | 9006 | assignment->expList.set(assignList); |
9000 | assignment->action.set(assign); | 9007 | assignment->action.set(assign); |