From 7650813bb72f5a0f197d9ec23d2e82d083bb4827 Mon Sep 17 00:00:00 2001 From: Ruben Date: Thu, 6 Jun 2024 12:06:28 +0200 Subject: [PATCH] feat: changes in Grammar and Parser so typeless Recs get recognised --- resources/bytecode/javFiles/SwitchInfered.jav | 17 +++++++++++++++++ .../dhbwstuttgart/parser/antlr/Java17Parser.g4 | 6 ++++-- .../SyntaxTreeGenerator/StatementGenerator.java | 2 +- src/test/java/TestComplete.java | 9 +++++++++ 4 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 resources/bytecode/javFiles/SwitchInfered.jav diff --git a/resources/bytecode/javFiles/SwitchInfered.jav b/resources/bytecode/javFiles/SwitchInfered.jav new file mode 100644 index 00000000..d11c760d --- /dev/null +++ b/resources/bytecode/javFiles/SwitchInfered.jav @@ -0,0 +1,17 @@ +import java.lang.Integer; +import java.lang.Object; +import java.lang.Float; + +public record Rec(Integer a, Object b) {} + +public class SwitchInfered { + public main(o) { + return switch (o) { + case Rec(a, b) -> a + b; + case Rec(Integer a, Float b) -> a + 10; + case Rec(Integer a, Rec(Integer b, Integer c)) -> a + b + c; + case Integer i -> i; + default -> 0; + }; + } +} \ No newline at end of file diff --git a/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 b/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 index be87c4d7..891c47b3 100644 --- a/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 +++ b/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 @@ -658,10 +658,12 @@ primaryPattern recordPattern : typeType recordStructurePattern identifier? + //| recordStructurePattern identifier? ; typePattern : variableModifier* typeType identifier + | variableModifier* identifier ; recordStructurePattern @@ -717,10 +719,10 @@ switchLabeledRule ; switchLabelCase - : CASE expressionList (ARROW | COLON) #labeledRuleExprList - | CASE NULL_LITERAL (ARROW | COLON) #labeledRuleNull + : CASE NULL_LITERAL (ARROW | COLON) #labeledRuleNull | CASE pattern (ARROW | COLON) #labeledRulePattern | DEFAULT (ARROW | COLON) #labeledRuleDefault + | CASE expressionList (ARROW | COLON) #labeledRuleExprList ; // Java20 diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index f4587b6b..11716318 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -457,7 +457,7 @@ public class StatementGenerator { case TPatternContext tPattern: TypePatternContext typePattern = tPattern.typePattern(); var text = typePattern.identifier().getText(); - var type = TypeGenerator.convert(typePattern.typeType(), reg, generics); + var type = typePattern.typeType() == null ? TypePlaceholder.fresh(typePattern.getStart()) : TypeGenerator.convert(typePattern.typeType(), reg, generics); localVars.put(text, type); return new FormalParameter(text, type, typePattern.getStart()); case RPatternContext rPattern: diff --git a/src/test/java/TestComplete.java b/src/test/java/TestComplete.java index fd4b8b5e..9d318ba6 100644 --- a/src/test/java/TestComplete.java +++ b/src/test/java/TestComplete.java @@ -673,6 +673,15 @@ public class TestComplete { assertEquals(swtch.invoke(instance, "Some string"), 0); } + @Ignore("Not implemented") + @Test + public void testSwitchInfered() throws Exception { + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "SwitchInfered.jav"); + var clazz = classFiles.get("SwitchInfered"); + var instance = clazz.getDeclaredConstructor().newInstance(); + var swtch = clazz.getDeclaredMethod("main", Object.class); + } + @Ignore("Not implemented") @Test public void testSwitch2() throws Exception {