one_char.qbk 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. [#one_char]
  2. [section one_char]
  3. [h1 Synopsis]
  4. struct one_char;
  5. This is a [link parser parser].
  6. [h1 Description]
  7. `one_char` accepts one character. The result of parsing is the accepted
  8. character. It fails for empty input.
  9. [h1 Header]
  10. #include <boost/metaparse/one_char.hpp>
  11. [h1 Expression semantics]
  12. For any `s` non-empty compile-time string and `pos` source position the
  13. following are equivalent
  14. get_result<one_char::apply<s, pos>>
  15. boost::mpl::front<s>
  16. get_remaining<one_char::apply<s, pos>>
  17. boost::mpl::pop_front<s>
  18. The value of `get_position<one_char::apply<s, pos>>` depends on the first
  19. character of the sequence and the value of the one parsed before that (which is
  20. stored in the source position). `one_char` updates the source position's `col`
  21. and `line` values based on the new line characters. It detects Linux (`\n`),
  22. Windows (`\r\n`) and Mac (`\r`) line endings.
  23. `one_char::apply<BOOST_METAPARSE_STRING(""), pos>` returns an error for every
  24. `pos` source position.
  25. [h1 Example]
  26. #include <boost/metaparse/one_char.hpp>
  27. #include <boost/metaparse/start.hpp>
  28. #include <boost/metaparse/string.hpp>
  29. #include <boost/metaparse/get_result.hpp>
  30. #include <boost/metaparse/get_remaining.hpp>
  31. #include <boost/metaparse/is_error.hpp>
  32. #include <type_traits>
  33. using namespace boost::metaparse;
  34. static_assert(
  35. get_result<
  36. one_char::apply<BOOST_METAPARSE_STRING("foo"), start>
  37. >::type::value == 'f',
  38. "the result of parsing should be the first character of the input"
  39. );
  40. static_assert(
  41. std::is_same<
  42. BOOST_METAPARSE_STRING("oo"),
  43. get_remaining<one_char::apply<BOOST_METAPARSE_STRING("foo"), start>>::type
  44. >::type::value,
  45. "one_char should consume the first character of the input"
  46. );
  47. static_assert(
  48. is_error<one_char::apply<BOOST_METAPARSE_STRING(""), start>>::type::value,
  49. "it should return an error for empty input"
  50. );
  51. [endsect]