dranges.typepattern

This module implements a basic pattern matching engine for type tuples. Think regexes, but for types: "one or more int, followed by a string or a double, and then anything but a float".

Pattern expressions are built by aliasing the basic building blocks provided here and tested with the Match!(Pattern, Tuple...) template. A match result is a struct exposing three fields:

  • hasMatched: a boolean that tells us whether or not the typetuple matched the pattern
  • Result: a typetuple holding the match result. If hasMatched is false, it will be empty.
  • Post: the rest of the input typetuple, the part that was not consumed by the pattern.


Usage:
// Let's define some basic patterns first:
alias OneOrMore!(int) Ints;
alias Either!(int, double) IOrD;
alias _ Any; // Predefined '_' pattern, matching any type.
alias And!(_,_) Two; // Matches any two types in succession. Will fail if passed 0 or 1 type.

// A more complicated example:
alias Successively!(
                    int,
                    None!(int,double,string),
                    _,
                    Repeat!(int, 2, 4),
                    Not!int,
                    End) Pattern;

// To match, use Match!(Pattern, TypeTuple):
alias Match!(Ints, int, int, int, double)    Res1;
alias Match!(IOrD, double, int, int, double) Res2;
alias Match!(Any,  double, int, int, double) Res3;
alias Match!(Two,  double, int, int, double) Res4;
alias Match!(Pattern, int, float, string, int,int,int, string) Res5;

// All is done at compile-time, of course.
pragma(msg, Res1.Result.stringof, " // ", Res1.Post.stringof);
pragma(msg, Res2.Result.stringof, " // ", Res2.Post.stringof);
pragma(msg, Res3.Result.stringof, " // ", Res3.Post.stringof);
pragma(msg, Res4.Result.stringof, " // ", Res4.Post.stringof);
pragma(msg, Res5.Result.stringof, " // ", Res5.Post.stringof);


Features:
  • Any type is its own pattern. 'int' matches an int and so on.
  • Patterns can be tested in succession (And) or in alternation (Or). Predefined compositions like Successively and Either are there to help you.
  • .
  • You can Repeat patterns min times, up to max times. There are predefined ZeroOrMore, OneOrMore, Option (aka ZeroOrOne) for your convenience.
  • Patterns can (and will) backtrack to find a match.
  • All patterns are tested only from the beginning of the tuple. In regex terms, it's as if they all begin with '^'. Use UnAnchored!Pattern to make a pattern consume input until it finds something.
  • You can easily extend the module by defining your own patterns: just define a Match(T...) template inside a struct that alias itself to a correct match result.


TODO:
  • Maybe extend that to all template parameters, and not only types.
  • No capture is implemented yet. That would mean passing an entire state around, to remember previous captures to give them to a BackReference!(ref number) template. It's doable, but a bit to much for me right now.
  • No named captures either...


template Match (Pattern,Types...)
The match engine, driving the entire pattern evaluation.

template AllMatches (Pattern)
Returns a tuple of all matches of a pattern in an input tuple. To distinguish them from one another, the matches are wrapped in Tuples.

Usage:
alias AllMatches!(Pattern).In!(InputTuple) AM;


Example:
// finds all couples where a type is followed by an int.
alias AllMatches!(And!(_,int)).In!( int,double,int,string,int*,int,int ) Ints;
assert(is(AM == TypeTuple!(
                           Tuple!(double,int),
                           Tuple!(int*,int),
                           Tuple!(int,int)
                          )));


template Replace (Pattern,WithThis)
Replaces the first instance of Pattern with WithThis in an input typetuple.

Example:
alias Replace!(And!(_,int), float).In!(double,int, string,string,int) Rep;
assert(is( Rep == TypeTuple!(float, string, string, int)));


template ReplaceAll (Pattern,WithThis)
Replaces every instance of Pattern with WithThis in an input typetuple.

Example:
alias ReplaceAll!(And!(_,int), float).In!(double,int, string,string,int) Rep;
assert(is( Rep == TypeTuple!(float, string, float)));


template Transform (Pattern,alias WithThis)
A cousin of Replace: finds the first match of Pattern in an input typetuple and replaces it with WithThis!(Match).

Example:
// transforms the first integral type with its unsigned equivalent
alias Transform!(If!(isIntegral), Unsigned).In!( int,double,float,byte,ulong,string ) T;
assert(is( T == TypeTuple!(uint,double,float,byte,ulong,string)));


template TransformAll (Pattern,alias WithThis)
A cousin of ReplaceAll: finds all successive matches of Pattern in an input typetuple and replaces them with (M WithThis!(Match)).

Example:
// transforms all integral types with their unsigned equivalent
alias TransformAll!(If!(isIntegral), Unsigned).In!( int,double,float,byte,ulong,string ) T;
assert(is( T == TypeTuple!(uint,double,float,ubyte,ulong,string)));


template match (Pattern,alias action,Rest...)
Another take on pattern-matching. It takes couples of a pattern and a function (maybe a templated function) as template parameters. When given a value as input, it will test the value type against all patterns in succession. When a pattern matches, it will return the associated function's return.

Example:
alias match!(
             OneOrMore!int,     fun1,
             And!(double, int), fun2,
             _,                 fun3,
             ) matcher;


See Also:
dranges.patternmatch, dranges.functional.eitherFun.

struct _ ;
Matches any type. Think '.' for regexes. Does not match the empty typetuple.

struct End ;
Matches the end of a tuple (aka, the empty typetuple). Think '$' for regexes.

struct Or (Pattern1,Pattern2);
It matches if Pattern1 or Pattern2 matches. More precisely, it tests Pattern1. If it matches, Or returns the match result. If it fails, Or will try the second pattern and return its match result (which may be a failure). If it tested only the first pattern, it can backtrack when asked to, by testing the second pattern.

struct And (Pattern1,Pattern2);
Will match if and only if Pattern1 matches and Pattern2 matches after Pattern1. It's the basic building block for all type patterns, since it threads them in succession. When you need to test P1 and then P2 and then P3, it's And you need. If the second pattern fails, it will ask the first pattern to backtrack (if possible) and test the second pattern again.

See Also:
Successively.

template Successively (Patterns...) if (Patterns.length > 0)
Convenience template, equivalent to And!(P1, And!(P2, And!(P3, ...))). It will try to match all the Pi: if one fails, the entire template fails. It's composed of Ands, so it will backtrack internally as much as necessary to match all patterns.

template Either (Patterns...) if (Patterns.length > 0)
Convenience template, equivalent to Or!(P1, Or!(P2, Or!(...))). It will try P1, and stop there if it matches. Else, it will test P2 and so on. If forced to backtrack, it will continue with the yet untested patterns.

template Repeat (Pattern,int min,int max) if (min >= 0 && max >= 0 && min <= max)
Greedily matches Pattern at least min times up to (included) max times. If forced to backtrack, it will try one match less.

template LazyRepeat (Pattern,int min,int max) if (min <= max)
Repeat Pattern min times. If forced to backtrack, it will try one more match, up to max.

template ZeroOrMore (Pattern)
Tests for 0 or more Pattern. I stopped at 128 max due to template instantiation limits.

template OneOrMore (Pattern)
Tests for 1 or more Pattern. I stopped at 128 max due to template instantiation limits.

template Option (Pattern)
0 or 1 Pattern

struct Not (Pattern);
Invert the pattern.

template None (Patterns...)
Matches only iff none of the Patterns match.

struct isA (alias templ,Patterns...);
isA is a very powerful pattern: it matches if the input type is an instance of templ (a templated type) with the inner types matching Patterns:
alias isA!(Tuple, _, _) Tuple2; // matches any Tuple!(_,_): a Tuple with two types
alias isA!(Tuple, OneOrMore!_) Tuple1; // matches any Tuple with one or more type inside.
                                       // So, it matchesTuple!(int, string, double) but not Tuple!()
alias isA!(Tuple, int, ZeroOrMore!_) Tuple3; // matches Tuple!(int, double) or Tuple!(int, double, string) or even Tuple!(int)


struct Inside (alias templ,Patterns...);
Inside is like isA, but the match result is the inner types, *not* the global type:
alias isA!(Tuple, _, _)    Tuple2;  // matches Tuple!(int, string), produces Tuple!(int, string)
alias Inside!(Tuple, _, _) Inside2; // matches Tuple!(int, string), but produces (int, string)


struct If (alias condition);
Matches if T[0] verifies the condition (ie if condition!(T[0]) is statically true)
alias If!(isIntegral) Integer; // will match int, byte, short, long, etc.


struct If (alias condition,Then,Else);
An extended version of the previous If . If T[0] verifies the condition (ie if condition!(T[0]) is statically true), it'll use the Then pattern on T. Else, it'll use the Else pattern.

struct If (alias condition,alias Then,Else);
Variation on the same theme: Then is here a template that will be applied on the If match result. That is, if condition!(T[0]), returns Then!(T[0]), else return Else.

struct If (alias condition,Then,alias Else);
The same, with Else being an alias.

struct If (alias condition,alias Then,alias Else);
The same, with Then and Else being aliases.

struct ReleaseMatch (Pattern);
Transforms a pattern such that, if it matches, it puts back its match in the input tuple, for the next pattern to consume.

See Also:
LookAhead.

struct DiscardMatch (Pattern);
Transforms a pattern such that, if it matches, it discards its match: the match result is the empty typetuple.

See Also:
Left, Right, LeftMost, RightMost.

template Left (Pattern1,Pattern2)
Matches iff Pattern1 and then Pattern2 match, but returns only Pattern1's match result.

template Right (Pattern1,Pattern2)
Matches iff Pattern1 and then Pattern2 match, but returns only Pattern2's match result.

template LeftMost (Patterns...)
Matches iff all Patterns match, but returns only the first (leftmost) pattern match result.

template RightMost (Patterns...)
Matches iff all Patterns match, but returns only the last (rightmost) pattern match result.

template LookAhead (Patterns...)
Matches iff all Patterns match, returns the leftmost pattern match result, without consuming the rest.

template Simultaneously (Patterns...)
Tests the first pattern. If it matches, it releases its match and then tests the second pattern in the same manner. All patterns will be tested in this way, getting them to match on the same input (more or less). Only if all of them match simultaneously will Simultaneously returns a match: the last pattern's match. Not to be confused with And or Successively that test for matches while consuming the first patterns matches.
// S matches if there are two types in a row and the first one is an int. Returns 'int'.
alias Simultaneously!(And!(_,_), int) S;


Note:
I'm not sure this is interesting, as there is frequently a way to express the same pattern in another way. I'm just playing along with ReleaseMatch, really.

struct UnAnchored (Pattern);
Transforms a pattern so that it'll consume input until it finds a match. Only if it reaches the end of the typetuple without matching does it return NoMatch.

Page was generated with on Fri Nov 12 11:55:12 2010