c++boostboost-spiritboost-phoenix

boost spirit and phoenix parsing into a std::string


How do I parse into a std::string with boost spirit and phoenix?

For example, below I successfully parse 1234 into an int, but when I try to parse into a string the parse fails. If I replace qi::alnum with +qi::alnum then the parse succeeds but the string number has the undesired value "4". It seems like qi::alnum only matches a single character; how do I parse a multi-character alpha-numeric string?

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>

#include <iostream>
#include <string>

int main() {

    namespace qi = boost::spirit::qi;
    namespace phoenix = boost::phoenix;

    std::string s("--1234--");

    {
        int number = 0;
        bool success = qi::parse(s.begin(), s.end(),
              qi::lit("--") >> qi::int_[ phoenix::ref(number) = qi::_1 ] >> qi::lit("--")
            );
        std::cout << success << " qi::int_ parses: " << number << std::endl;
    }

    {
        std::string number;
        bool success = qi::parse(s.begin(), s.end(),
              qi::lit("--") >> qi::alnum[ phoenix::ref(number) = qi::_1 ] >> qi::lit("--")
            );
        std::cout << success << " qi::alpha parses: " << number << std::endl;
    }

    return 0;
}

Solution

  • Use qi::as_string, e.g.

    #include <boost/spirit/include/qi.hpp>
    #include <boost/spirit/include/phoenix_core.hpp>
    #include <boost/spirit/include/phoenix_operator.hpp>
    
    #include <iostream>
    #include <string>
    
    int main() {
    
        namespace qi = boost::spirit::qi;
        namespace phoenix = boost::phoenix;
    
        std::string s("--1234--");
    
        std::string number;
        bool success = qi::parse( s.begin(), s.end(),
            qi::lit("--")
            >> qi::as_string [ +qi::alnum ] [ phoenix::ref(number) = qi::_1 ]
            >> qi::lit("--"));
        std::cout << success << " qi::as_string parses: " << number << std::endl;
    
        return 0;
    }