2
0
mirror of https://github.com/boostorg/spirit.git synced 2026-01-19 04:42:11 +00:00

Revert "Merge pull request #820 from saki7/modernize-expect"

This reverts commit 70f968ef6c, reversing
changes made to b6acdc57d3.
This commit is contained in:
Nana Sakisaka
2025-09-13 08:56:56 +09:00
parent ac6ed01642
commit 385a9c3f06
2 changed files with 40 additions and 61 deletions

View File

@@ -1,7 +1,7 @@
/*=============================================================================
Copyright (c) 2001-2014 Joel de Guzman
Copyright (c) 2017 wanghan02
Copyright (c) 2024-2025 Nana Sakisaka
Copyright (c) 2024 Nana Sakisaka
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)
@@ -14,30 +14,21 @@
#include <boost/spirit/home/x3/core/parser.hpp>
#include <boost/spirit/home/x3/core/detail/parse_into_container.hpp>
#include <iterator>
#include <type_traits>
#include <utility>
namespace boost::spirit::x3
namespace boost { namespace spirit { namespace x3
{
template <typename Subject>
struct expect_directive : unary_parser<Subject, expect_directive<Subject>>
{
using base_type = unary_parser<Subject, expect_directive<Subject>>;
static constexpr bool is_pass_through_unary = true;
typedef unary_parser<Subject, expect_directive<Subject> > base_type;
static bool const is_pass_through_unary = true;
template <typename SubjectT>
requires std::is_constructible_v<Subject, SubjectT>
constexpr expect_directive(SubjectT&& subject)
noexcept(std::is_nothrow_constructible_v<Subject, SubjectT>)
: base_type(std::forward<SubjectT>(subject))
{}
constexpr expect_directive(Subject const& subject)
: base_type(subject) {}
template <std::forward_iterator It, std::sentinel_for<It> Se, typename Context, typename RContext, typename Attribute>
[[nodiscard]] constexpr bool
parse(
It& first, Se const& last, Context const& context, RContext& rcontext, Attribute& attr
) const // never noexcept; expectation failure requires construction of debug information
template <typename Iterator, typename Context
, typename RContext, typename Attribute>
bool parse(Iterator& first, Iterator const& last
, Context const& context, RContext& rcontext, Attribute& attr) const
{
bool const r = this->subject.parse(first, last, context, rcontext, attr);
@@ -45,12 +36,12 @@ namespace boost::spirit::x3
{
#if BOOST_SPIRIT_X3_THROW_EXPECTATION_FAILURE
boost::throw_exception(
expectation_failure<It>(
first, x3::what(this->subject)));
expectation_failure<Iterator>(
first, what(this->subject)));
#else
if (!x3::has_expectation_failure(context))
if (!has_expectation_failure(context))
{
x3::set_expectation_failure(first, this->subject, context);
set_expectation_failure(first, this->subject, context);
}
#endif
}
@@ -58,59 +49,50 @@ namespace boost::spirit::x3
}
};
namespace detail
{
struct expect_gen
{
template <X3Subject Subject>
[[nodiscard]] constexpr expect_directive<as_parser_plain_t<Subject>>
operator[](Subject&& subject) const
noexcept(is_parser_nothrow_constructible_v<expect_directive<as_parser_plain_t<Subject>>, Subject>)
template <typename Subject>
constexpr expect_directive<typename extension::as_parser<Subject>::value_type>
operator[](Subject const& subject) const
{
return { as_parser(std::forward<Subject>(subject)) };
return { as_parser(subject) };
}
};
} // detail
inline namespace cpos
{
inline constexpr detail::expect_gen expect{};
} // cpos
constexpr auto expect = expect_gen{};
}}}
} // boost::spirit::x3
namespace boost::spirit::x3::detail
namespace boost { namespace spirit { namespace x3 { namespace detail
{
// Special case handling for expect expressions.
template <typename Subject, typename Context, typename RContext>
struct parse_into_container_impl<expect_directive<Subject>, Context, RContext>
{
template <std::forward_iterator It, std::sentinel_for<It> Se, typename Attribute>
[[nodiscard]] static constexpr bool
call(
expect_directive<Subject> const& parser,
It& first, Se const& last, Context const& context, RContext& rcontext, Attribute& attr
) // never noexcept; expectation failure requires construction of debug information
template <typename Iterator, typename Attribute>
static bool call(
expect_directive<Subject> const& parser
, Iterator& first, Iterator const& last
, Context const& context, RContext& rcontext, Attribute& attr)
{
bool const r = detail::parse_into_container(
bool const r = parse_into_container(
parser.subject, first, last, context, rcontext, attr);
if (!r)
{
#if BOOST_SPIRIT_X3_THROW_EXPECTATION_FAILURE
boost::throw_exception(
expectation_failure<It>(
first, x3::what(parser.subject)));
expectation_failure<Iterator>(
first, what(parser.subject)));
#else
if (!x3::has_expectation_failure(context))
if (!has_expectation_failure(context))
{
x3::set_expectation_failure(first, parser.subject, context);
set_expectation_failure(first, parser.subject, context);
}
#endif
}
return r;
}
};
} // boost::spirit::x3::detail
}}}}
#endif

View File

@@ -1,14 +1,11 @@
/*=============================================================================
Copyright (c) 2001-2013 Joel de Guzman
Copyright (c) 2017 wanghan02
Copyright (c) 2024-2025 Nana Sakisaka
Copyright (c) 2024 Nana Sakisaka
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)
=============================================================================*/
#define BOOST_SPIRIT_X3_USE_BOOST_OPTIONAL 0
#define BOOST_SPIRIT_X3_NO_BOOST_ITERATOR_RANGE
#include <boost/spirit/home/x3.hpp>
#include <boost/spirit/home/x3/binary.hpp>
#include <boost/spirit/home/x3/directive/with.hpp>
@@ -265,10 +262,10 @@ int TEST_MAIN_FUNC()
using x3::eps;
using x3::eoi;
using x3::eol;
//using x3::attr;
using x3::attr;
using x3::dword;
using x3::int_;
using x3::shared_symbols;
using x3::symbols;
using x3::confix;
using x3::with;
using x3::expectation_failure;
@@ -491,7 +488,7 @@ int TEST_MAIN_FUNC()
});
int n = 0;
TEST_ATTR_SUCCESS_PASS("abc", lit("abc") > x3::attr(12) > eoi, n);
TEST_ATTR_SUCCESS_PASS("abc", lit("abc") > attr(12) > eoi, n);
BOOST_TEST(n == 12);
}
@@ -512,7 +509,7 @@ int TEST_MAIN_FUNC()
BOOST_TEST(x.where() == "a"sv);
});
shared_symbols<> s;
symbols<> s;
s.add("cat");
TEST_SUCCESS_PASS("12cat", +digit > s);
TEST_FAILURE("12dog", +digit > s, {