boost::proto pattern matching and transforms

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: a) the entire pattern being matched (e.g. terminal<> or number<>) 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. 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. This is blocking me at the moment, so if anybody has suggestions, they would be very much appreciated. Best regards, Maurizio

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

Eric Niebler <eric@boost-consulting.com> writes:
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"?
if you see a transform as pattern -> transformed tree, then pattern would be the lhs and the transformed tree would be the 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 ...; } };
Oh. Yes, makes sense. Now I got it. What I was trying was to use the default transform proto::always<pattern, replacement> hoping that somehow I would have gotten mpl::placeholders bound inside replacement.
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.
Would have been nice, though...
This is blocking me at the moment, so if anybody has suggestions, they would be very much appreciated.
HTH,
Hugely. Obviously I'll find problems as soon as I start implementing it, but I think now I've got it. Thanks again, Maurizio
participants (2)
-
Eric Niebler
-
Maurizio Vitale