Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions include/boost/spirit/x4/core/action_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(...)`.
struct rule_val
struct rule_var
{
static constexpr bool is_unique = true;
};

// _as_val
// _as_var
// Refers to the attribute of `x4::as<T>(...)`.
struct as_val
struct as_var
{
static constexpr bool is_unique = true;
};
Expand All @@ -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 {

Expand All @@ -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<class Context>
[[nodiscard]] static constexpr auto&&
operator()(Context const& ctx BOOST_SPIRIT_LIFETIMEBOUND) noexcept
{
return x4::get<contexts::rule_val>(ctx);
return x4::get<contexts::rule_var>(ctx);
}

template<class Context>
static void operator()(Context const&&) = delete; // dangling
};

struct _as_val_fn
struct _as_var_fn
{
template<class Context>
[[nodiscard]] static constexpr auto&&
operator()(Context const& ctx BOOST_SPIRIT_LIFETIMEBOUND) noexcept
{
return x4::get<contexts::as_val>(ctx);
return x4::get<contexts::as_var>(ctx);
}

template<class Context>
Expand Down Expand Up @@ -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
Expand Down
22 changes: 11 additions & 11 deletions include/boost/spirit/x4/directive/as.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct as_directive_ctx_impl // false
template<class Context, X4Attribute Attr>
struct as_directive_ctx_impl<true, Context, Attr>
{
using type = std::remove_cvref_t<decltype(x4::replace_first_context<contexts::as_val>(
using type = std::remove_cvref_t<decltype(x4::replace_first_context<contexts::as_var>(
std::declval<Context const&>(),
std::declval<Attr&>()
))>;
Expand All @@ -47,18 +47,18 @@ struct as_directive : unary_parser<Subject, as_directive<T, Subject>>
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<T>(as<T>(subject))` forwards the outer `T&` for `subject`
template<std::forward_iterator It, std::sentinel_for<It> Se, class Context, X4Attribute OuterAttr>
requires std::same_as<std::remove_const_t<OuterAttr>, T>
[[nodiscard]] constexpr bool
parse(It& first, Se const& last, Context const& ctx, OuterAttr& outer_attr) const
noexcept(is_nothrow_parsable_v<Subject, It, Se, typename detail::as_directive_ctx_impl<need_as_val, Context, OuterAttr>::type, OuterAttr>)
noexcept(is_nothrow_parsable_v<Subject, It, Se, typename detail::as_directive_ctx_impl<need_as_var, Context, OuterAttr>::type, OuterAttr>)
{
if constexpr (need_as_val) {
return this->subject.parse(first, last, x4::replace_first_context<contexts::as_val>(ctx, outer_attr), outer_attr);
if constexpr (need_as_var) {
return this->subject.parse(first, last, x4::replace_first_context<contexts::as_var>(ctx, outer_attr), outer_attr);
} else {
return this->subject.parse(first, last, ctx, outer_attr);
}
Expand All @@ -70,10 +70,10 @@ struct as_directive : unary_parser<Subject, as_directive<T, Subject>>
(!std::same_as<std::remove_const_t<OuterAttr>, T>)
[[nodiscard]] constexpr bool
parse(It& first, Se const& last, Context const& ctx, OuterAttr&) const
noexcept(is_nothrow_parsable_v<Subject, It, Se, typename detail::as_directive_ctx_impl<need_as_val, Context, unused_type>::type, unused_type>)
noexcept(is_nothrow_parsable_v<Subject, It, Se, typename detail::as_directive_ctx_impl<need_as_var, Context, unused_type>::type, unused_type>)
{
if constexpr (need_as_val) {
return this->subject.parse(first, last, x4::replace_first_context<contexts::as_val>(ctx, unused), unused);
if constexpr (need_as_var) {
return this->subject.parse(first, last, x4::replace_first_context<contexts::as_var>(ctx, unused), unused);
} else {
return this->subject.parse(first, last, ctx, unused);
}
Expand All @@ -87,7 +87,7 @@ struct as_directive : unary_parser<Subject, as_directive<T, Subject>>
[[nodiscard]] constexpr bool
parse(It& first, Se const& last, Context const& ctx, OuterAttr& outer_attr) const
noexcept(
is_nothrow_parsable_v<Subject, It, Se, typename detail::as_directive_ctx_impl<need_as_val, Context, T>::type, T> &&
is_nothrow_parsable_v<Subject, It, Se, typename detail::as_directive_ctx_impl<need_as_var, Context, T>::type, T> &&
noexcept(x4::move_to(std::declval<T>(), outer_attr))
)
{
Expand All @@ -101,8 +101,8 @@ struct as_directive : unary_parser<Subject, as_directive<T, Subject>>

T attr_{}; // value-initialize

if constexpr (need_as_val) {
if (!this->subject.parse(first, last, x4::replace_first_context<contexts::as_val>(ctx, attr_), attr_)) return false;
if constexpr (need_as_var) {
if (!this->subject.parse(first, last, x4::replace_first_context<contexts::as_var>(ctx, attr_), attr_)) return false;
} else {
if (!this->subject.parse(first, last, ctx, attr_)) return false;
}
Expand Down
16 changes: 8 additions & 8 deletions include/boost/spirit/x4/rule.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ struct rule_impl
private:
template<class Context, X4Attribute RHSAttr>
using rcontext_t = std::remove_cvref_t<
decltype(x4::replace_first_context<contexts::rule_val>(
decltype(x4::replace_first_context<contexts::rule_var>(
std::declval<Context const&>(),
std::declval<typename traits::transform_attribute<Attr, RHSAttr>::type&>()
))
Expand Down Expand Up @@ -126,15 +126,15 @@ struct rule_impl
{
static_assert(!need_rcontext<RHS, It, Context, RHSAttr>); // 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(...)`
// - `RuleID::on_error(...)`
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
Expand All @@ -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<contexts::rule_val>(ctx, rhs_attr);
return x4::replace_first_context<contexts::rule_var>(ctx, rhs_attr);
}

public:
Expand Down Expand Up @@ -430,16 +430,16 @@ struct rule : parser<rule<RuleID, RuleAttr, ForceAttr>>
{
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
// instantiation in `BOOST_SPIRIT_X4_INSTANTIATE`.
//
// 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<contexts::rule_val>(ctx);
// which resets the `_rule_var` context to the appropriate reference.
auto&& rule_agnostic_ctx = x4::remove_first_context<contexts::rule_var>(ctx);

using detail::parse_rule; // ADL

Expand Down Expand Up @@ -488,7 +488,7 @@ struct rule : parser<rule<RuleID, RuleAttr, ForceAttr>>
attribute_type no_attr; // default-initialize

// See the comments on the primary overload of `rule::parse(...)`
auto&& rule_agnostic_ctx = x4::remove_first_context<contexts::rule_val>(ctx);
auto&& rule_agnostic_ctx = x4::remove_first_context<contexts::rule_var>(ctx);

using detail::parse_rule; // ADL
return static_cast<bool>(parse_rule(detail::rule_id<RuleID>{}, first, last, rule_agnostic_ctx, no_attr)); // NOLINT(bugprone-non-zero-enum-to-bool-conversion)
Expand Down
4 changes: 2 additions & 2 deletions test/x4/alternative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,13 @@ TEST_CASE("alternative")

using x4::rule;
using x4::_attr;
using x4::_val;
using x4::_rule_var;

rule<class r1, wchar_t> r1;
rule<class r2, wchar_t> r2;
rule<class r3, wchar_t> 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]);
Expand Down
36 changes: 18 additions & 18 deletions test/x4/as.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<struct _, decltype(result)>{""} =
x4::as<std::string>(
eps[([](auto&& ctx) {
_val(ctx) = "default";
_rule_var(ctx) = "default";
})] >>

eps[([](auto&& ctx) {
_as_val(ctx) = "foo";
_as_var(ctx) = "foo";
})]
);

Expand All @@ -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<struct _, decltype(result)>{""} =
x4::as<std::string>(
eps[([](auto&& ctx) {
_val(ctx) = "default";
_rule_var(ctx) = "default";
})] >>

eps[([]([[maybe_unused]] auto&& ctx) {
static_assert(std::same_as<std::remove_cvref_t<decltype(_as_val(ctx))>, unused_type>);
static_assert(std::same_as<std::remove_cvref_t<decltype(_as_var(ctx))>, unused_type>);
})]
) >> disable_attr; // <----------

Expand All @@ -203,14 +203,14 @@ TEST_CASE("as")
REQUIRE(string_rule.parse(first, last, unused, result));
CHECK(result == "default"sv);
}
// `as_val(ctx)` (within `as<unused_type>(as<std::string>(...))`)
// `_as_var(ctx)` (within `as<unused_type>(as<std::string>(...))`)
{
std::string result{"default"};

constexpr auto unused_rule = x4::as<unused_type>(
x4::as<std::string>(
eps[([]([[maybe_unused]] auto&& ctx) {
static_assert(std::same_as<std::remove_cvref_t<decltype(_as_val(ctx))>, unused_type>);
static_assert(std::same_as<std::remove_cvref_t<decltype(_as_var(ctx))>, unused_type>);
})]
)
);
Expand All @@ -222,20 +222,20 @@ TEST_CASE("as")
REQUIRE(unused_rule.parse(first, last, unused, result));
CHECK(result == "default"sv);
}
// `as_val(ctx)` (within `as<std::string>(as<unused_type>(...))`)
// `_as_var(ctx)` (within `as<std::string>(as<unused_type>(...))`)
{
std::string result;

/*constexpr*/ auto unused_rule = x4::as<std::string>(
x4::attr("default") >>

eps[([]([[maybe_unused]] auto&& ctx) {
static_assert(std::same_as<std::remove_cvref_t<decltype(_as_val(ctx))>, std::string>);
static_assert(std::same_as<std::remove_cvref_t<decltype(_as_var(ctx))>, std::string>);
})] >>

x4::as<unused_type>(
eps[([]([[maybe_unused]] auto&& ctx) {
static_assert(std::same_as<std::remove_cvref_t<decltype(_as_val(ctx))>, unused_type>);
static_assert(std::same_as<std::remove_cvref_t<decltype(_as_var(ctx))>, unused_type>);
})]
)
);
Expand All @@ -248,7 +248,7 @@ TEST_CASE("as")
CHECK(result == "default"sv);
}

// Use `_val(ctx)` inside `as<T>(...)`
// Use `_rule_var(ctx)` inside `as<T>(...)`
{
struct StringLiteral
{
Expand All @@ -259,15 +259,15 @@ TEST_CASE("as")
StringLiteral result;

constexpr auto string_literal = x4::rule<struct _, StringLiteral>{"StringLiteral"} =
eps[([](auto& ctx) { _val(ctx).is_quoted = false; })] >>
eps[([](auto& ctx) { _rule_var(ctx).is_quoted = false; })] >>
x4::as<std::string>(
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();
Expand Down
8 changes: 4 additions & 4 deletions test/x4/rule3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -105,7 +105,7 @@ TEST_CASE("rule3")
using rule_type = rule<class r, std::string>;

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));
Expand All @@ -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));
Expand All @@ -129,7 +129,7 @@ TEST_CASE("rule3")
{
auto r = rule<class r_id, int>{} = eps[([] ([[maybe_unused]] auto&& ctx) {
static_assert(
std::is_same_v<std::decay_t<decltype(_val(ctx))>, unused_type>,
std::is_same_v<std::decay_t<decltype(_rule_var(ctx))>, unused_type>,
"Attr must not be synthesized"
);
})];
Expand Down
Loading