Reputation: 1090
The documentation says that the attribute of two composited rules (a >> b) should be tuple if both A and B are used.
Assuming this I tried to read out the first attribute of such a tuple. But it fails:
(I try to store the parsed integer in 'i')
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
template <typename ForwardIterator> class TestGrammar
: public boost::spirit::qi::grammar<ForwardIterator, boost::spirit::ascii::space_type>
{
boost::spirit::qi::rule<ForwardIterator, boost::spirit::ascii::space_type> foo_;
public:
TestGrammar( void ) : TestGrammar::base_type( foo_ )
{
int i;
foo_ = ((boost::spirit::qi::int_ >> boost::spirit::qi::float_)
[boost::phoenix::ref(i) = boost::phoenix::at_c<0>(boost::spirit::_1)]);
}
};
int main( void )
{
TestGrammar<std::string::iterator> g;
return 0;
}
Writing:
foo_ = ((boost::spirit::qi::int_ >> boost::spirit::qi::float_)
[boost::phoenix::ref(i) = boost::spirit::_1]);
will always work though as long as A is an int.
Changing types back and forth and writing custom rules revealed that the attribute of (a >> b) will always be A regardless of what B is.
Even
Upvotes: 1
Views: 222
Reputation: 392989
I guess you wanted to do this:
int i;
float f;
foo_ = (boost::spirit::qi::int_ >> boost::spirit::qi::float_)
[ boost::phoenix::ref(i) = boost::spirit::_1,
boost::phoenix::ref(f) = boost::spirit::_2 ];
If you really wanted to use the sequence, try:
foo_ = qi::attr_cast<>(qi::int_ >> qi::float_)
[ boost::phoenix::ref(i) = phx::at_c<0>(qi::_1),
boost::phoenix::ref(f) = phx::at_c<1>(qi::_1) ]
Or with a helper rule:
helper = qi::int_ >> qi::float_;
foo_ = helper
[ boost::phoenix::ref(i) = phx::at_c<0>(qi::_1),
boost::phoenix::ref(f) = phx::at_c<1>(qi::_1) ]
;
All three versions compiling at http://liveworkspace.org/code/518f2bd03e1fed7ed734d62071a88eab
Upvotes: 2
Reputation: 4812
If you compile this:
struct TestDelegate
{
template <typename T, typename U, typename V>
void operator()(T const& t, U const& u, V const& v) const {
bar;
}
};
template <typename ForwardIterator> class TestGrammar
: public boost::spirit::qi::grammar<ForwardIterator, boost::spirit::ascii::space_type>
{
boost::spirit::qi::rule<ForwardIterator, boost::spirit::ascii::space_type> foo_;
public:
TestGrammar( void ) : TestGrammar::base_type( foo_ )
{
//int i;
TestDelegate test_delegate;
//foo_ = ((boost::spirit::qi::int_ >> boost::spirit::qi::float_)
// [boost::phoenix::ref(i) = boost::phoenix::at_c<0>(boost::spirit::_1)]);
foo_ = ((boost::spirit::qi::int_ >> boost::spirit::qi::float_)
[test_delegate]);
}
};
int main( void )
{
TestGrammar<std::string::iterator> g;
return 0;
}
You should see in the resulting error essay that the attribute type is a tuple:
1>e:\work\test\spiritprob\spiritprob\spiritprob.cpp(14) : error C2065: 'bar' : undeclared identifier
1> e:\boost\boost_1_44_0\boost_1_44_0\boost\spirit\home\support\action_dispatch.hpp(29) : see reference to function template instantiation 'void TestDelegate::operator ()<Attribute,Context,bool>(const T &,const U &,const V &) const' being compiled
1> with
1> [
1> Attribute=boost::fusion::vector2<int,float>,
1> Context=boost::spirit::context<boost::fusion::cons<boost::fusion::unused_type &,boost::fusion::nil>,boost::fusion::vector0<>>,
1> T=boost::fusion::vector2<int,float>,
1> U=boost::spirit::context<boost::fusion::cons<boost::fusion::unused_type &,boost::fusion::nil>,boost::fusion::vector0<>>,
1> V=bool
1> ]
(compiled with visual studio 2008)
Doesn't directly solve our problem, but at least you know it does come out as a fusion::vector<int,float>
Upvotes: 1