Strong-parsing syntax strings

I work with Spirit 2.4, and I would like to parse such a structure:

Text {text_field};

The fact is that there is an escaped string in the text field with the characters '{', '}' and '\'. I would like to create a parser for this using qi. I tried this:

using boost::spirit::standard::char_;
using boost::spirit::standard::string;
using qi::lexeme;
using qi::lit;

qi::rule< IteratorT, std::string(), ascii::space_type > text;
qi::rule< IteratorT, std::string(), ascii::space_type > content;
qi::rule< IteratorT, std::string(), ascii::space_type > escChar;


text %= 
  lit( "Text" ) >> '{' >>
    content >>
  "};"
  ;

content %= lexeme[ +( +(char_ - ( lit( '\\' ) | '}' ) )  >> escChar ) ];

escChar %= string( "\\\\" ) 
  | string( "\\{" ) 
  | string( "\\}" );

But it doesn't even compile. Any idea?

+4
source share
1 answer

Your grammar can be written as:

qi::rule< IteratorT, std::string(), ascii::space_type > text; 
qi::rule< IteratorT, std::string() > content;   
qi::rule< IteratorT, char() > escChar;   

text = "Text{" >> content >> "};";  
content = +(~char_('}') | escChar); 
escChar = '\\' >> char_("\\{}");

i.e.

  • text Text{followed by content followed by}

  • content is at least one instance of either a character (but not }) or escChar

  • escChar is a single escape code \\, {or}

, escChar escape- \\. , . , escChar, lexeme[] ( ).

+7

All Articles