one_of.qbk 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. [#one_of]
  2. [section one_of]
  3. [h1 Synopsis]
  4. template <class... Ps>
  5. struct one_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. It accepts an input when any of the `Ps...` parsers accept it. The result of
  13. parsing is the result of applying the first parser that accepts the input. The
  14. parsers are tried in order, therefore in case of ambiguous grammars the result
  15. of parsing depends on the order of the `Ps...` parsers.
  16. On compilers, which are not C++11-compliant, the maximum number of accepted
  17. parsers is defined by the `BOOST_METAPARSE_LIMIT_ONE_OF_SIZE` macro. Its default
  18. value is `20`.
  19. [h1 Header]
  20. #include <boost/metaparse/one_of.hpp>
  21. [h1 Expression semantics]
  22. For any `p1`, ..., `pn` parsers, `s` compile-time string and `pos` source
  23. position
  24. one_of<p1, ..., pn>::apply<s, pos>
  25. is equivalent to
  26. pk::apply<s, pos>
  27. when there is a `k`, that `pi::apply<s, pos>::type` returns an error for every
  28. `i` in the range `[1..k)` and `pk::apply<s, pos>::type` doesn't return an error.
  29. The parser combinator returns an error when there is no such `k`.
  30. [h1 Example]
  31. #include <boost/metaparse/one_of.hpp>
  32. #include <boost/metaparse/lit_c.hpp>
  33. #include <boost/metaparse/start.hpp>
  34. #include <boost/metaparse/string.hpp>
  35. #include <boost/metaparse/get_result.hpp>
  36. #include <boost/metaparse/is_error.hpp>
  37. using namespace boost::metaparse;
  38. using whitespace =
  39. one_of<lit_c<' '>, lit_c<'\n'>, lit_c<'\r'>, lit_c<'\t'>, lit_c<'\v'>>;
  40. static_assert(
  41. get_result<
  42. whitespace::apply<BOOST_METAPARSE_STRING(" "), start>
  43. >::type::value == ' ',
  44. "the result of parsing should be the first character of the input"
  45. );
  46. static_assert(
  47. is_error<whitespace::apply<BOOST_METAPARSE_STRING("x"), start>>::type::value,
  48. "it should return an error when the input does not begin with a whitespace"
  49. );
  50. [endsect]