antlr / antlr/antlr4

Java Provide default implementations for ParseTreeListener exit methods and others

Open
#3,986 8 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
19k
Forks
3.5k
PR merge metrics
No merged PRs in 30d

Description

**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 https://github.com/antlr/antlr4/pull/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.

Contributor guide

Open the contributing guide

Research direction

Start by reviewing the Java ParseTreeListener API and the referenced pull request 3985, focusing on the proposed default enter, exit, and visitErrorNode behavior. Done means the requested optional implementations and default error handling are provided without changing the API.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
compilers
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.