用作某些 AST 节点的基类的逻辑是什么boost::spirit::x3::position_tagged
(如何选择应该标记的节点,例如对于类 C 语言?)以及规则 ID 定义中使用的其他结构,例如:
struct error_handler_tag;
struct error_handler_base
{
template< typename Iterator, typename Exception, typename Context >
x3::error_handler_result
on_error(Iterator & /*first*/, Iterator const & /*last*/,
Exception const & x, Context const & context)
{
std::string message_ = "Error! Expecting: " + x.which() + " here:";
auto & error_handler = x3::get< error_handler_tag >(context).get();
error_handler(x.where(), message_);
return x3::error_handler_result::fail;
}
};
struct annotation_base
{
template< typename T, typename Iterator, typename Context >
void
on_success(Iterator const & first, Iterator const & last,
T & ast, Context const & context)
{
auto & error_handler = x3::get< error_handler_tag >(context).get();
error_handler.tag(ast, first, last);
}
};
// ...
error_handler_type error_handler(beg, end, std::cerr);
auto const parser_ = x3::with< error_handler_tag >(std::ref(error_handler))[grammar];
// ...
?
如果输入错误(语法不匹配),这部分代码什么也不做(即使是最简单的语法,它应该识别标识符) - 不打印错误消息。