c++ - 使用 Boost Spirit Qi qi::iter_pos 但无法捕获值

标签 c++ boost-spirit boost-spirit-qi

我需要跟踪一些项目在我使用提升灵气解析的文本中的位置。我找到了 this example并适应这样:

#include <iostream>
#include <string>
#include <boost/spirit/home/qi.hpp>
#include <boost/spirit/repository/include/qi_iter_pos.hpp>
#include <boost/spirit/include/phoenix.hpp>

namespace phx = boost::phoenix;
namespace qi = boost::spirit::qi;

template<typename Iterator>
struct CurrentPos 
{
    CurrentPos() 
    {
        save_start_pos = qi::omit[boost::spirit::repository::qi::iter_pos[
                phx::bind(&CurrentPos::setStartPos, this, qi::_1)]];

        current_pos = boost::spirit::repository::qi::iter_pos[
                qi::_val = phx::bind(&CurrentPos::getCurrentPos, this, qi::_1)];
    }

    qi::rule<Iterator> save_start_pos;
    qi::rule<Iterator, std::size_t()> current_pos;

private:
    void setStartPos(const Iterator &iterator) 
    {
        start_pos_ = iterator;
    }

    std::size_t getCurrentPos(const Iterator &iterator) 
    {
        return std::distance(start_pos_, iterator);
    }

    Iterator start_pos_;
};

using InfoTuple = std::tuple<std::size_t, std::uint32_t>;

struct Header
{
    std::uint32_t x;
    InfoTuple y;
};

BOOST_FUSION_ADAPT_STRUCT(
    Header,
    (std::uint32_t, x)
    (InfoTuple, y)
)

template<typename Iterator>
struct HeaderParse
    : boost::spirit::qi::grammar<Iterator, Header()>
{
    HeaderParse()
        : HeaderParse::base_type(_start)
    {
        using boost::spirit::qi::uint_parser;

        _thing = (current_pos.current_pos >> uint_parser<std::uint32_t, 10, 1, 3>());
        _start = current_pos.save_start_pos 
            >> '<' 
            >> uint_parser<std::uint32_t, 10, 1, 3>() 
            >> '>'
            >> _thing;
    }   

    qi::rule<Iterator, InfoTuple()> _thing; 
    qi::rule<Iterator, Header()> _start;

    CurrentPos<Iterator> current_pos;
};

int main()
{
    const std::string d1 = "<13>937";

    const HeaderParse<std::string::const_iterator> parser;
    Header header;

    std::string::const_iterator begin = d1.begin();
    std::string::const_iterator end = d1.end();

    assert(boost::spirit::qi::parse(begin, end, parser, header));
    assert(begin == end);

    std::cout << "x    : " << header.x << std::endl;
    std::cout << "y    : " << std::get<1>(header.y) << std::endl;
    std::cout << "y pos: " << std::get<0>(header.y) << std::endl;
}

当我运行它时,我看到以下输出:

$> ./testme
x    : 13
y    : 0
y pos: 4

出于某种原因,它不会捕获值 937

我在 Coliru 上试过这个,起初它没有编译,现在我不断收到“执行已过期”。看这里:https://coliru.stacked-crooked.com/a/724c7fcd296c8803但这让我想知道它是否有效。我使用的是 clang,Coliru 使用的是 gcc。

有人可以提供任何关于为什么这行不通的见解吗?

最佳答案

由于缺少 Fusion std::tuple 集成 header (boost/fusion/include/std_tuple.hpp),您的示例无法为我编译。添加后,示例编译并输出预期结果。 https://wandbox.org/permlink/wOf8JxnKKeBGCihk

P.S:我建议不要在解析器中计算偏移量,它不会为您节省任何东西,存储迭代器本身具有相同的内存占用,但复杂性和痛苦较少。

关于c++ - 使用 Boost Spirit Qi qi::iter_pos 但无法捕获值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55959166/

相关文章:

python - 如何在C++ Builder 2010(非Delphi)应用程序中嵌入Python?

c++ - C++ 中 std::is_trivially_copy_constructible 中的琐碎操作是什么

c++ - 自动工具编译器

c++ - 使用 boost::spirit 解析类似 c-struct 的声明

c++ - 在语义 Action 中去掉一个灵气符号

c++ - 在opencv中找到物体的凸包?

c++ - Boost::spirit 如何解析和调用c++类函数表达式

c++ - 使用boost spirit从括号中提取字符串

c++ - boost spirit 解析器前瞻解析

c++ - 从逗号分隔的整数中解析 std::vector<int>