From 27f50b9c6622fd20f9c55aa43d9d14aa33d0c164 Mon Sep 17 00:00:00 2001 From: Purplumbi504 Date: Thu, 27 Jun 2024 21:14:02 +0200 Subject: [PATCH] Adding EmptyClass Test AST --- src/main/java/ast/ClassNode.java | 16 +-- .../java/parser/astBuilder/ASTBuilder.java | 25 +++- .../generated/SimpleJavaBaseListener.java | 2 +- .../generated/SimpleJavaBaseVisitor.java | 2 +- .../parser/generated/SimpleJavaLexer.java | 2 +- .../parser/generated/SimpleJavaListener.java | 2 +- .../parser/generated/SimpleJavaParser.java | 2 +- .../parser/generated/SimpleJavaVisitor.java | 2 +- src/test/java/parser/AstBuilderTest.java | 108 +++++------------- .../resources/input/javaCases/EmptyClass.java | 1 + .../input/javaCases/MultipleClasses.java | 7 ++ 11 files changed, 66 insertions(+), 103 deletions(-) create mode 100644 src/test/resources/input/javaCases/EmptyClass.java create mode 100644 src/test/resources/input/javaCases/MultipleClasses.java diff --git a/src/main/java/ast/ClassNode.java b/src/main/java/ast/ClassNode.java index 63dfed2..61134ab 100644 --- a/src/main/java/ast/ClassNode.java +++ b/src/main/java/ast/ClassNode.java @@ -15,30 +15,20 @@ public class ClassNode implements ASTNode, Visitable { public AccessModifierNode accessType; public String identifier; public List members = new ArrayList<>(); - public boolean hasConstructor; - public ClassNode() {} + public ClassNode(){ + + } public ClassNode(String accessType, String identifier){ this.accessType = new AccessModifierNode(accessType); this.identifier = identifier; - hasConstructor = false; } public void addMember(MemberNode member) { - if (member instanceof ConstructorNode) { - this.hasConstructor = true; - } members.add(member); } - public void ensureConstructor(){ - if(!hasConstructor) { - ConstructorNode constructor = new ConstructorNode(new AccessModifierNode("public"), identifier); - members.addFirst(constructor); - } - } - public List getMethods(){ List methods = new ArrayList<>(); for (MemberNode member : members) { diff --git a/src/main/java/parser/astBuilder/ASTBuilder.java b/src/main/java/parser/astBuilder/ASTBuilder.java index b6d8283..32137eb 100644 --- a/src/main/java/parser/astBuilder/ASTBuilder.java +++ b/src/main/java/parser/astBuilder/ASTBuilder.java @@ -44,11 +44,28 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { @Override public ASTNode visitClassDeclaration(SimpleJavaParser.ClassDeclarationContext ctx) { - ClassNode classNode = new ClassNode(ctx.AccessModifier().getText(), ctx.Identifier().getText()); - for (SimpleJavaParser.MemberDeclarationContext member : ctx.memberDeclaration()) { - classNode.addMember((MemberNode) visit(member)); + ClassNode classNode; + if(ctx.AccessModifier() != null){ + classNode = new ClassNode(ctx.AccessModifier().getText(), ctx.Identifier().getText()); } - classNode.ensureConstructor(); + else{ + classNode = new ClassNode("public", ctx.Identifier().getText()); + } + + boolean hasConstructor = false; + + for (SimpleJavaParser.MemberDeclarationContext member : ctx.memberDeclaration()) { + MemberNode memberNode = (MemberNode) visit(member); + if(memberNode instanceof ConstructorNode){ + hasConstructor = true; + } + classNode.addMember(memberNode); + } + + if(!hasConstructor){ + classNode.members.addFirst(new ConstructorNode(new AccessModifierNode("public"), ctx.Identifier().getText())); + } + return classNode; } diff --git a/src/main/java/parser/generated/SimpleJavaBaseListener.java b/src/main/java/parser/generated/SimpleJavaBaseListener.java index 5cc0d6c..eecc12f 100644 --- a/src/main/java/parser/generated/SimpleJavaBaseListener.java +++ b/src/main/java/parser/generated/SimpleJavaBaseListener.java @@ -1,4 +1,4 @@ -// Generated from C:/Users/Maxi/Documents/DHBW/Compilerbau/NichtHaskell2.0/src/main/java/parser/grammar/SimpleJava.g4 by ANTLR 4.13.1 +// Generated from C:/Users/janni/Desktop/NichtHaskell2.0/src/main/java/parser/grammar/SimpleJava.g4 by ANTLR 4.13.1 package parser.generated; import org.antlr.v4.runtime.ParserRuleContext; diff --git a/src/main/java/parser/generated/SimpleJavaBaseVisitor.java b/src/main/java/parser/generated/SimpleJavaBaseVisitor.java index 97b6b28..b3a6029 100644 --- a/src/main/java/parser/generated/SimpleJavaBaseVisitor.java +++ b/src/main/java/parser/generated/SimpleJavaBaseVisitor.java @@ -1,4 +1,4 @@ -// Generated from C:/Users/Maxi/Documents/DHBW/Compilerbau/NichtHaskell2.0/src/main/java/parser/grammar/SimpleJava.g4 by ANTLR 4.13.1 +// Generated from C:/Users/janni/Desktop/NichtHaskell2.0/src/main/java/parser/grammar/SimpleJava.g4 by ANTLR 4.13.1 package parser.generated; import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor; diff --git a/src/main/java/parser/generated/SimpleJavaLexer.java b/src/main/java/parser/generated/SimpleJavaLexer.java index 51b5bfc..23296ee 100644 --- a/src/main/java/parser/generated/SimpleJavaLexer.java +++ b/src/main/java/parser/generated/SimpleJavaLexer.java @@ -1,4 +1,4 @@ -// Generated from C:/Users/Maxi/Documents/DHBW/Compilerbau/NichtHaskell2.0/src/main/java/parser/grammar/SimpleJava.g4 by ANTLR 4.13.1 +// Generated from C:/Users/janni/Desktop/NichtHaskell2.0/src/main/java/parser/grammar/SimpleJava.g4 by ANTLR 4.13.1 package parser.generated; import org.antlr.v4.runtime.Lexer; import org.antlr.v4.runtime.CharStream; diff --git a/src/main/java/parser/generated/SimpleJavaListener.java b/src/main/java/parser/generated/SimpleJavaListener.java index 3e7bf6d..580bfe1 100644 --- a/src/main/java/parser/generated/SimpleJavaListener.java +++ b/src/main/java/parser/generated/SimpleJavaListener.java @@ -1,4 +1,4 @@ -// Generated from C:/Users/Maxi/Documents/DHBW/Compilerbau/NichtHaskell2.0/src/main/java/parser/grammar/SimpleJava.g4 by ANTLR 4.13.1 +// Generated from C:/Users/janni/Desktop/NichtHaskell2.0/src/main/java/parser/grammar/SimpleJava.g4 by ANTLR 4.13.1 package parser.generated; import org.antlr.v4.runtime.tree.ParseTreeListener; diff --git a/src/main/java/parser/generated/SimpleJavaParser.java b/src/main/java/parser/generated/SimpleJavaParser.java index cff16d5..3a80a6a 100644 --- a/src/main/java/parser/generated/SimpleJavaParser.java +++ b/src/main/java/parser/generated/SimpleJavaParser.java @@ -1,4 +1,4 @@ -// Generated from C:/Users/Maxi/Documents/DHBW/Compilerbau/NichtHaskell2.0/src/main/java/parser/grammar/SimpleJava.g4 by ANTLR 4.13.1 +// Generated from C:/Users/janni/Desktop/NichtHaskell2.0/src/main/java/parser/grammar/SimpleJava.g4 by ANTLR 4.13.1 package parser.generated; import org.antlr.v4.runtime.atn.*; import org.antlr.v4.runtime.dfa.DFA; diff --git a/src/main/java/parser/generated/SimpleJavaVisitor.java b/src/main/java/parser/generated/SimpleJavaVisitor.java index 5d66eae..beefef9 100644 --- a/src/main/java/parser/generated/SimpleJavaVisitor.java +++ b/src/main/java/parser/generated/SimpleJavaVisitor.java @@ -1,4 +1,4 @@ -// Generated from C:/Users/Maxi/Documents/DHBW/Compilerbau/NichtHaskell2.0/src/main/java/parser/grammar/SimpleJava.g4 by ANTLR 4.13.1 +// Generated from C:/Users/janni/Desktop/NichtHaskell2.0/src/main/java/parser/grammar/SimpleJava.g4 by ANTLR 4.13.1 package parser.generated; import org.antlr.v4.runtime.tree.ParseTreeVisitor; diff --git a/src/test/java/parser/AstBuilderTest.java b/src/test/java/parser/AstBuilderTest.java index b5bec24..de3f40b 100644 --- a/src/test/java/parser/AstBuilderTest.java +++ b/src/test/java/parser/AstBuilderTest.java @@ -1,103 +1,51 @@ package parser; + +import ast.ASTNode; import ast.ClassNode; import ast.ProgramNode; +import ast.members.ConstructorNode; +import ast.members.MemberNode; +import ast.type.AccessModifierNode; import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.CharStreams; import org.antlr.v4.runtime.CommonTokenStream; import org.antlr.v4.runtime.tree.ParseTree; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import parser.astBuilder.ASTBuilder; import parser.generated.SimpleJavaLexer; import parser.generated.SimpleJavaParser; -import static org.junit.jupiter.api.Assertions.assertEquals; +import java.io.IOException; -public class AstBuilderTest { +import static org.assertj.core.api.Assertions.assertThat; + +@DisplayName("Untyped Abstract Syntax Tree") +class AstBuilderTest { @Test - public void astBuilderTest() { - // ---------------- Leere Klasse nachgebaut ---------------- + public void emptyClassTest(){ + MemberNode constructor = new ConstructorNode(new AccessModifierNode("public"),"EmptyClass"); + ClassNode emptyClass = new ClassNode("public", "EmptyClass"); + emptyClass.addMember(constructor); + var expected = new ProgramNode(); + expected.addClass(emptyClass); - ProgramNode expectedASTEmptyClass = new ProgramNode(); - - // public class Name {} - ClassNode nameClass = new ClassNode("public", "Name"); - - expectedASTEmptyClass.addClass(nameClass); - - - // ---------------- Leere Klasse erzeugt ---------------- - - // init - CharStream inputCharStream = CharStreams.fromString("public class Name {}"); - SimpleJavaLexer lexer = new SimpleJavaLexer(inputCharStream); + CharStream testFile = null; + try { + testFile = CharStreams.fromFileName("src/test/resources/input/javaCases/EmptyClass.java"); + } catch (IOException e) { + throw new RuntimeException(e); + } + SimpleJavaLexer lexer = new SimpleJavaLexer(testFile); CommonTokenStream tokenStream = new CommonTokenStream(lexer); tokenStream.fill(); - - /* Parser -> Parsetree */ SimpleJavaParser parser = new SimpleJavaParser(tokenStream); - ParseTree parseTreeEmptyClass = parser.program(); // parse the input - - /* AST builder -> AST */ + ParseTree parseTree = parser.program(); ASTBuilder astBuilder = new ASTBuilder(); - ProgramNode actualASTEmptyClass = (ProgramNode) new ASTBuilder().visit(parseTreeEmptyClass); + var actual = astBuilder.visit(parseTree); - - // ---------------- Vergleichen ---------------- - - String expectedASTasString = expectedASTEmptyClass.toString(); - String actualASTasString = new ASTBuilder().visit(parseTreeEmptyClass).toString(); - - // Wie vergleiche ich das? - assertEquals(expectedASTasString, actualASTasString); - assertEquals(expectedASTEmptyClass, actualASTEmptyClass); + assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); } - - - - - - - - - - - - - - - // ---------------- Alter CompilerInput nachgebaut ---------------- - // ProgramNode startNode = new ProgramNode(); - // public class CompilerInput {} - // ClassNode compilerInputClass = new ClassNode(new AccessTypeNode(EnumAccessTypeNode.PUBLIC), "CompilerInput"); - // public int a; - // compilerInputClass.addMember(new FieldNode(new AccessTypeNode(EnumAccessTypeNode.PUBLIC), new BaseTypeNode(EnumTypeNode.INT), "a")); - // public static int testMethod(char x) { return 0; } - /* compilerInputClass.addMember( - new MethodNode( - new AccessTypeNode(EnumAccessTypeNode.PUBLIC), - new BaseTypeNode(EnumTypeNode.INT), - "testMethod", - new ParameterListNode(List.of(new ParameterNode(new BaseTypeNode(EnumTypeNode.CHAR), "x"))), - List.of(new ReturnStatementNode(new LiteralNode(0))) - )); - - ClassNode testClass = new ClassNode(new AccessTypeNode(EnumAccessTypeNode.PUBLIC), "Test"); - testClass.addMember( - new MethodNode( - new AccessTypeNode(EnumAccessTypeNode.PUBLIC), - new BaseTypeNode(EnumTypeNode.INT), - "testMethod", - new ParameterListNode(List.of(new ParameterNode(new BaseTypeNode(EnumTypeNode.CHAR), "x"), new ParameterNode(new BaseTypeNode(EnumTypeNode.INT), "a"))), - List.of(new ReturnStatementNode(new LiteralNode(0))) - ) - ); - - */ - - //compilerInputClass.addClass(testClass); - - // startNode.addClass(compilerInputClass); - // startNode.addClass(testClass); -} +} \ No newline at end of file diff --git a/src/test/resources/input/javaCases/EmptyClass.java b/src/test/resources/input/javaCases/EmptyClass.java new file mode 100644 index 0000000..393c29c --- /dev/null +++ b/src/test/resources/input/javaCases/EmptyClass.java @@ -0,0 +1 @@ +class EmptyClass { } \ No newline at end of file diff --git a/src/test/resources/input/javaCases/MultipleClasses.java b/src/test/resources/input/javaCases/MultipleClasses.java new file mode 100644 index 0000000..8f8e5c7 --- /dev/null +++ b/src/test/resources/input/javaCases/MultipleClasses.java @@ -0,0 +1,7 @@ +class EmptyClass1{ + +} + +class EmptyClass2{ + +} \ No newline at end of file