summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2021-08-19 00:14:08 +0800
committerLi Jin <dragon-fly@qq.com>2021-08-19 00:14:08 +0800
commiteb4c34ed2ff266877e90749afa512391e470321b (patch)
treeb22fc74c288a68d0c85b9b8b03ec58602798a72c
parent38908616d0e08b72e6b00d18490ed917fa643e4f (diff)
downloadyuescript-0.8.1.tar.gz
yuescript-0.8.1.tar.bz2
yuescript-0.8.1.zip
fix a missed case for issue #64.v0.8.1
-rw-r--r--spec/inputs/destructure.yue7
-rwxr-xr-x[-rw-r--r--]src/yuescript/yue_ast.h2
-rwxr-xr-x[-rw-r--r--]src/yuescript/yue_compiler.cpp35
-rwxr-xr-x[-rw-r--r--]src/yuescript/yue_parser.cpp6
-rwxr-xr-x[-rw-r--r--]src/yuescript/yue_parser.h1
5 files changed, 42 insertions, 9 deletions
diff --git a/spec/inputs/destructure.yue b/spec/inputs/destructure.yue
index c46cef5..e1c6b27 100644
--- a/spec/inputs/destructure.yue
+++ b/spec/inputs/destructure.yue
@@ -171,3 +171,10 @@ do
171 171
172do 172do
173 {x: {#: mt = {}, sub#: subFunc}} = tb 173 {x: {#: mt = {}, sub#: subFunc}} = tb
174
175do
176 {a = 1, b = 2, c: {d.e = 3}} = tb
177
178 for {left = "null", right = false} in *tuples
179 print left, right
180
diff --git a/src/yuescript/yue_ast.h b/src/yuescript/yue_ast.h
index 69c9607..3efc0dc 100644..100755
--- a/src/yuescript/yue_ast.h
+++ b/src/yuescript/yue_ast.h
@@ -427,7 +427,7 @@ AST_NODE(normal_pair)
427AST_END(normal_pair) 427AST_END(normal_pair)
428 428
429AST_NODE(default_pair) 429AST_NODE(default_pair)
430 ast_sel<true, Variable_t, KeyName_t> key; 430 ast_sel<true, Variable_t, KeyName_t, Exp_t> key;
431 ast_ptr<true, Seperator_t> sep; 431 ast_ptr<true, Seperator_t> sep;
432 ast_ptr<false, Exp_t> value; 432 ast_ptr<false, Exp_t> value;
433 ast_ptr<true, Exp_t> defVal; 433 ast_ptr<true, Exp_t> defVal;
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp
index e404cd4..f2a0b8c 100644..100755
--- a/src/yuescript/yue_compiler.cpp
+++ b/src/yuescript/yue_compiler.cpp
@@ -56,7 +56,7 @@ using namespace parserlib;
56 56
57typedef std::list<std::string> str_list; 57typedef std::list<std::string> str_list;
58 58
59const std::string_view version = "0.8.0"sv; 59const std::string_view version = "0.8.1"sv;
60const std::string_view extension = "yue"sv; 60const std::string_view extension = "yue"sv;
61 61
62class YueCompilerImpl { 62class YueCompilerImpl {
@@ -1671,6 +1671,36 @@ private:
1671 } 1671 }
1672 case id<default_pair_t>() : { 1672 case id<default_pair_t>() : {
1673 auto dp = static_cast<default_pair_t*>(pair); 1673 auto dp = static_cast<default_pair_t*>(pair);
1674 if (auto exp = dp->key.as<Exp_t>()) {
1675 ++index;
1676 if (!isAssignable(static_cast<Exp_t*>(exp))) {
1677 throw std::logic_error(_info.errorMessage("can't destructure value"sv, exp));
1678 }
1679 auto value = singleValueFrom(exp);
1680 auto item = value->item.get();
1681 if (ast_is<simple_table_t>(item) ||
1682 item->getByPath<TableLit_t>()) {
1683 throw std::logic_error(_info.errorMessage("invalid use of default value"sv, dp->defVal));
1684 } else {
1685 bool lintGlobal = _config.lintGlobalVariable;
1686 _config.lintGlobalVariable = false;
1687 auto varName = singleVariableFrom(exp);
1688 bool isVariable = !varName.empty();
1689 if (!isVariable) {
1690 str_list temp;
1691 transformExp(exp, temp, ExpUsage::Closure);
1692 varName = std::move(temp.back());
1693 }
1694 _config.lintGlobalVariable = lintGlobal;
1695 pairs.push_back({
1696 isVariable,
1697 varName,
1698 '[' + std::to_string(index) + ']',
1699 dp->defVal
1700 });
1701 }
1702 break;
1703 }
1674 std::string keyName, valueStr; 1704 std::string keyName, valueStr;
1675 if (dp->key) { 1705 if (dp->key) {
1676 auto key = dp->key->getByPath<Name_t>(); 1706 auto key = dp->key->getByPath<Name_t>();
@@ -1679,8 +1709,7 @@ private:
1679 if (!dp->value) valueStr = keyName; 1709 if (!dp->value) valueStr = keyName;
1680 if (Keywords.find(keyName) != Keywords.end()) { 1710 if (Keywords.find(keyName) != Keywords.end()) {
1681 keyName = "[\""s + keyName + "\"]"s; 1711 keyName = "[\""s + keyName + "\"]"s;
1682 } 1712 } else {
1683 else {
1684 keyName = "."s + keyName; 1713 keyName = "."s + keyName;
1685 } 1714 }
1686 } 1715 }
diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp
index a3e70c6..d6e8c67 100644..100755
--- a/src/yuescript/yue_parser.cpp
+++ b/src/yuescript/yue_parser.cpp
@@ -465,7 +465,7 @@ YueParser::YueParser() {
465 and_(expr('[')) >> LuaString | 465 and_(expr('[')) >> LuaString |
466 and_(expr('{')) >> TableLit); 466 and_(expr('{')) >> TableLit);
467 467
468 TableValue = KeyValueDef | Exp; 468 TableValue = ((KeyValue | Exp) >> not_(sym('='))) | meta_default_pair | default_pair;
469 469
470 table_lit_lines = SpaceBreak >> TableLitLine >> *(-sym(',') >> SpaceBreak >> TableLitLine) >> -sym(','); 470 table_lit_lines = SpaceBreak >> TableLitLine >> *(-sym(',') >> SpaceBreak >> TableLitLine) >> -sym(',');
471 471
@@ -538,7 +538,7 @@ YueParser::YueParser() {
538 symx(':') >> not_(':') >> 538 symx(':') >> not_(':') >>
539 (Exp | TableBlock | +SpaceBreak >> Exp); 539 (Exp | TableBlock | +SpaceBreak >> Exp);
540 540
541 default_pair = (sym(':') >> Variable >> not_('#') >> Seperator | KeyName >> symx(':') >> Seperator >> Exp) >> sym('=') >> Exp; 541 default_pair = (sym(':') >> Variable >> not_('#') >> Seperator | KeyName >> symx(':') >> Seperator >> Exp | Exp >> Seperator) >> sym('=') >> Exp;
542 542
543 meta_variable_pair = sym(':') >> Variable >> expr('#'); 543 meta_variable_pair = sym(':') >> Variable >> expr('#');
544 544
@@ -548,8 +548,6 @@ YueParser::YueParser() {
548 meta_default_pair = (sym(':') >> Variable >> expr('#') >> Seperator | -Name >> expr("#:") >> Seperator >> Exp) >> sym('=') >> Exp; 548 meta_default_pair = (sym(':') >> Variable >> expr('#') >> Seperator | -Name >> expr("#:") >> Seperator >> Exp) >> sym('=') >> Exp;
549 549
550 KeyValue = variable_pair | normal_pair | meta_variable_pair | meta_normal_pair; 550 KeyValue = variable_pair | normal_pair | meta_variable_pair | meta_normal_pair;
551 KeyValueDef = (variable_pair | normal_pair | meta_variable_pair | meta_normal_pair) >> not_(sym('=')) | default_pair | meta_default_pair;
552
553 KeyValueList = KeyValue >> *(sym(',') >> KeyValue); 551 KeyValueList = KeyValue >> *(sym(',') >> KeyValue);
554 KeyValueLine = CheckIndent >> (KeyValueList >> -sym(',') | TableBlockIndent | Space >> expr('*') >> (Exp | TableBlock)); 552 KeyValueLine = CheckIndent >> (KeyValueList >> -sym(',') | TableBlockIndent | Space >> expr('*') >> (Exp | TableBlock));
555 553
diff --git a/src/yuescript/yue_parser.h b/src/yuescript/yue_parser.h
index 71519f3..c80e622 100644..100755
--- a/src/yuescript/yue_parser.h
+++ b/src/yuescript/yue_parser.h
@@ -148,7 +148,6 @@ private:
148 rule CompClause; 148 rule CompClause;
149 rule Chain; 149 rule Chain;
150 rule KeyValue; 150 rule KeyValue;
151 rule KeyValueDef;
152 rule single_string_inner; 151 rule single_string_inner;
153 rule interp; 152 rule interp;
154 rule double_string_plain; 153 rule double_string_plain;