first_of.qbk 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. [#first_of]
  2. [section first_of]
  3. [h1 Synopsis]
  4. template <class... Ps>
  5. struct first_of;
  6. This is a [link parser_combinator parser combinator].
  7. [table Arguments
  8. [[Name] [Type]]
  9. [[`Ps`] [[link parser parser]s]]
  10. ]
  11. [h1 Description]
  12. `first_of` applies the `Ps...` parsers in sequence. It accepts an input when all
  13. parsers accept it. The result of parsing is the result of the first parser.
  14. On compilers, which are not C++11-compliant, the maximum number of parsers
  15. `first_of` accepts can be specified with the
  16. `BOOST_METAPARSE_LIMIT_SEQUENCE_SIZE` macro. Its default value is `5`.
  17. [h1 Header]
  18. #include <boost/metaparse/first_of.hpp>
  19. [h1 Expression semantics]
  20. For any `p1`, ... `pn` parsers
  21. first_of<p1, ..., pn>
  22. is equivalent to
  23. nth_of_c<0, p1, ..., pn>
  24. [h1 Example]
  25. #include <boost/metaparse/first_of.hpp>
  26. #include <boost/metaparse/int_.hpp>
  27. #include <boost/metaparse/lit_c.hpp>
  28. #include <boost/metaparse/string.hpp>
  29. #include <boost/metaparse/start.hpp>
  30. #include <boost/metaparse/is_error.hpp>
  31. #include <boost/metaparse/get_result.hpp>
  32. #include <type_traits>
  33. using namespace boost::metaparse;
  34. using int_with_semicolon = first_of<int_, lit_c<';'>>;
  35. static_assert(
  36. is_error<
  37. int_with_semicolon::apply<BOOST_METAPARSE_STRING("13"), start>
  38. >::type::value,
  39. "int without semicolon is rejected"
  40. );
  41. static_assert(
  42. get_result<
  43. int_with_semicolon::apply<BOOST_METAPARSE_STRING("13;"), start>
  44. >::type::value,
  45. "the result is the result of the first parser"
  46. );
  47. [endsect]