I have the following grammar that works as expected.
struct query_term { std::string term; bool is_tag; query_term(const std::string &a, bool tag = false): term(a), is_tag(tag) { } }; template<typename Iterator> struct query_grammar: grammar<Iterator, std::vector<query_term>(), space_type> { query_grammar(): query_grammar::base_type(query) { word %= +alnum; tag = (omit[word >> ':'] >> word[_val = phoenix::construct<query_term>(_1, true)]); non_tag = word[_val = phoenix::construct<query_term>(_1, false)]; query = ( (omit[word >> ':'] >> word[push_back(_val, phoenix::construct<query_term>(_1, true))]) | word[push_back(_val, phoenix::construct<query_term>(_1)) ] ) % space; }; qi::rule<Iterator, std::string(), space_type> word; qi::rule<Iterator, query_term, space_type> tag; qi::rule<Iterator, query_term, space_type> non_tag; qi::rule<Iterator, std::vector<query_term>(), space_type> query; };
But when I replace the request
query = ( tag[phoenix::push_back(_val, _1)] | word[push_back(_val, phoenix::construct<query_term>(_1)) ] ) % space;
the code does not compile. I am basically trying to break down the grammar into components that can be reused in a larger grammar. When parsing words or tags, create a query_term object with the appropriate flag in the tag and word rule. Reuse these attributes in the query rule.
In the previous version, tag rules and words are embedded in the query grammar.
I'm not sure what I am missing here. Any help would be greatly appreciated.
FYI: This is not the final code. I am trying to use rules before using them in production code.
Thanx, - baliga