Search code examples
c++boost-spirit

Boost Spirit Qi rule for XML comment tag


EBNF rule for XML comment tag is:

Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'

How to get Boost Spirit Qi rule for it?

using boost::spirit::qi::ascii::char_;
using boost::spirit::qi::lit;
comment = lit("<!--") >> *((~char_('-') >> char_) | (char_('-') >> ~char_('-'))) >> lit("-->");

This is my the best try, but not correct...


Solution

  • I'd phrase it more directly:

    comment = "<!--" > *(qi::char_ - "--") > "-->";
    

    Remember to make the rule lexeme (disable/ignore skipper). (See Boost spirit skipper issues)

    #include <boost/spirit/include/qi.hpp>
    namespace qi = boost::spirit::qi;
    using It = std::string::const_iterator;
    
    int main() {
        qi::rule<It> comment; // lexeme
        comment = "<!--" > *(qi::char_ - "--") > "-->";
    }