|
Boost Users : |
From: kila (semeegozy_at_[hidden])
Date: 2019-10-12 16:35:01
I also open the same question on stackoverflow. For better formatting of
code, please refer to
https://stackoverflow.com/questions/58353782/attributes-of-sequence-and-list-operator-in-boost-spirit-qi
<https://stackoverflow.com/questions/58353782/attributes-of-sequence-and-list-operator-in-boost-spirit-qi>
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.
Note there are only two elements on the outer vector. 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 . And
because *a: A, b: vector --> (a >> b): vector *, 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?
-- Sent from: http://boost.2283326.n4.nabble.com/Boost-Users-f2553780.html
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net