c++ - 提神气: change multiple target fields from semantic action

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

我有以下输入 123, test, test456 我想运行一个 boost::qi 语法,以便输出是一个对 vector ,其中每个匹配都与某种类型相关联信息如: [(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);

关于c++ - 提神气: change multiple target fields from semantic action,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35825138/

相关文章:

c++ - SWIG python 模块创建问题

c++ - QLabel 在 Qt 中获取 QImage 时如何设置 QSpinBox?

c++ - ALSA 回调(SIGIO 处理程序)有时会在 boost::posix_time::microsec_clock::local_time() 中的某处挂起

c++ - 如何在字符串中找到一个字符串并替换它?

c++ - 使用 Boost Spirit 提取未分隔的字符串和整数

c++ - 在创建显示索引如何在另一个数组中移动的数组时遇到问题

c++ - 为什么 ctime() 返回的字符串包含换行符?

c++ - GCC 在 Boost 源中的警告

c++ - Spirit X3,如何在非 ascii 输入上解析失败?

c++ - Boost Spirit Grammar 检测登录失败