This code compiles and works as intended; an input like "{a;b}" is parsed and stored in a custom class
#include <vector>
#include <string>
#include <iostream>
#include <boost/spirit/include/qi.hpp>
namespace t {
using std::vector;
using std::string;
namespace qi = boost::spirit::qi;
struct Block {
Block() = default;
Block(vector<string> const& s) : value(s) {}
vector<string> value;
};
template <typename Iterator, typename Skipper=qi::space_type>
struct G1 : qi::grammar<Iterator, Block(), Skipper> {
template <typename T>
using rule = qi::rule<Iterator, T, Skipper>;
rule<Block()> start;
G1() : G1::base_type(start, "G1") {
start = qi::as<vector<string>>()[
qi::lit('{')
>> *(+(qi::char_ - ';') >> ';')
>> '}'
];
}
};
Block parse(string const input) {
G1<string::const_iterator> g;
Block result;
phrase_parse(begin(input), end(input), g, qi::standard::space, result);
return result;
}
};
int main() {
using namespace std;
auto r = t::parse("{a;b;}");
for (auto& s : r.value) {
cout << s << endl;
}
}
What I don't understand is why the as<> directive is needed; from what I can infer from the docs the synthesized attribute of the primitive parsers should be already a vector of strings.
I've read this article about attribute propagation and attribute compatibility, but I miss the big picture; what happens when the as directive is (not) used?