token.qbk 1.63 KB
[#token]
[section token]

[h1 Synopsis]

  template <class P>
  struct token;

This is a [link parser_combinator parser combinator].

[table Arguments
  [[Name] [Type]]
  [[`P`]  [[link parser parser]]]
]

[h1 Description]

`token` parses the input using `P` when it succeeds, `token` consumes all
whitespaces afterwards. The result of parsing is the result of `P`.

[h1 Header]

  #include <boost/metaparse/token.hpp>

[h1 Expression semantics]

For any `p` parser the following are equivalent:

  token<p>
  
  first_of<p, spaces>

[h1 Example]

  #include <boost/metaparse/token.hpp>
  #include <boost/metaparse/int_.hpp>
  #include <boost/metaparse/start.hpp>
  #include <boost/metaparse/get_result.hpp>
  #include <boost/metaparse/get_remaining.hpp>
  #include <boost/metaparse/is_error.hpp>
  #include <boost/metaparse/string.hpp>
  
  #include <type_traits>
  
  using namespace boost::metaparse;
  
  using int_token = token<int_>;
  
  static_assert(
    get_result<
      int_token::apply<BOOST_METAPARSE_STRING("13 "), start>
    >::type::value,
    "the result of int_token is the number"
  );
  
  static_assert(
    std::is_same<
      BOOST_METAPARSE_STRING(""),
      get_remaining<int_token::apply<BOOST_METAPARSE_STRING("13 "), start>>::type
    >::type::value,
    "token consumes whitespaces after the number"
  );
  
  static_assert(
    get_result<
      int_token::apply<BOOST_METAPARSE_STRING("13"), start>
    >::type::value,
    "whitespaces after the number are optional"
  );
  
  static_assert(
    is_error<int_token::apply<BOOST_METAPARSE_STRING("foo"), start>>::type::value,
    "when there is no number, token fails"
  );

[endsect]