]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | [#foldl_reject_incomplete] |
2 | [section foldl_reject_incomplete] | |
3 | ||
4 | [h1 Synopsis] | |
5 | ||
6 | template <class P, class State, class ForwardOp> | |
7 | struct foldl_reject_incomplete; | |
8 | ||
9 | This is a [link parser_combinator parser combinator]. | |
10 | ||
11 | [table Arguments | |
12 | [[Name] [Type]] | |
13 | [[`P`] [[link parser parser]]] | |
14 | [[`State`] [[link metaprogramming_value template metaprogramming value]]] | |
15 | [[`ForwardOp`] [[link metafunction_class template metafunction class] taking two arguments]] | |
16 | ] | |
17 | ||
18 | [h1 Description] | |
19 | ||
20 | The same as [link foldl `foldl`], but once `P` rejects the input, | |
21 | `foldl_reject_incomplete` checks if `P` consumes any characters before rejecting | |
22 | the input. If so, `foldl_reject_incomplete` rejects the input with the same | |
23 | error message this last application of `P` returned. Otherwise | |
24 | `foldl_reject_incomplete` accepts the input and gives the same result as | |
25 | [link foldl `foldl`]. | |
26 | ||
27 | Here is a diagram showing how `foldl_reject_incomplete` works by example: | |
28 | ||
29 | using int_token = token<int_>; | |
30 | using plus_token = token<lit_c<'+'>>; | |
31 | using plus_int = last_of<plus_token, int_token>; | |
32 | using sum_op = mpl::lambda<mpl::plus<mpl::_1, mpl::_2>>::type; | |
33 | ||
34 | [$images/metaparse/foldl_reject_incomplete_diag1.png [width 70%]] | |
35 | ||
36 | [h1 Header] | |
37 | ||
38 | #include <boost/metaparse/foldl_reject_incomplete.hpp> | |
39 | ||
40 | [h1 Expression semantics] | |
41 | ||
42 | For any `p` parser, `t` class, `f` metafunction class taking two arguments, | |
43 | `s` compile-time string and `pos` source position | |
44 | ||
45 | foldl_reject_incomplete<p, t, f>::apply<s, pos> | |
46 | ||
47 | is equivalent to | |
48 | ||
49 | first_of<foldl<p, t, f>, fail_at_first_char_expected<p> >::apply<s, pos> | |
50 | ||
51 | [h1 Example] | |
52 | ||
53 | #include <boost/metaparse/foldl_reject_incomplete.hpp> | |
54 | #include <boost/metaparse/lit_c.hpp> | |
55 | #include <boost/metaparse/last_of.hpp> | |
56 | #include <boost/metaparse/token.hpp> | |
57 | #include <boost/metaparse/int_.hpp> | |
58 | #include <boost/metaparse/string.hpp> | |
59 | #include <boost/metaparse/start.hpp> | |
60 | #include <boost/metaparse/get_result.hpp> | |
61 | #include <boost/metaparse/is_error.hpp> | |
62 | ||
63 | #include <boost/mpl/lambda.hpp> | |
64 | #include <boost/mpl/plus.hpp> | |
65 | #include <boost/mpl/int.hpp> | |
66 | ||
67 | using namespace boost::metaparse; | |
68 | ||
69 | using int_token = token<int_>; | |
70 | using plus_token = token<lit_c<'+'>>; | |
71 | using plus_int = last_of<plus_token, int_token>; | |
72 | using sum_op = | |
73 | boost::mpl::lambda<boost::mpl::plus<boost::mpl::_1, boost::mpl::_2>>::type; | |
74 | ||
75 | using ints = foldl_reject_incomplete<plus_int, boost::mpl::int_<11>, sum_op>; | |
76 | ||
77 | static_assert( | |
78 | get_result< | |
79 | ints::apply<BOOST_METAPARSE_STRING("+ 13 + 3 + 21"), start> | |
80 | >::type::value == 48, | |
81 | "ints should sum the numbers" | |
82 | ); | |
83 | ||
84 | static_assert( | |
85 | is_error< | |
86 | ints::apply<BOOST_METAPARSE_STRING("+ 13 + 3 +"), start> | |
87 | >::type::value, | |
88 | "when the last number is missing, it should be an error" | |
89 | ); | |
90 | ||
91 | [endsect] | |
92 |