Reputation: 49448
I'm very new to boost::spirit/fusion. Could someone please explain to me why the following is not compiling? It's compiling and working fine if I put the m_name
and m_settings
variables directly in the config struct, but fails to compile when I separate them into two different structs. What am I missing?
Btw, the code line that makes the compiler spew the gazillion 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;
}
Thanks!
Upvotes: 0
Views: 806
Reputation: 1521
The problem seems to be that you don’t specify how to convert from a config_section to a config. You told spirit that a config_section is composed of 2 strings but try to parse a config without a rule to link the conversion between a config and a config_section. This seems to compile.
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;
};
Upvotes: 1
Reputation: 1102
The problem is that the attribute for your cfg
rule and for config_grammar
grammar is not config
, but (as far as I understand -- because it is two consequent strings) config_section
. After this replacements:
struct config_grammar : qi::grammar<Iterator, config_section(), ascii::space_type>
....
qi::rule<Iterator, config_section(), ascii::space_type> cfg;
It compiles.
I'd suggest to you to learn more about rules' attributes and how they work -- otherwise you'll get lost all the time.
Upvotes: 0