boost::phoenix::at_c 与 boost::spirit::qi::grammar 结合使用是否有替代方案

发布于 2024-09-11 06:47:45 字数 2672 浏览 4 评论 0原文

我创建了一个测试应用程序来说明我的问题。它解析以“a=”或“b=”开头并用“\r\n”分隔的整数列表。该列表以任意顺序包含这些字段的多次出现。

#include <string>
#include <vector>
#include <iostream>

#include <boost/spirit/include/qi.hpp>

#include <boost/spirit/include/phoenix.hpp>

#include <boost/fusion/include/adapt_struct.hpp>


typedef std::vector<unsigned int> uint_vector_t;

std::ostream& operator<<(std::ostream& out, const uint_vector_t &data)
{
    for (unsigned int i(0); i < data.size(); i++)
    {
        out << data[i] << '\n';
    }
    return out;
}

struct MyStruct
{
    uint_vector_t m_aList;
    uint_vector_t m_bList;
};

BOOST_FUSION_ADAPT_STRUCT
(
    MyStruct,
    (uint_vector_t, m_aList)
    (uint_vector_t, m_bList)
)
;

template<typename Iterator>
struct MyParser : public boost::spirit::qi::grammar<Iterator,
        MyStruct()>
{
    MyParser() :
        MyParser::base_type(Parser, "Parser")
    {
        using boost::spirit::qi::uint_;
        using boost::spirit::qi::_val;
        using boost::spirit::qi::_1;

        using boost::phoenix::at_c;
        using boost::phoenix::push_back;

        Parser =
                *(
                        aParser [push_back(at_c<0>(_val), _1)]
                    |
                        bParser [push_back(at_c<1>(_val), _1)]
                );
        aParser = "a=" >> uint_ >> "\r\n";
        bParser = "b=" >> uint_ >> "\r\n";
    }
        boost::spirit::qi::rule<Iterator, MyStruct()> Parser;
        boost::spirit::qi::rule<Iterator, unsigned int()> aParser, bParser;
};

int main()
{
    using boost::spirit::qi::phrase_parse;

    std::string input("a=0\r\nb=7531\r\na=2\r\na=3\r\nb=246\r\n");
    std::string::const_iterator begin = input.begin();
    std::string::const_iterator end = input.end();
    MyParser<std::string::const_iterator> parser;

    MyStruct result;
    bool succes = phrase_parse(begin, end, parser, "", result);
    assert(succes);

    std::cout << "===A===\n" <<result.m_aList << "===B===\n" << result.m_bList << std::endl;
}

在实践中,有更多不同类型的字段需要解析。我对这种方法的反对意见在于以下表达: [push_back(at_c<0>(_val), _1)] 这是赋值和 MyStruct 的第一个元素之间的“隐藏依赖关系”。这使得代码很容易发生变化。如果结构发生更改,它可能仍然可以编译,但不再执行预期的操作。

我希望有这样的建筑: [push_back(at_c<0>bind(&MyStruct::aList, arg1)(_val), _1)] 请参阅 this. 这样它就真的被名字绑定了。

这样的事情可能吗?或者我应该采取完全不同的方法?

I have created a test application to illustrate my problem. It parses a list of integers preceded by "a=" or "b=" and is separated by "\r\n". The list contains multiple occurrences of those fields in any order.

#include <string>
#include <vector>
#include <iostream>

#include <boost/spirit/include/qi.hpp>

#include <boost/spirit/include/phoenix.hpp>

#include <boost/fusion/include/adapt_struct.hpp>


typedef std::vector<unsigned int> uint_vector_t;

std::ostream& operator<<(std::ostream& out, const uint_vector_t &data)
{
    for (unsigned int i(0); i < data.size(); i++)
    {
        out << data[i] << '\n';
    }
    return out;
}

struct MyStruct
{
    uint_vector_t m_aList;
    uint_vector_t m_bList;
};

BOOST_FUSION_ADAPT_STRUCT
(
    MyStruct,
    (uint_vector_t, m_aList)
    (uint_vector_t, m_bList)
)
;

template<typename Iterator>
struct MyParser : public boost::spirit::qi::grammar<Iterator,
        MyStruct()>
{
    MyParser() :
        MyParser::base_type(Parser, "Parser")
    {
        using boost::spirit::qi::uint_;
        using boost::spirit::qi::_val;
        using boost::spirit::qi::_1;

        using boost::phoenix::at_c;
        using boost::phoenix::push_back;

        Parser =
                *(
                        aParser [push_back(at_c<0>(_val), _1)]
                    |
                        bParser [push_back(at_c<1>(_val), _1)]
                );
        aParser = "a=" >> uint_ >> "\r\n";
        bParser = "b=" >> uint_ >> "\r\n";
    }
        boost::spirit::qi::rule<Iterator, MyStruct()> Parser;
        boost::spirit::qi::rule<Iterator, unsigned int()> aParser, bParser;
};

int main()
{
    using boost::spirit::qi::phrase_parse;

    std::string input("a=0\r\nb=7531\r\na=2\r\na=3\r\nb=246\r\n");
    std::string::const_iterator begin = input.begin();
    std::string::const_iterator end = input.end();
    MyParser<std::string::const_iterator> parser;

    MyStruct result;
    bool succes = phrase_parse(begin, end, parser, "", result);
    assert(succes);

    std::cout << "===A===\n" <<result.m_aList << "===B===\n" << result.m_bList << std::endl;
}

In practice there are more fields with different types which need to be parsed. My objection with this approach lies in the following expression:
[push_back(at_c<0>(_val), _1)]
Here is a 'hidden dependency' between the assignment and the first element of MyStruct. This makes the code fragile to changes. If the struct is changed it might still compile, but no longer do what is expected.

I'm hoping for a construction like:
[push_back(at_c<0>bind(&MyStruct::aList, arg1)(_val), _1)]
See this. So that it is really bound by name.

Is something like this possible? Or should I take a total different approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半透明的墙 2024-09-18 06:47:46

Phoenix 还允许您绑定数据成员,因此您可以编写:

Parser = 
   *(  aParser [push_back(bind(&MyStruct::m_aList, _val), _1)] 
    |  bParser [push_back(bind(&MyStruct::m_bList, _val), _1)] 
    ); 

此外,在这种情况下,您的结构不再需要 FUSION_ADAPT 魔法。

Phoenix allows you to bind data members as well, so you can write:

Parser = 
   *(  aParser [push_back(bind(&MyStruct::m_aList, _val), _1)] 
    |  bParser [push_back(bind(&MyStruct::m_bList, _val), _1)] 
    ); 

Moreover, in this case you don't need the FUSION_ADAPT magic for your structure anymore.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文