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
74 changes: 73 additions & 1 deletion include/iris/x4/core/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ template<class ID, class T>
void make_context(T const&&) = delete; // dangling


// Remove the contained reference of the leftmost context having the id `ID_To_Remove`.
// Remove the *leftmost* context having the id `ID_To_Remove`.
template<class ID_To_Remove, class ID, class T, class Next>
[[nodiscard]] constexpr decltype(auto) // may return existing reference in some cases
remove_first_context(context<ID, T, Next> const& ctx) noexcept
Expand Down Expand Up @@ -444,6 +444,78 @@ remove_first_context(unused_type const&) noexcept
template<class ID_To_Remove, class ID, class T, class Next>
void remove_first_context(context<ID, T, Next> const&&) = delete; // dangling


// Remove *all* contexts having any of the ids `IDs_To_Remove...`.
template<class... IDs_To_Remove, class ID, class T, class Next>
[[nodiscard]] constexpr decltype(auto) // may return existing reference in some cases
remove_all_contexts(context<ID, T, Next> const& ctx) noexcept
{
static_assert(sizeof...(IDs_To_Remove) > 0);

if constexpr (iris::is_in_v<ID, IDs_To_Remove...>) { // Match
if constexpr (std::same_as<Next, unused_type>) {
// Existing context found; removing it will result in an
// empty context, so create a monostate placeholder.
return unused_type{};

} else {
// Existing context found; remove it and continue the search.
return x4::remove_all_contexts<IDs_To_Remove...>(ctx.next);
}

} else { // No match
if constexpr (std::same_as<Next, unused_type>) {
// No match at all. Return as-is.
return ctx;

} else {
// No match. Continue the replacement recursively.
using NewNext_ = decltype(x4::remove_all_contexts<IDs_To_Remove...>(ctx.next));
using NewNext = std::conditional_t<
std::is_reference_v<NewNext_>,
NewNext_,
std::remove_const_t<NewNext_>
>;

if constexpr (std::same_as<std::remove_cvref_t<NewNext>, std::remove_cvref_t<Next>>) {
// Avoid creating copy on exact same type
return ctx;

} else {
// If the recursive replacement resulted in a monostate context,
// prevent appending it; return the context without `next`.
if constexpr (std::same_as<std::remove_cvref_t<NewNext>, unused_type>) {
return context<ID, T>{ctx.val};

} else if constexpr (std::is_reference_v<NewNext>) {
// Assert `decltype(auto)` is working as intended; i.e., no dangling reference
static_assert(std::is_lvalue_reference_v<NewNext>);

return context<ID, T, NewNext>{
ctx.val, x4::remove_all_contexts<IDs_To_Remove...>(ctx.next)
};

} else { // prvalue context
return context<ID, T, NewNext>{
ctx.val, x4::remove_all_contexts<IDs_To_Remove...>(ctx.next)
};
}
}
}
}
}

template<class... IDs_To_Remove>
[[nodiscard]] constexpr unused_type const&
remove_all_contexts(unused_type const&) noexcept
{
return unused;
}

template<class... IDs_To_Remove, class ID, class T, class Next>
void remove_all_contexts(context<ID, T, Next> const&&) = delete; // dangling


// Replaces the contained reference of the leftmost context
// having the id `ID_To_Replace`. If no such context exists,
// append a new one.
Expand Down
1 change: 1 addition & 0 deletions include/iris/x4/directive.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
#include <iris/x4/directive/skip.hpp>
#include <iris/x4/directive/with.hpp>
#include <iris/x4/directive/with_local.hpp>
#include <iris/x4/directive/without.hpp>

#endif
73 changes: 73 additions & 0 deletions include/iris/x4/directive/without.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#ifndef IRIS_X4_DIRECTIVE_WITHOUT_HPP
#define IRIS_X4_DIRECTIVE_WITHOUT_HPP

/*=============================================================================
Copyright (c) 2026 The Iris Project Contributors

Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/

#include <iris/x4/core/parser.hpp>
#include <iris/x4/core/context.hpp>

#include <format>
#include <iterator>
#include <type_traits>
#include <utility>

namespace iris::x4 {

template<class Subject, class... IDs>
struct without_directive : proxy_parser<Subject, without_directive<Subject, IDs...>>
{
template<std::forward_iterator It, std::sentinel_for<It> Se, class Context, X4Attribute Attr>
[[nodiscard]] constexpr bool
parse(It& first, Se const& last, Context const& ctx, Attr& attr) const
noexcept(
x4::is_nothrow_parsable_v<
Subject, It, Se,
std::remove_cvref_t<decltype(x4::remove_all_contexts<IDs...>(ctx))>,
Attr
>
)
{
return this->subject.parse(first, last, x4::remove_all_contexts<IDs...>(ctx), attr);
}

[[nodiscard]] constexpr std::string get_x4_info() const
{
return std::format("without<...>[{}]", get_info<Subject>{}(this->subject));
}
};

namespace detail {

template<class... IDs>
struct without_gen
{
template<class Subject>
[[nodiscard]] constexpr without_directive<std::remove_cvref_t<Subject>, IDs...>
operator[](Subject&& subject) const // TODO: MSVC 2022 does not properly handle static operator[]
noexcept(std::is_nothrow_constructible_v<without_directive<std::remove_cvref_t<Subject>, IDs...>, Subject>)
{
return without_directive<std::remove_cvref_t<Subject>, IDs...>{
std::forward<Subject>(subject)
};
}
};

} // detail

namespace parsers::directive {

template<class... IDs>
[[maybe_unused]] inline constexpr detail::without_gen<IDs...> without{};

} // parsers::directive

using parsers::directive::without;

} // iris::x4

#endif
1 change: 1 addition & 0 deletions test/x4/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ x4_define_tests(
unused
with
with_local
without
x3_rule_problem
alloy_wrong_substitute_test_case
)
Expand Down
95 changes: 95 additions & 0 deletions test/x4/without.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*=============================================================================
Copyright (c) 2026 The Iris Project Contributors

Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/

#include "iris_x4_test.hpp"

#include <iris/x4/auxiliary/eps.hpp>
#include <iris/x4/directive/without.hpp>
#include <iris/x4/directive/with.hpp>
#include <iris/x4/core/parser.hpp>

#include <concepts>
#include <iterator>

template<class ExpectedContext>
struct context_checker : x4::parser<context_checker<ExpectedContext>>
{
template<std::forward_iterator It, std::sentinel_for<It> Se, class Context, x4::X4Attribute Attr>
[[nodiscard]] static constexpr bool parse(It&, Se const&, Context const&, Attr const&)
{
STATIC_CHECK(std::same_as<Context, ExpectedContext>);
return true;
}
};

static_assert(x4::X4ExplicitParser<context_checker<unused_type>, char const*, char const*>);

struct OK {};
struct Bad0 {};
struct NonExistent {};

TEST_CASE("without")
{
using x4::without;
using x4::with;
using x4::context;

constexpr int dummy = 0;
char const* first = nullptr, *const last = nullptr;

{
(void)x4::with<OK>(dummy)[
x4::without<OK>[
context_checker<unused_type>{}
]
].parse(first, last, unused, unused);
}
{
(void)x4::with<OK>(dummy)[
x4::without<NonExistent>[
context_checker<context<OK, int const>>{}
]
].parse(first, last, unused, unused);
}

{
(void)x4::with<OK>(dummy)[
x4::with<Bad0>(dummy)[
x4::without<Bad0>[
context_checker<context<OK, int const>>{}
]
]
].parse(first, last, unused, unused);
}
{
(void)x4::with<OK>(dummy)[
x4::with<Bad0>(dummy)[
x4::without<OK>[
context_checker<context<Bad0, int const>>{}
]
]
].parse(first, last, unused, unused);
}
{
(void)x4::with<OK>(dummy)[
x4::with<Bad0>(dummy)[
x4::without<NonExistent>[
context_checker<context<Bad0, int const, context<OK, int const> const&>>{}
]
]
].parse(first, last, unused, unused);
}
{
(void)x4::with<OK>(dummy)[
x4::with<Bad0>(dummy)[
x4::without<OK, Bad0>[
context_checker<unused_type>{}
]
]
].parse(first, last, unused, unused);
}
}