我对boost :: spirit / fusion很新。有人可以向我解释为什么以下不编译?如果我将m_name
和m_settings
变量直接放在config结构中,它正在编译和工作正常,但是当我将它们分成两个不同的结构时无法编译。我错过了什么?
顺便说一下,使编译器喷出大量boost::spirit errors is: cfg = section >> node;
#include <boost/config/warning_disable.hpp>
#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_fusion.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <iostream>
#include <string>
#include <vector>
namespace qi = boost::spirit::qi;
namespace fusion = boost::fusion;
namespace phoenix = boost::phoenix;
namespace ascii = boost::spirit::ascii;
struct config_section
{
std::string m_name;
std::string m_settings;
};
struct config
{
config_section m_sections;
};
BOOST_FUSION_ADAPT_STRUCT(
config_section,
(std::string, m_name)
(std::string, m_settings)
)
BOOST_FUSION_ADAPT_STRUCT(
config,
(config_section, m_sections)
)
template <typename Iterator>
struct config_grammar : qi::grammar<Iterator, config(), ascii::space_type>
{
config_grammar() : config_grammar::base_type(cfg)
{
using qi::lexeme;
using qi::lit;
using ascii::string;
using ascii::char_;
using namespace qi::labels;
section %= '[' >> lexeme[+(char_ - ']')] >> ']';
node %= !lit('[') >> lexeme[+(char_ - '\n')];
cfg %= section >> node;
}
qi::rule<Iterator, config(), ascii::space_type> cfg;
qi::rule<Iterator, std::string(), ascii::space_type> node;
qi::rule<Iterator, std::string(), ascii::space_type> section;
};
template <typename Iterator>
bool parse_config(Iterator first, Iterator last)
{
using qi::double_;
using qi::phrase_parse;
using ascii::space;
using boost::phoenix::ref;
config result;
config_grammar<Iterator> config_parser;
bool r = phrase_parse(first, last, config_parser, space, result);
if (first != last) // fail if we did not get a full match
return false;
return r;
}
int main()
{
std::string input = "[section]\nstuff";
bool b = parse_config(input.begin(), input.end());
if (b)
std::cout << "Success" << std::endl;
else
std::cout << "Failure" << std::endl;
return 0;
}
谢谢!
答案 0 :(得分:1)
问题似乎是您没有指定如何从config_section转换为配置。你告诉精神,config_section由2个字符串组成,但尝试解析配置而没有规则来链接config和config_section之间的转换。这似乎是编译。
template <typename Iterator>
struct config_grammar : qi::grammar<Iterator, config(), ascii::space_type>
{
config_grammar() : config_grammar::base_type(cfg)
{
using qi::lexeme;
using qi::lit;
using ascii::string;
using ascii::char_;
using namespace qi::labels;
section %= '[' >> lexeme[+(char_ - ']')] >> ']';
node %= !lit('[') >> lexeme[+(char_ - '\n')];
//create a rule to specify conversion of a config_section to a config
cfg %= cfg_sec;
//this a now a cfg_sec (which is what you declared to be composed of 2
// strings)
cfg_sec %= section >> node;
}
qi::rule<Iterator, config(), ascii::space_type> cfg;
//add new declaration here for cfg_sec
qi::rule<Iterator, config_section(), ascii::space_type> cfg_sec;
qi::rule<Iterator, std::string(), ascii::space_type> node;
qi::rule<Iterator, std::string(), ascii::space_type> section;
};
答案 1 :(得分:0)
问题是cfg
规则和config_grammar
语法的属性不是config
,而是(据我所知 - 因为它是两个结果字符串){ {1}}。在此替换之后:
config_section
它编译。</ p>
我建议你更多地了解规则的属性及其工作原理 - 否则你会一直迷路。