I'm trying to parse a quoted string with escape sequences using Boost::Spirit. Unfortunately, it seems that including the quotes in the grammar definition causes massive(-ly unhelpful) compile-time errors (as one might expect with Boost). Omitting quotes lets the program compile, but obviously it won't behave as it's supposed to. This is the code (actually part of a bigger picture, but it demonstrates the issue):
#include "boost/spirit/include/qi.hpp"
#include "boost/proto/deep_copy.hpp"
#include "boost/optional.hpp"
#include <string>
using boost::spirit::qi::char_;
using boost::spirit::qi::lexeme;
using boost::proto::deep_copy;
auto string_literal = deep_copy(
lexeme[
// char_('"')
/* >> */ *((char_ - '"' - '\\') | (char_('\\') >> char_))
// >> char_('"')
]);
template <class Iterator, class Grammar>
boost::optional<std::string> parse_string(Iterator first, Iterator last, Grammar&& gr)
{
using boost::spirit::qi::space;
using boost::spirit::qi::phrase_parse;
std::string temp;
bool success = phrase_parse(
first,
last,
gr,
space,
temp
);
if (first == last && success)
return temp;
else return boost::none;
}
int main()
{
std::string str;
std::cout << "string_literal: ";
getline(std::cin, str);
auto presult = parse_string(str.begin(), str.end(), string_literal);
if (presult) {
std::cout << "parsed: " << *presult;
} else
std::cout << "failure\n";
return 0;
}
Uncommenting the commented parts of string_literal
's definition causes errors. In its current state (with comments) the code compiles. I've tried several things such as moving the quotes into parse_string
, as well as using a less specific definition (the one above is the least specific I could come up with that was still useful, the correct grammar is in the OCaml language manual, but I figured I can just validate escape sequences separately), but nothing worked.
My Boost version is 1.56.0, and my compiler is MinGW-w64 g++ 4.9.1. Any help at all most appreciated.