Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

one_of

Synopsis

template <class P1, class P2, /* ... */, class Pn>
struct one_of;

This is a parser combinator.

Table 67. Arguments

Name

Type

P1 .. Pn

parsers


Description

It accepts an input when any of the P1, ... Pn 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 P1 ... Pn parsers.

The maximum number of accepted parsers is defined by the BOOST_METAPARSE_LIMIT_ONE_OF_SIZE macro. Its default value is 20.

Header

#include <boost/metaparse/one_of.hpp>

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.

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"
);

PrevUpHomeNext