]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | [#look_ahead] |
2 | [section look_ahead] | |
3 | ||
4 | [h1 Synopsis] | |
5 | ||
6 | template <class P> | |
7 | struct look_ahead; | |
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 | Parses the input using parser `P`. When `P` returns an error, `look_ahead` | |
19 | returns the error. When `P` returns a result, `look_ahead` returns the result | |
20 | without consuming anything from the input string. | |
21 | ||
22 | [h1 Header] | |
23 | ||
24 | #include <boost/metaparse/look_ahead.hpp> | |
25 | ||
26 | [h1 Expression semantics] | |
27 | ||
28 | For any `p` parser, `s` compile-time string and `pos` source position | |
29 | ||
30 | look_ahead<p>::apply<s, pos> | |
31 | ||
32 | is equivalent to | |
33 | ||
34 | return_<get_result<p::apply<s, pos>>::type>::apply<s, pos> | |
35 | ||
36 | when `p::apply<s, pos>` succeeds. It is | |
37 | ||
38 | p::apply<s, pos> | |
39 | ||
40 | otherwise. | |
41 | ||
42 | [h1 Example] | |
43 | ||
44 | #include <boost/metaparse/look_ahead.hpp> | |
45 | #include <boost/metaparse/int_.hpp> | |
46 | #include <boost/metaparse/start.hpp> | |
47 | #include <boost/metaparse/string.hpp> | |
48 | #include <boost/metaparse/is_error.hpp> | |
49 | #include <boost/metaparse/get_result.hpp> | |
50 | #include <boost/metaparse/get_remaining.hpp> | |
51 | ||
52 | #include <type_traits> | |
53 | ||
54 | using namespace boost::metaparse; | |
55 | ||
56 | static_assert( | |
57 | get_result< | |
58 | look_ahead<int_>::apply<BOOST_METAPARSE_STRING("13"), start> | |
59 | >::type::value == 13, | |
60 | "it should return the same result as the wrapped parser" | |
61 | ); | |
62 | ||
63 | static_assert( | |
64 | std::is_same< | |
65 | BOOST_METAPARSE_STRING("13"), | |
66 | get_remaining< | |
67 | look_ahead<int_>::apply<BOOST_METAPARSE_STRING("13"), start> | |
68 | >::type | |
69 | >::type::value, | |
70 | "it should not consume any input" | |
71 | ); | |
72 | ||
73 | static_assert( | |
74 | is_error< | |
75 | look_ahead<int_>::apply<BOOST_METAPARSE_STRING("six"), start> | |
76 | >::type::value, | |
77 | "it should fail when the wrapped parser fails" | |
78 | ); | |
79 | ||
80 | [endsect] | |
81 |