From 90e5ee7ee432f558b3c55d79fba1fcb2a8ce502f Mon Sep 17 00:00:00 2001 From: Li Jin Date: Wed, 10 Feb 2021 20:45:46 +0800 Subject: fix issue #38 with a better solution. --- src/MoonP/moon_compiler.cpp | 2 +- src/MoonP/moon_parser.cpp | 22 ++++++++++++++++++++-- src/MoonP/moon_parser.h | 5 +++++ 3 files changed, 26 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/MoonP/moon_compiler.cpp b/src/MoonP/moon_compiler.cpp index 11d4ed8..2ed153e 100644 --- a/src/MoonP/moon_compiler.cpp +++ b/src/MoonP/moon_compiler.cpp @@ -53,7 +53,7 @@ inline std::string s(std::string_view sv) { return std::string(sv); } -const std::string_view version = "0.6.0"sv; +const std::string_view version = "0.6.1"sv; const std::string_view extension = "mp"sv; class MoonCompilerImpl { diff --git a/src/MoonP/moon_parser.cpp b/src/MoonP/moon_parser.cpp index f15492f..aafe730 100644 --- a/src/MoonP/moon_parser.cpp +++ b/src/MoonP/moon_parser.cpp @@ -219,7 +219,7 @@ MoonParser::MoonParser() { Return = key("return") >> -ExpListLow; - WithExp = ExpList >> -Assign; + WithExp = DisableChainBlock >> ensure(ExpList >> -Assign, PopChainBlock); With = key("with") >> -existential_op >> DisableDo >> ensure(WithExp, PopDo) >> -key("do") >> Body; SwitchCase = key("when") >> ExpList >> -key("then") >> Body; @@ -338,7 +338,25 @@ MoonParser::MoonParser() { exp_op_value = Space >> BinaryOperator >> *SpaceBreak >> backcall_exp; Exp = Seperator >> backcall_exp >> *exp_op_value; - ChainValue = Seperator >> (Chain | Callable) >> -existential_op >> -InvokeArgs; + DisableChainBlock = pl::user(true_(), [](const item_t& item) { + State* st = reinterpret_cast(item.user_data); + st->chainBlockStack.push(false); + return true; + }); + + PopChainBlock = pl::user(true_(), [](const item_t& item) { + State* st = reinterpret_cast(item.user_data); + st->chainBlockStack.pop(); + return true; + }); + + chain_line = CheckIndent >> (chain_item | Space >> (chain_dot_chain | ColonChain)) >> -InvokeArgs; + chain_block = pl::user(true_(), [](const item_t& item) { + State* st = reinterpret_cast(item.user_data); + return st->chainBlockStack.empty() || st->chainBlockStack.top(); + }) >> +SpaceBreak >> Advance >> ensure( + chain_line >> *(+SpaceBreak >> chain_line), PopIndent); + ChainValue = Seperator >> (Chain | Callable) >> -existential_op >> (chain_block | -InvokeArgs); simple_table = Seperator >> KeyValue >> *(sym(',') >> KeyValue); Value = SimpleValue | simple_table | ChainValue | String; diff --git a/src/MoonP/moon_parser.h b/src/MoonP/moon_parser.h index bee3c9c..66f2cd9 100644 --- a/src/MoonP/moon_parser.h +++ b/src/MoonP/moon_parser.h @@ -78,6 +78,7 @@ protected: std::string buffer; std::stack indents; std::stack doStack; + std::stack chainBlockStack; }; template @@ -155,6 +156,10 @@ private: rule ColonChain; rule chain_with_colon; rule ChainItem; + rule chain_line; + rule chain_block; + rule DisableChainBlock; + rule PopChainBlock; rule Index; rule invoke_chain; rule TableValue; -- cgit v1.2.3-55-g6feb