-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Java Provide default implementations for ParseTreeListener exit methods and others #3986
Comments
May I ask why you're not using the generated BaseParserListener ? |
I'm a bit skeptical re your proposal to make enter methods mandatory while making exit ones optional. Many developers, including myself override exit methods and do not care about enter methods. As a library provider, not sure we can favor half of the community against the other half ? |
The problem with the base listener is it doesn't provide type safety when another team modifies the grammar by adding a new rule. It makes it possible for other implementations to silently do nothing without other teams realizing it. This causes code to break in ways that are difficult to track down at scale. What we want is for a compile error to occur until all other teams explicitly handle the new rule. But since you've brought it up, it seems like many people using ANTLR only care about half of the listener methods. Would any of these alternatives be feasible?
|
"It makes it possible for other implementations to silently do nothing without other teams realizing it" |
I think there is a misunderstanding here. If someone extends the base listener there is no requirement for anyone to @OverRide any new added method because, well, it is already inherited with an empty implementation. This is where the silent failures can get introduced. The problem with using the interface instead is that roughly half of all generated methods are no-ops and are forced to be given empty implementations. From a design point of view this is undesirable because it violates the Interface Segregation Principle. It's fairly safe to make this claim when most users of ANTLR (including both of us) are outright ignoring half of the methods because we don't use them. In my experience this has been somewhat egregious because there are often dozens of such rules being given empty implementations. While I have no problem taking an alternative route (thank you for proposing the feasibility of using a post-processor) it doesn't feel like a strength when half of the generated code is being ignored by the community on a regular basis. I'm not looking to persuade you on this point, just highlighting that it is a pain point we all deal with. If this still doesn't seem like a good fit for the library feel free to close the issue and I'll pursue one of the alternative solutions. Otherwise I'm happy to continue the discussion if it's deemed productive. |
Am I correct in observing that you have an unusual setup, where grammar producers and grammar consumers sit in different teams ? |
You are correct. The producers and consumers sit in different teams. If @parrt feels like it is a worthwhile change I'm happy to to make the changes and write tests for the language targets. The modifications to the stg files seem like they'd be simple enough anyway. |
Hi @incident-recipient if this is an internal google thing, perhaps contact me inside the wall and you can tell me more specifically what's going on with the project. I'm nervous about changing this interface, particularly because it would require a change across nine other languages. It's getting very hard for me to keep up with all of the support, given the explosion of languages then tests etc... I wonder if it's a simple matter of providing a Java.stg during the build process via blaze, possibly with a tweet .bzl file from third_party |
Target: Java
Feature Request
We use ANTLR daily to create dozens of small, shared languages for testing large systems. These DSLs help us stay coordinated in a way that would otherwise not be possible.
When using Java we prefer to use the ParseTreeListener because it adds type safety that we need when someone modifies a DSL (because multiple listeners are implemented across different projects for the same grammar).
However we keep finding ourselves providing stubbed implementations for the exit methods. We also occasionally get burned by members forgetting to make visitErrorNode throw an exception.
We propose a middle ground in #3985 that will make it so that all enter methods must be implemented (guaranteeing type safety) while making the implementation for exit methods and most of the standard ParseTreeListener methods optional. We also propose throwing a runtime exception on visitErrorNode by default.
Since ANTLR upgraded to Java 8 we can now use the default keyword in the generated interface to provide this behavior without changing the API.
We feel this will make ANTLR a much more viable tool for cross functional projects.
The text was updated successfully, but these errors were encountered: