Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:51:23

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_GUARD_FERBRUARY_02_2013_0649PM)
0010 #define BOOST_SPIRIT_X3_GUARD_FERBRUARY_02_2013_0649PM
0011 
0012 #include <boost/spirit/home/x3/support/context.hpp>
0013 #include <boost/spirit/home/x3/support/expectation.hpp>
0014 #include <boost/spirit/home/x3/core/parser.hpp>
0015 
0016 namespace boost { namespace spirit { namespace x3
0017 {
0018     enum class error_handler_result
0019     {
0020         fail
0021       , retry
0022       , accept
0023       , rethrow // see BOOST_SPIRIT_X3_THROW_EXPECTATION_FAILURE for alternative behaviors
0024     };
0025 
0026     template <typename Subject, typename Handler>
0027     struct guard : unary_parser<Subject, guard<Subject, Handler>>
0028     {
0029         typedef unary_parser<Subject, guard<Subject, Handler>> base_type;
0030         static bool const is_pass_through_unary = true;
0031 
0032         constexpr guard(Subject const& subject, Handler handler)
0033           : base_type(subject), handler(handler) {}
0034 
0035         template <typename Iterator, typename Context
0036           , typename RuleContext, typename Attribute>
0037         bool parse(Iterator& first, Iterator const& last
0038           , Context const& context, RuleContext& rcontext, Attribute& attr) const
0039         {
0040             for (;;)
0041             {
0042                 Iterator i = first;
0043 
0044             #if BOOST_SPIRIT_X3_THROW_EXPECTATION_FAILURE
0045                 try
0046             #endif
0047                 {
0048                     if (this->subject.parse(i, last, context, rcontext, attr))
0049                     {
0050                         first = i;
0051                         return true;
0052                     }
0053                 }
0054 
0055             #if BOOST_SPIRIT_X3_THROW_EXPECTATION_FAILURE
0056                 catch (expectation_failure<Iterator> const& x) {
0057             #else
0058                 if (has_expectation_failure(context)) {
0059                     auto& x = get_expectation_failure(context);
0060             #endif
0061                     // X3 developer note: don't forget to sync this implementation with x3::detail::rule_parser
0062                     switch (handler(first, last, x, context))
0063                     {
0064                         case error_handler_result::fail:
0065                             clear_expectation_failure(context);
0066                             return false;
0067 
0068                         case error_handler_result::retry:
0069                             continue;
0070 
0071                         case error_handler_result::accept:
0072                             return true;
0073 
0074                         case error_handler_result::rethrow:
0075                         #if BOOST_SPIRIT_X3_THROW_EXPECTATION_FAILURE
0076                             throw;
0077                         #else
0078                             return false; // TODO: design decision required
0079                         #endif
0080                     }
0081                 }
0082                 return false;
0083             }
0084         }
0085 
0086         Handler handler;
0087     };
0088 }}}
0089 
0090 #endif