Fix some yield expressions not recognized #3714
Merged
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.
Existing parser fails to recognize some yield expressions. Especially yielding the result of a method call or the result of a conditional expression.
E.g. the following snippet currently fails:
The problem I found is that yield is not a keyword but just a restricted identifier or since Java 17 called contextual keyword. Javaparser currently tries to parse the statement as
VariableDeclarationExpression
.This pull request is a minimal change to fix the problem of yield statements not recognized. For a better solution I think javaparser would need to better different between Identifier and TypeIdentifier according to https://docs.oracle.com/javase/specs/jls/se17/html/jls-3.html#jls-3.8
Then the
VariableDeclarationExpression
would start with a type which cannot be the name "yield".If a class could be named "yield" a statement like
yield x = null;
is ambiguous and could be a local variable declaration plus initialization or a yield statement whose expression is an assignment.But I expect this to be a notably larger and riskier change.
PS: if someone wants to try the
SwitchExprTest.yieldYield
example, the following code compiles: