boost.spirit qi中的序列和列表运算符的属性?

问题描述 投票:2回答:1

我想解析类似内容

"{xxxx}
{xxxx}"

由eol分隔为vector<vector<wchar_t>>({xxxx},{xxxx}),因此“ {”和“}”与内部字符在一起。我的代码是:

#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 ;
}

但是输出是:

{
"id":23,"text":"sf
sf"
}
{
"id":23,"text":"sfsf"
}
Size of result6

看起来“ {”成为外部向量的类型为vector<wchar_t>的单个元素。

然后考虑规则:

ru = (qi::char_(L"{") >> *(char_-char_(L"}")) >> char_(L"}")) % qi::eol;

根据文档,*(char_-char_(L"}"))应为vector<A>。并且因为a: A, b: vector<A> --> (a >> b): vector<A>,所以我认为(qi::char_(L"{") >> *(char_-char_(L"}")) >> char_(L"}"))应该是vector<wchar_t>。这与结果矛盾。

我错了?

c++ boost-spirit-qi
1个回答
2
投票

并且因为a:A,b:向量->(a >> b):向量,所以我认为(qi :: char_(L“ {”)>> *(char_-char_(L“}” ))>> char_(L“}”))应该是向量。这与结果矛盾。

确实不是这样。应用Detecting the parameter types in a Spirit semantic action

中的现代化技巧
struct sense_f {
    template <typename T> void operator()(T&&) const {
        std::cout << boost::core::demangle(typeid(T).name()) << "\n";
    }
};
static const boost::phoenix::function<sense_f> sense;

我们可以打印实际的属性类型:

ru = (char_(L'{') >> *(char_ - char_(L'}')) >> char_(L'}')) [sense(qi::_0)] % qi::eol;

哪个会打印[Live On Coliru

boost::fusion::vector<wchar_t, std::vector<wchar_t, std::allocator<wchar_t> >, wchar_t>

简单解决方案

假设您不需要捕获{},则只需将它们设为文字,而不是char_

ru = (L'{' >> *(char_ - L'}') >> L'}') [sense(qi::_0)] % qi::eol;

哪个会打印[Live On Coliru

boost::fusion::vector<std::vector<wchar_t, std::allocator<wchar_t> >&>

的确,如果您还使其传播属性:

ru %= (L'{' >> *(char_ - L'}') >> L'}') [sense(qi::_0)] % qi::eol;

程序打印:

boost::fusion::vector<std::vector<wchar_t, std::allocator<wchar_t> >&>
boost::fusion::vector<std::vector<wchar_t, std::allocator<wchar_t> >&>
"\"id\":23,\"text\":\"sf
sf\""
"\"id\":23,\"text\":\"sfsf\""

请注意,std::vector<wchar_t>std::wstring之间存在属性兼容性,这就是我使用后者的原因。

奖励

如果您想包含{}和任何中间空格,请使用qi::raw

ru %= qi::raw [L'{' >> *(char_ - L'}') >> L'}'] [sense(qi::_0)] % qi::eol;

现在打印:

boost::fusion::vector<boost::iterator_range<__gnu_cxx::__normal_iterator<wchar_t const*, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > > >&>
boost::fusion::vector<boost::iterator_range<__gnu_cxx::__normal_iterator<wchar_t const*, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > > >&>
"{\"id\":23,\"text\":\"sf
sf\"}"
"{\"id\":23,\"text\":\"sfsf\"}"

您可以看到,即使iterator_range<It>也具有与std::wstring的属性兼容性,因为输入也是wchar_t的序列。

当然,除非需要此输出,否则请采取sense操作。

完整列表

使用qi::raw方法的最终结果:

[Live On Coliru] >>

#define BOOST_SPIRIT_UNICODE

#include <boost/spirit/include/qi.hpp>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>

namespace sw = boost::spirit::standard_wide;
namespace qi = boost::spirit::qi;
using sw::char_;

int main() {
    std::wstring s = LR"({"id":23,"text":"sf
sf"}
{"id":23,"text":"sfsf"})";

    using Data = std::vector<std::wstring>;
    using It = std::wstring::const_iterator;

    qi::rule<It, Data(), sw::blank_type> ru
        = qi::raw [L'{' >> *(char_ - L'}') >> L'}'] % qi::eol;

    Data result;
    It f = s.begin(), l = s.end();

    if (qi::phrase_parse(f, l, ru, sw::blank, result)) {
        for (auto& s : result) {
            std::wcout << std::quoted(s) << std::endl;
        };
    } else {
        std::wcout << "Parse failed\n";
    }

    if (f!=l) {
        std::wcout << L"Remaining unparsed: " << std::quoted(std::wstring(f,l)) << std::endl;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.