只需放弃将一切都归于规则的旧习惯。
#include <boost/spirit/home/x3.hpp>
#include <iostream>
namespace x3 = boost::spirit::x3;
template <typename... Args>
auto negate(Args&&... p) {
return +(x3::char_ - x3::char_(std::forward<Args>(p)...));
};
int main() {
std::string input("all the king's men and all the king's horses"), parsed;
if (parse(input.begin(), input.end(), negate("horse"), parsed))
std::cout << "'" << input << "' -> '" << parsed << "'\n";
}
Live On Coliru, 印刷:
'all the king's men and all the king's horses' -> 'all t'
第二种味道:
#include <boost/spirit/home/x3.hpp>
#include <iostream>
namespace x3 = boost::spirit::x3;
template <typename Sub>
auto negate(Sub p) {
return +(x3::char_ - x3::as_parser(p));
};
int main() {
std::string input("all the king's men and all the king's horses"), parsed;
if (parse(input.begin(), input.end(), negate("horse"), parsed))
std::cout << "'" << input << "' -> '" << parsed << "'\n";
}
Live On Coliru, 印刷:
'all the king's men and all the king's horses' -> 'all the king's men and all the king's '
更复杂的东西
您还可以在自定义解析器中聚合子解析器:
如果您需要通过传递的递归规则,我建议x3::with<>
(尽管我不确定上下文为 建立可重入状态,with<>
除非您能找到它的文档,否则您需要测试精确的语义)