spaceAnd

The engine formerly behind the '< ' Pegged rule: all sequence subelements of a rule are interspersed with a space-consuming rule, given as the first template parameter. It's not used by Pegged anymore but can be useful for low-level code. It might become deprecated, but it's not there yet.

1 alias and!(literal!"abc", literal!"def") rule1; // "abc" "def", equivalent to "abcdef"
2 alias spaceAnd!(oneOrMore!blank, literal!"abc", literal!"def") rule2; // "abc" "def", but with spaces in-between.
3 
4 string input1 = "abcdef";
5 string input2 = "  abc
6 
7 def  "; // with spaces and end of line markers.
8 
9 assert(rule1(input1).successful); // OK
10 assert(!rule1(input2).successful); // NOK, couldn't find "def" after "abc"
11 
12 assert(rule2(input1).successful); // OK
13 assert(rule2(input2).successful); // Still OK
14 assert(rule2(input2).matches == ["abc","def"]);// rule2 finds the literals among the spaces

As you can see on the previous line, spaceAnd discards the matched spaces and returns matches only for the 'real' subrules.

Note: by using a non-space rule as the first template argument, you can use spaceAnd as a generic 'find these patterns, possibly separated by this pattern' rule.

For example, using digits as separators:

1 alias spaceAnd!(digit, literal!"abc", litera!"def") rule3;
2 
3 ParseTree result = rule3("123abc45def67890");
4 assert(rule3.successful);
5 assert(rule3.matches == ["abc", "def"]);
6 assert(rule3.children.length == 2);
7 
8 assert(rule3.begin == 0;)
9 assert(rule3.end == "123abc45def67890".length);
template spaceAnd (
alias sp
rules...
) {}

Meta