middle_of.qbk 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. [#middle_of]
  2. [section middle_of]
  3. [h1 Synopsis]
  4. template <class P1, class P2, class P3>
  5. struct middle_of;
  6. This is a [link parser_combinator parser combinator].
  7. [table Arguments
  8. [[Name] [Type]]
  9. [[`P1`] [[link parser parser]]]
  10. [[`P2`] [[link parser parser]]]
  11. [[`P3`] [[link parser parser]]]
  12. ]
  13. [h1 Description]
  14. `middle_of` applies `P1`, `P2` and `P3` in sequence. It accepts an input when
  15. all of these three parsers accept it. The result of parsing is the result of
  16. `P2`.
  17. [h1 Header]
  18. #include <boost/metaparse/middle_of.hpp>
  19. [h1 Expression semantics]
  20. For any `p1`, `p2` and `p3` parsers
  21. middle_of<p1, p2, p3>
  22. is equivalent to
  23. nth_of<1, p1, p2, p3>
  24. [h1 Example]
  25. #include <boost/metaparse/middle_of.hpp>
  26. #include <boost/metaparse/int_.hpp>
  27. #include <boost/metaparse/lit_c.hpp>
  28. #include <boost/metaparse/token.hpp>
  29. #include <boost/metaparse/start.hpp>
  30. #include <boost/metaparse/string.hpp>
  31. #include <boost/metaparse/is_error.hpp>
  32. #include <boost/metaparse/get_result.hpp>
  33. using namespace boost::metaparse;
  34. using int_token = token<int_>;
  35. using left_paren_token = token<lit_c<'('>>;
  36. using right_paren_token = token<lit_c<')'>>;
  37. using int_in_parens = middle_of<left_paren_token, int_token, right_paren_token>;
  38. static_assert(
  39. get_result<
  40. int_in_parens::apply<BOOST_METAPARSE_STRING("(13)"), start>
  41. >::type::value == 13,
  42. "it should return the result of the middle parser"
  43. );
  44. static_assert(
  45. is_error<
  46. int_in_parens::apply<BOOST_METAPARSE_STRING("13"), start>
  47. >::type::value,
  48. "it should reject the input when there are no parens"
  49. );
  50. static_assert(
  51. is_error<
  52. int_in_parens::apply<BOOST_METAPARSE_STRING("(13"), start>
  53. >::type::value,
  54. "it should reject the input when there is no closing paren"
  55. );
  56. [endsect]