How to throw an expectation_failure from a function in Boost Spirit?
Asked Answered
L

2

6

In Boost::Spirit, how can I trigger an expectation_failure from a function bound with Boost::Bind?

Background: I parse a large file that contains complex entries. When an entry is inconsistent with a previous entry I want to fail and throw an expectation_failure (containing proper parse position information). When I parse an entry I bind a function that decides if the entry is inconsistent with something seen before.

I made up a little toy example that shows the point. Here I simply want to throw an expectation_failure when the int is not divisible by 10:

#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;
}

Throwing an expectation_failure would mean that I get an error message like this on an int that is not divisible by 10:

parse error at file <string> line 1 column 2
'11'
  ^- here
Lori answered 22/5, 2012 at 17:2 Comment(3)
Can you create another rule instead of int_, which would match integer only when your condition is satisfied? I don't know Spirit well, but I would assume there is a rule similar to r_bool in AXE which wraps a predicate, it's quite common situation.Schoenfelder
I think I'll need something like this, unfortunately: boost-spirit.com/home/articles/qi-example/…Lori
Sorry to see that, it's very user unfriendly. That's why you need AXE :-)Schoenfelder
B
6

You can use the _pass placeholder in phoenix to enforce a parse fail. Something like this should work.

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

...
_int [ _pass = phoenix::bind(myfunc,_1)] 
Boylan answered 22/5, 2012 at 17:56 Comment(1)
@Lori please note, that setting _pass to false will only stop the current rule but not the parser as a whole with all the other rules (aka your grammar).Hackneyed
L
2

Years late but anyway:

If you absolutely want to throw an exception and want the on_error to catch it, you have to throw the expectation_exception from the qi namespace because the error handler on_error does catch nothing else.

This might apply to a semantic action or a custom parser implementation.

Would look like:

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

where Exception is a qi::expactation_exception and nothing else.

If you do not have a component at hand like in a semantic action you must provide your own qi::info object instead of component.what(..).

You can throw from everywhere inside a context guarded by on_error.

Lori answered 4/6, 2016 at 4:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.