boost spirit parse with the source

后端 未结 1 850
庸人自扰
庸人自扰 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 
    

    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  struct result { typedef size_t type; };
        template  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(begin(_1), end(_1)),
                       at_c<2>(_val) = get_line_(begin(_1)) 
                   ]
        ;
    
        // with
    
    boost::phoenix::function get_line_;
    

    Note I changed a few minor points.

    Fully running demo with output: Live On Coliru

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