google / polymorphicDSL

Apache License 2.0
9 stars 7 forks source link

Adjust ANTLR4 to generate a new interface that provides stubbed implementations for rules we don't care about #36

Open incident-recipient opened 2 years ago

incident-recipient commented 2 years ago

For Gherkin-style tests ANTLR4 gives us two choices for the listener: an abstract class with empty implementations for everything or an interface.

The problem with the abstract class is it is too easy for the grammar to change and other clients will not realize they need to provide an implementation. This can lead to false positives.

The interface requires implementing a lot of methods that have no value (such as the exit methods).

It would be great to have a third option that provided an empty implementation for all exit methods as well as any rules that follow some convention (such as a '_' character at the end of the rule name. In this manner the user will only need to provide implementations for the enter methods of consequence and changes will allow other clients to see they need to add new functionality.

incident-recipient commented 1 year ago

I think another useful way to do this would be to have ANTLRs tool generate another java interface that has a default implementation that does nothing for all exit methods and ParseTreeListener methods. This would only force the user to implement the enter methods.

incident-recipient commented 1 year ago

So I made a pull request and issue for the antlr team at https://github.com/antlr/antlr4/issues/3986

However there seems to be mixed feelings.

I have adjusted this framework to support generated visitors which can act as a work around, but I'll keep this issue open until Parr has made a decision on the pull request.