使用Boost.Spirit X3,我想将逗号分隔的范围列表和单个数字(例如 1-4、6、7、9-12)解析为单个std::vector<int>
. 这是我想出的:
namespace ast {
struct range
{
int first_, last_;
};
using expr = std::vector<int>;
}
namespace parser {
template<typename T>
auto as_rule = [](auto p) { return x3::rule<struct _, T>{} = x3::as_parser(p); };
auto const push = [](auto& ctx) {
x3::_val(ctx).push_back(x3::_attr(ctx));
};
auto const expand = [](auto& ctx) {
for (auto i = x3::_attr(ctx).first_; i <= x3::_attr(ctx).last_; ++i)
x3::_val(ctx).push_back(i);
};
auto const number = x3::uint_;
auto const range = as_rule<ast::range> (number >> '-' >> number );
auto const expr = as_rule<ast::expr> ( -(range [expand] | number [push] ) % ',' );
}
给定输入
"1,2,3,4,6,7,9,10,11,12", // individually enumerated
"1-4,6-7,9-12", // short-hand: using three ranges
这被成功解析为(Live On Coliru):
OK! Parsed: 1, 2, 3, 4, 6, 7, 9, 10, 11, 12,
OK! Parsed: 1, 2, 3, 4, 6, 7, 9, 10, 11, 12,
问题:我想我明白对部件应用语义动作expand
是range
必要的,但为什么我还必须对部件应用语义push
动作number
?没有它(即有一个简单的( -(range [expand] | number) % ',')
规则expr
,单个数字不会传播到 AST(Live On Coliru)中:
OK! Parsed:
OK! Parsed: 1, 2, 3, 4, 6, 7, 9, 10, 11, 12,
奖励问题:我什至需要语义动作来做到这一点吗?Spirit X3 文档似乎不鼓励他们。