【问题标题】:Error reporting in Boost SpiritBoost Spirit 中的错误报告
【发布时间】:2013-08-12 17:34:44
【问题描述】:

我的解析器语法底部有以下错误处理程序:

qi::on_error<qi::fail>(
    launch,
    std::cerr << phoenix::val("Paring error in ") << spirit::_4 << std::endl
              << phoenix::construct<std::string>(spirit::_3, spirit::_2)
              << std::endl
    );

问题在于解析器的输入事先没有被新行分解,因此生成的错误语句将是源代码中从错误点到结尾的所有行。有没有直接的替代方法

phoenix::construct<std::string>(spirit::_3, spirit::_2)

只打印发生错误的那一行?如果我尝试仅搜索 '\n',Phoenix 语义会给我带来麻烦。

【问题讨论】:

    标签: c++ boost-spirit boost-spirit-qi


    【解决方案1】:

    我们需要创建一个可以接受 Spirit 参数的 Phoenix 函数。

    // lazy function for error reporting
    struct ReportError {
      // the result type must be explicit for Phoenix
      template<typename, typename, typename, typename>
      struct result { typedef void type; };
    
      // contract the string to the surrounding new-line characters
      template<typename Iter>
      void operator()(Iter first_iter, Iter last_iter,
                      Iter error_iter, const qi::info& what) const {
        std::string first(first_iter, error_iter);
        std::string last(error_iter, last_iter);
        auto first_pos = first.rfind('\n');
        auto last_pos = last.find('\n');
        auto error_line = ((first_pos == std::string::npos) ? first
                            : std::string(first, first_pos + 1))
                          + std::string(last, 0, last_pos);
        auto error_pos = (error_iter - first_iter) + 1;
        if (first_pos != std::string::npos) {
          error_pos -= (first_pos + 1);
        }
        std::cerr << "Parsing error in " << what << std::endl
                  << error_line << std::endl
                  << std::setw(error_pos) << '^'
                  << std::endl;
      }
    };
    
    const phoenix::function<ReportError> report_error = ReportError();
    

    然后我们只需在错误处理程序中调用这个函数。

    qi::on_error<qi::fail>(
        launch,
        report_error(spirit::_1, spirit::_2, spirit::_3, spirit::_4)
        );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-17
      • 2019-07-03
      • 1970-01-01
      相关资源
      最近更新 更多