在解析过程中,我需要在语义操作中设置一些属性(因为它们是从被解析的数据派生的,我想避免global
变量和依赖BOOST_FUSION_ADAPT_STRUCT
以及我的代码应该是通用的,以便我可以将它用于多个类型)。如果我使用多个传入的变量,qi::phrase_parse
我会得到很长的编译错误列表。我非常需要帮助:-)
#define BOOST_RESULT_OF_USE_DECLTYPE
#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/function.hpp>
#include <boost/phoenix/fusion.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/qi.hpp>
#include <iostream>
#include <string>
#include <climits>
namespace qi = boost::spirit::qi;
namespace ph = boost::phoenix;
namespace ascii = boost::spirit::ascii;
int main( int argc, char**argv )
{
bool rc;
std::string input("");
//Test case 1 works fine
{
auto iter( input.begin() );
auto last( input.end() );
int val1=33;
rc = qi::phrase_parse( iter, last, qi::eps[ qi::_val=11 ] ,
ascii::space, val1 ) && iter==last;
if( rc )
std::cout << "val1=" << val1 << std::endl;
}
//Test case 2 does not compile
{
auto iter( input.begin() );
auto last( input.end() );
int val1=33;
int val2=0;
rc = qi::phrase_parse( iter, last,
qi::eps[ ph::at_c<0>(qi::_val)=1,ph::at_c<1>(qi::_val)=2 ],
ascii::space, val1,val2 ) && iter==last;
if( rc )
std::cout << "val1=" << val1 <<
" val2=" << val2 << std::endl;
}
//Test case 3 works fine
{
auto iter( input.begin() );
auto last( input.end() );
int val1=33;
int val2=0;
rc = qi::phrase_parse( iter, last,
qi::attr(1)>>qi::attr(2),
ascii::space, val1,val2 ) && iter==last;
if( rc )
std::cout << "val1=" << val1 <<
" val2=" << val2 << std::endl;
}
return 0;
}
我从中获取了“自定义” my_phrase_parse ,cv_and_he
但它破坏了我想要运行的完整测试用例的编译:
template<typename T,typename R>
void testParser( R rule )
{
for ( const auto input : std::vector< std::string >{ "5 1.0 2.0 3.0 4.0 5.0", "1 1.0", "0" , "", "2 3 ab" } )
{
bool rc;
T maxValue;
T minValue;
auto iter( input.begin() );
auto last( input.end() );
std::vector< T > v;
rc = my_phrase_parse( iter, last,
qi::eps[
ph::at_c<0>(qi::_val)=std::numeric_limits<T>::max(),
ph::at_c<1>(qi::_val)=std::numeric_limits<T>::min()
]
>> -( qi::omit[ qi::int_]
>> *rule[ ph::if_(ph::at_c<0>(qi::_val)>qi::_1)[ ph::at_c<0>(qi::_val)=qi::_1 ],
ph::if_(ph::at_c<1>(qi::_val)<qi::_1)[ ph::at_c<1>(qi::_val)=qi::_1 ]
] )
,ascii::space, minValue, maxValue,v ) && iter==last;
std::cout << ( rc ? "ok :`" : "err:`" ) << input << "` -> ";
if( rc )
{
std::cout << "min=" << minValue << " max=" << maxValue << "\t";
std::copy( v.begin(), v.end(), std::ostream_iterator<T>( std::cout," " ));
}
else
std::cout << *iter;
std::cout << std::endl;
}
}
...
testParser<double>( qi::double_ );