I want to parse something like
"{xxxx}
{xxxx}"
which is separated by eol into a vector<vector<wchar_t>>
: ({xxxx},{xxxx})
so that "{" and "}" stays with internal characters together.
My code is:
#define BOOST_SPIRIT_UNICODE
#include <iostream>
#include<boost/spirit/include/qi.hpp>
#include<string>
#include<vector>
using namespace std;
namespace sw=boost::spirit::standard_wide;
namespace qi= boost::spirit::qi;
using boost::spirit::standard_wide::char_;
int main()
{
wstring s = L"{\"id\":23,\"text\":\"sf\nsf\"}\n{\"id\":23,\"text\":\"sfsf\"}";
qi::rule<wstring::iterator, vector<vector<wchar_t>>(), sw::blank_type> ru;
ru = (qi::char_(L"{") >> *(char_-char_(L"}")) >> char_(L"}")) % qi::eol;
vector<vector<wchar_t>> result;
qi::phrase_parse(s.begin(), s.end(), ru, sw::blank, result);
for (auto& v : result) {
//cout << "Size of string: " << v.size() << endl;
for (auto& s : v) {
wcout << s;
};
cout << endl;
};
std::cout << "Size of result"<<result.size()<<endl ;
}
However ouput is:
{
"id":23,"text":"sf
sf"
}
{
"id":23,"text":"sfsf"
}
Size of result6
It looks like that "{" becomes a single element of type vector<wchar_t>
for the outer vector.
Then consider the rule:
ru = (qi::char_(L"{") >> *(char_-char_(L"}")) >> char_(L"}")) % qi::eol;
According to the documentation, *(char_-char_(L"}"))
should be vector<A>
. And because a: A, b: vector<A> --> (a >> b): vector<A>
, then I think that (qi::char_(L"{") >> *(char_-char_(L"}")) >> char_(L"}"))
should be vector<wchar_t>
. This is contracdicted to the result.
Where I'm wrong?