nth_of_c.qbk 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. [#nth_of_c]
  2. [section nth_of_c]
  3. [h1 Synopsis]
  4. template <int N, class... Ps>
  5. struct nth_of_c;
  6. This is a [link parser_combinator parser combinator].
  7. [table Arguments
  8. [[Name] [Type]]
  9. [[`N`] [`int` value in the range `[0..sizeof...(Ps)]`]]
  10. [[`Ps`] [[link parser parser]s]]
  11. ]
  12. [h1 Description]
  13. `nth_of_c` applies the `Ps...` parsers in sequence. It accepts an input
  14. when all of these parsers accept it. The result of parsing is the result of the
  15. `N`. parser.
  16. On compilers, which are not C++11-compliant, the maximum number of parsers
  17. `nth_of_c` accepts can be specified with the
  18. `BOOST_METAPARSE_LIMIT_SEQUENCE_SIZE` macro. Its default value is `5`.
  19. [h1 Header]
  20. #include <boost/metaparse/nth_of.hpp>
  21. [h1 Expression semantics]
  22. For any `p0`, ..., `pn` parsers and `k` integer value, where `0 <= k < n` the
  23. following are equivalent
  24. nth_of_c<k, p0, ..., pn>
  25. transform<sequence<p0, ..., pn>, boost::mpl::at_c<boost::mpl::_1, k>>
  26. [h1 Example]
  27. #include <boost/metaparse/nth_of_c.hpp>
  28. #include <boost/metaparse/int_.hpp>
  29. #include <boost/metaparse/lit_c.hpp>
  30. #include <boost/metaparse/token.hpp>
  31. #include <boost/metaparse/start.hpp>
  32. #include <boost/metaparse/string.hpp>
  33. #include <boost/metaparse/is_error.hpp>
  34. #include <boost/metaparse/get_result.hpp>
  35. using namespace boost::metaparse;
  36. using int_token = token<int_>;
  37. using left_paren_token = token<lit_c<'('>>;
  38. using right_paren_token = token<lit_c<')'>>;
  39. using int_in_parens =
  40. nth_of_c<1, left_paren_token, int_token, right_paren_token>;
  41. static_assert(
  42. get_result<
  43. int_in_parens::apply<BOOST_METAPARSE_STRING("(13)"), start>
  44. >::type::value == 13,
  45. "it should return the result of the second parser"
  46. );
  47. static_assert(
  48. is_error<
  49. int_in_parens::apply<BOOST_METAPARSE_STRING("13"), start>
  50. >::type::value,
  51. "it should reject the input when there are no parens"
  52. );
  53. static_assert(
  54. is_error<
  55. int_in_parens::apply<BOOST_METAPARSE_STRING("(13"), start>
  56. >::type::value,
  57. "it should reject the input when there is no closing paren"
  58. );
  59. [endsect]