metaparse/doc/one_of.qbk

76 lines
1.9 KiB
Plaintext

[#one_of]
[section one_of]
[h1 Synopsis]
template <class... Ps>
struct one_of;
This is a [link parser_combinator parser combinator].
[table Arguments
[[Name] [Type]]
[[`Ps`] [[link parser parser]s]]
]
[h1 Description]
It accepts an input when any of the `Ps...` parsers accept it. The result of
parsing is the result of applying the first parser that accepts the input. The
parsers are tried in order, therefore in case of ambiguous grammars the result
of parsing depends on the order of the `Ps...` parsers.
On compilers, which are not C++11-compliant, the maximum number of accepted
parsers is defined by the `BOOST_METAPARSE_LIMIT_ONE_OF_SIZE` macro. Its default
value is `20`.
[h1 Header]
#include <boost/metaparse/one_of.hpp>
[h1 Expression semantics]
For any `p1`, ..., `pn` parsers, `s` compile-time string and `pos` source
position
one_of<p1, ..., pn>::apply<s, pos>
is equivalent to
pk::apply<s, pos>
when there is a `k`, that `pi::apply<s, pos>::type` returns an error for every
`i` in the range `[1..k)` and `pk::apply<s, pos>::type` doesn't return an error.
The parser combinator returns an error when there is no such `k`.
[h1 Example]
#include <boost/metaparse/one_of.hpp>
#include <boost/metaparse/lit_c.hpp>
#include <boost/metaparse/start.hpp>
#include <boost/metaparse/string.hpp>
#include <boost/metaparse/get_result.hpp>
#include <boost/metaparse/is_error.hpp>
using namespace boost::metaparse;
using whitespace =
one_of<lit_c<' '>, lit_c<'\n'>, lit_c<'\r'>, lit_c<'\t'>, lit_c<'\v'>>;
static_assert(
get_result<
whitespace::apply<BOOST_METAPARSE_STRING(" "), start>
>::type::value == ' ',
"the result of parsing should be the first character of the input"
);
static_assert(
is_error<whitespace::apply<BOOST_METAPARSE_STRING("x"), start>>::type::value,
"it should return an error when the input does not begin with a whitespace"
);
[endsect]