]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | [#accept_when] |
2 | [section accept_when] | |
3 | ||
4 | [h1 Synopsis] | |
5 | ||
6 | template <class P, class Pred, class Msg> | |
7 | struct accept_when; | |
8 | ||
9 | This is a [link parser_combinator parser combinator]. | |
10 | ||
11 | [table Arguments | |
12 | [[Name] [Type]] | |
13 | [[`P`] [[link parser parser]]] | |
14 | [[`Pred`] [[link predicate predicate]]] | |
15 | [[`Msg`] [[link parsing_error_message parsing error message]]] | |
16 | ] | |
17 | ||
18 | [h1 Description] | |
19 | ||
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. | |
25 | ||
26 | [h1 Header] | |
27 | ||
28 | #include <boost/metaparse/accept_when.hpp> | |
29 | ||
30 | [h1 Expression semantics] | |
31 | ||
32 | For any `p` parser, `pred` predicate, `msg` parsing error message, `s` | |
33 | compile-time string and `pos` source position | |
34 | ||
35 | accept_when<p, pred, msg>i::apply<s, pos>::type | |
36 | ||
37 | is equivalent to | |
38 | ||
39 | p::apply<s, pos>::type | |
40 | ||
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 | |
43 | equivalent to | |
44 | ||
45 | fail<msg> | |
46 | ||
47 | [h1 Example] | |
48 | ||
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> | |
57 | ||
58 | using namespace boost::metaparse; | |
59 | ||
60 | BOOST_METAPARSE_DEFINE_ERROR(digit_expected, "Digit expected"); | |
61 | ||
62 | using accept_digit = accept_when<one_char, util::is_digit<>, digit_expected>; | |
63 | ||
64 | static_assert( | |
65 | !is_error< | |
66 | accept_digit::apply<BOOST_METAPARSE_STRING("0"), start> | |
67 | >::type::value, | |
68 | "accept_digit should accept a digit" | |
69 | ); | |
70 | ||
71 | static_assert( | |
72 | get_result< | |
73 | accept_digit::apply<BOOST_METAPARSE_STRING("0"), start> | |
74 | >::type::value == '0', | |
75 | "the result of parsing should be the character value" | |
76 | ); | |
77 | ||
78 | static_assert( | |
79 | is_error< | |
80 | accept_digit::apply<BOOST_METAPARSE_STRING("x"), start> | |
81 | >::type::value, | |
82 | "accept_digit should reject a character that is not a digit" | |
83 | ); | |
84 | ||
85 | [endsect] | |
86 |