boost spirit parse with the source

后端 未结 1 848
庸人自扰
庸人自扰 2020-12-21 02:30

I would like to be able to parse a Number, to store its original source and to track its position in the source preserving it in the structure itself.

This is what I

相关标签:
1条回答
  • 2020-12-21 03:16

    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

    0 讨论(0)
提交回复
热议问题