Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-15 10:09:01

0001 /*=============================================================================
0002     Copyright (c) 2001-2014 Joel de Guzman
0003     Copyright (c) 2017 wanghan02
0004     Copyright (c) 2024 Nana Sakisaka
0005 
0006     Distributed under the Boost Software License, Version 1.0. (See accompanying
0007     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0008 ==============================================================================*/
0009 #if !defined(BOOST_SPIRIT_X3_SKIP_APRIL_16_2006_0625PM)
0010 #define BOOST_SPIRIT_X3_SKIP_APRIL_16_2006_0625PM
0011 
0012 #include <boost/spirit/home/x3/support/expectation.hpp>
0013 #include <boost/spirit/home/x3/support/unused.hpp>
0014 #include <boost/spirit/home/x3/support/context.hpp>
0015 #include <boost/spirit/home/x3/support/traits/attribute_category.hpp>
0016 #include <boost/mpl/bool.hpp>
0017 #include <boost/mpl/not.hpp>
0018 #include <boost/type_traits/remove_cv.hpp>
0019 #include <boost/type_traits/remove_reference.hpp>
0020 #include <boost/utility/declval.hpp>
0021 #include <boost/core/ignore_unused.hpp>
0022 
0023 namespace boost { namespace spirit { namespace x3
0024 {
0025     ///////////////////////////////////////////////////////////////////////////
0026     // Move the /first/ iterator to the first non-matching position
0027     // given a skip-parser. The function is a no-op if unused_type or
0028     // unused_skipper is passed as the skip-parser.
0029     ///////////////////////////////////////////////////////////////////////////
0030     template <typename Skipper>
0031     struct unused_skipper : unused_type
0032     {
0033         unused_skipper(Skipper const& skipper)
0034           : skipper(skipper) {}
0035         Skipper const& skipper;
0036     };
0037 
0038     namespace detail
0039     {
0040         template <typename Skipper>
0041         struct is_unused_skipper
0042           : mpl::false_ {};
0043 
0044         template <typename Skipper>
0045         struct is_unused_skipper<unused_skipper<Skipper>>
0046           : mpl::true_ {};
0047 
0048         template <>
0049         struct is_unused_skipper<unused_type>
0050           : mpl::true_ {};
0051 
0052         template <typename Skipper>
0053         inline Skipper const&
0054         get_unused_skipper(Skipper const& skipper)
0055         {
0056             return skipper;
0057         }
0058         template <typename Skipper>
0059         inline Skipper const&
0060         get_unused_skipper(unused_skipper<Skipper> const& unused_skipper)
0061         {
0062             return unused_skipper.skipper;
0063         }
0064 
0065         template <typename Iterator, typename Context, typename Skipper>
0066         inline void skip_over(
0067             Iterator& first, Iterator const& last, Context& context, Skipper const& skipper)
0068         {
0069         #if BOOST_SPIRIT_X3_THROW_EXPECTATION_FAILURE
0070             boost::ignore_unused(context);
0071             while (skipper.parse(first, last, unused, unused, unused))
0072                 /* loop */;
0073         #else
0074             if constexpr (std::is_same_v<expectation_failure_t<Context>, unused_type>)
0075             {
0076                 // The context given by parent was truly `unused_type`.
0077                 // There exists only one such case in core; that is
0078                 // `x3::phrase_parse(...)` which creates a fresh context
0079                 // for the (post)skipper.
0080                 //
0081                 // In that case, it is perfectly fine to pass `unused`
0082                 // because the skipper should have been wrapped
0083                 // like `x3::with<x3::expectation_failure_tag>(failure)[skipper]`.
0084                 // (Note that we have plenty of static_asserts in other
0085                 // locations to detect the absence of the context.)
0086                 //
0087                 // If we encounter this branch in any other situations,
0088                 // that should be a BUG of `expectation_failure` logic.
0089 
0090                 while (skipper.parse(first, last, unused, unused, unused))
0091                     /* loop */;
0092             }
0093             else
0094             {
0095                 // In order to cut the template instantiation chain,
0096                 // we must *forget* the original context at least once
0097                 // during the (recursive) invocation of skippers.
0098                 //
0099                 // Traditionally, implementation detail of `skip_over`
0100                 // was disposing the context because we can clearly assume
0101                 // that any 'context,' including those provided by users,
0102                 // is semantically meaningless as long as we're just
0103                 // *skipping* iterators. As you can see in the other branch,
0104                 // `unused` was passed for that purpose.
0105                 //
0106                 // However, we need to do a quite different thing when the
0107                 // non-throwing expectation_failure mode is enabled.
0108                 //
0109                 // Since the reference bound to `x3::expectation_failure_tag` is
0110                 // provided by the user in the first place, if we do forget it
0111                 // then it will be impossible to resurrect the value afterwards.
0112                 // It will also be problematic for `skip_over` itself because the
0113                 // underlying skipper may (or may not) raise an expectation failure.
0114                 // In traditional mode, the error was thrown by a C++ exception.
0115                 // But how can we propagate that error without throwing?
0116                 //
0117                 // For this reason we're going to cherry-pick the reference
0118                 // and repack it into a brand new context.
0119 
0120                 auto const local_ctx = make_context<expectation_failure_tag>(
0121                     x3::get<expectation_failure_tag>(context));
0122 
0123                 while (skipper.parse(first, last, local_ctx, unused, unused))
0124                     /* loop */;
0125             }
0126         #endif
0127         }
0128 
0129         template <typename Iterator, typename Context>
0130         inline void skip_over(Iterator&, Iterator const&, Context&, unused_type)
0131         {
0132         }
0133 
0134         template <typename Iterator, typename Context, typename Skipper>
0135         inline void skip_over(
0136             Iterator&, Iterator const&, Context&, unused_skipper<Skipper> const&)
0137         {
0138         }
0139     }
0140 
0141     // this tag is used to find the skipper from the context
0142     struct skipper_tag;
0143 
0144     template <typename Context>
0145     struct has_skipper
0146       : mpl::not_<detail::is_unused_skipper<
0147             typename remove_cv<typename remove_reference<
0148                 decltype(x3::get<skipper_tag>(boost::declval<Context>()))
0149             >::type>::type
0150         >> {};
0151 
0152     template <typename Iterator, typename Context>
0153     inline void skip_over(
0154         Iterator& first, Iterator const& last, Context& context)
0155     {
0156         detail::skip_over(first, last, context, x3::get<skipper_tag>(context));
0157     }
0158 }}}
0159 
0160 #endif