正如我一直以来之前警告人们 https://stackoverflow.com/a/37931344/853711 您正在突破经常破坏 Spirit 齿轮的事物交叉点的限制:
- 单元件融合序列
- 一般 ADT 适应
- 开发中修复了 ADT 的持续错误(after1.67.0 版本)
1. 单元素难题
我不会在这个问题上花太多时间,因为它是一个相当古老、无聊、文档齐全的问题,而且对你的问题来说并不重要。
让我们通过添加一个虚拟字段来回避它:
struct rexpr
{
rexpr_map i_entries;
const rexpr_map& entries() const { return i_entries; }
rexpr_map& entries() { return i_entries; }
void entries(const rexpr_map& ent) { i_entries = ent; }
int i_dummy;
int dummy() const { return i_dummy; }
void dummy(int i) { i_dummy = i; }
};
// ... later:
BOOST_FUSION_ADAPT_ADT(client::ast::rexpr,
(obj.entries(), obj.entries(val))
(obj.dummy(), obj.dummy(val))
)
// ... even later:
auto const rexpr_def =
'{' >> *rexpr_key_value >> '}' >> x3::attr(42);
2.ADT代理
精神的属性类别机制检测到entries
property 作为容器属性(is_container<...>{}
评估为true
).
然而,必要的容器特征还没有到位。
此外,由于 ADT 代理授予的限制性接口,属性值可以only替换了 Whole-Sale,这意味着我们只能实现它的一个非常次优的版本:
namespace boost { namespace spirit { namespace x3 { namespace traits {
template <typename T, auto... Other>
struct container_value<boost::fusion::extension::adt_attribute_proxy<T, Other...> >
: container_value<typename boost::fusion::extension::adt_attribute_proxy<T, Other...>::type>
{ };
template <typename T, auto... Other>
struct push_back_container<boost::fusion::extension::adt_attribute_proxy<T, Other...> >
{
using underlying_type = typename boost::fusion::extension::adt_attribute_proxy<T, Other...>::type;
template <typename X, typename Y>
static bool call(X& proxy, Y&& v) {
auto u = proxy.get();
bool b = push_back_container<underlying_type>::call(u, std::forward<Y>(v));
proxy = u;
return b;
}
};
} } } }
3.惊喜:1.67.0之后修复了老bug
您需要提交:
commit ae78e1ec2431517a8b0580099aeba8f9122d8abb
Author: Nikita Kniazev <[email protected] /cdn-cgi/l/email-protection>
Date: Thu Mar 15 17:33:36 2018 +0300
X3: sequence: Fixed reference to temporary bug
commit e7f31017ec7c0b5584d12ec1b718d8c415b26fa1
Author: Nikita Kniazev <[email protected] /cdn-cgi/l/email-protection>
Date: Wed Mar 14 18:54:35 2018 +0300
Qi: Fixed ADT support by permutation and sequence_or operator
This is follow-up to 0f2b3c49ce55a41a7d22cc5533e0f4ba59e491ae
这些版本比 1.67.0 更新,目前位于develop
分支。他们(部分)解决了一个老问题:https://github.com/boostorg/spirit/pull/153#issuecomment-152879056 https://github.com/boostorg/spirit/pull/153#issuecomment-152879056。当前的行为也可能受到提交的影响
commit a0df3c098ff4e42c0958796c4f47d4d72a20c164
Merge: f73b121 fac9dfa
Author: Nikita Kniazev <[email protected] /cdn-cgi/l/email-protection>
Date: Thu Mar 1 13:44:27 2018 +0300
Merge pull request #370 from Kojoley/x3-pass-container-attribute-through-sequence
X3: Pass container attribute through sequence
在这个……动荡的环境中,很难判断影响是积极的还是消极的。
Demo
只要这么说就够了iff you
- 针对 ae78e1ec243151 或更高版本进行编译(
develop
)
- Apply both上述解决方法
then我看到了预期的输出:
-------------------------
Parsing succeeded
-------------------------
{
"color" = "blue"
"position" = {
"x" = "123"
"y" = "456"
}
"size" = "29 cm."
}
(基于libs/spirit/example/x3/rexpr/rexpr_examples/a.rexpr
input).
总结
我希望你不要认为这“很好”。请考虑在邮件列表/github 上提交问题。还要考虑这些:
更不用说在大多数情况下我不喜欢它:用精神解析成类? https://stackoverflow.com/questions/44581141/using-spirit-to-parse-into-classes/44585105#44585105
² 单成员结构的灵气属性传播问题 https://stackoverflow.com/questions/19823413/spirit-qi-attribute-propagation-issue-with-single-member-struct/19824426#19824426, X3,什么是attr_gen? https://stackoverflow.com/questions/45157367/x3-what-is-attr-gen/45161853, boost::spirit::x3 属性兼容性规则、直觉还是代码? https://stackoverflow.com/questions/37201023/boostspiritx3-attribute-compatibility-rules-intuition-or-code/37204957