6 template <class P, class Pred, class Msg>
9 This is a [link parser_combinator parser combinator].
13 [[`P`] [[link parser parser]]]
14 [[`Pred`] [[link predicate predicate]]]
15 [[`Msg`] [[link parsing_error_message parsing error message]]]
20 It parses the input with `P`. When `P` rejects the input, `accept_when` rejects
21 it as well. When `P` accepts it, `accept_when` evaluates `Pred` with the result
22 of parsing the input with `P`. When `Pred` returns `true`, `accept_when` accepts
23 the input and the result of parsing will be what `P` returned. Otherwise
24 `accept_when` rejects the input and `Msg` is used as the error reason.
28 #include <boost/metaparse/accept_when.hpp>
30 [h1 Expression semantics]
32 For any `p` parser, `pred` predicate, `msg` parsing error message, `s`
33 compile-time string and `pos` source position
35 accept_when<p, pred, msg>i::apply<s, pos>::type
39 p::apply<s, pos>::type
41 when `p::apply<s, pos>` doesn't return an error and
42 `pred::apply<get_result<p::apply<s, pos>>>::type` is `true`. Otherwise it is
49 #include <boost/metaparse/accept_when.hpp>
50 #include <boost/metaparse/one_char.hpp>
51 #include <boost/metaparse/util/is_digit.hpp>
52 #include <boost/metaparse/start.hpp>
53 #include <boost/metaparse/string.hpp>
54 #include <boost/metaparse/is_error.hpp>
55 #include <boost/metaparse/get_result.hpp>
56 #include <boost/metaparse/define_error.hpp>
58 using namespace boost::metaparse;
60 BOOST_METAPARSE_DEFINE_ERROR(digit_expected, "Digit expected");
62 using accept_digit = accept_when<one_char, util::is_digit<>, digit_expected>;
66 accept_digit::apply<BOOST_METAPARSE_STRING("0"), start>
68 "accept_digit should accept a digit"
73 accept_digit::apply<BOOST_METAPARSE_STRING("0"), start>
74 >::type::value == '0',
75 "the result of parsing should be the character value"
80 accept_digit::apply<BOOST_METAPARSE_STRING("x"), start>
82 "accept_digit should reject a character that is not a digit"