Skip to content

Fix some yield expressions not recognized #3714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.stmt.BlockStmt;
import com.github.javaparser.ast.stmt.SwitchEntry;
import org.junit.jupiter.api.Disabled;
import com.github.javaparser.ast.stmt.SwitchStmt;

import org.junit.jupiter.api.Test;

import static com.github.javaparser.ast.stmt.SwitchEntry.Type.*;
Expand Down Expand Up @@ -116,4 +117,99 @@ void jep325Example6() {
" yield 0;\n" +
"};");
}

@Test
void yieldMethodCall() {
parseStatement("int randomNumber = switch (5) {\n" +
" default -> {\n" +
" yield a.randomNumberGenerator();\n" +
" }\n" +
" case 1 -> {\n" +
" yield method();\n" +
" }\n" +
" case 2 -> {\n" +
" yield method(args);\n" +
" }\n" +
" case 3 -> {\n" +
" yield this.method();\n" +
" }\n" +
" case 4 -> {\n" +
" yield Clazz.this.method(args);\n" +
" }\n" +
"};");
}

@Test
void yieldExpression1() {
parseStatement("int randomNumber = switch (5) {\n" +
" default -> {\n" +
" yield 1 * 1;\n" +
" }\n" +
" case 1 -> {\n" +
" yield (5 + 5);\n" +
" }\n" +
" case 2 -> {\n" +
" yield (5 + 5) * 3;\n" +
" }\n" +
"};");
}

@Test
void yieldExpression2() {
parseStatement("boolean b = switch (5) {\n" +
" case 3 -> {\n" +
" yield true || false;\n" +
" }\n" +
" default -> {\n" +
" yield !true;\n" +
" }\n" +
"};");
}

@Test
void yieldAssignment() {
parseStatement("int randomNumber = switch (5) {\n" +
" default -> {\n" +
" int x;\n" +
" yield (x = 5);\n" +
" }\n" +
" case 'a' -> {\n" +
" int x;\n" +
" yield x = 3;\n" +
" }\n" +
"};");
}

@Test
void yieldConditional() {
parseStatement("int randomNumber = switch (5) {\n" +
" default -> {\n" +
" yield x ? 1 : 2;\n" +
" }\n" +
" case 1 -> {\n" +
" yield (x ? 1 : 2);\n" +
" }\n" +
" case 2 -> {\n" +
" yield x < 0 ? 0 : x > y ? y : x;\n" +
" }\n" +
"};");
}

@Test
void yieldYield() {
parseStatement("yield = switch (yield) {\n" +
" default -> {\n" +
" yield yield;\n" +
" }\n" +
" case yield -> {\n" +
" yield Clazz.yield();\n" +
" }\n" +
" case enumValue2 -> {\n" +
" yield yield = yield;\n" +
" }\n" +
" case enumValue3 -> {\n" +
" yield yield == yield ? yield : yield;\n" +
" }\n" +
"};");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.BinaryExpr;
import com.github.javaparser.ast.expr.ConditionalExpr;
import com.github.javaparser.ast.expr.EnclosedExpr;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.expr.NameExpr;

import org.junit.jupiter.api.Test;

import static com.github.javaparser.ParserConfiguration.LanguageLevel.JAVA_12;
Expand All @@ -38,6 +43,48 @@ void yield() {
assertEquals(BinaryExpr.class, statement.getExpression().getClass());
}

@Test
void yield2() {
YieldStmt statement;
statement = parseStatement("yield (2 + 2);").asYieldStmt();
assertEquals(EnclosedExpr.class, statement.getExpression().getClass());
statement = parseStatement("yield ((2 + 2) * 3);").asYieldStmt();
assertEquals(EnclosedExpr.class, statement.getExpression().getClass());
}

@Test
void yieldMethodCall() {
YieldStmt statement;
statement = parseStatement("yield a();").asYieldStmt();
assertEquals(MethodCallExpr.class, statement.getExpression().getClass());
statement = parseStatement("yield a(5, arg);").asYieldStmt();
assertEquals(MethodCallExpr.class, statement.getExpression().getClass());
statement = parseStatement("yield a.b();").asYieldStmt();
assertEquals(MethodCallExpr.class, statement.getExpression().getClass());
statement = parseStatement("yield a.b(5, arg);").asYieldStmt();
assertEquals(MethodCallExpr.class, statement.getExpression().getClass());
statement = parseStatement("yield this.b();").asYieldStmt();
assertEquals(MethodCallExpr.class, statement.getExpression().getClass());
statement = parseStatement("yield this.b(5, arg);").asYieldStmt();
assertEquals(MethodCallExpr.class, statement.getExpression().getClass());
statement = parseStatement("yield Clazz.this.b();").asYieldStmt();
assertEquals(MethodCallExpr.class, statement.getExpression().getClass());
statement = parseStatement("yield Clazz.this.b(5, arg);").asYieldStmt();
assertEquals(MethodCallExpr.class, statement.getExpression().getClass());
}

@Test
void yieldAssignment() {
YieldStmt statement = parseStatement("yield (x = 5);").asYieldStmt();
assertEquals(EnclosedExpr.class, statement.getExpression().getClass());
}

@Test
void yieldConditional() {
YieldStmt statement = parseStatement("yield x ? 5 : 6;").asYieldStmt();
assertEquals(ConditionalExpr.class, statement.getExpression().getClass());
}

@Test
void threadYieldShouldNotBreak() {
parseStatement("Thread.yield();").asExpressionStmt().getExpression().asMethodCallExpr();
Expand All @@ -53,4 +100,24 @@ void keywordShouldNotInterfereWithIdentifiers() {
" }\n" +
"}\n", compilationUnit.toString());
}

@Test
void keywordShouldNotInterfereWithIdentifiers2() {
CompilationUnit compilationUnit = parseCompilationUnit("enum X { yield, }");
assertEqualsStringIgnoringEol("enum X {\n" +
"\n" +
" yield\n" +
"}\n", compilationUnit.toString());
}

@Test
void keywordShouldNotInterfereWithIdentifiers3() {
YieldStmt statement;
statement = parseStatement("yield yield;").asYieldStmt();
assertEquals(NameExpr.class, statement.getExpression().getClass());
statement = parseStatement("yield Clazz.yield();").asYieldStmt();
assertEquals(MethodCallExpr.class, statement.getExpression().getClass());
statement = parseStatement("yield yield.yield();").asYieldStmt();
assertEquals(MethodCallExpr.class, statement.getExpression().getClass());
}
}
5 changes: 5 additions & 0 deletions javaparser-core/src/main/javacc/java.jj
Original file line number Diff line number Diff line change
Expand Up @@ -4312,6 +4312,11 @@ Statement BlockStatement():
LOOKAHEAD( Modifiers() "record" SimpleName() [ TypeParameters() ] Parameters() )
modifier = Modifiers()
recordDeclaration = RecordDeclaration(modifier) { ret = new LocalRecordDeclarationStmt(range(recordDeclaration, token()), recordDeclaration); }
|
// try yield statement separate from more general Statement() because yield is not a keyword but
// just a restricted identifier and a yield statement can be confused with VariableDeclarationExpression sometimes
LOOKAHEAD( YieldStatement() )
ret = YieldStatement()
|
LOOKAHEAD( VariableDeclarationExpression() )
expr = VariableDeclarationExpression()
Expand Down