1 /*=============================================================================
2 Copyright (c) 2001-2014 Joel de Guzman
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 =============================================================================*/
7 #if !defined(SPIRIT_EXPECT_MARCH_16_2012_1024PM)
8 #define SPIRIT_EXPECT_MARCH_16_2012_1024PM
10 #include <boost/spirit/home/x3/support/context.hpp>
11 #include <boost/spirit/home/x3/core/parser.hpp>
12 #include <boost/spirit/home/x3/core/detail/parse_into_container.hpp>
14 #include <boost/throw_exception.hpp>
17 namespace boost { namespace spirit { namespace x3
19 template <typename Iterator>
20 struct expectation_failure : std::runtime_error
24 expectation_failure(Iterator where, std::string const& which)
25 : std::runtime_error("boost::spirit::x3::expectation_failure")
26 , where_(where), which_(which)
28 ~expectation_failure() throw() {}
30 std::string which() const { return which_; }
31 Iterator const& where() const { return where_; }
39 template <typename Subject>
40 struct expect_directive : unary_parser<Subject, expect_directive<Subject>>
42 typedef unary_parser<Subject, expect_directive<Subject> > base_type;
43 static bool const is_pass_through_unary = true;
45 expect_directive(Subject const& subject)
46 : base_type(subject) {}
48 template <typename Iterator, typename Context
49 , typename RContext, typename Attribute>
50 bool parse(Iterator& first, Iterator const& last
51 , Context const& context, RContext& rcontext, Attribute& attr) const
53 bool r = this->subject.parse(first, last, context, rcontext, attr);
57 boost::throw_exception(
58 expectation_failure<Iterator>(
59 first, what(this->subject)));
67 template <typename Subject>
68 expect_directive<typename extension::as_parser<Subject>::value_type>
69 operator[](Subject const& subject) const
71 return { as_parser(subject) };
75 auto const expect = expect_gen{};
78 namespace boost { namespace spirit { namespace x3 { namespace detail
80 // Special case handling for expect expressions.
81 template <typename Subject, typename Context, typename RContext>
82 struct parse_into_container_impl<expect_directive<Subject>, Context, RContext>
84 template <typename Iterator, typename Attribute>
86 expect_directive<Subject> const& parser
87 , Iterator& first, Iterator const& last
88 , Context const& context, RContext& rcontext, Attribute& attr)
90 bool r = parse_into_container(
91 parser.subject, first, last, context, rcontext, attr);
95 boost::throw_exception(
96 expectation_failure<Iterator>(
97 first, what(parser.subject)));