提升灵气:从语义动作改变多个目标场

boost spirit qi: change multiple target fields from semantic action

本文关键字:改变 目标 语义      更新时间:2023-10-16

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