• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1[#middle_of]
2[section middle_of]
3
4[h1 Synopsis]
5
6  template <class P1, class P2, class P3>
7  struct middle_of;
8
9This is a [link parser_combinator parser combinator].
10
11[table Arguments
12  [[Name] [Type]]
13  [[`P1`] [[link parser parser]]]
14  [[`P2`] [[link parser parser]]]
15  [[`P3`] [[link parser parser]]]
16]
17
18[h1 Description]
19
20`middle_of` applies `P1`, `P2` and `P3` in sequence. It accepts an input when
21all of these three parsers accept it. The result of parsing is the result of
22`P2`.
23
24[h1 Header]
25
26  #include <boost/metaparse/middle_of.hpp>
27
28[h1 Expression semantics]
29
30For any `p1`, `p2` and `p3` parsers
31
32  middle_of<p1, p2, p3>
33
34is equivalent to
35
36  nth_of<1, p1, p2, p3>
37
38[h1 Example]
39
40  #include <boost/metaparse/middle_of.hpp>
41  #include <boost/metaparse/int_.hpp>
42  #include <boost/metaparse/lit_c.hpp>
43  #include <boost/metaparse/token.hpp>
44  #include <boost/metaparse/start.hpp>
45  #include <boost/metaparse/string.hpp>
46  #include <boost/metaparse/is_error.hpp>
47  #include <boost/metaparse/get_result.hpp>
48
49  using namespace boost::metaparse;
50
51  using int_token = token<int_>;
52  using left_paren_token = token<lit_c<'('>>;
53  using right_paren_token = token<lit_c<')'>>;
54
55  using int_in_parens = middle_of<left_paren_token, int_token, right_paren_token>;
56
57  static_assert(
58    get_result<
59      int_in_parens::apply<BOOST_METAPARSE_STRING("(13)"), start>
60    >::type::value == 13,
61    "it should return the result of the middle parser"
62  );
63
64  static_assert(
65    is_error<
66      int_in_parens::apply<BOOST_METAPARSE_STRING("13"), start>
67    >::type::value,
68    "it should reject the input when there are no parens"
69  );
70
71  static_assert(
72    is_error<
73      int_in_parens::apply<BOOST_METAPARSE_STRING("(13"), start>
74    >::type::value,
75    "it should reject the input when there is no closing paren"
76  );
77
78[endsect]
79
80