【发布时间】:2013-09-03 23:02:24
【问题描述】:
假设我有一个我想用灵气解析成的结构,它是这样定义的:
struct data_
{
bool export;
std::wstring name;
data_() : export(false) {}
};
另外,假设结构已经像这样适应融合:
BOOST_FUSION_ADAPT_STRUCT(
data_,
(bool, export)
(std::wstring, name)
)
而相关的规则是:
qi::rule<Iterator, data_(), skipper<Iterator> > rule_data;
rule_data = -lexeme["SpecialText" >> !(alnum | '_')] [ boost::phoenix::at_c<0> = true ] // If this string is found, , set "export" to true
> lexeme["Name" >> !(alnum | '_')] // this is supposed to go into the "name" member
到目前为止,这编译得很好。但是,“名称”现在保持为空!
所以本质上,我问的是:鉴于“SpecialText”在“Name”之前,我将如何正确合成“export”的布尔属性,而不是字符串?
编辑 在为此拔出头发之后,我偶然发现了“matches []”解析器,它似乎可以满足我的需求。
尽管如此,问题仍然存在于一般形式中,例如,如果我想返回某个字符串或其他数据类型而不是 bool。 本质上,如何通过语义操作设置结构属性的特定成员。
【问题讨论】:
-
如果要结合语义动作和自动属性合成,需要使用
%=运算符rule_data %= ... -
查看这里以了解如何在语义操作中设置值:boost.org/doc/libs/1_53_0/libs/spirit/phoenix/doc/html/phoenix/…
-
谢谢!我正想问你在哪里找到 %=,因为这里没有列出:boost.org/doc/libs/1_46_1/libs/spirit/doc/html/spirit/qi/…
-
@namezero 这有点像常见问题... stackoverflow.com/a/17385379/85371
标签: c++ attributes boost-spirit boost-spirit-qi synthesize