Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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!

like image 534
djfm Avatar asked Oct 01 '12 17:10

djfm


1 Answers

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.

like image 189
djfm Avatar answered Oct 18 '22 14:10

djfm