Skip to content

Instantly share code, notes, and snippets.

@vasi
Created July 7, 2012 11:00
Show Gist options
  • Save vasi/3065861 to your computer and use it in GitHub Desktop.
Save vasi/3065861 to your computer and use it in GitHub Desktop.
take 2
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/vector.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <iostream>
namespace qi = boost::spirit::qi;
namespace fusion = boost::fusion;
struct my_type {
int inner;
int x;
};
BOOST_FUSION_ADAPT_STRUCT(
my_type,
(int, inner)
(int, x)
)
template <typename Iterator>
struct my_grammar : qi::grammar<Iterator, my_type()> {
my_grammar() : my_grammar::base_type(outer_rule) {
using qi::_val;
using qi::_1;
inner_rule = qi::int_ >> ',' >> qi::int_;
outer_rule = '{' >> inner_rule >> '}'; // Doesn't work...
// outer_rule = '{' >> qi::int_ >> '}'; // ...but this does!
}
qi::rule<Iterator, my_type()> inner_rule, outer_rule;
};
int main() {
std::string input("{123,456}");
my_type out;
my_grammar<std::string::iterator> g;
if (qi::parse(input.begin(), input.end(), g, out)) {
std::cout << out.inner << "\n";
} else {
std::cout << "failed\n";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment