2

I would like to write a boost::spirit parser that parses a simple string in double quotes that uses escaped double quotes, e.g. "a \"b\" c".

Here is what I tried:

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>

#include <iostream>
#include <string>

namespace client
{
  namespace qi = boost::spirit::qi;
  namespace ascii = boost::spirit::ascii;

  template <typename Iterator>
  bool parse(Iterator first, Iterator last)
  { 
    using qi::char_;

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

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

    bool r = qi::phrase_parse(first, last, text, ascii::space);
    if (first != last) // fail if we did not get a full match
      return false;
    return r;
  }
}

int main() {
  std::string str = "\"a \\\"b\\\" c\"";
  if (client::parse(str.begin(), str.end()))
    std::cout << str << " Parses OK: " << std::endl;
  else
    std::cout << "Fail\n";
  return 0;
}

It follows the example on Parsing escaped strings with boost spirit, but the output is "Fail". How can I get it to work?

Community
  • 1
  • 1
Frank
  • 64,140
  • 93
  • 237
  • 324

1 Answers1

2

Been a while since I had a go at spirit, but I think one of your rules is the wrong way round.

Try:

content = +(escChar | ~char_('"'))

instead of:

content = +(~char_('"') | escChar)

It is matching your \ using ~char('"') and therefore never gets round to checking if escChar matches. It then reads the next " as the end of the string and stops parsing.

DaedalusFall
  • 8,335
  • 6
  • 30
  • 43