djfm
djfm

Reputation: 2468

How to build a synthesized argument from a C++11 lambda semantic action in boost spirit?

I'm trying to build an AST with boost spirit and I've been struggling with how to build synthesized arguments when using C++11 lambda expressions as semantic actions.

Say I have a rule:

qi::rule<char*,ascii::space_type,SomeStruct()> rule = some_parser[[](some_type val){/*code to build SomeStruct from val*/}];

How does my lambda return the synthesized argument (SomeStruct)? By the return value? Because qi::_val is not available in this context right? (this is a bit obscure to me, sorry if this question is not well formulated)

Thanks in advance for any pointer in the right direction!

Upvotes: 7

Views: 930

Answers (1)

djfm
djfm

Reputation: 2468

This seems to do the trick: http://ereethahksors.blogspot.fr/2012/05/using-c11-lambdas-with-boostspiritqi.html

Relevant quote:

typedef rule<Iterator, Label*(), space_type> label_rule_type;  
 label = lit(':') > symbol[[&](string& name, typename label_rule_type::context_type& context)   
          {   
           boost::fusion::at_c<0>(context.attributes) = _ast->addLabel(name);  
          }];  

The most important part here is the typedef and its context_type. If you just want to use C++11 lambdas to do very simple things with your passed attribute things are very easy, but if you want access to the locals or the qi::_val, you'll have to use the context parameter. context is a very templated instance of boost::spirit::context that gives you access to two boost::fusion sequences; attributes and locals.

Upvotes: 7

Related Questions