提神气:从语义动作改变多个目标领域

boost spirit qi: change multiple target fields from semantic action

我有以下输入 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;

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

我只是建议

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

以后

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