I have a qi::symbol<char, std::string>
escapedDoubleQuote
that converts a double ""
into \"
.
I try to use this into a more complex parser, and want the result still to be a single string. But didn't succeed. I tried with and without qi::lexeme
, qi::as_string
and qi::as<std::string>
.
#include <iostream>
#include <boost/spirit/home/qi.hpp>
#include <vector>
#include <string>
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
struct escapedDoubleQuote_ : qi::symbols<char, std::string >
{
escapedDoubleQuote_()
{
add("\"\"", "\\\"");
}
} escapedDoubleQuote;
template<typename Iterator>
struct MyGrammar : qi::grammar<Iterator, std::string()>
{
MyGrammar() : MyGrammar::base_type(escapedField)
{
subField = +(~qi::char_("\""));
escapedField =
qi::lexeme[ // or qi::as<std::string> ... both seems to do nothing.
subField
>> -(escapedDoubleQuote >> escapedField)
];
}
qi::rule<Iterator, std::string()> subField;
qi::rule<Iterator, std::string()> escapedField;
};
int main()
{
typedef std::string::const_iterator iterator_type;
typedef MyGrammar<iterator_type> parser_type;
parser_type parser;
std::string input = "123\"\"456\"\"789";
std::string output;
iterator_type it = input.begin();
bool parsed = parse(
it,
input.cend(),
parser,
output
);
if(parsed && it == input.end())
{
std::cout << output << std::endl;
// expected: 123\"456\"789
// actual : \"789
}
return 0;
}
In the end, I want to use the parser and have as output a single std::string
. In the example 123\"\"456\"\"789
should output 123\"456\"789
.
Note: I know I can define the escapedDoubleQuote
as qi::symbols<char, std::vector<char> >
so that the result is achieved, but I want to understand if and how one can combine the strings.