From ea82666506b57d6e905b7f2e5fe78498fe5a7abd Mon Sep 17 00:00:00 2001 From: Li Jin Date: Mon, 19 Apr 2021 20:56:05 +0800 Subject: fix issue #49 and issue #50. --- spec/inputs/tables.yue | 18 ++++++++++++++++++ src/yuescript/yue_compiler.cpp | 4 ++-- src/yuescript/yue_parser.cpp | 34 ++++++++++++++++++++++++++-------- src/yuescript/yue_parser.h | 4 ++++ 4 files changed, 50 insertions(+), 10 deletions(-) diff --git a/spec/inputs/tables.yue b/spec/inputs/tables.yue index 2c6dff9..4cd9b4d 100644 --- a/spec/inputs/tables.yue +++ b/spec/inputs/tables.yue @@ -219,5 +219,23 @@ items = name: "necklace" amount: 1 +menus = + * text: "Save" + sub: + * text: "Slot 1: " .. (slots[1].name or "None") + click: -> + * text: {"Slot 2"} + click: -> + * text: [[Slot 3]] + click: -> + * text: ("Slot 4")\name! + click: -> + * text: ({{"slot5"}})[1]\name! + click: -> + [6]: { + text: ("Slot 6") + click: -> + } + nil diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 85744d5..bc55eb3 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -59,7 +59,7 @@ inline std::string s(std::string_view sv) { return std::string(sv); } -const std::string_view version = "0.7.7"sv; +const std::string_view version = "0.7.8"sv; const std::string_view extension = "yue"sv; class YueCompilerImpl { @@ -4111,7 +4111,7 @@ private: transformExp(star_exp->value, temp, ExpUsage::Closure); if (newListVal) _buf << indent() << "local "sv << listVar << " = "sv << temp.back() << nll(nameList); _buf << indent() << "for "sv << indexVar << " = 1, #"sv << listVar << " do"sv << nlr(loopTarget); - _buf << indent(1) << "local "sv << join(vars) << " = "sv << listVar << "["sv << indexVar << "]"sv << nll(nameList); + _buf << indent(1) << "local "sv << join(vars, ", "sv) << " = "sv << listVar << "["sv << indexVar << "]"sv << nll(nameList); out.push_back(clearBuf()); } break; diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index 30c5598..185d54b 100644 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp @@ -75,6 +75,7 @@ YueParser::YueParser() { #define disable_do(patt) (DisableDo >> ((patt) >> EnableDo | EnableDo >> Cut)) #define disable_chain(patt) (DisableChain >> ((patt) >> EnableChain | EnableChain >> Cut)) #define disable_do_chain(patt) (DisableDoChain >> ((patt) >> EnableDoChain | EnableDoChain >> Cut)) + #define disable_arg_table_block(patt) (DisableArgTableBlock >> ((patt) >> EnableArgTableBlock | EnableArgTableBlock >> Cut)) #define plain_body_with(str) (-key(str) >> InBlock | key(str) >> Statement) #define plain_body (InBlock | Statement) @@ -290,6 +291,18 @@ YueParser::YueParser() { return true; }); + DisableArgTableBlock = pl::user(true_(), [](const item_t& item) { + State* st = reinterpret_cast(item.user_data); + st->noTableBlockStack.push(true); + return true; + }); + + EnableArgTableBlock = pl::user(true_(), [](const item_t& item) { + State* st = reinterpret_cast(item.user_data); + st->noTableBlockStack.pop(); + return true; + }); + Comprehension = sym('[') >> Exp >> CompInner >> sym(']'); comp_value = sym(',') >> Exp; TblComprehension = sym('{') >> Exp >> -comp_value >> CompInner >> sym('}'); @@ -472,8 +485,9 @@ YueParser::YueParser() { TableBlockInner = Seperator >> KeyValueLine >> *(+SpaceBreak >> KeyValueLine); TableBlock = +SpaceBreak >> Advance >> ensure(TableBlockInner, PopIndent); - TableBlockIndent = sym('*') >> Seperator >> KeyValueList >> -sym(',') >> - -(+SpaceBreak >> Advance >> ensure(KeyValueList >> -sym(',') >> *(+SpaceBreak >> KeyValueLine), PopIndent)); + TableBlockIndent = sym('*') >> Seperator >> disable_arg_table_block( + KeyValueList >> -sym(',') >> + -(+SpaceBreak >> Advance >> ensure(KeyValueList >> -sym(',') >> *(+SpaceBreak >> KeyValueLine), PopIndent))); class_member_list = Seperator >> KeyValue >> *(sym(',') >> KeyValue); ClassLine = CheckIndent >> (class_member_list | Statement) >> -sym(','); @@ -571,18 +585,22 @@ YueParser::YueParser() { ArgLine = CheckIndent >> Exp >> *(sym(',') >> Exp); ArgBlock = ArgLine >> *(sym(',') >> SpaceBreak >> ArgLine) >> PopIndent; + arg_table_block = pl::user(true_(), [](const item_t& item) { + State* st = reinterpret_cast(item.user_data); + return st->noTableBlockStack.empty() || !st->noTableBlockStack.top(); + }) >> TableBlock; + invoke_args_with_table = - sym(',') >> - ( + sym(',') >> ( TableBlock | - SpaceBreak >> Advance >> ArgBlock >> -TableBlock - ); + SpaceBreak >> Advance >> ArgBlock >> -arg_table_block + ) | arg_table_block; InvokeArgs = not_(set("-~")) >> Seperator >> ( - Exp >> *(sym(',') >> Exp) >> -(invoke_args_with_table | TableBlock) | - TableBlock + (Exp >> *(sym(',') >> Exp) >> -invoke_args_with_table) | + arg_table_block ); const_value = (expr("nil") | expr("true") | expr("false")) >> not_(AlphaNum); diff --git a/src/yuescript/yue_parser.h b/src/yuescript/yue_parser.h index cb9cfba..b3c311a 100644 --- a/src/yuescript/yue_parser.h +++ b/src/yuescript/yue_parser.h @@ -79,6 +79,7 @@ protected: std::stack indents; std::stack doStack; std::stack chainBlockStack; + std::stack noTableBlockStack; }; template @@ -136,6 +137,8 @@ private: rule EnableChain; rule DisableDoChain; rule EnableDoChain; + rule DisableArgTableBlock; + rule EnableArgTableBlock; rule SwitchElse; rule SwitchBlock; rule IfElseIf; @@ -175,6 +178,7 @@ private: rule ArgLine; rule ArgBlock; rule invoke_args_with_table; + rule arg_table_block; rule PipeOperator; rule ExponentialOperator; rule pipe_value; -- cgit v1.2.3-55-g6feb