boost spirit parse with the source

混江龙づ霸主 提交于 2019-11-29 14:40:33

Have a look at

#include <boost/spirit/repository/include/qi_iter_pos.hpp>

This defines a parser that directly exposes the position as an attribute. Let me add an example in a few minutes.

Edit I found it hard to shoe-horn iter_pos into your sample without "assuming" things and changing your data type layout. I'd very much favour this (I'd strive to lose the semantic actions all the way.). However, time's limited.

Here's a little helper that you can use to fix your problem:

struct get_line_f
{
    template <typename> struct result { typedef size_t type; };
    template <typename It> size_t operator()(It const& pos_iter) const
    {
        return get_line(pos_iter);
    }
};

^ The polymorphic actor, use as such:

    start = raw[ qi::no_case["0x"] >> hex [at_c<0>(_val) = _1] ]
               [ 
                   at_c<1>(_val) = construct<std::string>(begin(_1), end(_1)),
                   at_c<2>(_val) = get_line_(begin(_1)) 
               ]
    ;

    // with

boost::phoenix::function<get_line_f> get_line_;

Note I changed a few minor points.

Fully running demo with output: Live On Coliru

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!