82 lines
1.7 KiB
Plaintext
82 lines
1.7 KiB
Plaintext
[#look_ahead]
|
|
[section look_ahead]
|
|
|
|
[h1 Synopsis]
|
|
|
|
template <class P>
|
|
struct look_ahead;
|
|
|
|
This is a [link parser_combinator parser combinator].
|
|
|
|
[table Arguments
|
|
[[Name] [Type]]
|
|
[[`P`] [[link parser parser]]]
|
|
]
|
|
|
|
[h1 Description]
|
|
|
|
Parses the input using parser `P`. When `P` returns an error, `look_ahead`
|
|
returns the error. When `P` returns a result, `look_ahead` returns the result
|
|
without consuming anything from the input string.
|
|
|
|
[h1 Header]
|
|
|
|
#include <boost/metaparse/look_ahead.hpp>
|
|
|
|
[h1 Expression semantics]
|
|
|
|
For any `p` parser, `s` compile-time string and `pos` source position
|
|
|
|
look_ahead<p>::apply<s, pos>
|
|
|
|
is equivalent to
|
|
|
|
return_<get_result<p::apply<s, pos>>::type>::apply<s, pos>
|
|
|
|
when `p::apply<s, pos>` succeeds. It is
|
|
|
|
p::apply<s, pos>
|
|
|
|
otherwise.
|
|
|
|
[h1 Example]
|
|
|
|
#include <boost/metaparse/look_ahead.hpp>
|
|
#include <boost/metaparse/int_.hpp>
|
|
#include <boost/metaparse/start.hpp>
|
|
#include <boost/metaparse/string.hpp>
|
|
#include <boost/metaparse/is_error.hpp>
|
|
#include <boost/metaparse/get_result.hpp>
|
|
#include <boost/metaparse/get_remaining.hpp>
|
|
|
|
#include <type_traits>
|
|
|
|
using namespace boost::metaparse;
|
|
|
|
static_assert(
|
|
get_result<
|
|
look_ahead<int_>::apply<BOOST_METAPARSE_STRING("13"), start>
|
|
>::type::value == 13,
|
|
"it should return the same result as the wrapped parser"
|
|
);
|
|
|
|
static_assert(
|
|
std::is_same<
|
|
BOOST_METAPARSE_STRING("13"),
|
|
get_remaining<
|
|
look_ahead<int_>::apply<BOOST_METAPARSE_STRING("13"), start>
|
|
>::type
|
|
>::type::value,
|
|
"it should not consume any input"
|
|
);
|
|
|
|
static_assert(
|
|
is_error<
|
|
look_ahead<int_>::apply<BOOST_METAPARSE_STRING("six"), start>
|
|
>::type::value,
|
|
"it should fail when the wrapped parser fails"
|
|
);
|
|
|
|
[endsect]
|
|
|