我编写了这个代码示例并希望它能够打印OPERATION( OPERATOR(aaa) ID(bbb) )
,但我得到的只是OPERATION ( OPERATOR(aaa) )
相反。两者都是真实的result2
。it1 == it2
为什么不解析操作数?
#include "stdafx.h"
#include <boost/serialization/strong_typedef.hpp>
#include <boost/bind.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/phoenix1.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/support.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/variant/recursive_variant.hpp>
#include <iostream>
#include <string>
namespace NsSemSDK
{
struct STreeConstructionRuleRegexp {
std::string m_strEntity;
};
struct STreeConstructionRuleString {
std::string m_strEntity;
};
struct STreeConstructionRuleIdentifier {
std::string m_strEntity;
};
typedef int STreeConstructionRuleNumber;
typedef std::string STreeConstructionRuleOperation;
typedef boost::variant<STreeConstructionRuleRegexp, STreeConstructionRuleNumber, STreeConstructionRuleString, STreeConstructionRuleIdentifier> STreeConstructionRuleOperand;
typedef boost::tuple<STreeConstructionRuleOperation, std::vector<STreeConstructionRuleOperand> > STreeConstructionRuleOperationWithOperands;
std::ostream& operator<<(std::ostream& stream, const NsSemSDK::STreeConstructionRuleIdentifier& val);
std::ostream& operator<<(std::ostream& stream, const NsSemSDK::STreeConstructionRuleString& val);
std::ostream& operator<<(std::ostream& stream, const NsSemSDK::STreeConstructionRuleRegexp& val);
std::ostream& operator<<(std::ostream& stream, const NsSemSDK::STreeConstructionRuleOperationWithOperands& val);
std::ostream& operator<<(std::ostream& stream, const NsSemSDK::STreeConstructionRuleOperationWithOperands& val)
{
stream << "OPERATION( " << "OPERATOR(" << val.get<0>() << ")";
for (int i = 0; i < val.get<1>().size(); i++)
{
stream << " " << val.get<1>()[i];
}
stream << " )";
return stream;
}
std::ostream& operator<<(std::ostream& stream, const NsSemSDK::STreeConstructionRuleRegexp& val)
{
return stream << "REGEXP(" << val.m_strEntity << ")";
}
std::ostream& operator<<(std::ostream& stream, const NsSemSDK::STreeConstructionRuleString& val)
{
return stream << "STR(" << val.m_strEntity << ")";
}
std::ostream& operator<<(std::ostream& stream, const NsSemSDK::STreeConstructionRuleIdentifier& val)
{
return stream << "ID(" << val.m_strEntity << ")";
}
}
BOOST_FUSION_ADAPT_STRUCT(
NsSemSDK::STreeConstructionRuleRegexp,
(std::string, m_strEntity)
)
BOOST_FUSION_ADAPT_STRUCT(
NsSemSDK::STreeConstructionRuleString,
(std::string, m_strEntity)
)
BOOST_FUSION_ADAPT_STRUCT(
NsSemSDK::STreeConstructionRuleIdentifier,
(std::string, m_strEntity)
)
namespace NsSemSDK{
namespace qi = boost::spirit::qi;
namespace phoenix = boost::phoenix;
template <typename Iterator, typename Skipper>
struct STreeContructionRulesGrammar : qi::grammar<Iterator, STreeConstructionRuleOperationWithOperands(), Skipper> // std::vector<STreeConstructionRule>()
{
qi::rule<Iterator, STreeConstructionRuleOperationWithOperands(), Skipper> m_oOperationWithOperands;
qi::rule<Iterator, STreeConstructionRuleOperation(), Skipper> m_oOperation;
qi::rule<Iterator, std::vector<STreeConstructionRuleOperand>(), Skipper> m_oOperandsList;
qi::rule<Iterator, STreeConstructionRuleOperand(), Skipper> m_oOperand;
qi::rule<Iterator, STreeConstructionRuleString(), Skipper> m_oString;
qi::rule<Iterator, STreeConstructionRuleRegexp(), Skipper> m_oRegexp;
qi::rule<Iterator, STreeConstructionRuleNumber(), Skipper> m_oNumber;
qi::rule<Iterator, STreeConstructionRuleIdentifier(), Skipper> m_oIdentifier;
STreeContructionRulesGrammar() : STreeContructionRulesGrammar::base_type(m_oOperationWithOperands)
{
m_oOperationWithOperands %= m_oOperation > m_oOperandsList;
m_oOperation %= qi::lexeme[+(qi::alnum)];
m_oOperandsList %= qi::lit("[") >> (m_oOperand % ',') >> qi::lit("]");
m_oOperand %= m_oString | m_oRegexp | m_oNumber | m_oIdentifier;
m_oString %= qi::lit("\"") >> qi::lexeme[*(qi::char_ - '"')] >> qi::lit("\"");
m_oRegexp %= qi::lit("'") >> qi::lexeme[*(qi::char_ - '\'')] >> qi::lit("'");
m_oNumber %= qi::int_;
m_oIdentifier %= +(qi::alpha) >> qi::eps;
}
};
}
using namespace NsSemSDK;
int _tmain(int argc, _TCHAR* argv[])
{
std::string str("aaa [bbb]");
STreeContructionRulesGrammar<std::string::iterator, boost::spirit::ascii::space_type> grammar;
STreeConstructionRuleOperationWithOperands result;
std::string::iterator it1 = str.begin(), it2 = str.end();
bool result2 = qi::phrase_parse(it1, it2, grammar, boost::spirit::ascii::space, result);
std::cout << result << std::endl;
return 0;
}