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