diff --git a/include/boost/spirit/x4/core/action_context.hpp b/include/boost/spirit/x4/core/action_context.hpp index 065b16010..2bf2899ba 100644 --- a/include/boost/spirit/x4/core/action_context.hpp +++ b/include/boost/spirit/x4/core/action_context.hpp @@ -16,20 +16,20 @@ namespace boost::spirit::x4 { namespace contexts { -// _val +// _rule_var // Refers to the attribute of `x4::rule`. // This always points to the *innermost* attribute for the (potentially recursive) invocation of `x4::rule`. // Note: we currently provide no method to obtain the outer attribute, as it is discouraged for maintainability. // // This does NOT point to the attribute variable created by `x4::as(...)`. -struct rule_val +struct rule_var { static constexpr bool is_unique = true; }; -// _as_val +// _as_var // Refers to the attribute of `x4::as(...)`. -struct as_val +struct as_var { static constexpr bool is_unique = true; }; @@ -42,9 +42,6 @@ struct attr } // contexts -using rule_val_context_tag [[deprecated("Use `x4::contexts::rule_val`")]] = contexts::rule_val; -using attr_context_tag [[deprecated("Use `x4::contexts::attr`")]] = contexts::attr; - namespace detail { @@ -55,26 +52,26 @@ struct _pass_fn operator()(Context const&) = delete; // `_pass(ctx)` is obsolete. Just return bool from semantic action. }; -struct _val_fn +struct _rule_var_fn { template [[nodiscard]] static constexpr auto&& operator()(Context const& ctx BOOST_SPIRIT_LIFETIMEBOUND) noexcept { - return x4::get(ctx); + return x4::get(ctx); } template static void operator()(Context const&&) = delete; // dangling }; -struct _as_val_fn +struct _as_var_fn { template [[nodiscard]] static constexpr auto&& operator()(Context const& ctx BOOST_SPIRIT_LIFETIMEBOUND) noexcept { - return x4::get(ctx); + return x4::get(ctx); } template @@ -106,9 +103,14 @@ struct _attr_fn inline namespace cpos { [[maybe_unused]] inline constexpr detail::_pass_fn _pass{}; -[[maybe_unused]] inline constexpr detail::_val_fn _val{}; -[[maybe_unused]] inline constexpr detail::_as_val_fn _as_val{}; -[[maybe_unused]] inline constexpr detail::_where_fn _where{}; + +[[maybe_unused]] inline constexpr detail::_rule_var_fn _rule_var{}; + +[[maybe_unused, deprecated("Use `_rule_var`")]] +inline constexpr detail::_rule_var_fn _val{}; + +[[maybe_unused]] inline constexpr detail::_as_var_fn _as_var{}; +[[maybe_unused]] inline constexpr detail::_where_fn _where{}; // obsolete [[maybe_unused]] inline constexpr detail::_attr_fn _attr{}; } // cpos diff --git a/include/boost/spirit/x4/directive/as.hpp b/include/boost/spirit/x4/directive/as.hpp index ef6e14869..aaee6d7db 100644 --- a/include/boost/spirit/x4/directive/as.hpp +++ b/include/boost/spirit/x4/directive/as.hpp @@ -22,7 +22,7 @@ struct as_directive_ctx_impl // false template struct as_directive_ctx_impl { - using type = std::remove_cvref_t( + using type = std::remove_cvref_t( std::declval(), std::declval() ))>; @@ -47,7 +47,7 @@ struct as_directive : unary_parser> static constexpr bool has_action = false; // Explicitly re-enable attribute detection in `x4::rule` private: - static constexpr bool need_as_val = Subject::has_action; + static constexpr bool need_as_var = Subject::has_action; public: // `as(as(subject))` forwards the outer `T&` for `subject` @@ -55,10 +55,10 @@ struct as_directive : unary_parser> requires std::same_as, T> [[nodiscard]] constexpr bool parse(It& first, Se const& last, Context const& ctx, OuterAttr& outer_attr) const - noexcept(is_nothrow_parsable_v::type, OuterAttr>) + noexcept(is_nothrow_parsable_v::type, OuterAttr>) { - if constexpr (need_as_val) { - return this->subject.parse(first, last, x4::replace_first_context(ctx, outer_attr), outer_attr); + if constexpr (need_as_var) { + return this->subject.parse(first, last, x4::replace_first_context(ctx, outer_attr), outer_attr); } else { return this->subject.parse(first, last, ctx, outer_attr); } @@ -70,10 +70,10 @@ struct as_directive : unary_parser> (!std::same_as, T>) [[nodiscard]] constexpr bool parse(It& first, Se const& last, Context const& ctx, OuterAttr&) const - noexcept(is_nothrow_parsable_v::type, unused_type>) + noexcept(is_nothrow_parsable_v::type, unused_type>) { - if constexpr (need_as_val) { - return this->subject.parse(first, last, x4::replace_first_context(ctx, unused), unused); + if constexpr (need_as_var) { + return this->subject.parse(first, last, x4::replace_first_context(ctx, unused), unused); } else { return this->subject.parse(first, last, ctx, unused); } @@ -87,7 +87,7 @@ struct as_directive : unary_parser> [[nodiscard]] constexpr bool parse(It& first, Se const& last, Context const& ctx, OuterAttr& outer_attr) const noexcept( - is_nothrow_parsable_v::type, T> && + is_nothrow_parsable_v::type, T> && noexcept(x4::move_to(std::declval(), outer_attr)) ) { @@ -101,8 +101,8 @@ struct as_directive : unary_parser> T attr_{}; // value-initialize - if constexpr (need_as_val) { - if (!this->subject.parse(first, last, x4::replace_first_context(ctx, attr_), attr_)) return false; + if constexpr (need_as_var) { + if (!this->subject.parse(first, last, x4::replace_first_context(ctx, attr_), attr_)) return false; } else { if (!this->subject.parse(first, last, ctx, attr_)) return false; } diff --git a/include/boost/spirit/x4/rule.hpp b/include/boost/spirit/x4/rule.hpp index 3fbb3adcf..f5f5bfa17 100644 --- a/include/boost/spirit/x4/rule.hpp +++ b/include/boost/spirit/x4/rule.hpp @@ -97,7 +97,7 @@ struct rule_impl private: template using rcontext_t = std::remove_cvref_t< - decltype(x4::replace_first_context( + decltype(x4::replace_first_context( std::declval(), std::declval::type&>() )) @@ -126,7 +126,7 @@ struct rule_impl { static_assert(!need_rcontext); // sanity check - // A parse invocation does not require `_val` context if and only if it doesn't + // A parse invocation does not require `_rule_var` context if and only if it doesn't // involve any of the below features: // - Semantic action // - `RuleID::on_success(...)` @@ -134,7 +134,7 @@ struct rule_impl return ctx; } - // Replace or insert the `_val` context, while avoiding infinite instantiation on + // Replace or insert the `_rule_var` context, while avoiding infinite instantiation on // recursive grammars. // // In pre-X4 codebase, this was done by passing the extraneous `rcontext` parameter @@ -157,7 +157,7 @@ struct rule_impl [[nodiscard]] static constexpr decltype(auto) make_rcontext(Context const& ctx BOOST_SPIRIT_LIFETIMEBOUND, RHSAttr& rhs_attr BOOST_SPIRIT_LIFETIMEBOUND) noexcept { - return x4::replace_first_context(ctx, rhs_attr); + return x4::replace_first_context(ctx, rhs_attr); } public: @@ -430,7 +430,7 @@ struct rule : parser> { static_assert(has_attribute, "A rule must have an attribute. Check your rule definition."); - // Remove the `_val` context. This makes the actual `context` type passed to + // Remove the `_rule_var` context. This makes the actual `context` type passed to // the (potentially ADL-found) `parse_rule` function to be rule-agnostic. // If we don't do this, the specialized function signature becomes // nondeterministic, and we lose the opportunity to do explicit template @@ -438,8 +438,8 @@ struct rule : parser> // // Note that this removal is possible only because the actual invocation of // `parse_rule` *ALWAYS* results in subsequent invocation of `call_rule_definition`, - // which resets the `_val` context to the appropriate reference. - auto&& rule_agnostic_ctx = x4::remove_first_context(ctx); + // which resets the `_rule_var` context to the appropriate reference. + auto&& rule_agnostic_ctx = x4::remove_first_context(ctx); using detail::parse_rule; // ADL @@ -488,7 +488,7 @@ struct rule : parser> attribute_type no_attr; // default-initialize // See the comments on the primary overload of `rule::parse(...)` - auto&& rule_agnostic_ctx = x4::remove_first_context(ctx); + auto&& rule_agnostic_ctx = x4::remove_first_context(ctx); using detail::parse_rule; // ADL return static_cast(parse_rule(detail::rule_id{}, first, last, rule_agnostic_ctx, no_attr)); // NOLINT(bugprone-non-zero-enum-to-bool-conversion) diff --git a/test/x4/alternative.cpp b/test/x4/alternative.cpp index 675f7ac5b..0b86af587 100644 --- a/test/x4/alternative.cpp +++ b/test/x4/alternative.cpp @@ -152,13 +152,13 @@ TEST_CASE("alternative") using x4::rule; using x4::_attr; - using x4::_val; + using x4::_rule_var; rule r1; rule r2; rule r3; - constexpr auto f = [&](auto& ctx){ _val(ctx) = _attr(ctx); }; + constexpr auto f = [&](auto& ctx){ _rule_var(ctx) = _attr(ctx); }; (void)(r3 = (eps >> r1)[f]); (void)(r3 = (r1 | r2)[f]); diff --git a/test/x4/as.cpp b/test/x4/as.cpp index aa0d97d02..b660b260d 100644 --- a/test/x4/as.cpp +++ b/test/x4/as.cpp @@ -34,8 +34,8 @@ TEST_CASE("as") using x4::eps; using x4::string; using x4::_attr; - using x4::_val; - using x4::_as_val; + using x4::_rule_var; + using x4::_as_var; using It = std::string_view::const_iterator; using Se = It; @@ -159,18 +159,18 @@ TEST_CASE("as") } } - // `as_val(ctx)` (with auto attribute propagation) + // `_as_var(ctx)` (with auto attribute propagation) { std::string result; constexpr auto string_rule = x4::rule{""} = x4::as( eps[([](auto&& ctx) { - _val(ctx) = "default"; + _rule_var(ctx) = "default"; })] >> eps[([](auto&& ctx) { - _as_val(ctx) = "foo"; + _as_var(ctx) = "foo"; })] ); @@ -181,18 +181,18 @@ TEST_CASE("as") REQUIRE(string_rule.parse(first, last, unused, result)); CHECK(result == "foo"sv); } - // `as_val(ctx)` (with disabled attribute) + // `_as_var(ctx)` (with disabled attribute) { std::string result; constexpr auto string_rule = x4::rule{""} = x4::as( eps[([](auto&& ctx) { - _val(ctx) = "default"; + _rule_var(ctx) = "default"; })] >> eps[([]([[maybe_unused]] auto&& ctx) { - static_assert(std::same_as, unused_type>); + static_assert(std::same_as, unused_type>); })] ) >> disable_attr; // <---------- @@ -203,14 +203,14 @@ TEST_CASE("as") REQUIRE(string_rule.parse(first, last, unused, result)); CHECK(result == "default"sv); } - // `as_val(ctx)` (within `as(as(...))`) + // `_as_var(ctx)` (within `as(as(...))`) { std::string result{"default"}; constexpr auto unused_rule = x4::as( x4::as( eps[([]([[maybe_unused]] auto&& ctx) { - static_assert(std::same_as, unused_type>); + static_assert(std::same_as, unused_type>); })] ) ); @@ -222,7 +222,7 @@ TEST_CASE("as") REQUIRE(unused_rule.parse(first, last, unused, result)); CHECK(result == "default"sv); } - // `as_val(ctx)` (within `as(as(...))`) + // `_as_var(ctx)` (within `as(as(...))`) { std::string result; @@ -230,12 +230,12 @@ TEST_CASE("as") x4::attr("default") >> eps[([]([[maybe_unused]] auto&& ctx) { - static_assert(std::same_as, std::string>); + static_assert(std::same_as, std::string>); })] >> x4::as( eps[([]([[maybe_unused]] auto&& ctx) { - static_assert(std::same_as, unused_type>); + static_assert(std::same_as, unused_type>); })] ) ); @@ -248,7 +248,7 @@ TEST_CASE("as") CHECK(result == "default"sv); } - // Use `_val(ctx)` inside `as(...)` + // Use `_rule_var(ctx)` inside `as(...)` { struct StringLiteral { @@ -259,15 +259,15 @@ TEST_CASE("as") StringLiteral result; constexpr auto string_literal = x4::rule{"StringLiteral"} = - eps[([](auto& ctx) { _val(ctx).is_quoted = false; })] >> + eps[([](auto& ctx) { _rule_var(ctx).is_quoted = false; })] >> x4::as( x4::lit('"')[([](auto&& ctx) { - StringLiteral& rule_val = _val(ctx); - rule_val.is_quoted = true; + StringLiteral& rule_var = _rule_var(ctx); + rule_var.is_quoted = true; })] >> *~x4::char_('"') >> '"' - )[([](auto&& ctx) { _val(ctx).text = std::move(_attr(ctx)); })]; + )[([](auto&& ctx) { _rule_var(ctx).text = std::move(_attr(ctx)); })]; std::string_view input = R"("foo")"; It first = input.begin(); diff --git a/test/x4/rule3.cpp b/test/x4/rule3.cpp index 9c2eecd21..1fe5dac17 100644 --- a/test/x4/rule3.cpp +++ b/test/x4/rule3.cpp @@ -96,7 +96,7 @@ TEST_CASE("rule3") using x4::rule; using x4::lit; using x4::eps; - using x4::_val; + using x4::_rule_var; using x4::_attr; // synth attribute value-init @@ -105,7 +105,7 @@ TEST_CASE("rule3") using rule_type = rule; auto rdef = rule_type{} = alpha[([](auto&& ctx) { - x4::_val(ctx) += x4::_attr(ctx); + x4::_rule_var(ctx) += x4::_attr(ctx); })]; REQUIRE(parse("abcdef", +rdef, s)); @@ -119,7 +119,7 @@ TEST_CASE("rule3") auto rdef = rule_type() = alpha[([](auto&& ctx) { - _val(ctx) += _attr(ctx); + _rule_var(ctx) += _attr(ctx); })]; REQUIRE(parse("abcdef", +rdef, s)); @@ -129,7 +129,7 @@ TEST_CASE("rule3") { auto r = rule{} = eps[([] ([[maybe_unused]] auto&& ctx) { static_assert( - std::is_same_v, unused_type>, + std::is_same_v, unused_type>, "Attr must not be synthesized" ); })]; diff --git a/test/x4/rule_separate_tu.cpp b/test/x4/rule_separate_tu.cpp index bb7fbc3b9..e30b3c35e 100644 --- a/test/x4/rule_separate_tu.cpp +++ b/test/x4/rule_separate_tu.cpp @@ -69,7 +69,7 @@ TEST_CASE("rule_separate_tu") x4::contexts::expectation_failure, x4::expectation_failure, x4::context< - x4::contexts::rule_val, + x4::contexts::rule_var, int > >; @@ -80,7 +80,7 @@ TEST_CASE("rule_separate_tu") static_assert(std::same_as< std::remove_cvref_t(std::declval()) + x4::remove_first_context(std::declval()) )>, RuleAgnosticContext >); diff --git a/test/x4/with.cpp b/test/x4/with.cpp index 5f8fa107b..fc01d27ca 100644 --- a/test/x4/with.cpp +++ b/test/x4/with.cpp @@ -31,14 +31,14 @@ struct match_counter_rule_id template Se, class Failure, class Context> void on_error(It const&, Se const&, Context const& ctx, Failure const&) { - STATIC_CHECK(x4::has_context_of_v); + STATIC_CHECK(x4::has_context_of_v); ++x4::get(ctx); } template Se, class Context, x4::X4Attribute Attr> void on_success(It const&, Se const&, Context const& ctx, Attr&) { - STATIC_CHECK(x4::has_context_of_v); + STATIC_CHECK(x4::has_context_of_v); ++x4::get(ctx); } }; @@ -161,7 +161,7 @@ TEST_CASE("with") })]; auto const r2 = rule() = x4::lit('(') >> (r1 % ',') >> x4::lit(')')[([](auto&& ctx){ - x4::_val(ctx) = x4::get(ctx); + x4::_rule_var(ctx) = x4::get(ctx); })]; int attr = 0; REQUIRE(parse("(1,2,3)", with(100)[r2], attr)); @@ -183,7 +183,7 @@ TEST_CASE("with") }; auto f = [](auto&& ctx){ - x4::_val(ctx) = x4::_attr(ctx) + functor()(x4::get(ctx)); + x4::_rule_var(ctx) = x4::_attr(ctx) + functor()(x4::get(ctx)); }; auto const r = rule() = int_[f]; diff --git a/test/x4/with_local.cpp b/test/x4/with_local.cpp index 65775df88..14b3f60b0 100644 --- a/test/x4/with_local.cpp +++ b/test/x4/with_local.cpp @@ -25,7 +25,7 @@ TEST_CASE("with_local") using x4::as; using x4::_local_var; using x4::_attr; - using x4::_as_val; + using x4::_as_var; using x4::int_; using x4::eps; @@ -38,7 +38,7 @@ TEST_CASE("with_local") _local_var(ctx) = _attr(ctx) * 100; })] >> eps[([](auto&& ctx) { - _as_val(ctx) = _local_var(ctx); + _as_var(ctx) = _local_var(ctx); })] ) ];