Fix grammar ambiguities causing crashes when using assert and module as names#4929
Merged
johannescoetzee merged 2 commits intojavaparser:masterfrom Dec 18, 2025
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #4929 +/- ##
===============================================
+ Coverage 58.445% 58.453% +0.007%
Complexity 2560 2560
===============================================
Files 689 689
Lines 39553 39553
Branches 7176 7176
===============================================
+ Hits 23117 23120 +3
+ Misses 13497 13494 -3
Partials 2939 2939
Flags with carried forward coverage won't be shown. Click here to find out more. Continue to review full report in Codecov by Sentry.
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4928
There are two different bugs here, but I decided to include them in one issue/PR since the fix for both is quite simple and they are related.
Problem 1
In Java < 1.4,
assertis not yet a keyword and can therefore be used as a regular identifier. There is an ambiguity in the grammar that causes a parse error when attempting to parse the statement:assert++;It is very unlikely that any JavaParser user would encounter this error in practice given the fact that this is only valid code for Java 1-3, but I don't see any potential negative consequences for the fix.
The parser currently tries to parse this as an
assertstatement followed by aPREFIX_INCREMENTUnaryExpr. Parsing the UnaryExpr then fails since there is no expression following the++.This is fixed by adding a lookahead for the
assertstatement to check whether the++is followed by an expression or not.Problem 2
moduleis a conditional keyword that can still be used as a package name. #4910 introduced a new grammar ambiguity causing a parse error when attempting to parse an import statement for a package starting withmodule:The parser attempts to parse this as a module import
import module .Fooand crashes when attempting to parse the name since a name cannot start with..The fix is to consider two cases separately:
moduletoken followed by a nameOnce these are separate cases, a lookahead can be used to figure out which of these should be applied