Boost.Spirit X3中的错误处理和注释

Ori*_*ent 5 c++ boost-spirit-qi boost-spirit-x3

使用boost::spirit::x3::position_tagged某些AST节点的基类(如何选择应标记哪些,例如用于类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];
// ...
Run Code Online (Sandbox Code Playgroud)

如果输入错误(语法不匹配),这部分代码什么都不做(即使是最简单的语法,应该识别标识符) - 不打印错误消息.

And*_*dré -1

语法正确的解析并不意味着 AST 也能被成功评估。考虑一个语法,它定义了一些可以评估数学表达式(如“3+4/(5-5)”)的评估器。它会很好地解析,但在 AST 评估期间,您可能希望在“5-5”上引发错误作为除法的参数。为此,获得您所抱怨的元素的位置会很方便。