sequence_apply.qbk 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. [#sequence_apply]
  2. [section sequence_apply]
  3. [h1 Synopsis]
  4. template <template <class, ..., class> class T, class P1, ..., class Pn>
  5. struct sequence_applyn;
  6. This is a [link parser_combinator parser combinator].
  7. [table Arguments
  8. [[T] [Template class taking `n` arguments]]
  9. [[`P1`..`Pn`] [[link parser parser]s]]
  10. ]
  11. [h1 Description]
  12. It applies the `P1` ... `Pn` [link parser parser]s on the input in order. When
  13. all of them succeed, the result of parsing with `sequence_applyn` is the `T`
  14. template class instantiated with the results of the `P1` ... `Pn`
  15. [link parser parser]s. When any of the `P1` ... `Pn` [link parser parser]s
  16. reject the input, the error is propagated.
  17. `n` has to be in the `[1..BOOST_METAPARSE_LIMIT_SEQUENCE_SIZE)` range.
  18. [h1 Header]
  19. #include <boost/metaparse/sequence_apply.hpp>
  20. [h1 Expression semantics]
  21. For any `n > 0`, `p1` ... `pn` [link parser parser]s, `t` template class with
  22. `n` `class` arguments, `s` compile-time string and `pos` source position the
  23. following are equivalent:
  24. sequence_apply<t, p1, ..., pn>::apply<s, pos>::type
  25. when [link sequence `sequence`]`<p1, ..., pn>` accepts the input:
  26. return_<
  27. t<
  28. mpl::at_c<0, get_result<sequence<p1,...,pn>::apply<s, pos>>::type>::type,
  29. ...
  30. mpl::at_c<n, get_result<sequence<p1,...,pn>::apply<s, pos>>::type>::type,
  31. >
  32. >::apply<s, pos>::type
  33. when [link sequence `sequence`]`<p1, ..., pn>` rejects the input:
  34. sequence<p1, ..., pn>::apply<s, pos>::type
  35. [h1 Example]
  36. #include <boost/metaparse/sequence_apply.hpp>
  37. #include <boost/metaparse/int_.hpp>
  38. #include <boost/metaparse/middle_of.hpp>
  39. #include <boost/metaparse/lit_c.hpp>
  40. #include <boost/metaparse/start.hpp>
  41. #include <boost/metaparse/string.hpp>
  42. #include <boost/metaparse/get_result.hpp>
  43. #include <boost/type_traits/is_same.hpp>
  44. using namespace boost::metaparse;
  45. template <int Real, int Imaginary>
  46. struct complex_c { typedef complex_c type; };
  47. template <class Real, class Imaginary>
  48. struct complex : complex_c<Real::type::value, Imaginary::type::value> {};
  49. typedef
  50. sequence_apply2<complex, int_, middle_of<lit_c<'+'>, int_, lit_c<'i'>>>
  51. complex_parser;
  52. static_assert(
  53. boost::is_same<
  54. complex_c<1, 2>,
  55. get_result<
  56. complex_parser::apply<BOOST_METAPARSE_STRING("1+2i"), start>
  57. >::type::type
  58. >::type::value,
  59. "the result of parsing should be the list of digit values"
  60. );
  61. [endsect]