accept_when.qbk 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. [#accept_when]
  2. [section accept_when]
  3. [h1 Synopsis]
  4. template <class P, class Pred, class Msg>
  5. struct accept_when;
  6. This is a [link parser_combinator parser combinator].
  7. [table Arguments
  8. [[Name] [Type]]
  9. [[`P`] [[link parser parser]]]
  10. [[`Pred`] [[link predicate predicate]]]
  11. [[`Msg`] [[link parsing_error_message parsing error message]]]
  12. ]
  13. [h1 Description]
  14. It parses the input with `P`. When `P` rejects the input, `accept_when` rejects
  15. it as well. When `P` accepts it, `accept_when` evaluates `Pred` with the result
  16. of parsing the input with `P`. When `Pred` returns `true`, `accept_when` accepts
  17. the input and the result of parsing will be what `P` returned. Otherwise
  18. `accept_when` rejects the input and `Msg` is used as the error reason.
  19. [h1 Header]
  20. #include <boost/metaparse/accept_when.hpp>
  21. [h1 Expression semantics]
  22. For any `p` parser, `pred` predicate, `msg` parsing error message, `s`
  23. compile-time string and `pos` source position
  24. accept_when<p, pred, msg>i::apply<s, pos>::type
  25. is equivalent to
  26. p::apply<s, pos>::type
  27. when `p::apply<s, pos>` doesn't return an error and
  28. `pred::apply<get_result<p::apply<s, pos>>>::type` is `true`. Otherwise it is
  29. equivalent to
  30. fail<msg>
  31. [h1 Example]
  32. #include <boost/metaparse/accept_when.hpp>
  33. #include <boost/metaparse/one_char.hpp>
  34. #include <boost/metaparse/util/is_digit.hpp>
  35. #include <boost/metaparse/start.hpp>
  36. #include <boost/metaparse/string.hpp>
  37. #include <boost/metaparse/is_error.hpp>
  38. #include <boost/metaparse/get_result.hpp>
  39. #include <boost/metaparse/define_error.hpp>
  40. using namespace boost::metaparse;
  41. BOOST_METAPARSE_DEFINE_ERROR(digit_expected, "Digit expected");
  42. using accept_digit = accept_when<one_char, util::is_digit<>, digit_expected>;
  43. static_assert(
  44. !is_error<
  45. accept_digit::apply<BOOST_METAPARSE_STRING("0"), start>
  46. >::type::value,
  47. "accept_digit should accept a digit"
  48. );
  49. static_assert(
  50. get_result<
  51. accept_digit::apply<BOOST_METAPARSE_STRING("0"), start>
  52. >::type::value == '0',
  53. "the result of parsing should be the character value"
  54. );
  55. static_assert(
  56. is_error<
  57. accept_digit::apply<BOOST_METAPARSE_STRING("x"), start>
  58. >::type::value,
  59. "accept_digit should reject a character that is not a digit"
  60. );
  61. [endsect]