Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

sequence

Synopsis

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

This is a parser combinator.

Table 79. Arguments

Name

Type

P1 .. Pn

parser


Description

sequence applies the P1, ..., Pn parsers in sequence on the input. It accepts an input when all of these parsers accept it. The result of parsing is a sequence of the results of the parsers.

The maximum number of parsers sequence accepts can be specified with the BOOST_METAPARSE_LIMIT_SEQUENCE_SIZE macro. Its default value is 5.

Header

#include <boost/metaparse/sequence.hpp>

Expression semantics

For any n > 0, p0, ..., pn parsers the result of sequence<p0, ..., p1> is a compile-time sequence of the results of the parsers, applied after each other in order on the input string when none of them returns an error. The remaining string is the remaining string the last parser returns.

When one of the parsers returns an error, the combinator returns that error.

Example

#include <boost/metaparse/sequence.hpp>
#include <boost/metaparse/token.hpp>
#include <boost/metaparse/int_.hpp>
#include <boost/metaparse/lit_c.hpp>
#include <boost/metaparse/start.hpp>
#include <boost/metaparse/string.hpp>
#include <boost/metaparse/is_error.hpp>
#include <boost/metaparse/get_result.hpp>

#include <boost/mpl/at.hpp>

using namespace boost::metaparse;

using int_token = token<int_>;
using plus_token = token<lit_c<'+'>>;

using a_plus_b = sequence<int_token, plus_token, int_token>;

static_assert(
  boost::mpl::at_c<
    get_result<a_plus_b::apply<BOOST_METAPARSE_STRING("1 + 2"), start>>::type,
    0
  >::type::value == 1,
  "the first element of the sequence should be the first number"
);

static_assert(
  boost::mpl::at_c<
    get_result<a_plus_b::apply<BOOST_METAPARSE_STRING("1 + 2"), start>>::type,
    1
  >::type::value == '+',
  "the second element of the sequence should be the plus"
);

static_assert(
  boost::mpl::at_c<
    get_result<a_plus_b::apply<BOOST_METAPARSE_STRING("1 + 2"), start>>::type,
    2
  >::type::value == 2,
  "the third element of the sequence should be the second number"
);

static_assert(
  is_error<a_plus_b::apply<BOOST_METAPARSE_STRING("1 +"), start>>::type::value,
  "when not all of the parsers accept the input, sequence should fail"
);

PrevUpHomeNext