Optional

class for matching something optional

class Optional : Parser!(T)(
T
) {
Parser!(T) fParser;
}

Examples

unittests to show the usage of OptionalParser and its dsl '-'

1 import unit_threaded;
2 
3 auto abc = match("abc");
4 auto opt = -abc;
5 auto res = opt.parse("abc");
6 res.success.shouldBeTrue;
7 res.results.length.shouldEqual(1);
8 res.results[0].shouldEqual("abc");
9 res.rest.length.shouldEqual(0);

unittest to show optional in action.

1 import unit_threaded;
2 
3 auto abc = match("abc");
4 auto opt = -abc;
5 auto res = opt.parse("efg");
6 auto withoutOptional = abc.parse("efg");
7 withoutOptional.success.shouldBeFalse;
8 res.success.shouldBeTrue;
9 res.results.length.shouldEqual(0);
10 res.rest.shouldEqual("efg");

parse a number with or without sign

1 import unit_threaded;
2 
3 auto sign = match("+");
4 auto value = match("1");
5 auto test = (-sign) ~ value;
6 auto resWithSign = test.parse("+1");
7 resWithSign.success.shouldBeTrue;
8 resWithSign.results.length.shouldEqual(2);
9 auto resWithoutSign = test.parse("1");
10 resWithoutSign.success.shouldBeTrue;

Meta