diff --git a/test/parser.cpp b/test/parser.cpp index 3dc2faf9..a58a18dd 100644 --- a/test/parser.cpp +++ b/test/parser.cpp @@ -627,61 +627,63 @@ TEST(parser, star_and_plus_collapsing) } } -TEST(parser, action) -{ - {{std::string str = ""; - std::stringstream ss; - auto action = [&ss](auto & context) { ss << _attr(context); }; - auto parser = *char_('b')[action]; - EXPECT_TRUE(parse(str, parser)); - EXPECT_EQ(ss.str(), ""); -} -{ - std::string str = "b"; - std::stringstream ss; - auto action = [&ss](auto & context) { ss << _attr(context); }; - auto parser = *char_('b')[action]; - EXPECT_TRUE(parse(str, parser)); - EXPECT_EQ(ss.str(), "b"); -} -{ - std::string str = "bb"; - std::stringstream ss; - auto action = [&ss](auto & context) { ss << _attr(context); }; - auto parser = *char_('b')[action]; - EXPECT_TRUE(parse(str, parser)); - EXPECT_TRUE(parse(str, parser)); - EXPECT_EQ(ss.str(), "bbbb"); -} -} - +TEST(parser, action_) { { std::string str = ""; - std::stringstream ss; - auto action = [&ss](auto & context) { ss << _attr(context); }; - auto parser = +char_('b')[action]; - EXPECT_FALSE(parse(str, parser)); - EXPECT_EQ(ss.str(), ""); + { + std::stringstream ss; + auto action = [&ss](auto & ctx) { ss << _attr(ctx); }; + auto parser = *char_('b')[action]; + EXPECT_TRUE(parse(str, parser)); + EXPECT_EQ(ss.str(), ""); + } + { + str = "b"; + std::stringstream ss; + auto action = [&ss](auto & ctx) { ss << _attr(ctx); }; + auto parser = *char_('b')[action]; + EXPECT_TRUE(parse(str, parser)); + EXPECT_EQ(ss.str(), "b"); + } + { + str = "bb"; + std::stringstream ss; + auto action = [&ss](auto & ctx) { ss << _attr(ctx); }; + auto parser = *char_('b')[action]; + EXPECT_TRUE(parse(str, parser)); + EXPECT_TRUE(parse(str, parser)); + EXPECT_EQ(ss.str(), "bbbb"); + } } + { - std::string str = "b"; - std::stringstream ss; - auto action = [&ss](auto & context) { ss << _attr(context); }; - auto parser = +char_('b')[action]; - EXPECT_TRUE(parse(str, parser)); - EXPECT_EQ(ss.str(), "b"); + { + std::string str = ""; + std::stringstream ss; + auto action = [&ss](auto & ctx) { ss << _attr(ctx); }; + auto parser = +char_('b')[action]; + EXPECT_FALSE(parse(str, parser)); + EXPECT_EQ(ss.str(), ""); + } + { + std::string str = "b"; + std::stringstream ss; + auto action = [&ss](auto & ctx) { ss << _attr(ctx); }; + auto parser = +char_('b')[action]; + EXPECT_TRUE(parse(str, parser)); + EXPECT_EQ(ss.str(), "b"); + } + { + std::string str = "bb"; + std::stringstream ss; + auto action = [&ss](auto & ctx) { ss << _attr(ctx); }; + auto parser = +char_('b')[action]; + EXPECT_TRUE(parse(str, parser)); + EXPECT_TRUE(parse(str, parser)); + EXPECT_EQ(ss.str(), "bbbb"); + } } - { - std::string str = "bb"; - std::stringstream ss; - auto action = [&ss](auto & context) { ss << _attr(context); }; - auto parser = +char_('b')[action]; - EXPECT_TRUE(parse(str, parser)); - EXPECT_TRUE(parse(str, parser)); - EXPECT_EQ(ss.str(), "bbbb"); - } -} } TEST(parser, star_as_string_or_vector) diff --git a/test/parser_action.cpp b/test/parser_action.cpp index 3c3ac45a..685c2d0d 100644 --- a/test/parser_action.cpp +++ b/test/parser_action.cpp @@ -14,31 +14,29 @@ constexpr rule abc_def = "abc or def"; constexpr auto abc_def_def = string("abc") | string("def"); BOOST_PARSER_DEFINE_RULES(abc_def); -auto const fail = [](auto & context) { _pass(context) = false; }; +auto const fail = [](auto & ctx) { _pass(ctx) = false; }; constexpr rule fail_abc_pass_def = "abc"; constexpr auto fail_abc_pass_def_def = string("abc")[fail] | string("def"); BOOST_PARSER_DEFINE_RULES(fail_abc_pass_def); -auto const attr_to_val = [](auto & context) { _val(context) = _attr(context); }; +auto const attr_to_val = [](auto & ctx) { _val(ctx) = _attr(ctx); }; constexpr rule action_copy_abc_def = "abc or def"; constexpr auto action_copy_abc_def_def = string("abc")[attr_to_val] | string("def")[attr_to_val]; BOOST_PARSER_DEFINE_RULES(action_copy_abc_def); -auto const abc_value = [](auto & context) { _val(context) = "abc"; }; -auto const def_value = [](auto & context) { _val(context) = "def"; }; +auto const abc_value = [](auto & ctx) { _val(ctx) = "abc"; }; +auto const def_value = [](auto & ctx) { _val(ctx) = "def"; }; constexpr rule rev_abc_def = "abc or def"; constexpr auto rev_abc_def_def = string("abc")[def_value] | string("def")[abc_value]; BOOST_PARSER_DEFINE_RULES(rev_abc_def); -auto const append_attr = [](auto & context) { - _locals(context) += _attr(context); -}; -auto const locals_to_val = [](auto & context) { - _val(context) = std::move(_locals(context)); +auto const append_attr = [](auto & ctx) { _locals(ctx) += _attr(ctx); }; +auto const locals_to_val = [](auto & ctx) { + _val(ctx) = std::move(_locals(ctx)); }; rule const locals_abc_def = "abc or def"; @@ -49,7 +47,7 @@ BOOST_PARSER_DEFINE_RULES(locals_abc_def); TEST(parser, side_effects) { int i = 0; - auto increment_i = [&i](auto & context) { ++i; }; + auto increment_i = [&i](auto & ctx) { ++i; }; using no_attribute_return = decltype(parse("xyz", char_('a')[increment_i])); static_assert(std::is_same_v);