token.qbk 1.6 KB

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