Attributes of sequence and list operator in boost.spirit qi?

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Attributes of sequence and list operator in boost.spirit qi?

Boost - Users mailing list
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&lt;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&lt;vector&lt;wchar_t>>(),
sw::blank_type> ru;
    ru = (qi::char_(L"{") >> *(char_-char_(L"}")) >> char_(L"}")) % qi::eol;
    vector<vector&lt;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()&lt;&lt;endl ;
}
However ouput is:

{
&quot;id&quot;:23,&quot;text&quot;:&quot;sf
sf&quot;
}
{
&quot;id&quot;:23,&quot;text&quot;:&quot;sfsf&quot;
}
Size of result6


It looks like that &quot;{&quot; becomes a single element of type
vector&lt;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 mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users