Header lexy/dsl/repeat.hpp
Rule DSL lexy::dsl::repeat
lexy/dsl/repeat.hppnamespace lexy::dsl
{
struct repeat-dsl // note: not a rule itself
{
constexpr rule auto operator()(rule auto item);
constexpr rule auto operator()(rule auto item, separator auto sep);
constexpr rule auto capture(rule auto item);
constexpr rule auto capture(rule auto item, separator auto sep);
constexpr rule auto list(rule auto item);
constexpr rule auto list(rule auto item, separator auto sep);
};
constexpr repeat-dsl repeat(rule auto count);
}repeat is not a rule, but a DSL for parsing something N times in sequence with optional separator in between,
where N is determined as the result of parsing a count rule.
The actual rule is obtained by calling operator(), capture(), or list() on the result of repeat().
All three have the same parsing behavior and differ only in the values they produce.
The resulting rule is a branch rule, if count is a branch rule.
- Requires
countmust produce a value convertible tostd::size_twhen parsed with the parse actionlexy::parse.If
operator()orcapture()is used,itemandsepmust not produce any values.
- Parsing
Parses
countas if the parse actionlexy::parsewas used; the result is astd::size_tn. Then parsesitemntimes in sequence. Ifsephas been specified, parses it in between. After the last rule, handles a trailing separator as necessary.- Branch parsing
Same as above, but branch parses
count. Ifcountbacktracks, backtracks as well. Otherwise, does not backtrack anymore.- Errors
All errors raised by parsing
count. The rule then fails.All errors raised by parsing
itemorsepin any iteration. 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
If
operator()is used, does not produce any values.If
capture()is used, produces a single value: alexy::lexemespanning everything consumed by parsingruleandsepntimes. This is like the behavior oflexy::dsl::captureexcept thatcountis not captured.If
list()is used, creates a sink of the current context. All values produced byitemandsepare forwarded to it; there are separate calls for every iteration and foritemandsep. The value of the finished sink is produced as the only value. This is like the behavior oflexy::dsl::list.
Note | Use lexy::dsl::times if the number of repetitions is hard-coded into the grammar. |