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
54 changes: 45 additions & 9 deletions include/iris/x4/core/action.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <iris/x4/core/context.hpp>
#include <iris/x4/core/action_context.hpp>

#include <ranges> // subrange
#include <iterator>
#include <concepts>
#include <type_traits>
Expand Down Expand Up @@ -71,6 +70,7 @@ struct action : proxy_parser<Subject, action<Subject, ActionF>>

static constexpr bool has_action = true;
static constexpr bool need_rcontext = true;
static constexpr bool requires_exact_attribute_type = false; // reset

ActionF f;

Expand All @@ -83,29 +83,65 @@ struct action : proxy_parser<Subject, action<Subject, ActionF>>
{
}

// attr==unused, action wants attribute
template<std::forward_iterator It, std::sentinel_for<It> Se, class Context>
// When the exposed attribute is `unused_type`.
// Since we can assume that the subject unconditionally requires `_attr`,
// we must create a temporary variable to pass it to the subject.
template<std::forward_iterator It, std::sentinel_for<It> Se, class Context, X4UnusedAttribute UnusedAttr>
[[nodiscard]] constexpr bool
parse(It& first, Se const& last, Context const& ctx, unused_type) const
parse(It& first, Se const& last, Context const& ctx, UnusedAttr&) const
noexcept(
std::is_nothrow_default_constructible_v<typename base_type::attribute_type> &&
noexcept(this->parse_main(first, last, ctx, std::declval<typename base_type::attribute_type&>()))
)
{
// Synthesize the attribute since one is not supplied
typename base_type::attribute_type attribute; // default-initialize
return this->parse_main(first, last, ctx, attribute);
typename base_type::attribute_type attr_temp; // default-initialize
return this->parse_main(first, last, ctx, attr_temp);
}

// Catch-all overload for non-unused_type attribute
template<std::forward_iterator It, std::sentinel_for<It> Se, class Context, X4Attribute Attr>
private:
// We need to handle the general case when `Attr` and the subject's attribute
// type are different.
//
// We can't unconditionally create `attr_temp` of the exact matching type
// because there exists some cases where the temporary variable is truly
// unnecessary.
//
// For instance, when the exposed attribute is `std::vector<int>` and the
// underlying parser is `int_ >> int_` (attr is `alloy::tuple<int, int>`),
// we must just pass the exposed vector variable directly.
//
// Conversely, the only reliable method to determine whether the underlying
// parser really needs the exact matching type is by checking the dedicated
// trait flag like below.
template<X4NonUnusedAttribute Attr>
static constexpr bool can_pass_exposed_attr =
std::same_as<Attr, typename base_type::attribute_type> || !Subject::requires_exact_attribute_type;

public:
// When the exposed attribute is NOT `unused_type`.
template<std::forward_iterator It, std::sentinel_for<It> Se, class Context, X4NonUnusedAttribute Attr>
requires can_pass_exposed_attr<Attr>
[[nodiscard]] constexpr bool
parse(It& first, Se const& last, Context const& ctx, Attr& attr) const
noexcept(noexcept(this->parse_main(first, last, ctx, attr)))
{
return this->parse_main(first, last, ctx, attr);
}

// When the exposed attribute is NOT `unused_type`.
template<std::forward_iterator It, std::sentinel_for<It> Se, class Context, X4NonUnusedAttribute Attr>
requires (!can_pass_exposed_attr<Attr>)
[[nodiscard]] constexpr bool
parse(It& first, Se const& last, Context const& ctx, Attr& /* attr is discarded */) const
noexcept(
std::is_nothrow_default_constructible_v<typename base_type::attribute_type> &&
noexcept(this->parse_main(first, last, ctx, std::declval<typename base_type::attribute_type&>()))
)
{
typename base_type::attribute_type attr_temp; // default-initialize
return this->parse_main(first, last, ctx, attr_temp);
}

constexpr void operator[](auto const&) const = delete; // You can't add semantic action for semantic action

[[nodiscard]] constexpr std::string get_x4_info() const
Expand Down
1 change: 1 addition & 0 deletions include/iris/x4/core/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ struct parser : private detail::parser_base

static constexpr bool has_action = false;
static constexpr bool need_rcontext = false;
static constexpr bool requires_exact_attribute_type = false;

[[nodiscard]] constexpr Derived& derived() & noexcept
{
Expand Down
44 changes: 20 additions & 24 deletions include/iris/x4/directive/as.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ namespace iris::x4 {

namespace detail {

template<bool NeedAsVal, class Context, X4Attribute Attr>
template<bool SubjectHasAction, class Context, X4Attribute OuterAttr>
struct as_directive_ctx_impl // false
{
using type = Context;
};
template<class Context, X4Attribute Attr>
struct as_directive_ctx_impl<true, Context, Attr>
template<class Context, X4Attribute OuterAttr>
struct as_directive_ctx_impl<true, Context, OuterAttr>
{
using type = std::remove_cvref_t<decltype(x4::replace_first_context<contexts::as_var>(
std::declval<Context const&>(),
std::declval<Attr&>()
std::declval<OuterAttr&>()
))>;
};

Expand All @@ -53,54 +53,58 @@ struct as_directive : unary_parser<Subject, as_directive<T, Subject>>

static constexpr bool has_attribute = !std::same_as<T, unused_type>;
static constexpr bool has_action = false; // Explicitly re-enable attribute detection in `x4::rule`
static constexpr bool requires_exact_attribute_type = true;

// `as_directive` should NOT inherit underlying parser's `handles_container`
// because `as_directive` is an atomic parser. The default implementation of
// `parser_traits<as_directive<...>>::handles_container` must transparently
// handle this case.

private:
static constexpr bool need_as_var = Subject::has_action;
template<X4Attribute Attr>
using exposed_attr_for_child_t = std::conditional_t<
Subject::has_action, unused_type, Attr
>;

public:
// `as<T>(as<T>(subject))` forwards the outer `T&` for `subject`
// `outer_parser<T>(as<T>(subject))` forwards the outer `T&` (exposed attribute) for the 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_var, Context, OuterAttr>::type, OuterAttr>)
noexcept(is_nothrow_parsable_v<Subject, It, Se, typename detail::as_directive_ctx_impl<Subject::has_action, Context, OuterAttr>::type, exposed_attr_for_child_t<OuterAttr>>)
{
if constexpr (need_as_var) {
return this->subject.parse(first, last, x4::replace_first_context<contexts::as_var>(ctx, outer_attr), outer_attr);
if constexpr (Subject::has_action) {
return this->subject.parse(first, last, x4::replace_first_context<contexts::as_var>(ctx, outer_attr), unused);
} else {
return this->subject.parse(first, last, ctx, outer_attr);
}
}

// `as<unused_type>(as<T>(subject))` gives `unused_type` for `subject`
// `outer_parser<unused_type>(as<T>(subject))` forwards `unused` for the subject
template<std::forward_iterator It, std::sentinel_for<It> Se, class Context, X4UnusedAttribute OuterAttr>
requires
(!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_var, Context, unused_type>::type, unused_type>)
noexcept(is_nothrow_parsable_v<Subject, It, Se, typename detail::as_directive_ctx_impl<Subject::has_action, Context, unused_type>::type, unused_type>)
{
if constexpr (need_as_var) {
if constexpr (Subject::has_action) {
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);
}
}

// `as<U>(as<T>(subject))` gives `T` for `subject`, then move `T&&` to `U&`
// `outer_parser<U>(as<T>(subject))` forwards temporary `T` local variable for the subject, then move the variable to `U&`
template<std::forward_iterator It, std::sentinel_for<It> Se, class Context, X4NonUnusedAttribute OuterAttr>
requires
(!std::same_as<std::remove_const_t<OuterAttr>, T>) &&
X4Movable<T, OuterAttr>
[[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_var, Context, T>::type, T> &&
is_nothrow_parsable_v<Subject, It, Se, typename detail::as_directive_ctx_impl<Subject::has_action, Context, T>::type, exposed_attr_for_child_t<T>> &&
noexcept(x4::move_to(std::declval<T>(), outer_attr))
)
{
Expand All @@ -111,25 +115,17 @@ struct as_directive : unary_parser<Subject, as_directive<T, Subject>>
// Note that this behavior is our implementation details. The underlying parser should
// not rely on this behavior; they should never assume the given attribute is defaulted
// to some arbitrary initial value.

T attr_{}; // value-initialize

if constexpr (need_as_var) {
if (!this->subject.parse(first, last, x4::replace_first_context<contexts::as_var>(ctx, attr_), attr_)) return false;
if constexpr (Subject::has_action) {
if (!this->subject.parse(first, last, x4::replace_first_context<contexts::as_var>(ctx, attr_), unused)) return false;
} else {
if (!this->subject.parse(first, last, ctx, attr_)) return false;
}

x4::move_to(std::move(attr_), outer_attr);
return true;
}

//template<std::forward_iterator It, std::sentinel_for<It> Se, class Context, X4NonUnusedAttribute OuterAttr>
// requires
// (!std::same_as<std::remove_const_t<OuterAttr>, T>) &&
// (!X4Movable<T, OuterAttr>)
//constexpr void
//parse(It&, Se const&, Context const&, OuterAttr&) const = delete; // `T` is not movable to the exposed attribute
};

namespace detail {
Expand Down
Loading
Loading