I am using boost::spirit for constructing a parser for a simple script language. In a rule, I want to use a call to a fabric method to create a new object which is the return value of this rule.
The rule is
qi::rule<Iterator, NodeHandle(), Skipper> value;
and defined as
value = ( '%' >> +(qi::alpha) >> '%')
[phoenix::bind(&CreateVarNode, qi::_val, qi::_1)];
Actually, these “NodeHandles” are constructed via these kind of methods
NodeHandle CreateVarNode(std::wstring var)
{
Node::ptr node(new VarTerminal(var));
return NodeHandle(node);
}
The problem is, that spirit does not like these kind of constructs for rules. Can you help me on how to implement a rule which uses this fabric method to return this object?
Thank you!
Cheers,
Chris
Assuming the factory functions are actually namespace-level functions:
This should work nicely:
This should work unaltered if it was actually a static member of a class named
Factories. You could also implement a bit of convenience and write it like:This requires a bit of plumbing to use Phoenix functions. Full sample: