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