look_ahead.qbk 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. [#look_ahead]
  2. [section look_ahead]
  3. [h1 Synopsis]
  4. template <class P>
  5. struct look_ahead;
  6. This is a [link parser_combinator parser combinator].
  7. [table Arguments
  8. [[Name] [Type]]
  9. [[`P`] [[link parser parser]]]
  10. ]
  11. [h1 Description]
  12. Parses the input using parser `P`. When `P` returns an error, `look_ahead`
  13. returns the error. When `P` returns a result, `look_ahead` returns the result
  14. without consuming anything from the input string.
  15. [h1 Header]
  16. #include <boost/metaparse/look_ahead.hpp>
  17. [h1 Expression semantics]
  18. For any `p` parser, `s` compile-time string and `pos` source position
  19. look_ahead<p>::apply<s, pos>
  20. is equivalent to
  21. return_<get_result<p::apply<s, pos>>::type>::apply<s, pos>
  22. when `p::apply<s, pos>` succeeds. It is
  23. p::apply<s, pos>
  24. otherwise.
  25. [h1 Example]
  26. #include <boost/metaparse/look_ahead.hpp>
  27. #include <boost/metaparse/int_.hpp>
  28. #include <boost/metaparse/start.hpp>
  29. #include <boost/metaparse/string.hpp>
  30. #include <boost/metaparse/is_error.hpp>
  31. #include <boost/metaparse/get_result.hpp>
  32. #include <boost/metaparse/get_remaining.hpp>
  33. #include <type_traits>
  34. using namespace boost::metaparse;
  35. static_assert(
  36. get_result<
  37. look_ahead<int_>::apply<BOOST_METAPARSE_STRING("13"), start>
  38. >::type::value == 13,
  39. "it should return the same result as the wrapped parser"
  40. );
  41. static_assert(
  42. std::is_same<
  43. BOOST_METAPARSE_STRING("13"),
  44. get_remaining<
  45. look_ahead<int_>::apply<BOOST_METAPARSE_STRING("13"), start>
  46. >::type
  47. >::type::value,
  48. "it should not consume any input"
  49. );
  50. static_assert(
  51. is_error<
  52. look_ahead<int_>::apply<BOOST_METAPARSE_STRING("six"), start>
  53. >::type::value,
  54. "it should fail when the wrapped parser fails"
  55. );
  56. [endsect]