Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost.Spirit.Qi - Errors at the beginning of a rule

How would I detect an error at the start of a rule? For example, consider the Mini XML example included in the docs. If I feed the parser something like:

<element>this is an error<element>

Then I get:

Error! Expecting here: ""

Error! Expecting here: ""

Parsing failed.

That's fine, but then consider feeding it:

element>this is an error</element>

And I get the very generic and not so useful:

Parsing failed.

How could I modify the rule to report the error in an informative way?

like image 980
Addy Avatar asked Feb 16 '23 19:02

Addy


1 Answers

You'd want to require an element at the document root level.

The other messages are generated by failed expectation points. You'll want an extra expectation point at the start. I'd do this:

  1. rename old xml rule to element
  2. create a new xml rule that has the element at an expectation point:

        xml = qi::eps > element;
    
  3. [Don't change anything else]

  4. profit!

The output becomes:

Error! Expecting <element> here: "element>this is a test</element>
"
-------------------------
Parsing failed
-------------------------

See it live here

like image 135
sehe Avatar answered Feb 19 '23 10:02

sehe