【问题标题】:How to throw an expectation_failure from a function in Boost Spirit?如何从 Boost Spirit 中的函数中抛出一个expectation_failure?
【发布时间】:2012-05-29 05:12:39
【问题描述】:

在 Boost::Spirit 中,如何从与 Boost::Bind 绑定的函数中触发 expectation_failure

背景:我解析一个包含复杂条目的大文件。当一个条目与之前的条目不一致时,我想失败并抛出一个expectation_failure(包含正确的解析位置信息)。当我解析一个条目时,我绑定了一个函数,该函数决定该条目是否与之前看到的内容不一致。

我编了一个小玩具示例来说明这一点。在这里,当int 不能被 10 整除时,我只想抛出一个 expectation_failure

#include <iostream>
#include <iomanip>
#include <boost/spirit/include/qi.hpp>
#include <boost/bind.hpp>
#include <boost/spirit/include/classic_position_iterator.hpp>
namespace qi = boost::spirit::qi;
namespace classic = boost::spirit::classic;

void checkNum(int const& i) {
  if (i % 10 != 0) // >> How to throw proper expectation_failure? <<
    std::cerr << "ERROR: Number check failed" << std::endl;
}

template <typename Iterator, typename Skipper>
struct MyGrammar : qi::grammar<Iterator, int(), Skipper> {
  MyGrammar() : MyGrammar::base_type(start) {
    start %= qi::eps > qi::int_[boost::bind(&checkNum, _1)];
  }
  qi::rule<Iterator, int(), Skipper> start;
};

template<class PosIter>
std::string errorMsg(PosIter const& iter) {
  const classic::file_position_base<std::string>& pos = iter.get_position();
  std::stringstream msg;
  msg << "parse error at file " << pos.file
      << " line " << pos.line << " column " << pos.column << std::endl
      << "'" << iter.get_currentline() << "'" << std::endl
      << std::setw(pos.column) << " " << "^- here";
  return msg.str();
}

int main() {
  std::string in = "11";
  typedef std::string::const_iterator Iter;
  typedef classic::position_iterator2<Iter> PosIter;
  MyGrammar<PosIter, qi::space_type> grm;
  int i;
  PosIter it(in.begin(), in.end(), "<string>");
  PosIter end;
  try {
    qi::phrase_parse(it, end, grm, qi::space, i);
    if (it != end)
      throw std::runtime_error(errorMsg(it));
  } catch(const qi::expectation_failure<PosIter>& e) {
    throw std::runtime_error(errorMsg(e.first));
  }
  return 0;
}

抛出expectation_failure 意味着我会在不能被 10 整除的 int 上收到这样的错误消息:

parse error at file <string> line 1 column 2
'11'
  ^- here

【问题讨论】:

  • 你可以创建另一个规则而不是 int_,它只在你的条件满足时才匹配整数?我不太了解 Spirit,但我认为 AX 中有一个类似于 r_bool 的规则,它包含一个谓词,这是很常见的情况。
  • 不幸的是,我想我需要这样的东西:boost-spirit.com/home/articles/qi-example/…
  • 很抱歉,这对用户非常不友好。这就是为什么你需要 AX :-)

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


【解决方案1】:

您可以在 phoenix 中使用 _pass 占位符来强制解析失败。像这样的东西应该工作。

bool myfunc(int i) {return i%10 == 0;}

...
_int [ _pass = phoenix::bind(myfunc,_1)] 

【讨论】:

  • @Frank 请注意,将 _pass 设置为 false 只会停止当前规则,但不会停止整个解析器以及所有其他规则(也就是您的语法)。
【解决方案2】:

晚了几年,但无论如何:

如果您绝对想抛出异常并希望 on_error 捕获它,则必须从 qi 命名空间中抛出 expectation_exception,因为错误处理程序 on_error 不会捕获任何其他内容。

这可能适用于语义操作或自定义解析器实现。

看起来像:

boost::throw_exception(Exception(first, last, component.what(context)));

其中Exceptionqi::expactation_exception,仅此而已。

如果您手头没有语义操作中的组件,则必须提供自己的 qi::info 对象而不是 component.what(..)

您可以在on_error 保护的上下文中从任何地方抛出。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    • 2011-04-03
    • 1970-01-01
    • 2019-07-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    相关资源
    最近更新 更多