Header lexy/dsl/times.hpp

The times and twice rules.

Rule lexy::dsl::times

lexy/dsl/times.hpp
namespace lexy::dsl
{
    template <std::size_t N>
    constexpr rule auto times(rule auto rule);

    template <std::size_t N>
    constexpr rule auto times(rule auto rule, separator auto sep);
}

times is a rule that parses rule exactly N times in sequence, with an optional separator in between.

Parsing

Parses rule exactly N times in sequence . If a separator  has been specified, parses it in between. After the last rule, handles a trailing separator as necessary.

Errors
  • All errors raised by parsing rule or sep. The rule then fails.

  • lexy::unexpected_trailing_separator: if a trailing separator can be matched after the last item and that is not allowed; at the position of the separator. It then recovers by simply consuming the separator.

Values

All values produced by each rule invocation in order.

Example 1. Parse and sum three integers separated by comma
struct production
{
    static constexpr auto rule = [] {
        auto item = dsl::integer<int>;
        auto sep  = dsl::trailing_sep(dsl::comma);
        return dsl::times<3>(item, sep);
    }();

    static constexpr auto value
        = lexy::callback<int>([](int a, int b, int c) { return a + b + c; });
};
Note
See lexy::dsl::repeat  if the number of repetitions should be determined by the input.

Rule lexy::dsl::twice

lexy/dsl/times.hpp
namespace lexy::dsl
{
    constexpr rule auto twice(rule auto rule)
    {
        return times<2>(rule);
    }

    constexpr rule auto twice(rule auto rule, separator auto sep)
    {
        return times<2>(rule, sep);
    }
}

twice is a rule that is an alias for times where N == 2.

See also