Skip to content

Fix #6655: Deprecate antlr4 generated lexers - #7172

Merged
adangel merged 4 commits into
pmd:mainfrom
UncleOwen:issue-6655-deprecate-antlr4-generated-classes
Nov 12, 2026
Merged

adangel merged 4 commits into
pmd:mainfrom
UncleOwen:issue-6655-deprecate-antlr4-generated-classes

Conversation

@UncleOwen

@UncleOwen UncleOwen commented Oct 8, 2026 •

Copy link
Copy Markdown
Member

Describe the PR

This PR deprecates all antlr4 generated FooLexer, FooParser, FooBaseListener and FooListener classes (where Foo is a language name). It does not deprecate KotlinVisitor and SwiftVisitor, as they are needed to create custom rules.

Related issues

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

@UncleOwen UncleOwen added the is:deprecation The main focus is deprecating public APIs or rules, eg to make them internal, or removing them label Oct 8, 2026
@pmd-actions-helper

pmd-actions-helper Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.

Regression Tester Report

(comment created at 2026-11-12 10:31:57+00:00 for 67ea287)

@stokpop

stokpop commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Thanks for the heads-up! I've been thinking about what this actually means for the pmd-kotlin module.

I assume the intent of keeping KotlinVisitor public is that the node classes it needs stay public as well. However, the public static class replacement doesn't only hit parser internals: in the ANTLR-based languages with a full AST (Kotlin and Swift), all AST node classes are nested in the generated parser. When I generate the sources from this PR, all 174 KotlinParser.KtXxx classes (KtPropertyDeclaration, KtFunctionDeclaration, ...) are deprecated with "will be changed to package-private in PMD-8.0.0", while all visitXxx(KtXxx) methods of KotlinVisitor take these node types. It also touches the public API in pmd-kotlin that builds on them: the Kt*Attributes views, the Has* interfaces and KotlinNodeTypeData.

And even deprecating only the outer KotlinParser class might not be enough: once it is package-private in 8.0, its nested node classes are no longer reachable from outside the package either.

On the other hand, backwards compatibility is probably hardly an issue for Kotlin: there are about 40 XPath-based Kotlin rules, and close to zero Java-based ones I know of. XPath-based rules only depend on node names, not on Java class names. So we have some freedom here.

Just to brainstorm, one possible path could be:

  1. For this PR: exclude the nested node classes from the deprecation, and only deprecate the parser itself (or its internals), the lexer and the listeners.
  2. Before 8.0: add a post-processing step to the antlr4 wrapper that moves the nested node classes into top-level classes in the same package (e.g. net.sourceforge.pmd.lang.kotlin.ast.KtPropertyDeclaration). The parser and lexer could then become package-private, while the node classes still access the parser's constants from the same package. Java-based rules would only need an import change, XPath-based rules and tree dumps would stay the same. This would work the same way for Swift.

There may well be other or better options, e.g. keeping the parser public but marking its internals as internal API, which is less work but leaves more ANTLR details in the public API. What do you think? Happy to open an issue to work this out further.

@UncleOwen

Copy link
Copy Markdown
Member Author

Oops. I completely missed these inner classes in KotlinParser and SwiftParser. I'll need to think more about this, but in the meantime, I'll mark this as draft so it doesn't get merged.

@UncleOwen
UncleOwen marked this pull request as draft October 9, 2026 11:14
@UncleOwen
UncleOwen force-pushed the issue-6655-deprecate-antlr4-generated-classes branch from 6b89bda to c5f18bd Compare November 7, 2026 13:27
@UncleOwen UncleOwen changed the title [multiple languages] Fix #6655: deprecate antlr4 generated classes [multiple languages] Work on #6655: deprecate antlr4 generated classes Nov 7, 2026
@UncleOwen

Copy link
Copy Markdown
Member Author

I've undeprecated KotlinParser and SwiftParser for now.

KotlinParser has >100 inner classes. Extracting those into separate files feels like it's worth a separate PR.

@UncleOwen
UncleOwen marked this pull request as ready for review November 7, 2026 13:35
Update deprecation versions and release notes.
@UncleOwen
UncleOwen force-pushed the issue-6655-deprecate-antlr4-generated-classes branch from c5f18bd to e0fa95d Compare November 7, 2026 13:55
@adangel adangel mentioned this pull request Nov 12, 2026

@adangel adangel left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this.

I'll change the description of #6655 to only concentrate on lexer and leave out parser.

In general: public API would be

  • net.sourceforge.pmd.lang.swift.ast.PmdSwiftParser and all the AST nodes (which are in reality not AST nodes but parse rule context). The parser itself (net.sourceforge.pmd.lang.swift.ast.SwiftParser) would be package-private.
    As it is right now, we shouldn't deprecate this outer class, as it creates a mess with the inner classes (we would get a deprecation warning depending on IDE although technically only the outer class would be deprecated).
    • Update: the generated visitor classes are public API as well, as they are used for writing rules
  • net.sourceforge.pmd.lang.swift.cpd.SwiftCpdLexer and the implementation net.sourceforge.pmd.lang.swift.ast.SwiftLexer is package-private. This PR solves that by deprecating it.

For the parser: Moving the inner classes out will be a breaking, incompatible change. Can't do for PMD 7. We might do it for PMD 8, if it is worth (added to #5703). Note, that this circles back to the original problem, that antlr does not generate an AST, but a parse tree (see #4337).

@adangel adangel changed the title [multiple languages] Work on #6655: deprecate antlr4 generated classes Fix #6655: Deprecate antlr4 generated lexers Nov 12, 2026
@adangel adangel added this to the 7.30.0 milestone Nov 12, 2026
Comment thread docs/pages/release_notes.md Resolved
@adangel
adangel merged commit 32c878b into pmd:main Nov 12, 2026
2 checks passed
@UncleOwen

Copy link
Copy Markdown
Member Author

Should we create a follow-up issue for what is left to do?

@adangel

adangel commented Nov 12, 2026

Copy link
Copy Markdown
Member

Should we create a follow-up issue for what is left to do?

It's already tracked in #5703.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

is:deprecation The main focus is deprecating public APIs or rules, eg to make them internal, or removing them

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deprecate ANTLR generated lexer implementation classes

3 participants