<分区>
有点难以理解的标题,但我想解析这样的内容:
int_ >> repeat(N)[double_]
但是我希望 N 等于 int_
解析为
如何使用 Boost.Spirit.X3 做到这一点?
<分区>
有点难以理解的标题,但我想解析这样的内容:
int_ >> repeat(N)[double_]
但是我希望 N 等于 int_
解析为
如何使用 Boost.Spirit.X3 做到这一点?
最佳答案
解析后更容易验证这一点。即只解析不限制计数,然后验证:
auto const p = x3::int_ >> *x3::double_;
// ...
std::pair<int, std::vector<double>> result;
if (x3::phrase_parse(begin, end, p, x3::space, result)) {
if (result.first != result.second.size()) {
// invalid
}
}
如果你真的想在解析时验证这一点,可以使用语义操作:
int size;
auto const store_size = [&size](auto& ctx) { size = _attr(ctx); };
auto const validate_size = [&size](auto& ctx) {
if (size >= 0 && static_cast<std::size_t>(size) != _attr(ctx).size()) {
_pass(ctx) = false;
}
};
auto const p = x3::int_[store_size] >> (*x3::double_)[validate_size];
关于c++ - 用于解析 `int_` 然后重复等于 `int_` 的 double 的 X3 规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51863074/