]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | [#fail_at_first_char_expected] |
2 | [section fail_at_first_char_expected] | |
3 | ||
4 | [h1 Synopsis] | |
5 | ||
6 | template <class P> | |
7 | struct fail_at_first_char_expected; | |
8 | ||
9 | This is a [link parser_combinator parser combinator]. | |
10 | ||
11 | [table Arguments | |
12 | [[Name] [Type]] | |
13 | [[`P`] [[link parser parser]]] | |
14 | ] | |
15 | ||
16 | [h1 Description] | |
17 | ||
18 | It tries to parse the input using `P`. When `P` rejects the input without | |
19 | consuming any character, `fail_at_first_char_expected` accepts the input. | |
20 | Otherwise (when `P` accepts the input or when it consumes characters before | |
21 | rejecting the input) `fail_at_first_char_expected` rejects the input. | |
22 | ||
23 | [h1 Header] | |
24 | ||
25 | #include <boost/metaparse/fail_at_first_char_expected.hpp> | |
26 | ||
27 | [h1 Expression semantics] | |
28 | ||
29 | For any `p` parser, `s` compile-time string and `pos` source position: | |
30 | ||
31 | When `is_error<p::apply<s, pos>>::type` is false, the following are equivalent: | |
32 | ||
33 | fail_at_first_char_expected<p>::apply<s, pos>::type | |
34 | reject<error::expected_to_fail, pos> | |
35 | ||
36 | When `is_error<p::apply<s, pos>>::type` is true and | |
37 | `boost::mpl::equal_to<pos, get_position<p::apply<s, pos>>::type>::type` is also | |
38 | true, the following are equivalent: | |
39 | ||
40 | get_remaining<except<p, c, msg>, s, pos>::type | |
41 | accept</* unspecified */, s, pos> | |
42 | ||
43 | Otherwise the following are equivalent: | |
44 | ||
45 | get_remaining<except<p, c, msg>, s, pos>::type | |
46 | p::apply<s, pos>::type | |
47 | ||
48 | [h1 Example] | |
49 | ||
50 | #include <boost/metaparse/fail_at_first_char_expected.hpp> | |
51 | #include <boost/metaparse/int_.hpp> | |
52 | #include <boost/metaparse/lit_c.hpp> | |
53 | #include <boost/metaparse/foldl_start_with_parser.hpp> | |
54 | #include <boost/metaparse/first_of.hpp> | |
55 | #include <boost/metaparse/last_of.hpp> | |
56 | #include <boost/metaparse/string.hpp> | |
57 | #include <boost/metaparse/start.hpp> | |
58 | #include <boost/metaparse/is_error.hpp> | |
59 | #include <boost/metaparse/get_result.hpp> | |
60 | ||
61 | #include <boost/mpl/lambda.hpp> | |
62 | #include <boost/mpl/plus.hpp> | |
63 | ||
64 | using namespace boost::metaparse; | |
65 | ||
66 | using plus_int = last_of<lit_c<'+'>, int_>; | |
67 | ||
68 | using plus_exp = | |
69 | first_of< | |
70 | foldl_start_with_parser< | |
71 | plus_int, | |
72 | int_, | |
73 | boost::mpl::lambda<boost::mpl::plus<boost::mpl::_1, boost::mpl::_2>>::type | |
74 | >, | |
75 | fail_at_first_char_expected<plus_int> | |
76 | >; | |
77 | ||
78 | static_assert( | |
79 | get_result< | |
80 | plus_exp::apply<BOOST_METAPARSE_STRING("1+2+3"), start> | |
81 | >::type::value == 6, | |
82 | "it should accept the input when it is a list of '+' separated ints" | |
83 | ); | |
84 | ||
85 | static_assert( | |
86 | is_error< | |
87 | plus_exp::apply<BOOST_METAPARSE_STRING("1+2+3+"), start> | |
88 | >::type::value, | |
89 | "it should reject the input when it has an extra +" | |
90 | ); | |
91 | ||
92 | [endsect] | |
93 |