Ranges
Thus far the rules we have examined have one thing in common; the values they produce are fixed in size and known at compile-time. However, grammars can specify the repetition of elements. For example consider the following grammar (loosely adapted from rfc7230):
chunk-ext = *( ";" token )
The star operator in BNF notation means a repetition. In this case,
zero or more of the expression in parenthesis. This production can
be expressed using the function range_rule, which returns a
rule allowing for a prescribed number of repetitions of a specified
rule. The following rule matches the grammar for chunk-ext
defined above:
// code_grammar_4_1
This rule produces a range, a ForwardRange whose value type is
the same as the value type of the rule passed to the function. In this
case, the type is string_view because the tuple has one unsquelched
element, the token_rule. The range can be iterated to produce results,
without allocating memory for each element. The following code:
// code_grammar_4_2
produces this output:
johndoe
janedoe
end
Sometimes a repetition is not so easily expressed using a single rule. Take for example the following grammar for a comma delimited list of tokens, which must contain at least one element:
token-list = token *( "," token )
We can express this using the overload of range_rule which accepts
two parameters: the rule to use when performing the first match, and
the rule to use for performing every subsequent match. Both overloads
of the function have additional, optional parameters for specifying
the minimum number of repetitions, or both the minimum and maximum
number of repetitions. Since our list may not be empty, the following
rule perfectly captures the token-list grammar:
// code_grammar_4_3
The following code:
// code_grammar_4_4
produces this output:
johndoe
janedoe
end
In the next section we discuss the available rules
which are specific to rfc3986.