Boost logo

Boost Users :

Subject: Re: [Boost-users] qi::locals
From: Hartmut Kaiser (hartmut.kaiser_at_[hidden])
Date: 2010-05-27 11:41:44


> I would like to do some complex semantic operation, but I am not sure if
> it is possible to do with Spirit. At the moment it does'nt compile :-)
>
> I want a rule like this inside a template class with Iterator, and
> ActionHandler as template names:
>
> 1: qi::rule<
> 2: Iterator,
> 3: qi::locals<
> 4: std::vector<typename ActionHandler::AClass *>,
> 5: typename ActionHandler::BClass> *
> 6: >,
> 7: skipper
> 8: > rule1;
>
> later in the grammar I would like something like this:
>
> 9: rule1.name("rule1");
> 10: rule1 =
> 11: (
> 12: qi::lit("AClass") >
> 13: +(qi::int_ > qi::int_ > qi::char_)[
> 14: qi::_a.push_back(phoenix::construct<
> 15: typename ActionHandler::AClass>(qi::_1, qi::_2, qi::_3)
> 16: ] >
> 17: -(
> 18: qi::lit("BClass") >
> 19: +(qi::int_ > qi::char_)[qi::_b.addPair(qi::_1, qi::_2)]
> 20: )[phoenix::for_each(qi::_a.begin(), qi::_a.end(),
> 21: xxx::a_element.setBClass(qi::_b))]
> 22: )[phoenix::bind(&ActionHandler::addAClassVector,
> 23: phoenix::ref(handler), qi::_a)]
> 24: ;
>
> I guess I have some problem with:
> - The rule definition, here seems is expecting the rule's attribute type

Hmmm, I don't understand why you 'seem' to imply that.

> - Line 14:
> - qi::_a is an actor, does'nt have push_back method

Sure, it has none such member. Just use phoenix::push_back(_a, ...) instead.

> - construct returns a AClass pointer?

Use phoenix::new_<> instead of phoenix::construct<>.

> - Line 19, same as befor, qi::_b is an actor without addPair method

Sure, why do you expect the placeholders to expose your member functions?
Using phoenix::bind should do the trick.

> - Line 20:
> - is it possible to use for_each?, I guess phoenix can.

Yes.

> - begin/end qi::_a is an actors not a vector

It is possible. Again, placeholders don't know anything about your types.
Use phoenix::begin() and phoenix::end().

> Some idea to face it?, or may be is too complex and better do it in some
> other easier way?

Remember, all function calls inside a semantic action have to be lazy, i.e.
have to be function objects with their parameters pre-bound. Often it is
easier to write your own custom phoenix::functions encapsulating all the
functionality than to build overly complex phoenix expressions, though.

Regards Hartmut

---------------
Meet me at BoostCon
www.boostcon.com

PS: Generally, posting a minimal (non-)compiling code helps answering the
questions better.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net