diff options
author | Li Jin <dragon-fly@qq.com> | 2023-12-14 13:57:40 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2023-12-14 13:57:40 +0800 |
commit | 8cff6076041f4204a53a177359783d75698ec19f (patch) | |
tree | 5bd396a4c4c0db65434629936d4867627fe711cd | |
parent | 5490a7cf22a75653a2b1fca20470f7f0c4ae9ca8 (diff) | |
download | yuescript-0.21.3.tar.gz yuescript-0.21.3.tar.bz2 yuescript-0.21.3.zip |
fix invalid formation.v0.21.3
-rw-r--r-- | spec/outputs/lists.lua | 15 | ||||
-rw-r--r-- | src/yuescript/yue_ast.cpp | 123 | ||||
-rw-r--r-- | src/yuescript/yue_compiler.cpp | 7 | ||||
-rw-r--r-- | src/yuescript/yue_parser.cpp | 2 |
4 files changed, 112 insertions, 35 deletions
diff --git a/spec/outputs/lists.lua b/spec/outputs/lists.lua index e6f306d..e2d512d 100644 --- a/spec/outputs/lists.lua +++ b/spec/outputs/lists.lua | |||
@@ -313,20 +313,7 @@ do | |||
313 | end | 313 | end |
314 | c = (1 == a) | 314 | c = (1 == a) |
315 | c = (1 == a) | 315 | c = (1 == a) |
316 | do | 316 | c = (1 == a) |
317 | local _check_0 = { | ||
318 | 1 | ||
319 | } | ||
320 | local _find_0 = false | ||
321 | for _index_0 = 1, #_check_0 do | ||
322 | local _item_0 = _check_0[_index_0] | ||
323 | if _item_0 == a then | ||
324 | _find_0 = true | ||
325 | break | ||
326 | end | ||
327 | end | ||
328 | c = _find_0 | ||
329 | end | ||
330 | end | 317 | end |
331 | do | 318 | do |
332 | a, b = hello[1], hello[2] | 319 | a, b = hello[1], hello[2] |
diff --git a/src/yuescript/yue_ast.cpp b/src/yuescript/yue_ast.cpp index 6b2c96d..69695a0 100644 --- a/src/yuescript/yue_ast.cpp +++ b/src/yuescript/yue_ast.cpp | |||
@@ -822,9 +822,11 @@ std::string Slice_t::to_string(void* ud) const { | |||
822 | if (startValue.is<Exp_t>()) { | 822 | if (startValue.is<Exp_t>()) { |
823 | temp.emplace_back(startValue->to_string(ud)); | 823 | temp.emplace_back(startValue->to_string(ud)); |
824 | } | 824 | } |
825 | temp.emplace_back(", "s); | ||
826 | if (stopValue.is<Exp_t>()) { | 825 | if (stopValue.is<Exp_t>()) { |
827 | temp.emplace_back(stopValue->to_string(ud)); | 826 | temp.emplace_back(); |
827 | temp.emplace_back(", "s + stopValue->to_string(ud)); | ||
828 | } else { | ||
829 | temp.emplace_back(","s); | ||
828 | } | 830 | } |
829 | if (stepValue.is<Exp_t>()) { | 831 | if (stepValue.is<Exp_t>()) { |
830 | temp.emplace_back(", "s + stepValue->to_string(ud)); | 832 | temp.emplace_back(", "s + stepValue->to_string(ud)); |
@@ -832,10 +834,15 @@ std::string Slice_t::to_string(void* ud) const { | |||
832 | auto valueStr = join(temp); | 834 | auto valueStr = join(temp); |
833 | return '[' + (valueStr[0] == '[' ? " "s : ""s) + valueStr + ']'; | 835 | return '[' + (valueStr[0] == '[' ? " "s : ""s) + valueStr + ']'; |
834 | } | 836 | } |
835 | static bool isInBlockExp(ast_node* node) { | 837 | static bool isInBlockExp(ast_node* node, bool last = false) { |
836 | if (auto exp = ast_cast<Exp_t>(node)) { | 838 | if (auto exp = ast_cast<Exp_t>(node)) { |
837 | auto unaryExp = static_cast<UnaryExp_t*>(exp->pipeExprs.front()); | 839 | UnaryExp_t* unaryExp = nullptr; |
838 | auto value = static_cast<Value_t*>(unaryExp->expos.front()); | 840 | if (exp->opValues.empty()) { |
841 | unaryExp = static_cast<UnaryExp_t*>(exp->pipeExprs.back()); | ||
842 | } else { | ||
843 | unaryExp = static_cast<UnaryExp_t*>(static_cast<ExpOpValue_t*>(exp->opValues.back())->pipeExprs.back()); | ||
844 | } | ||
845 | auto value = static_cast<Value_t*>(unaryExp->expos.back()); | ||
839 | if (auto simpleValue = value->item.as<SimpleValue_t>()) { | 846 | if (auto simpleValue = value->item.as<SimpleValue_t>()) { |
840 | if (!ast_is<TableLit_t, ConstValue_t, Num_t, VarArg_t, | 847 | if (!ast_is<TableLit_t, ConstValue_t, Num_t, VarArg_t, |
841 | TblComprehension_t, Comprehension_t>(simpleValue->value)) { | 848 | TblComprehension_t, Comprehension_t>(simpleValue->value)) { |
@@ -845,9 +852,76 @@ static bool isInBlockExp(ast_node* node) { | |||
845 | if (ast_is<InvokeArgs_t>(chainValue->items.back())) { | 852 | if (ast_is<InvokeArgs_t>(chainValue->items.back())) { |
846 | return true; | 853 | return true; |
847 | } | 854 | } |
855 | } else if (!last && value->item.is<SimpleTable_t>()) { | ||
856 | return true; | ||
848 | } | 857 | } |
849 | } else if (ast_is<TableBlock_t>(node)) { | 858 | } else if (ast_is<TableBlock_t>(node)) { |
850 | return true; | 859 | return true; |
860 | } else { | ||
861 | switch (node->get_id()) { | ||
862 | case id<VariablePairDef_t>(): { | ||
863 | auto pair = static_cast<VariablePairDef_t*>(node); | ||
864 | if (pair->defVal) { | ||
865 | return true; | ||
866 | } | ||
867 | return false; | ||
868 | } | ||
869 | case id<NormalPairDef_t>(): { | ||
870 | auto pair = static_cast<NormalPairDef_t*>(node); | ||
871 | if (pair->defVal) { | ||
872 | return true; | ||
873 | } | ||
874 | return isInBlockExp(pair->pair->value); | ||
875 | } | ||
876 | case id<SpreadExp_t>(): { | ||
877 | auto pair = static_cast<SpreadExp_t*>(node); | ||
878 | return isInBlockExp(pair->exp); | ||
879 | } | ||
880 | case id<NormalDef_t>(): { | ||
881 | auto pair = static_cast<NormalDef_t*>(node); | ||
882 | if (pair->defVal) { | ||
883 | return true; | ||
884 | } | ||
885 | return isInBlockExp(pair->item); | ||
886 | } | ||
887 | case id<MetaVariablePairDef_t>(): { | ||
888 | auto pair = static_cast<MetaVariablePairDef_t*>(node); | ||
889 | if (pair->defVal) { | ||
890 | return true; | ||
891 | } | ||
892 | return false; | ||
893 | } | ||
894 | case id<MetaNormalPairDef_t>(): { | ||
895 | auto pair = static_cast<MetaNormalPairDef_t*>(node); | ||
896 | if (pair->defVal) { | ||
897 | return true; | ||
898 | } | ||
899 | return isInBlockExp(pair->pair->value); | ||
900 | } | ||
901 | case id<VariablePair_t>(): { | ||
902 | return false; | ||
903 | } | ||
904 | case id<NormalPair_t>(): { | ||
905 | auto pair = static_cast<NormalPair_t*>(node); | ||
906 | return isInBlockExp(pair->value); | ||
907 | } | ||
908 | case id<MetaVariablePair_t>(): { | ||
909 | return false; | ||
910 | } | ||
911 | case id<MetaNormalPair_t>(): { | ||
912 | auto pair = static_cast<MetaNormalPair_t*>(node); | ||
913 | return isInBlockExp(pair->value); | ||
914 | } | ||
915 | case id<TableBlockIndent_t>(): { | ||
916 | return true; | ||
917 | } | ||
918 | case id<SpreadListExp_t>(): { | ||
919 | auto pair = static_cast<SpreadListExp_t*>(node); | ||
920 | return isInBlockExp(pair->exp); | ||
921 | } | ||
922 | default: | ||
923 | return false; | ||
924 | } | ||
851 | } | 925 | } |
852 | return false; | 926 | return false; |
853 | } | 927 | } |
@@ -865,7 +939,7 @@ std::string Invoke_t::to_string(void* ud) const { | |||
865 | auto info = reinterpret_cast<YueFormat*>(ud); | 939 | auto info = reinterpret_cast<YueFormat*>(ud); |
866 | bool hasInBlockExp = false; | 940 | bool hasInBlockExp = false; |
867 | for (auto arg : args.objects()) { | 941 | for (auto arg : args.objects()) { |
868 | if (arg != args.back() && isInBlockExp(arg)) { | 942 | if (isInBlockExp(arg, arg == args.back())) { |
869 | hasInBlockExp = true; | 943 | hasInBlockExp = true; |
870 | break; | 944 | break; |
871 | } | 945 | } |
@@ -899,15 +973,30 @@ std::string TableLit_t::to_string(void* ud) const { | |||
899 | if (values.empty()) { | 973 | if (values.empty()) { |
900 | return "{ }"s; | 974 | return "{ }"s; |
901 | } | 975 | } |
902 | str_list temp; | 976 | bool hasInBlockExp = false; |
903 | temp.emplace_back("{"s); | ||
904 | info->pushScope(); | ||
905 | for (auto value : values.objects()) { | 977 | for (auto value : values.objects()) { |
906 | temp.emplace_back(info->ind() + value->to_string(ud)); | 978 | if (isInBlockExp(value, value == values.back())) { |
979 | hasInBlockExp = true; | ||
980 | break; | ||
981 | } | ||
982 | } | ||
983 | if (hasInBlockExp) { | ||
984 | str_list temp; | ||
985 | temp.emplace_back("{"s); | ||
986 | info->pushScope(); | ||
987 | for (auto value : values.objects()) { | ||
988 | temp.emplace_back(info->ind() + value->to_string(ud)); | ||
989 | } | ||
990 | info->popScope(); | ||
991 | temp.emplace_back(info->ind() + '}'); | ||
992 | return join(temp, "\n"sv); | ||
993 | } else { | ||
994 | str_list temp; | ||
995 | for (auto value : values.objects()) { | ||
996 | temp.emplace_back(value->to_string(ud)); | ||
997 | } | ||
998 | return '{' + join(temp, ", "sv) + '}'; | ||
907 | } | 999 | } |
908 | info->popScope(); | ||
909 | temp.emplace_back(info->ind() + '}'); | ||
910 | return join(temp, "\n"sv); | ||
911 | } | 1000 | } |
912 | std::string TableBlock_t::to_string(void* ud) const { | 1001 | std::string TableBlock_t::to_string(void* ud) const { |
913 | auto info = reinterpret_cast<YueFormat*>(ud); | 1002 | auto info = reinterpret_cast<YueFormat*>(ud); |
@@ -1108,7 +1197,7 @@ std::string FnArgDefList_t::to_string(void* ud) const { | |||
1108 | bool hasInBlockExp = false; | 1197 | bool hasInBlockExp = false; |
1109 | for (auto def : definitions.objects()) { | 1198 | for (auto def : definitions.objects()) { |
1110 | auto argDef = static_cast<FnArgDef_t*>(def); | 1199 | auto argDef = static_cast<FnArgDef_t*>(def); |
1111 | if (argDef->defaultValue && isInBlockExp(argDef->defaultValue)) { | 1200 | if (argDef->defaultValue && isInBlockExp(argDef->defaultValue, argDef == definitions.back())) { |
1112 | hasInBlockExp = true; | 1201 | hasInBlockExp = true; |
1113 | break; | 1202 | break; |
1114 | } | 1203 | } |
@@ -1144,7 +1233,7 @@ std::string FnArgsDef_t::to_string(void* ud) const { | |||
1144 | if (defList) { | 1233 | if (defList) { |
1145 | for (auto def : defList->definitions.objects()) { | 1234 | for (auto def : defList->definitions.objects()) { |
1146 | auto argDef = static_cast<FnArgDef_t*>(def); | 1235 | auto argDef = static_cast<FnArgDef_t*>(def); |
1147 | if (argDef->defaultValue && isInBlockExp(argDef->defaultValue)) { | 1236 | if (argDef->defaultValue && isInBlockExp(argDef->defaultValue, argDef == defList->definitions.back())) { |
1148 | hasInBlockExp = true; | 1237 | hasInBlockExp = true; |
1149 | break; | 1238 | break; |
1150 | } | 1239 | } |
@@ -1250,7 +1339,7 @@ std::string InvokeArgs_t::to_string(void* ud) const { | |||
1250 | auto info = reinterpret_cast<YueFormat*>(ud); | 1339 | auto info = reinterpret_cast<YueFormat*>(ud); |
1251 | bool hasInBlockExp = false; | 1340 | bool hasInBlockExp = false; |
1252 | for (auto arg : args.objects()) { | 1341 | for (auto arg : args.objects()) { |
1253 | if (isInBlockExp(arg)) { | 1342 | if (isInBlockExp(arg, arg == args.back())) { |
1254 | hasInBlockExp = true; | 1343 | hasInBlockExp = true; |
1255 | break; | 1344 | break; |
1256 | } | 1345 | } |
@@ -1309,7 +1398,7 @@ std::string InDiscrete_t::to_string(void* ud) const { | |||
1309 | for (auto value : values.objects()) { | 1398 | for (auto value : values.objects()) { |
1310 | temp.emplace_back(value->to_string(ud)); | 1399 | temp.emplace_back(value->to_string(ud)); |
1311 | } | 1400 | } |
1312 | return '{' + join(temp, ", "sv) + '}'; | 1401 | return '[' + join(temp, ", "sv) + (temp.size() == 1 ? ",]"s : "]"s); |
1313 | } | 1402 | } |
1314 | std::string In_t::to_string(void* ud) const { | 1403 | std::string In_t::to_string(void* ud) const { |
1315 | return (not_ ? "not "s : ""s) + "in "s + item->to_string(ud); | 1404 | return (not_ ? "not "s : ""s) + "in "s + item->to_string(ud); |
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 59f6d4b..044ee91 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.21.2"sv; | 78 | const std::string_view version = "0.21.3"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 { |
@@ -7713,8 +7713,9 @@ private: | |||
7713 | clsDecl = value->get_by_path<SimpleValue_t, ClassDecl_t>(); | 7713 | clsDecl = value->get_by_path<SimpleValue_t, ClassDecl_t>(); |
7714 | BLOCK_END | 7714 | BLOCK_END |
7715 | } else if (auto expList = expListFrom(statement)) { | 7715 | } else if (auto expList = expListFrom(statement)) { |
7716 | auto value = singleValueFrom(expList); | 7716 | if (auto value = singleValueFrom(expList)) { |
7717 | clsDecl = value->get_by_path<SimpleValue_t, ClassDecl_t>(); | 7717 | clsDecl = value->get_by_path<SimpleValue_t, ClassDecl_t>(); |
7718 | } | ||
7718 | } | 7719 | } |
7719 | if (clsDecl) { | 7720 | if (clsDecl) { |
7720 | std::string clsName; | 7721 | std::string clsName; |
diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index 89791d6..3557534 100644 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp | |||
@@ -521,7 +521,7 @@ YueParser::YueParser() { | |||
521 | NotIn = true_(); | 521 | NotIn = true_(); |
522 | InDiscrete = | 522 | InDiscrete = |
523 | '[' >> Seperator >> space >> exp_not_tab >> (+(space >> ',' >> space >> exp_not_tab) | space >> ',') >> space >> ']' | | 523 | '[' >> Seperator >> space >> exp_not_tab >> (+(space >> ',' >> space >> exp_not_tab) | space >> ',') >> space >> ']' | |
524 | '{' >> Seperator >> space >> exp_not_tab >> *(space >> ',' >> space >> exp_not_tab) >> space >> '}'; | 524 | '{' >> Seperator >> space >> exp_not_tab >> *(space >> ',' >> space >> exp_not_tab | space >> ',') >> space >> '}'; |
525 | In = -(key("not") >> NotIn >> space) >> key("in") >> space >> (InDiscrete | and_(key("not")) >> confusing_unary_not_error | Exp); | 525 | In = -(key("not") >> NotIn >> space) >> key("in") >> space >> (InDiscrete | and_(key("not")) >> confusing_unary_not_error | Exp); |
526 | 526 | ||
527 | UnaryOperator = | 527 | UnaryOperator = |