5
votes

When trying to parse text into a boost::variant, the variant's value does not get changed. The parsers by themselves appear to work fine, so my assumption is that I'm doing something wrong with the variant code.

I'm using boost 1.46.1 and the following code compiles in Visual Studio 2008.

1st Update

hkaiser noted that the rule and grammar template arguments must not be Variant but Variant().
This got a bit "further" as I now have a compilation error in boost_1_46_1\boost\variant\variant.hpp(1304). The comment says:

// NOTE TO USER :
// Compile error here indicates that the given type is not 
// unambiguously convertible to one of the variant's types
// (or that no conversion exists).

So apparently the attribute of the expression (qi::double_ | +qi::char_) is not boost::variant<double, std::string>. But what is it then?

2nd Update

Using typedef boost::variant<double, std::vector<char>> Variant; works for the parser. However, this is not as easy to use as std::string...

#include <boost/spirit/include/qi.hpp>
#include <boost/variant.hpp>

int main()
{
    namespace qi = boost::spirit::qi;

    typedef std::string::const_iterator Iterator;

    const std::string a("foo"), b("0.5");

    // This works
    {
        std::string stringResult;
        Iterator itA = a.begin();
        const bool isStringParsed =
            qi::parse(itA, a.end(), +qi::char_, stringResult);

        double doubleResult = -1;
        Iterator itB = b.begin();
        const bool isDoubleParsed =
            qi::parse(itB, b.end(), qi::double_, doubleResult);

        std::cout
                << "A Parsed? " << isStringParsed <<
                ", Value? " << stringResult << "\n"
                << "B Parsed? " << isDoubleParsed <<
                ", Value? " << doubleResult << std::endl;

        // Output:
        // A Parsed? 1, Value? foo
        // B Parsed? 1, Value? 0.5
    }


    // This also works now
    {
        typedef boost::variant<double, std::vector<char>> Variant; // vector<char>, not string!

        struct variant_grammar : qi::grammar<Iterator, Variant()> // "Variant()", not "Variant"!
        {
            qi::rule<Iterator, Variant()> m_rule; // "Variant()", not "Variant"!

            variant_grammar() : variant_grammar::base_type(m_rule)
            {
                m_rule %= (qi::double_ | +qi::char_);
            }
        };

        variant_grammar varGrammar;

        Variant varA(-1), varB(-1);
        Iterator itA = a.begin();
        const bool isVarAParsed = qi::parse(itA, a.end(), varGrammar, varA);
        Iterator itB = b.begin();
        const bool isVarBParsed = qi::parse(itB, b.end(), varGrammar, varB);

        // std::vector<char> cannot be put into std::cout but
        // needs to be converted to a std::string (or char*) first.
        // The conversion I came up with is very ugly but it's not the point
        // of this question anyway, so I omitted it.
        // You'll have to believe me here, when I'm saying it works..

        // Output:
        // A (variant): Parsed? 1, Value? foo, Remaining text = ''
        // B (variant): Parsed? 1, Value? 0.5, Remaining text = ''
    }

    return 0;
}
1

1 Answers

4
votes

The rule's attribute must be specified using the function declaration syntax:

qi::rule<Iterator, Variant()> m_rule;

I have not tried, but I believe it will work after this change (the same is required for the grammar, btw).