Boost Spirit Rule with Custom Attribute Analysis

I am writing a Boost Spirit grammar to parse text into a vector of these structures:

struct Pair
{
    double a;
    double b;
};

BOOST_FUSION_ADAPT_STRUCT(
    Pair,
    (double, a)
    (double, a)
)

This grammar has the following rule:

qi::rule<Iterator, Pair()> pairSequence;

However, the actual grammar pairSequenceis as follows:

double_ % separator

I want this grammar to create Pairwith aequal to double, and bequal to some constant. I want to do something like this:

pairSequence = double_[_val = Pair(_1, DEFAULT_B)] % separator;

The above does not compile, of course. I tried to add a constructor to Pair, but I still get compilation errors (there is no suitable function to call "Pair :: Pair" (const boost :: phoenix :: actor> &, double) ').

+3
1

, pairSequence :

qi::rule<Iterator, std::vector<Pair>()> pairSequence; 

list std::vector<Pair>.

, , "", :

namespace phx = boost::phoenix;

pairSequence = 
    double_[
        phx::push_back(_val, 
            phx::construct<Pair>(_1, phx::val(DEFAULT_B))
        )
    ] % separator
; 

( ) Pair:

struct Pair         
{         
    Pair(double a) : a(a), b(DEFAULT_B) {}

    double a;         
    double b;         
};         

:

pairSequence = double_ % separator; 

Spirit.

BTW, Pair Fusion.

+6

Source: https://habr.com/ru/post/1751985/


All Articles