transform.qbk 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. [#transform]
  2. [section transform]
  3. [h1 Synopsis]
  4. template <class P, class T>
  5. struct transform;
  6. This is a [link parser_combinator parser combinator].
  7. [table Arguments
  8. [[Name] [Type]]
  9. [[`P`] [[link parser parser]]]
  10. [[`T`] [[link metafunction_class template metafunction class] taking one argument]]
  11. ]
  12. [h1 Description]
  13. `transform` parses the input using `P` and transforms the result `P` returns
  14. with `T`. The result of parsing is what `T` returns. When `P` fails, the failure
  15. is returned unchanged.
  16. [h1 Header]
  17. #include <boost/metaparse/transform.hpp>
  18. [h1 Expression semantics]
  19. For any `p` parser, `t` metafunction class accepting one argument, `s`
  20. compile-time string and `pos` source position
  21. get_result<transform<p, t>::apply<s, pos>>::type
  22. is equivalent to
  23. t::apply<get_result<p::apply<s, pos>>::type>::type
  24. When `p::apply<s, pos>` doesn't return an error. The combinator returns the
  25. error otherwise.
  26. [h1 Example]
  27. #include <boost/metaparse/transform.hpp>
  28. #include <boost/metaparse/digit.hpp>
  29. #include <boost/metaparse/start.hpp>
  30. #include <boost/metaparse/string.hpp>
  31. #include <boost/metaparse/is_error.hpp>
  32. #include <boost/metaparse/get_result.hpp>
  33. #include <boost/metaparse/util/digit_to_int.hpp>
  34. using namespace boost::metaparse;
  35. using digit_value = transform<digit, util::digit_to_int<>>;
  36. static_assert(
  37. !is_error<
  38. digit_value::apply<BOOST_METAPARSE_STRING("0"), start>
  39. >::type::value,
  40. "digit_val should accept a digit"
  41. );
  42. static_assert(
  43. is_error<digit_value::apply<BOOST_METAPARSE_STRING("x"), start>>::type::value,
  44. "digit_val should reject a character"
  45. );
  46. static_assert(
  47. get_result<
  48. digit_value::apply<BOOST_METAPARSE_STRING("0"), start>
  49. >::type::value == 0,
  50. "the result of parsing should be the int value"
  51. );
  52. [endsect]