Boost logo

Boost :

From: Eric Niebler (eric_at_[hidden])
Date: 2007-04-27 11:43:27


Maurizio Vitale wrote:
> I think I haven't really understood how proto handles transforms and pattern matching.
> I'm probably confused on how exactly result_of, arg and mpl placeholders play together
> and I kind of get what I want in some cases.
> For instance, given the pattern:
>
> proto::terminal< number<bounds<run_time,_>,_,_,_,_,_> >
>
> how can I refer in the rhs of a transform to:

What do you mean by "rhs"?

> a) the entire pattern being matched (e.g. terminal<> or number<>)

Transforms are always of the following form:

template<typename Grammar>
struct some_transform
   : Grammar
{
   template<typename Expr, typename State, typename Visitor>
   struct apply
   {
     typedef ... type;
   };

   template<typename Expr, typename State, typename Visitor>
   static typename apply<Expr, State, Visitor>::type
   call(Expr const &expr, State const &state, Visitor &visitor)
   {
     return ...;
   }
};

You might attach some_transform<> to your terminal rule like this:

   some_transform<
     proto::terminal< number<bounds<run_time,_>,_,_,_,_,_> >
>

This says: When you find an expression that matches my pattern,
transform it by applying some_transform. In some_transform<>, the type
Expr corresponds to the expression that matched your pattern. It will be
some number<> terminal.

> b) the bound<> subobject
> c) the expression corresponding to the placeholder inside the bound object
> d) the expression corresponding to one of the other 5 placeholders in the number<> subtree
>
> I'm particular unclear on how placeholders are bound to mpl placeholders: left to right,
> depth/breath first traversal of the parse tree, some other way.

There are no mpl placeholders here, and the placeholders are not
positional like that. You can't say, find me what matched the Nth
occurance of proto::_. You only get the whole expression that matched.

> The only examples I've found uses mpl::_ and has a single proto::_ universal matcher in the
> lhs so is not like I have any evidence of an intention to bind other mpl placeholders.
> Maybe I have to dissect the matched expression myself, but I need at least to get an handle
> to the entire expression.

Yes, you get the whole expression that matched passed to the call()
member of your transform.

> This is blocking me at the moment, so if anybody has suggestions, they would be very much
> appreciated.

HTH,

-- 
Eric Niebler
Boost Consulting
www.boost-consulting.com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk