pegged.examples.pattern

This module is an attempt at introducing pattern-matching in D.

Pattern matching is a type- or value-matching present in functional languages like ML or Haskell.

The goal here is to obtain code like:

Pattern!"[ a, b, ... ]" p1; // match any range with at least two elements
                            // the rest being discarded.

auto m1 = p1([0,1,2,3,4]); // match and associate 'a' and 'b' with O and 1.
assert(m1.a == 0 && m1.b == 1);
auto m2 = p1("abc");
assert(m2.a = 'a' && m2.b == 'b');
auto m3 = p1(tuple("abc",0)); // not a range, the pattern fails
assert(m3 == matchFailure; // predefined constant

Pattern!"Tuple!(.,.) t" p2; // match any std.typecons.Tuple with two elements, of any type
auto m4 = p2(tuple("abc",0));
assert(m4.t = tuple("abc",0); // only the global pattern is named, and hence captured
assert(p2(tuple("abc")) == matchFailure); // only one member -> failure
assert(p2(tuple("abc",0,1)) == matchFailure); // three members -> failure (the pattern has no trailing ...

Pattern!" . { int, double, double }" p3; // match any aggregate (struct, class)
                                         // with three members being int,double and double, in that order.

Don't salivate, it's not implemented yet.

Members

Functions

anyValue
auto anyValue(Args args)
Undocumented in source. Be warned that the author may not have intended to support it.
endValue
auto endValue(Args args)
Undocumented in source. Be warned that the author may not have intended to support it.
failure
auto failure(R rest)
Undocumented in source. Be warned that the author may not have intended to support it.
rangeAny
Tuple!(ElementType!R, R) rangeAny(R r)
Undocumented in source. Be warned that the author may not have intended to support it.
rangeRest
Tuple!(R, R) rangeRest(R r)
Undocumented in source. Be warned that the author may not have intended to support it.
success
Success!(M) success(M match, size_t begin, size_t end)
Undocumented in source. Be warned that the author may not have intended to support it.

Structs

MatchResult
struct MatchResult(size_t junction, T...)
Undocumented in source.
Success
struct Success(M...)
Undocumented in source.
TypeAnd
struct TypeAnd(alias Pattern1, alias Pattern2)
Undocumented in source.
TypeAny
struct TypeAny
Undocumented in source.
TypeDiscardMatch
struct TypeDiscardMatch(alias Pattern)

Discards the matched types, but propagate the indices.

TypeEnd
struct TypeEnd
Undocumented in source.
TypeEps
struct TypeEps
Undocumented in source.
TypeLiteral
struct TypeLiteral(U...)
Undocumented in source.
TypeNegLookAhead
struct TypeNegLookAhead(alias Pattern)
Undocumented in source.
TypeOneOrMore
struct TypeOneOrMore(alias Pattern)
Undocumented in source.
TypeOption
struct TypeOption(alias Pattern)
Undocumented in source.
TypeOr
struct TypeOr(alias Pattern1, alias Pattern2)
Undocumented in source.
TypePosLookAhead
struct TypePosLookAhead(alias Pattern)
Undocumented in source.
TypeSatisfy
struct TypeSatisfy(alias test)
Undocumented in source.
TypeSubType
struct TypeSubType(U...)
Undocumented in source.
TypeZeroOrMore
struct TypeZeroOrMore(alias Pattern)
Undocumented in source.
isRange
struct isRange
Undocumented in source.

Templates

RangePatternResult
template RangePatternResult(R)
Undocumented in source.
TypeAnd
template TypeAnd(Patterns...)
Undocumented in source.
TypeAnd
template TypeAnd(Patterns...)
Undocumented in source.
TypeFailure
template TypeFailure(T...)
Undocumented in source.
TypeOr
template TypeOr(Patterns...)
Undocumented in source.
TypeOr
template TypeOr(Patterns...)
Undocumented in source.
isSubtype
template isSubtype(T...)
Undocumented in source.
onRange
template onRange(RangePatterns...)
Undocumented in source.

Meta