Download DSL in C++ Template Metaprogram

Transcript
of the ’]’ easily. Let’s see what the first phase of parsing does. With foldl1 we
parse with one of the range exp and set item parsers. We’ll specify range exp
later on, so let’s just concentrate on set item now, moreover we can just use
letter instead of that.
We start building up the set with the empty set metafunction class. On the
backward way this will construct our final object we return, but in this stage
this just has a metafunction which can be called with an after parameter.
struct e m p t y s e t
{
typedef e m p t y s e t type ;
template <c l a s s T>
s t a t i c x p r e s s i v e : : s r e g e x run (T a f t e r )
{
return x p r e s s i v e : : s e t [ a f t e r ] ;
}
};
The build set metafunction class uses the add set item interface of the
actual Next element and passes the current state (State) as its template parameter. The run method of add set item is called with the after parameter.
It’s again just a metafunction, like empty set, which has to be evaluated with
an after parameter. It’s like we’re unrolling these generated metafunctions as
a ”wick” after us and when we reach the last element, we’ll light it, that is, we
work it up backward.
struct b u i l d s e t
{
template <c l a s s Next , c l a s s S t a t e >
struct apply
{
typedef apply type ;
template <c l a s s T>
s t a t i c x p r e s s i v e : : s r e g e x run (T a f t e r )
{
return Next : : template a d d s e t i t e m < S t a t e >:: type : :
run ( a f t e r ) ;
}
};
};
The ”lighter” for this string of metafunctions, to close this metaphor, is the
start building set metafunction class. The transform parser around foldl1
calls it with the result of our folding, hence we can open it up. We start the
second phase of the parsing with the empty range (e.g. range(4,2)). This is