Compare commits

..

3 Commits

Author SHA1 Message Date
c135819ba0 Merge branch 'main' into code-generator
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-01 17:07:52 -04:00
e94cd9cc2a Merge pull request 'NewParser' (#16) from NewParser into main
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
Reviewed-on: #16
Reviewed-by: Johannes Ehlert <i22005@hb.dhbw-stuttgart.de>
2024-07-01 21:07:27 +00:00
Maximilian Stahl
dcd20a2394 AST Builder Tests and Fixes
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-01 16:40:02 +02:00
4 changed files with 254 additions and 38 deletions

View File

@ -56,11 +56,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
for (SimpleJavaParser.MemberDeclarationContext member : ctx.memberDeclaration()) { for (SimpleJavaParser.MemberDeclarationContext member : ctx.memberDeclaration()) {
MemberNode memberNode = (MemberNode) visit(member); MemberNode memberNode = (MemberNode) visit(member);
if(memberNode != null) {
if(memberNode instanceof ConstructorNode){ if(memberNode instanceof ConstructorNode){
hasConstructor = true; hasConstructor = true;
} }
classNode.addMember(memberNode); classNode.addMember(memberNode);
} }
}
if(!hasConstructor){ if(!hasConstructor){
BlockNode blockNode = new BlockNode(); BlockNode blockNode = new BlockNode();
@ -91,6 +93,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return mainMethod; return mainMethod;
} else { } else {
if(ctx.type() != null) { if(ctx.type() != null) {
if(ctx.AccessModifier() != null) {
MethodNode methodNode = new MethodNode(ctx.AccessModifier().getText(), createTypeNode(ctx.type().getText()), false, ctx.Identifier().getText(), (BlockNode) visit(ctx.blockStatement())); MethodNode methodNode = new MethodNode(ctx.AccessModifier().getText(), createTypeNode(ctx.type().getText()), false, ctx.Identifier().getText(), (BlockNode) visit(ctx.blockStatement()));
if(ctx.parameterList() != null) { if(ctx.parameterList() != null) {
for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) { for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) {
@ -99,6 +102,16 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
} }
return methodNode; return methodNode;
} else { } else {
MethodNode methodNode = new MethodNode("public", createTypeNode(ctx.type().getText()), false, ctx.Identifier().getText(), (BlockNode) visit(ctx.blockStatement()));
if(ctx.parameterList() != null) {
for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) {
methodNode.addParameter((ParameterNode) visit(parameter));
}
}
return methodNode;
}
} else {
if(ctx.AccessModifier() != null) {
MethodNode methodNode = new MethodNode(ctx.AccessModifier().getText(), null, true, ctx.Identifier().getText(), (BlockNode) visit(ctx.blockStatement())); MethodNode methodNode = new MethodNode(ctx.AccessModifier().getText(), null, true, ctx.Identifier().getText(), (BlockNode) visit(ctx.blockStatement()));
if(ctx.parameterList() != null) { if(ctx.parameterList() != null) {
for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) { for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) {
@ -107,6 +120,16 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
} }
methodNode.block.addStatement(new ReturnNode(null)); methodNode.block.addStatement(new ReturnNode(null));
return methodNode; return methodNode;
} else {
MethodNode methodNode = new MethodNode("public", null, true, ctx.Identifier().getText(), (BlockNode) visit(ctx.blockStatement()));
if(ctx.parameterList() != null) {
for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) {
methodNode.addParameter((ParameterNode) visit(parameter));
}
}
methodNode.block.addStatement(new ReturnNode(null));
return methodNode;
}
} }
} }
} }
@ -116,7 +139,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
if(ctx.AccessModifier() != null) { if(ctx.AccessModifier() != null) {
return new FieldNode(new AccessModifierNode(ctx.AccessModifier().getText()), createTypeNode(ctx.type().getText()), ctx.Identifier().getText()); return new FieldNode(new AccessModifierNode(ctx.AccessModifier().getText()), createTypeNode(ctx.type().getText()), ctx.Identifier().getText());
} else { } else {
return new FieldNode(null, createTypeNode(ctx.type().getText()), ctx.Identifier().getText()); return new FieldNode(new AccessModifierNode("public"), createTypeNode(ctx.type().getText()), ctx.Identifier().getText());
} }
} }
@ -289,7 +312,12 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override @Override
public ASTNode visitMethodCall(SimpleJavaParser.MethodCallContext ctx) { public ASTNode visitMethodCall(SimpleJavaParser.MethodCallContext ctx) {
MethodCallNode methodCallStatementExpressionNode = new MethodCallNode((TargetNode) visit(ctx.target()), ctx.Identifier().getText()); MethodCallNode methodCallStatementExpressionNode;
if(ctx.target() != null) {
methodCallStatementExpressionNode = new MethodCallNode((TargetNode) visit(ctx.target()), ctx.Identifier().getText());
} else {
methodCallStatementExpressionNode = new MethodCallNode(null, ctx.Identifier().getText());
}
for(SimpleJavaParser.ChainedMethodContext chainedMethod : ctx.chainedMethod()) { for(SimpleJavaParser.ChainedMethodContext chainedMethod : ctx.chainedMethod()) {
methodCallStatementExpressionNode.addChainedMethod((ChainedMethodNode) visit(chainedMethod)); methodCallStatementExpressionNode.addChainedMethod((ChainedMethodNode) visit(chainedMethod));
} }

View File

@ -4,12 +4,27 @@ package parser;
import ast.ASTNode; import ast.ASTNode;
import ast.ClassNode; import ast.ClassNode;
import ast.ProgramNode; import ast.ProgramNode;
import ast.expressions.IExpressionNode;
import ast.expressions.unaryexpressions.MemberAccessNode;
import ast.expressions.unaryexpressions.UnaryNode;
import ast.members.ConstructorNode; import ast.members.ConstructorNode;
import ast.members.FieldNode; import ast.members.FieldNode;
import ast.members.MemberNode; import ast.members.MemberNode;
import ast.members.MethodNode;
import ast.parameters.ParameterNode;
import ast.statementexpressions.AssignNode;
import ast.statementexpressions.AssignableNode;
import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
import ast.statements.BlockNode;
import ast.statements.IStatementNode;
import ast.statements.ReturnNode;
import ast.type.AccessModifierNode; import ast.type.AccessModifierNode;
import ast.type.EnumValueNode;
import ast.type.ValueNode;
import ast.type.type.BaseType; import ast.type.type.BaseType;
import ast.type.type.ITypeNode;
import ast.type.type.TypeEnum; import ast.type.type.TypeEnum;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams; import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream; import org.antlr.v4.runtime.CommonTokenStream;
@ -21,6 +36,7 @@ import parser.generated.SimpleJavaLexer;
import parser.generated.SimpleJavaParser; import parser.generated.SimpleJavaParser;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Member;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -41,11 +57,11 @@ class AstBuilderTest {
@Test @Test
@DisplayName("Multiple Empty Classes Test") @DisplayName("Multiple Empty Classes Test")
public void multipleEmptyClassesTest() { public void multipleEmptyClassesTest() {
ClassNode classNode1 = Helper.generateEmptyClass("TestClass1"); ClassNode class1 = Helper.generateEmptyClass("TestClass1");
ClassNode classNode2 = Helper.generateEmptyClass("TestClass2"); ClassNode class2 = Helper.generateEmptyClass("TestClass2");
ProgramNode expected = new ProgramNode(); ProgramNode expected = new ProgramNode();
expected.addClass(classNode1); expected.addClass(class1);
expected.addClass(classNode2); expected.addClass(class2);
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/MultipleClasses.java"); ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/MultipleClasses.java");
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
@ -54,9 +70,9 @@ class AstBuilderTest {
@Test @Test
@DisplayName("Empty Class Test with Constructor") @DisplayName("Empty Class Test with Constructor")
public void emptyClassWithConstructorTest() { public void emptyClassWithConstructorTest() {
ClassNode classNode = Helper.generateEmptyClass("TestClass"); ClassNode class1 = Helper.generateEmptyClass("TestClass");
ProgramNode expected = new ProgramNode(); ProgramNode expected = new ProgramNode();
expected.addClass(classNode); expected.addClass(class1);
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/EmptyClassWithConstructor.java"); ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/EmptyClassWithConstructor.java");
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
@ -65,11 +81,11 @@ class AstBuilderTest {
@Test @Test
@DisplayName("Field Test") @DisplayName("Field Test")
public void fieldTest() { public void fieldTest() {
ClassNode classNode = Helper.generateEmptyClass("TestClass"); ClassNode class1 = Helper.generateEmptyClass("TestClass");
classNode.addMember(new FieldNode(null, new BaseType(TypeEnum.INT), "a")); class1.addMember(new FieldNode(null, new BaseType(TypeEnum.INT), "a"));
ProgramNode expected = new ProgramNode(); ProgramNode expected = new ProgramNode();
expected.addClass(classNode); expected.addClass(class1);
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/Field.java"); ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/Field.java");
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
@ -78,11 +94,11 @@ class AstBuilderTest {
@Test @Test
@DisplayName("Field Test with Accessmodifier") @DisplayName("Field Test with Accessmodifier")
public void fieldTestWithModifier() { public void fieldTestWithModifier() {
ClassNode classNode = Helper.generateEmptyClass("TestClass"); ClassNode class1 = Helper.generateEmptyClass("TestClass");
classNode.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a")); class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
ProgramNode expected = new ProgramNode(); ProgramNode expected = new ProgramNode();
expected.addClass(classNode); expected.addClass(class1);
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/FieldWithAccessModifier.java"); ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/FieldWithAccessModifier.java");
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
@ -91,64 +107,236 @@ class AstBuilderTest {
@Test @Test
@DisplayName("Commments Ignore Test") @DisplayName("Commments Ignore Test")
public void commmentsIgnoreTest(){ public void commmentsIgnoreTest(){
ClassNode class1 = Helper.generateEmptyClass("TestClass");
class1.addMember(new FieldNode(new AccessModifierNode("private"), new BaseType(TypeEnum.INT), "a"));
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); ProgramNode expected = new ProgramNode();
expected.addClass(class1);
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/Comments.java");
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
} }
@Test @Test
@DisplayName("Constructor Paramerter Test") @DisplayName("Constructor Paramerter Test")
public void constructorParameterTest(){ public void constructorParameterTest(){
BlockNode block = new BlockNode();
block.addStatement(new ReturnNode(null));
ConstructorNode constructor = new ConstructorNode("public", "TestClass", block);
constructor.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a"));
constructor.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "b"));
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); ClassNode class1 = new ClassNode("public", "TestClass");
class1.addMember(constructor);
ProgramNode expected = new ProgramNode();
expected.addClass(class1);
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/ConstructorParameter.java");
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
} }
@Test @Test
@DisplayName("This Dot Test") @DisplayName("This Dot Test")
public void thisDotTest(){ public void thisDotTest(){
BlockNode block = new BlockNode();
MemberAccessNode memberAccess = new MemberAccessNode(true);
memberAccess.addIdentifier("a");
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); AssignableNode assignable = new AssignableNode(memberAccess);
ValueNode value = new ValueNode(EnumValueNode.INT_VALUE, "1");
IExpressionNode expression = new UnaryNode(value);
block.addStatement(new AssignNode(assignable, expression));
block.addStatement(new ReturnNode(null));
ConstructorNode constructor = new ConstructorNode("public", "TestClass", block);
ClassNode class1 = new ClassNode("public", "TestClass");
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
class1.addMember(constructor);
ProgramNode expected = new ProgramNode();
expected.addClass(class1);
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/ThisDot.java");
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
} }
@Test @Test
@DisplayName("Constructor This Dot Test") @DisplayName("Constructor This Dot Test")
public void constructorThisDotTest(){ public void constructorThisDotTest(){
BlockNode block = new BlockNode();
MemberAccessNode memberAccess = new MemberAccessNode(true);
memberAccess.addIdentifier("a");
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); AssignableNode assignable = new AssignableNode(memberAccess);
IExpressionNode expression = new UnaryNode("a");
block.addStatement(new AssignNode(assignable, expression));
block.addStatement(new ReturnNode(null));
ConstructorNode constructor = new ConstructorNode("public", "TestClass", block);
constructor.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a"));
ClassNode class1 = new ClassNode("public", "TestClass");
class1.addMember(new FieldNode(new AccessModifierNode("private"), new BaseType(TypeEnum.INT), "a"));
class1.addMember(constructor);
ProgramNode expected = new ProgramNode();
expected.addClass(class1);
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/ConstructorThisDot.java");
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
} }
@Test @Test
@DisplayName("Void Methoden Test") @DisplayName("Void Methoden Test")
public void voidMethodenTest(){ public void voidMethodenTest(){
ClassNode class1 = Helper.generateEmptyClass("TestClass");
BlockNode block = new BlockNode();
block.addStatement(new ReturnNode(null));
class1.addMember(new MethodNode("public", null, true, "test", block));
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); ProgramNode expected = new ProgramNode();
expected.addClass(class1);
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/VoidMethod.java");
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
} }
@Test @Test
@DisplayName("Constructor Method call Test") @DisplayName("Constructor Method call Test")
public void constructorMethodCallTest(){ public void constructorMethodCallTest(){
BlockNode blockCon = new BlockNode();
MemberAccessNode memberAccess = new MemberAccessNode(true);
memberAccess.addIdentifier("a");
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); AssignableNode assignable = new AssignableNode(memberAccess);
IExpressionNode expression = new UnaryNode(new MethodCallNode(null, "testMethod"));
blockCon.addStatement(new AssignNode(assignable, expression));
blockCon.addStatement(new ReturnNode(null));
ConstructorNode constructor = new ConstructorNode("public", "TestClass", blockCon);
BlockNode blockMethod = new BlockNode();
blockMethod.addStatement(new ReturnNode(new UnaryNode(new ValueNode(EnumValueNode.INT_VALUE, "1"))));
MethodNode method = new MethodNode("public", new BaseType(TypeEnum.INT), false, "testMethod", blockMethod);
ClassNode class1 = new ClassNode("public", "TestClass");
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
class1.addMember(constructor);
class1.addMember(method);
ProgramNode expected = new ProgramNode();
expected.addClass(class1);
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/ConstructorMethodCall.java");
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
} }
@Test @Test
@DisplayName("Constructor Method call Parameters Test") @DisplayName("Constructor Method call Parameters Test")
public void constructorMethodCallParametersTest(){ public void constructorMethodCallParametersTest(){
BlockNode blockCon = new BlockNode();
MemberAccessNode memberAccess = new MemberAccessNode(true);
memberAccess.addIdentifier("a");
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); AssignableNode assignable = new AssignableNode(memberAccess);
MethodCallNode methodCall = new MethodCallNode(null, "testMethod");
methodCall.addExpression(new UnaryNode("a"));
IExpressionNode expression = new UnaryNode(methodCall);
blockCon.addStatement(new AssignNode(assignable, expression));
blockCon.addStatement(new ReturnNode(null));
ConstructorNode constructor = new ConstructorNode("public", "TestClass", blockCon);
constructor.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a"));
BlockNode blockMethod = new BlockNode();
blockMethod.addStatement(new ReturnNode(new UnaryNode("a")));
MethodNode method = new MethodNode("public", new BaseType(TypeEnum.INT), false, "testMethod", blockMethod);
method.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a"));
ClassNode class1 = new ClassNode("public", "TestClass");
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
class1.addMember(constructor);
class1.addMember(method);
ProgramNode expected = new ProgramNode();
expected.addClass(class1);
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/ConstructorMethodCallParameters.java");
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
} }
@Test @Test
@DisplayName("Char Test") @DisplayName("Char Test")
public void charTest(){ public void charTest(){
BlockNode blockCon = new BlockNode();
MemberAccessNode memberAccess = new MemberAccessNode(true);
memberAccess.addIdentifier("a");
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); AssignableNode assignable = new AssignableNode(memberAccess);
MethodCallNode methodCall = new MethodCallNode(null, "testMethod");
methodCall.addExpression(new UnaryNode("a"));
IExpressionNode expression = new UnaryNode(methodCall);
blockCon.addStatement(new AssignNode(assignable, expression));
blockCon.addStatement(new ReturnNode(null));
ConstructorNode constructor = new ConstructorNode("public", "TestClass", blockCon);
constructor.addParameter(new ParameterNode(new BaseType(TypeEnum.CHAR), "a"));
BlockNode blockMethod = new BlockNode();
blockMethod.addStatement(new ReturnNode(new UnaryNode("a")));
MethodNode method = new MethodNode("public", new BaseType(TypeEnum.CHAR), false, "testMethod", blockMethod);
method.addParameter(new ParameterNode(new BaseType(TypeEnum.CHAR), "a"));
ClassNode class1 = new ClassNode("public", "TestClass");
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.CHAR), "a"));
class1.addMember(constructor);
class1.addMember(method);
ProgramNode expected = new ProgramNode();
expected.addClass(class1);
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/Char.java");
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
} }
@Test @Test
@DisplayName("Null Test") @DisplayName("Null Test")
public void nullTest(){ public void nullTest(){
BlockNode blockCon = new BlockNode();
MemberAccessNode memberAccess = new MemberAccessNode(true);
memberAccess.addIdentifier("a");
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); AssignableNode assignable = new AssignableNode(memberAccess);
blockCon.addStatement(new AssignNode(assignable, new UnaryNode(new ValueNode(EnumValueNode.NULL_VALUE, "null"))));
blockCon.addStatement(new ReturnNode(null));
ConstructorNode constructor = new ConstructorNode("public", "TestClass", blockCon);
ClassNode class1 = new ClassNode("public", "TestClass");
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
class1.addMember(constructor);
ProgramNode expected = new ProgramNode();
expected.addClass(class1);
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/Null.java");
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
} }
@Test @Test

View File

@ -2,7 +2,7 @@ class TestClass{
public int a; public int a;
public TestClass{ public TestClass() {
this.a = 1; this.a = 1;
} }
} }