Optional

class for matching something optional

Constructors

this
this(Parser!(T) parser)
Undocumented in source.

Members

Functions

parse
ParseResult!(T) parse(T[] s)
Undocumented in source. Be warned that the author may not have intended to support it.

Variables

fParser
Parser!(T) fParser;
Undocumented in source.

Examples

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

auto abc = match("abc");
auto opt = -abc;
auto res = opt.parse("abc");
res.success.should == true;
res.results.length.should == 1;
res.results[0].should == "abc";
res.rest.length.should == 0;

unittest to show optional in action.

auto abc = match("abc");
auto opt = -abc;
auto res = opt.parse("efg");
auto withoutOptional = abc.parse("efg");
withoutOptional.success.should == false;
res.success.should == true;
res.results.length.should == 0;
res.rest.should == "efg";

parse a number with or without sign

auto sign = match("+");
auto value = match("1");
auto test = (-sign) ~ value;
auto resWithSign = test.parse("+1");
resWithSign.success.should == true;
resWithSign.results.length.should == 2;
auto resWithoutSign = test.parse("1");
resWithoutSign.success.should == true;

Meta