sequence.qbk 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. [#sequence]
  2. [section sequence]
  3. [h1 Synopsis]
  4. template <class... Ps>
  5. struct sequence;
  6. This is a [link parser_combinator parser combinator].
  7. [table Arguments
  8. [[Name] [Type]]
  9. [[`Ps`] [[link parser parser]s]]
  10. ]
  11. [h1 Description]
  12. `sequence` applies the `Ps...` parsers in sequence on the input. It accepts an
  13. input when all of these parsers accept it. The result of parsing is a sequence
  14. of the results of the parsers.
  15. On compilers, which are not C++11-compliant, the maximum number of parsers
  16. `sequence` accepts can be specified with the
  17. `BOOST_METAPARSE_LIMIT_SEQUENCE_SIZE` macro. Its default value is `5`.
  18. [h1 Header]
  19. #include <boost/metaparse/sequence.hpp>
  20. [h1 Expression semantics]
  21. For any `n > 0`, `p0`, ..., `pn` parsers the result of `sequence<p0, ..., p1>`
  22. is a compile-time sequence of the results of the parsers, applied after each
  23. other in order on the input string when none of them returns an error.
  24. The remaining string is the remaining string the last parser returns.
  25. When one of the parsers returns an error, the combinator returns that error.
  26. [h1 Example]
  27. #include <boost/metaparse/sequence.hpp>
  28. #include <boost/metaparse/token.hpp>
  29. #include <boost/metaparse/int_.hpp>
  30. #include <boost/metaparse/lit_c.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 <boost/mpl/at.hpp>
  36. using namespace boost::metaparse;
  37. using int_token = token<int_>;
  38. using plus_token = token<lit_c<'+'>>;
  39. using a_plus_b = sequence<int_token, plus_token, int_token>;
  40. static_assert(
  41. boost::mpl::at_c<
  42. get_result<a_plus_b::apply<BOOST_METAPARSE_STRING("1 + 2"), start>>::type,
  43. 0
  44. >::type::value == 1,
  45. "the first element of the sequence should be the first number"
  46. );
  47. static_assert(
  48. boost::mpl::at_c<
  49. get_result<a_plus_b::apply<BOOST_METAPARSE_STRING("1 + 2"), start>>::type,
  50. 1
  51. >::type::value == '+',
  52. "the second element of the sequence should be the plus"
  53. );
  54. static_assert(
  55. boost::mpl::at_c<
  56. get_result<a_plus_b::apply<BOOST_METAPARSE_STRING("1 + 2"), start>>::type,
  57. 2
  58. >::type::value == 2,
  59. "the third element of the sequence should be the second number"
  60. );
  61. static_assert(
  62. is_error<a_plus_b::apply<BOOST_METAPARSE_STRING("1 +"), start>>::type::value,
  63. "when not all of the parsers accept the input, sequence should fail"
  64. );
  65. [endsect]