• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1[#except]
2[section except]
3
4[h1 Synopsis]
5
6  template <class P, class Result, class ErrorMsg>
7  struct except;
8
9This is a [link parser_combinator parser combinator].
10
11[table Arguments
12  [[Name]       [Type]]
13  [[`P`]        [[link parser parser]]]
14  [[`Result`]   [[link metaprogramming_value template metaprogramming value]]]
15  [[`ErrorMsg`] [[link parsing_error_message parsing error message]]]
16]
17
18[h1 Description]
19
20`except` accepts the input when `P` rejects it and the result of parsing is the
21`Result` argument. When `P` accepts the input, `except` rejects it and the
22reason is `ErrorMsg`.
23
24[h1 Header]
25
26  #include <boost/metaparse/except.hpp>
27
28[h1 Expression semantics]
29
30For any `p` parser, `c` class, `msg` parsing error message, `s` compile-time
31string and `pos` source position the following are equivalent
32
33  get_result<except<p, c, msg>, s, pos>::type
34  c
35
36  get_remaining<except<p, c, msg>, s, pos>::type
37  s
38
39  get_position<except<p, c, msg>, s, pos>::type
40  pos
41
42when `p` rejects the input. The result of the parser is an error with the error
43message `msg` otherwise.
44
45[h1 Example]
46
47  #include <boost/metaparse/except.hpp>
48  #include <boost/metaparse/int_.hpp>
49  #include <boost/metaparse/string.hpp>
50  #include <boost/metaparse/start.hpp>
51  #include <boost/metaparse/get_result.hpp>
52  #include <boost/metaparse/get_message.hpp>
53  #include <boost/metaparse/define_error.hpp>
54
55  #include <type_traits>
56
57  using namespace boost::metaparse;
58
59  BOOST_METAPARSE_DEFINE_ERROR(
60    number_is_not_allowed,
61    "numbers are not allowed here"
62  );
63
64  using except_int =
65    except<int_, std::integral_constant<int, 1>, number_is_not_allowed>;
66
67  static_assert(
68    get_result<
69      except_int::apply<BOOST_METAPARSE_STRING("foo"), start>
70    >::type::value == 1,
71    "it should accept the input when it is not an integer"
72  );
73
74  static_assert(
75    std::is_same<
76      number_is_not_allowed,
77      get_message<except_int::apply<BOOST_METAPARSE_STRING("13"), start>>::type
78    >::type::value,
79    "it should reject the input when it is an integer"
80  );
81
82[endsect]
83
84