nth_of.qbk 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. [#nth_of]
  2. [section nth_of]
  3. [h1 Synopsis]
  4. template <class N, class... Ps>
  5. struct nth_of;
  6. This is a [link parser_combinator parser combinator].
  7. [table Arguments
  8. [[Name] [Type]]
  9. [[`N`] [[link boxed_value boxed] integer value in the range `[0..sizeof...(Ps)]`]]
  10. [[`Ps`] [[link parser parser]s]]
  11. ]
  12. [h1 Description]
  13. `nth_of` applies the `Ps...` parsers in sequence. It accepts an input when all
  14. of these parsers accept it. The result of parsing is the result of the `N`.
  15. parser.
  16. On compilers, which are not C++11-compliant, the maximum number of parsers
  17. `nth_of` accepts can be specified with the `BOOST_METAPARSE_LIMIT_SEQUENCE_SIZE`
  18. 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` boxed integer value the following are
  23. equivalent
  24. nth_of<k, p0, ..., pn>
  25. nth_of_c<k::type::value, p0, ..., pn>
  26. [h1 Example]
  27. #include <boost/metaparse/nth_of.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. #include <type_traits>
  36. using namespace boost::metaparse;
  37. using int_token = token<int_>;
  38. using left_paren_token = token<lit_c<'('>>;
  39. using right_paren_token = token<lit_c<')'>>;
  40. using int_in_parens =
  41. nth_of<
  42. std::integral_constant<int, 1>,
  43. left_paren_token, int_token, right_paren_token
  44. >;
  45. static_assert(
  46. get_result<
  47. int_in_parens::apply<BOOST_METAPARSE_STRING("(13)"), start>
  48. >::type::value == 13,
  49. "it should return the result of the second parser"
  50. );
  51. static_assert(
  52. is_error<
  53. int_in_parens::apply<BOOST_METAPARSE_STRING("13"), start>
  54. >::type::value,
  55. "it should reject the input when there are no parens"
  56. );
  57. static_assert(
  58. is_error<
  59. int_in_parens::apply<BOOST_METAPARSE_STRING("(13"), start>
  60. >::type::value,
  61. "it should reject the input when there is no closing paren"
  62. );
  63. [endsect]