boost spirit qi:从语义动作中改变多个目标字段

时间:2016-03-06 09:19:40

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

我有以下输入123, test, test456并且我想运行boost :: qi语法,以便输出是成对的向量,其中每个匹配与某些类型的信息相关联,例如: [(123, Int), (test, String), (test456, String)]

这是我到目前为止所尝试的内容:

enum class MatchType
{
  Int,
  String
}

在语法中

match = qi::alpha >> *(qi::alnum)[/*How to set the tuple to (matched value _1, string)*/]
       | +qi::digit[ /*How to set the tuple to (matched value _1, Int)*/ ]

/* Tied but doesn't compile:
         +qi::digit[ 
            []() 
            {
                phoenix::at_c<0>(_val) = _1;
                phoenix::at_c<1>(_val) = MatchType::Int;
            }]
*/


qi::rule<Iterator, std::vector<std::pair<MatchType, std::string>>> match;

实现这一目标的最佳方法是什么?

1 个答案:

答案 0 :(得分:3)

我只是建议

#include <boost/fusion/adapted/std_pair.hpp>

以后

my_pair = (qi::attr(MatchType::Int)    >> qi::int_)
        | (qi::attr(MatchType::String) >> +qi::alnum);