File: transform_error.qbk

package info (click to toggle)
boost1.90 1.90.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 593,156 kB
  • sloc: cpp: 4,190,642; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,776; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (101 lines) | stat: -rw-r--r-- 2,775 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
[#transform_error]
[section transform_error]

[h1 Synopsis]

  template <class P, class F>
  struct transform_error;

This is a [link parser_combinator parser combinator].

[table Arguments
  [[Name] [Type]]
  [[`P`]  [[link parser parser]]]
  [[`F`]  [[link metafunction_class template metafunction class] taking one argument]]
]

[h1 Description]

It parses the input with `P`. When this succeeds, the result of parsing with
`transform_error` will be the result of parsing with `P`. When it fails, `F` is
evaluated with the error `P` returned as argument. Parsing with
`transform_error` will fail and the error will be what `F` returns. Therefore,
`F` is expected to accept and return a [link reject `reject`] value.

[h1 Header]

  #include <boost/metaparse/transform_error.hpp>

[h1 Expression semantics]

For any `p` parser and `f` metafunction class accepting one argument

  transform_error<p, f>::apply<s, pos>

is equivalent to `p::apply<s, pos>` when `p` accepts the input. It is equivalent
to `f::apply<p::apply<s, pos>::type>` otherwise.

[h1 Example]

  #include <boost/metaparse/transform_error.hpp>
  #include <boost/metaparse/repeated1.hpp>
  #include <boost/metaparse/letter.hpp>
  #include <boost/metaparse/keyword.hpp>
  #include <boost/metaparse/last_of.hpp>
  #include <boost/metaparse/token.hpp>
  #include <boost/metaparse/string.hpp>
  #include <boost/metaparse/is_error.hpp>
  #include <boost/metaparse/start.hpp>
  #include <boost/metaparse/get_message.hpp>
  #include <boost/metaparse/get_position.hpp>
  #include <boost/metaparse/define_error.hpp>
  #include <boost/metaparse/reject.hpp>
  
  #include <boost/mpl/lambda.hpp>
  
  #include <type_traits>
  
  using namespace boost::metaparse;
  
  BOOST_METAPARSE_DEFINE_ERROR(name_expected, "Name expected");
  
  using keyword_name = token<keyword<BOOST_METAPARSE_STRING("name")>>;
  using name_token = token<repeated1<letter>>;
  
  using name_parser =
    last_of<
      keyword_name,
      transform_error<
        name_token,
        boost::mpl::lambda<
          reject<name_expected, get_position<boost::mpl::_1> >
        >::type
      >
    >;
  
  static_assert(
    !is_error<
      name_parser::apply<BOOST_METAPARSE_STRING("name Bela"), start>
    >::type::value,
    "name_parser should accept \"name <a name>\""
  );
  
  static_assert(
    is_error<
      name_parser::apply<BOOST_METAPARSE_STRING("name ?"), start>
    >::type::value,
    "name_parser should reject input when name is a question mark"
  );
  
  static_assert(
    std::is_same<
      get_message<
        name_parser::apply<BOOST_METAPARSE_STRING("name ?"), start>
      >::type,
      name_expected
    >::type::value,
    "the error message should be the one specified by change_error_message"
  );

[endsect]