except.qbk 2.0 KB

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