Header lexy/dsl/repeat.hpp
Rule DSL lexy::dsl::repeat
lexy/dsl/repeat.hpp
namespace 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
count
must produce a value convertible tostd::size_t
when parsed with the parse actionlexy::parse
.If
operator()
orcapture()
is used,item
andsep
must not produce any values.
- Parsing
Parses
count
as if the parse actionlexy::parse
was used; the result is astd::size_t
n
. Then parsesitem
n
times in sequence. Ifsep
has 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
. Ifcount
backtracks, backtracks as well. Otherwise, does not backtrack anymore.- Errors
All errors raised by parsing
count
. The rule then fails.All errors raised by parsing
item
orsep
in 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::lexeme
spanning everything consumed by parsingrule
andsep
n
times. This is like the behavior oflexy::dsl::capture
except thatcount
is not captured.If
list()
is used, creates a sink of the current context. All values produced byitem
andsep
are forwarded to it; there are separate calls for every iteration and foritem
andsep
. 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. |