[xpressive] how to see how much of one's input that matched

Hi Eric, I'm trying to build a parser, this time with boost.xpressive instead of using boost.spirit. In spirit I have this function that displays when parsing failed: template< class ParseInfo > void printParsingError( boost::sub_range<std::string> range, const ParseInfo& info ) { std::cerr << "\n\n" << "Parsing of file failed. The parser matched:\n\n" << "-------------------------------------------\n[start]"; std::cerr << std::string( range.begin(), info.stop ); std::cerr << "[stop]\n"; std::cerr << "\nThe input was:\n\n" << "-------------------------------------------\n[start]"; std::cerr << std::string( range.begin(), range.end() ); std::cerr << "[stop]\n"; std::cerr << std::endl; } Can I get the same info with xpressive somehow? Thanks -Thorsten

Thorsten Ottosen wrote:
I'm trying to build a parser, this time with boost.xpressive instead of using boost.spirit.
In spirit I have this function that displays when parsing failed: <snip> Can I get the same info with xpressive somehow?
Not directly, no. One possibility is to define an iterator adaptor that keeps track of the farthest position to which it's incremented. You can then use that iterator with xpressive and find out how much of the string gets eaten before the match fails. Another possibility is to insert into your grammar strategically placed actions that throw the current position on parse failures. Something like ... // untested! struct parse_failure : std::exception { parse_failure(std::string::const_iterator pos) { ... } }; sregex ARule = this | that // this executes on parse failure | nil [ throw_<parse_failure>(second(_)) ]; -- Eric Niebler Boost Consulting www.boost-consulting.com
participants (2)
-
Eric Niebler
-
Thorsten Ottosen