zeroOrMore

Tries to match subrule 'r' zero or more times. It always succeeds, since if 'r' fails from the very beginning, it matched 'r' zero times...

Its matches are those of its subrules (they might be different for each match) and its children are all the parse trees returned by the successive application of 'r'.

alias zeroOrMore!(or!(literal!"abc", literal!"d")) rule; // in PEG-speak:  '("abc" / "d")*'
ParseTree input = ParseTree("",false,[], "abcdabce");

ParseTree result = rule(input);

assert(result.successful);
assert(result.matches == ["abc", "d", "abc"]);
assert(result.end == 7); // matched "abcdabce"[0..7] => "abcdabc". "e" at the end is not part of the parse tree.
assert(result.children.length == 3);
writeln(result);
/+
writes:
zeroOrMore  [0, 7]["abc", "d", "abc"]
 +-or  [0, 3]["abc"]
 |  +-literal(abc)  [0, 3]["abc"]
 +-or  [3, 4]["d"]
 |  +-literal(d)  [3, 4]["d"]
 +-or  [4, 7]["abc"]
    +-literal(abc)  [4, 7]["abc"]
+/

So we know the first child used the 'literal!"abc"' sub-rule and matched input[0..3]. The second matched input[3..4] and the third input[4..7].

input = ParseTree("",false,[], "efgh");
result = rule(input);
assert(result.successful); // succeed, even though all patterns failed.
assert(result.children.length == 0);
  1. ParseTree zeroOrMore(ParseTree p)
    template zeroOrMore(alias r)
    zeroOrMore
  2. ParseTree zeroOrMore(string input)
  3. string zeroOrMore(GetName g)

Members

Functions

zeroOrMore
ParseTree zeroOrMore(ParseTree p)
Undocumented in source. Be warned that the author may not have intended to support it.
zeroOrMore
string zeroOrMore(GetName g)
Undocumented in source. Be warned that the author may not have intended to support it.
zeroOrMore
ParseTree zeroOrMore(string input)
Undocumented in source. Be warned that the author may not have intended to support it.

Manifest constants

name
enum name;
Undocumented in source.

Meta