为什么boost :: spirit :: qi :: parse()没有设置这个boost :: variant的值?

时间:2011-07-04 12:59:03

标签: c++ boost boost-spirit boost-spirit-qi boost-variant

尝试将文本解析为boost :: variant时,变量的值不会更改。 解析器本身似乎工作正常,所以我的假设是我在做变量代码时出错了。

我正在使用boost 1.46.1和以下代码在 Visual Studio 2008中编译。

第一次更新

hkaiser注意到规则和语法模板参数不能是Variant,而是Variant()
这有点“进一步”,因为我现在在boost_1_46_1\boost\variant\variant.hpp(1304)中有一个编译错误。评论说:

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

所以表达式(qi::double_ | +qi::char_)的属性显然不是boost::variant<double, std::string>。但那又是什么呢?

第二次更新

使用typedef boost::variant<double, std::vector<char>> Variant;适用于解析器。但是,这不像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 个答案:

答案 0 :(得分:4)

必须使用函数声明语法指定规则的属性:

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

我没有尝试,但我相信它会在这个改变之后起作用(语法也是必需的,顺便说一下)。