Assign default value to variable using boost spirit

后端 未结 2 1984
故里飘歌
故里飘歌 2021-01-22 03:22

Suppose I have the following string to parse:

\"1.2, 2.0, 3.9\"

and when I apply the following parser for it:

struct DataStruct
{
    dou         


        
2条回答
  •  感情败类
    2021-01-22 03:57

    The usual trick is to use an alternative with qi::attr:

    rule_def = parser_expression | qi::attr(default_value);
    

    In your case, perhaps:

    reader_ = qi::double_ | qi::lit("null") >> qi::attr(0);
    

    Demo

    Live On Coliru

    #include 
    #include 
    struct DataStruct { double n1, n2, n3; };
    
    BOOST_FUSION_ADAPT_STRUCT(DataStruct, n1, n2, n3)
    
    namespace qi = boost::spirit::qi;
    
    int main() {
        using Iterator = typename std::string::const_iterator;
    
        qi::rule reader_   = qi::double_ | qi::lit("null") >> qi::attr(0);
        qi::rule data_ = reader_ >> ',' >> reader_ >> ',' >> reader_;
    
        DataStruct res;
        auto const str = std::string("1.2,null,3.9");
        Iterator start = str.begin(), end = str.end();
    
        if (qi::parse(start, end, data_ >> qi::eoi, res)) {
            std::cout << "parsed: " << boost::fusion::as_vector(res) << "\n";
        }
        else {
            std::cout << "parse failed\n";
        }
    }
    

    Prints

    parsed: (1.2 0 3.9)
    

    Note the review changes (don't using namespace, check for eoi).

提交回复
热议问题