antlr / antlr/antlr4

Generate Java 17 sealed classes?

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

Description

- [x] I am not submitting a question on how to use ANTLR; instead, go to [antlr4-discussion google group](https://groups.google.com/forum/#!forum/antlr-discussion) or ask at [stackoverflow](http://stackoverflow.com/questions/tagged/antlr4)
- [x] I have done a search of the existing issues to make sure I'm not sending in a duplicate

So for code like this:

```antlr
expr : atom # single
| expr argument+ # app
| NEW_KW expr LBRACE newArg* RBRACE # new
| expr TO expr # arr
| expr projFix # proj
| LSUC_KW atom # lsuc
| LMAX_KW atom+ # lmax
| PI tele+ TO expr # pi
| SIGMA tele+ SUCHTHAT expr # sigma
| LAMBDA tele+ (IMPLIES expr?)? # lam
| MATCH exprList clauses # match
;
```

ANTLR4 generate some abstract 'XXContext` classes as AST representations, extended by a fixed number of subclasses:
![image](https://user-images.githubusercontent.com/16398479/134292365-4d55222a-3eb6-4167-926f-d705c55fdc5c.png)

But with modern versions of Java, we have sealed classes that allow us to _pattern match_ against them. Would you consider adding an option to add a `sealed` modifier to these generated superclasses?

It is very easy to do: just add a modifier `sealed` to the classes with subclasses and you're done :wink:.

Contributor guide

Open the contributing guide

Research direction

The issue identifies ANTLR's Java target and the generated XXContext superclass/subclass hierarchy; start by tracing where those Java classes are emitted. Use the grammar shown to determine how an option could add sealed modifiers only to superclasses with subclasses, and verify the generated Java output.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.