Skip to content

Fix grammar ambiguities causing crashes when using assert and module as names#4929

Merged
johannescoetzee merged 2 commits intojavaparser:masterfrom
johannescoetzee:johannes/fix-grammar-ambiguities
Dec 18, 2025
Merged

Fix grammar ambiguities causing crashes when using assert and module as names#4929
johannescoetzee merged 2 commits intojavaparser:masterfrom
johannescoetzee:johannes/fix-grammar-ambiguities

Conversation

@johannescoetzee
Copy link
Copy Markdown
Collaborator

@johannescoetzee johannescoetzee commented Dec 18, 2025

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, assert is 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 assert statement followed by a PREFIX_INCREMENT UnaryExpr. Parsing the UnaryExpr then fails since there is no expression following the ++.

This is fixed by adding a lookahead for the assert statement to check whether the ++ is followed by an expression or not.

Problem 2

module is 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 with module:

import module.Foo

The parser attempts to parse this as a module import import module .Foo and crashes when attempting to parse the name since a name cannot start with ..

The fix is to consider two cases separately:

  • either there is a module token followed by a name
  • or there is just a name

Once these are separate cases, a lookahead can be used to figure out which of these should be applied

@johannescoetzee johannescoetzee added the PR: Fixed A PR that offers a fix or correction label Dec 18, 2025
@johannescoetzee johannescoetzee added this to the next release milestone Dec 18, 2025
@codecov
Copy link
Copy Markdown

codecov bot commented Dec 18, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 58.453%. Comparing base (0e6f25b) to head (7e650e8).
⚠️ Report is 6 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@               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               
Flag Coverage Δ
AlsoSlowTests 58.453% <ø> (+0.007%) ⬆️
javaparser-core 58.453% <ø> (+0.007%) ⬆️
javaparser-symbol-solver 58.453% <ø> (+0.007%) ⬆️
jdk-10 58.019% <ø> (+0.010%) ⬆️
jdk-11 58.016% <ø> (+0.007%) ⬆️
jdk-12 58.018% <ø> (+0.010%) ⬆️
jdk-13 58.016% <ø> (+0.005%) ⬆️
jdk-14 58.251% <ø> (+0.005%) ⬆️
jdk-15 58.253% <ø> (+0.007%) ⬆️
jdk-16 58.228% <ø> (+0.007%) ⬆️
jdk-17 58.380% <ø> (+0.007%) ⬆️
jdk-18 58.380% <ø> (+0.007%) ⬆️
jdk-8 57.861% <ø> (+0.007%) ⬆️
jdk-9 58.014% <ø> (+0.007%) ⬆️
macos-latest 58.445% <ø> (+0.007%) ⬆️
ubuntu-latest 58.440% <ø> (+0.007%) ⬆️
windows-latest 58.435% <ø> (+0.007%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.
see 3 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c798167...7e650e8. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@johannescoetzee johannescoetzee merged commit 2186f21 into javaparser:master Dec 18, 2025
35 checks passed
@johannescoetzee johannescoetzee deleted the johannes/fix-grammar-ambiguities branch December 18, 2025 10:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: Fixed A PR that offers a fix or correction

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Grammar ambiguities causing crashes when using assert and module as identifiers

1 participant