Richer error message for boost :: spirit :: qi parsing
I use the spirit of Boost to parse what is essentially a mathematical expression (some text in m_formula
that is std::string
)
I have installed
double value;
auto first = m_formula.begin();
auto last = m_formula.end();
then for grammar grammar
, I parse m_formula
:
boost::spirit::qi::phrase_parse(first, last, grammar, ascii::space, value);
I currently have
if (first != last){
/*ToDo - display "invalid formula " + m_formula*/
}
Is there a way to improve error handling, like letting me know which bit of the formula caused the parser to crash?
source to share
You can use waitpoints that will throw exceptions qi::expectation_failure<It>
.
They contain information about which rule failed (and the original iterators point to the start of the match for that parser expression, as well as the throw point).
qi::on_error
is a mechanism for handling these expectations in your grammar (in case you don't want to catch them externally).
Now, if your input is multi-line, you can keep track of the input row / column information. line_pos_iterator
does it. If you do, look at the directive repository::qi::iter_pos
to get information about the rows / columns that appear as attributes in your rules.
I'll leave that for now: you can search for boost-spirit with any of the above keywords for samples if you like.
source to share