boostorg / spirit

Boost.org spirit module
http://boost.org/libs/spirit
391 stars 161 forks source link

provide example for the on_success() handler #738

Open doj opened 2 years ago

doj commented 2 years ago

https://www.boost.org/doc/libs/1_80_0/libs/spirit/doc/html/spirit/qi/tutorials/mini_xml___error_handling.html mentions the on_success() handler, but the handler is not used in the example shown on this page. It is also not used in the full example source code https://www.boost.org/doc/libs/1_80_0/libs/spirit/example/qi/mini_xml3.cpp

It will be useful to show the on_success() handler in this example, ideally how the iterator parameters can be used to calculate line and column numbers of the input stream.

Kojoley commented 2 years ago

Documentation improvements are highly appreciated!

Note that Qi is in maintenance mode, it is preferred to write new parsers with X3.

djowel commented 2 years ago

Documentation improvements are highly appreciated!

I second that.

drexlerd commented 11 months ago

Edit: it is also possible return fail in the on_success callback via _pass(context) = false.

I also would like to see more examples, especially when parsing context-sensitive languages. I am parsing a context-sensitive language and therefore, I would need to reject successful parses depending on what is stored in the Context. My approach would be to provide template specializations of the on_success callback for specific ast nodes and then throw an expectation_failure if I want to reject. Is this a reasonable approach when parsing a context-sensitive language with x3?

cppljevans commented 10 months ago

Edit: it is also possible return fail in the on_success callback via _pass(context) = false.

I also would like to see more examples, especially when parsing context-sensitive languages. I am parsing a context-sensitive language and therefore, I would need to reject successful parses depending on what is stored in the Context. My approach would be to provide template specializations of the on_success callback for specific ast nodes and then throw an expectation_failure if I want to reject. Is this a reasonable approach when parsing a context-sensitive language with x3?

This example shows 2 methods. Which method is decided by defined(USE_SEMANTIC_ACTIONS) preprocessor macro. When defined, it uses semantic actions which obscure the grammar with the semantic action expressions. When !defined, it uses a single line expression, as_attr, in the Parser grammar which delegates the handling of the context sensitivity to the transform_attribute_tagged which is much cleaner. Maybe you could throw the expectation_failure in the [post function]

OTOH this says symbol tables are very useful for context sensitive parsers and semantic actions are one way to update the symbol table, hence, maybe the above suggested use of as_attr instead of semantic actions wouldn't work too well.

On the Other Other Hand, symbol tables are usually passed as inherited attributes, according to this, and x3 does not have inherited attributes. I'd assume inherited attribute could be simulated by storing the symbol table in the context.