Okay, on second glance I think you just missed the ability to fusion adapt std::pair:
#include <boost/fusion/adapted/std_pair.hpp>
// Or:
#include <boost/fusion/adapted.hpp>
Using this, the whole complexity vanishes and there is no need for anything involving the variant. Let's assume the following types:
typedef std::string column_name_t;
enum sql_faggregate
{
SUM,
// ....
AVG,
};
typedef std::pair<column_name_t, sql_faggregate> column_and_aggregate;
typedef std::vector<column_and_aggregate> column_and_aggregate_container;
A simple grammar would be:
template <typename It, typename Skipper = qi::space_type>
struct parser : qi::grammar<It, column_and_aggregate_container(), Skipper>
{
parser() : parser::base_type(aggregates_parser)
{
using namespace qi;
// using phx::bind; using phx::ref; using phx::val;
quoted_string = lexeme [ "'" >> *~qi::char_("'") >> "'" ];
faggr = int_;
agg_pair = quoted_string > ':' > faggr;
aggregates_parser = '{' > agg_pair % ',' > '}';
}
private:
qi::rule<It, std::string(), qi::space_type> quoted_string;
qi::rule<It, sql_faggregate(), qi::space_type> faggr;
qi::rule<It, column_and_aggregate(), qi::space_type> agg_pair;
qi::rule<It, column_and_aggregate_container(), qi::space_type> aggregates_parser;
};
You could add the input validation too:
faggr %= int_ [ qi::_pass = (qi::_1 >=SUM and qi::_1<=AVG) ];
Note the %=
to ensure attribute propagation.
Full Working Demonstration
Program code:
#include <boost/fusion/adapted/std_pair.hpp>
#include <boost/spirit/include/qi.hpp>
// #include <boost/spirit/include/phoenix.hpp>
namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;
typedef std::string column_name_t;
enum sql_faggregate
{
SUM,
// ....
AVG,
};
typedef std::pair<column_name_t, sql_faggregate> column_and_aggregate;
typedef std::vector<column_and_aggregate> column_and_aggregate_container;
template <typename It, typename Skipper = qi::space_type>
struct parser : qi::grammar<It, column_and_aggregate_container(), Skipper>
{
parser() : parser::base_type(aggregates_parser)
{
using namespace qi;
// using phx::bind; using phx::ref; using phx::val;
quoted_string = lexeme [ "'" >> *~qi::char_("'") >> "'" ];
faggr = int_;
agg_pair = quoted_string > ':' > faggr;
aggregates_parser = '{' > agg_pair % ',' > '}';
BOOST_SPIRIT_DEBUG_NODE(aggregates_parser);
}
private:
qi::rule<It, std::string(), qi::space_type> quoted_string;
qi::rule<It, sql_faggregate(), qi::space_type> faggr;
qi::rule<It, column_and_aggregate(), qi::space_type> agg_pair;
qi::rule<It, column_and_aggregate_container(), qi::space_type> aggregates_parser;
};
bool doParse(const std::string& input)
{
typedef std::string::const_iterator It;
auto f(begin(input)), l(end(input));
parser<It, qi::space_type> p;
column_and_aggregate_container data;
try
{
bool ok = qi::phrase_parse(f,l,p,qi::space,data);
if (ok)
{
std::cout << "parse success
";
for (auto& pair : data)
std::cout << "result: '" << pair.first << "' : " << (int) pair.second << "
";
}
else std::cerr << "parse failed: '" << std::string(f,l) << "'
";
if (f!=l) std::cerr << "trailing unparsed: '" << std::string(f,l) << "'
";
return ok;
} catch(const qi::expectation_failure<It>& e)
{
std::string frag(e.first, e.last);
std::cerr << e.what() << "'" << frag << "'
";
}
return false;
}
int main()
{
bool ok = doParse("{ 'column 1' : 1, 'column 2' : 0, 'column 3' : 1 }");
return ok? 0 : 255;
}
Print output:
parse success
result: 'column 1' : 1
result: 'column 2' : 0
result: 'column 3' : 1
PS: If you wanted to do the same in semantic actions, you'd probably want to write it like:
agg_pair =
quoted_string [ phx::bind(&column_and_aggregate::first, _val) = _1 ]
> ':'
> faggr [ phx::bind(&column_and_aggregate::second, _val) = _1 ];
You'll see that you can just drop it in the above sample and it works exactly the same. For this particular grammar, it's just more verbose, so I don't recommend it :)