c++ - Error reporting in Boost Spirit -
i have following error handler @ bottom of parser grammar:
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 );
the problem parser's input isn't broken-up new lines beforehand, resulting error statement all lines in source code error point until end. there straightforward alternative
phoenix::construct<std::string>(spirit::_3, spirit::_2)
to print 1 line error occurs on? phoenix semantics giving me trouble if try search '\n'
.
we need create phoenix function can take spirit parameters.
// lazy function error reporting struct reporterror { // result type must explicit phoenix template<typename, typename, typename, typename> struct result { typedef void type; }; // contract string 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 " << << std::endl << error_line << std::endl << std::setw(error_pos) << '^' << std::endl; } }; const phoenix::function<reporterror> report_error = reporterror();
then invoke function in error handler.
qi::on_error<qi::fail>( launch, report_error(spirit::_1, spirit::_2, spirit::_3, spirit::_4) );
Comments
Post a Comment