5

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;
}
Community
  • 1
  • 1
foraidt
  • 5,519
  • 5
  • 52
  • 80

1 Answers1

4

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).

hkaiser
  • 11,403
  • 1
  • 30
  • 35
  • Thanks! I missed that. However, now it's not compiling anymore. I updated the question. – foraidt Jul 04 '11 at 15:07
  • @hkaiser: isn't it time for a FAQ entry on this one. Perhaps it already exists, but it comes up often enoug... – sehe Jul 04 '11 at 15:16
  • @sehe Apparently there is a FAQ entry for this (http://boost-spirit.com/home/articles/doc-addendum/faq/#attributes). But maybe it should say "Why does my code not work when I use variants?" or something like that. – foraidt Jul 04 '11 at 15:47
  • @foraidt: sadly this has _nothing_ to do with Variant, it applies to all attribute typed. Perhaps you were looking for the FQA with your name on it :) – sehe Jul 04 '11 at 15:53
  • 1
    This has been updated in the new (upcoming) version of Spirit (Boost V1.47). There using variant should work. – hkaiser Jul 04 '11 at 16:41