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 '-'

import unit_threaded;

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

unittest to show optional in action.

import unit_threaded;

auto abc = match("abc");
auto opt = -abc;
auto res = opt.parse("efg");
auto withoutOptional = abc.parse("efg");
withoutOptional.success.shouldBeFalse;
res.success.shouldBeTrue;
res.results.length.shouldEqual(0);
res.rest.shouldEqual("efg");

parse a number with or without sign

import unit_threaded;

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

Meta