From eb4c34ed2ff266877e90749afa512391e470321b Mon Sep 17 00:00:00 2001 From: Li Jin Date: Thu, 19 Aug 2021 00:14:08 +0800 Subject: fix a missed case for issue #64. --- spec/inputs/destructure.yue | 7 +++++++ src/yuescript/yue_ast.h | 2 +- src/yuescript/yue_compiler.cpp | 35 ++++++++++++++++++++++++++++++++--- src/yuescript/yue_parser.cpp | 6 ++---- src/yuescript/yue_parser.h | 1 - 5 files changed, 42 insertions(+), 9 deletions(-) mode change 100644 => 100755 src/yuescript/yue_ast.h mode change 100644 => 100755 src/yuescript/yue_compiler.cpp mode change 100644 => 100755 src/yuescript/yue_parser.cpp mode change 100644 => 100755 src/yuescript/yue_parser.h 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 do {x: {#: mt = {}, sub#: subFunc}} = tb + +do + {a = 1, b = 2, c: {d.e = 3}} = tb + + for {left = "null", right = false} in *tuples + print left, right + diff --git a/src/yuescript/yue_ast.h b/src/yuescript/yue_ast.h old mode 100644 new mode 100755 index 69c9607..3efc0dc --- a/src/yuescript/yue_ast.h +++ b/src/yuescript/yue_ast.h @@ -427,7 +427,7 @@ AST_NODE(normal_pair) AST_END(normal_pair) AST_NODE(default_pair) - ast_sel key; + ast_sel key; ast_ptr sep; ast_ptr value; ast_ptr defVal; diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp old mode 100644 new mode 100755 index e404cd4..f2a0b8c --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -56,7 +56,7 @@ using namespace parserlib; typedef std::list str_list; -const std::string_view version = "0.8.0"sv; +const std::string_view version = "0.8.1"sv; const std::string_view extension = "yue"sv; class YueCompilerImpl { @@ -1671,6 +1671,36 @@ private: } case id() : { auto dp = static_cast(pair); + if (auto exp = dp->key.as()) { + ++index; + if (!isAssignable(static_cast(exp))) { + throw std::logic_error(_info.errorMessage("can't destructure value"sv, exp)); + } + auto value = singleValueFrom(exp); + auto item = value->item.get(); + if (ast_is(item) || + item->getByPath()) { + throw std::logic_error(_info.errorMessage("invalid use of default value"sv, dp->defVal)); + } else { + bool lintGlobal = _config.lintGlobalVariable; + _config.lintGlobalVariable = false; + auto varName = singleVariableFrom(exp); + bool isVariable = !varName.empty(); + if (!isVariable) { + str_list temp; + transformExp(exp, temp, ExpUsage::Closure); + varName = std::move(temp.back()); + } + _config.lintGlobalVariable = lintGlobal; + pairs.push_back({ + isVariable, + varName, + '[' + std::to_string(index) + ']', + dp->defVal + }); + } + break; + } std::string keyName, valueStr; if (dp->key) { auto key = dp->key->getByPath(); @@ -1679,8 +1709,7 @@ private: if (!dp->value) valueStr = keyName; if (Keywords.find(keyName) != Keywords.end()) { keyName = "[\""s + keyName + "\"]"s; - } - else { + } else { keyName = "."s + keyName; } } diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp old mode 100644 new mode 100755 index a3e70c6..d6e8c67 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp @@ -465,7 +465,7 @@ YueParser::YueParser() { and_(expr('[')) >> LuaString | and_(expr('{')) >> TableLit); - TableValue = KeyValueDef | Exp; + TableValue = ((KeyValue | Exp) >> not_(sym('='))) | meta_default_pair | default_pair; table_lit_lines = SpaceBreak >> TableLitLine >> *(-sym(',') >> SpaceBreak >> TableLitLine) >> -sym(','); @@ -538,7 +538,7 @@ YueParser::YueParser() { symx(':') >> not_(':') >> (Exp | TableBlock | +SpaceBreak >> Exp); - default_pair = (sym(':') >> Variable >> not_('#') >> Seperator | KeyName >> symx(':') >> Seperator >> Exp) >> sym('=') >> Exp; + default_pair = (sym(':') >> Variable >> not_('#') >> Seperator | KeyName >> symx(':') >> Seperator >> Exp | Exp >> Seperator) >> sym('=') >> Exp; meta_variable_pair = sym(':') >> Variable >> expr('#'); @@ -548,8 +548,6 @@ YueParser::YueParser() { meta_default_pair = (sym(':') >> Variable >> expr('#') >> Seperator | -Name >> expr("#:") >> Seperator >> Exp) >> sym('=') >> Exp; KeyValue = variable_pair | normal_pair | meta_variable_pair | meta_normal_pair; - KeyValueDef = (variable_pair | normal_pair | meta_variable_pair | meta_normal_pair) >> not_(sym('=')) | default_pair | meta_default_pair; - KeyValueList = KeyValue >> *(sym(',') >> KeyValue); KeyValueLine = CheckIndent >> (KeyValueList >> -sym(',') | TableBlockIndent | Space >> expr('*') >> (Exp | TableBlock)); diff --git a/src/yuescript/yue_parser.h b/src/yuescript/yue_parser.h old mode 100644 new mode 100755 index 71519f3..c80e622 --- a/src/yuescript/yue_parser.h +++ b/src/yuescript/yue_parser.h @@ -148,7 +148,6 @@ private: rule CompClause; rule Chain; rule KeyValue; - rule KeyValueDef; rule single_string_inner; rule interp; rule double_string_plain; -- cgit v1.2.3-55-g6feb