• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1[#token]
2[section token]
3
4[h1 Synopsis]
5
6  template <class P>
7  struct token;
8
9This is a [link parser_combinator parser combinator].
10
11[table Arguments
12  [[Name] [Type]]
13  [[`P`]  [[link parser parser]]]
14]
15
16[h1 Description]
17
18`token` parses the input using `P` when it succeeds, `token` consumes all
19whitespaces afterwards. The result of parsing is the result of `P`.
20
21[h1 Header]
22
23  #include <boost/metaparse/token.hpp>
24
25[h1 Expression semantics]
26
27For any `p` parser the following are equivalent:
28
29  token<p>
30
31  first_of<p, spaces>
32
33[h1 Example]
34
35  #include <boost/metaparse/token.hpp>
36  #include <boost/metaparse/int_.hpp>
37  #include <boost/metaparse/start.hpp>
38  #include <boost/metaparse/get_result.hpp>
39  #include <boost/metaparse/get_remaining.hpp>
40  #include <boost/metaparse/is_error.hpp>
41  #include <boost/metaparse/string.hpp>
42
43  #include <type_traits>
44
45  using namespace boost::metaparse;
46
47  using int_token = token<int_>;
48
49  static_assert(
50    get_result<
51      int_token::apply<BOOST_METAPARSE_STRING("13 "), start>
52    >::type::value,
53    "the result of int_token is the number"
54  );
55
56  static_assert(
57    std::is_same<
58      BOOST_METAPARSE_STRING(""),
59      get_remaining<int_token::apply<BOOST_METAPARSE_STRING("13 "), start>>::type
60    >::type::value,
61    "token consumes whitespaces after the number"
62  );
63
64  static_assert(
65    get_result<
66      int_token::apply<BOOST_METAPARSE_STRING("13"), start>
67    >::type::value,
68    "whitespaces after the number are optional"
69  );
70
71  static_assert(
72    is_error<int_token::apply<BOOST_METAPARSE_STRING("foo"), start>>::type::value,
73    "when there is no number, token fails"
74  );
75
76[endsect]
77
78