always.qbk 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. [#always]
  2. [section always]
  3. [h1 Synopsis]
  4. template <class P, class T>
  5. struct always;
  6. This is a [link parser_combinator parser combinator].
  7. [table Arguments
  8. [[Name] [Type]]
  9. [[`P`] [[link parser parser]]]
  10. [[`T`] [[link metaprogramming_value template metaprogramming value]]]
  11. ]
  12. [h1 Description]
  13. It accepts an input if and only if `P` accepts it, but the result of parsing
  14. will be `T` instead of the result `P` returned.
  15. [h1 Header]
  16. #include <boost/metaparse/always.hpp>
  17. [h1 Expression semantics]
  18. For any `p` parser and `t` class the following are equivalent:
  19. always<p, t>
  20. last_of<p, return_<t>>
  21. [h1 Example]
  22. #include <boost/metaparse/always.hpp>
  23. #include <boost/metaparse/lit_c.hpp>
  24. #include <boost/metaparse/start.hpp>
  25. #include <boost/metaparse/string.hpp>
  26. #include <boost/metaparse/is_error.hpp>
  27. #include <boost/metaparse/get_result.hpp>
  28. #include <type_traits>
  29. using namespace boost::metaparse;
  30. using always13 = always<lit_c<'x'>, std::integral_constant<int, 13>>;
  31. static_assert(
  32. !is_error<always13::apply<BOOST_METAPARSE_STRING("x"), start>>::type::value,
  33. "always13 should accept x"
  34. );
  35. static_assert(
  36. std::is_same<
  37. get_result<always13::apply<BOOST_METAPARSE_STRING("x"), start>>::type,
  38. std::integral_constant<int, 13>
  39. >::type::value,
  40. "the result of parsing should be the integral_constant type"
  41. );
  42. static_assert(
  43. is_error<always13::apply<BOOST_METAPARSE_STRING("y"), start>>::type::value,
  44. "always13 should reject characters other than x"
  45. );
  46. [endsect]