Compare commits

..

No commits in common. "093beb9ef1692e436855d71c33fcdc77b450cf53" and "0298e2cb7678304ae82a4ab0abf86fbeeae67483" have entirely different histories.

10 changed files with 49 additions and 175 deletions

View File

@ -15,6 +15,11 @@ public class ConstructorNode extends MethodNode implements Visitable {
public List<ParameterNode> parameters = new ArrayList<>();
public BlockNode block;
public ConstructorNode(AccessModifierNode accessType, String identifier) {
this.accessType = accessType;
this.identifier = identifier;
}
public ConstructorNode(String accessType, String identifier, BlockNode block) {
this.accessType = new AccessModifierNode(accessType);
this.identifier = identifier;

View File

@ -63,9 +63,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
}
if(!hasConstructor){
BlockNode blockNode = new BlockNode();
blockNode.addStatement(new ReturnNode(null));
classNode.members.addFirst(new ConstructorNode("public", ctx.Identifier().getText(), blockNode));
classNode.members.addFirst(new ConstructorNode(new AccessModifierNode("public"), ctx.Identifier().getText()));
}
return classNode;
@ -113,11 +111,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override
public ASTNode visitFieldDeclaration(SimpleJavaParser.FieldDeclarationContext ctx) {
if(ctx.AccessModifier() != null) {
return new FieldNode(new AccessModifierNode(ctx.AccessModifier().getText()), createTypeNode(ctx.type().getText()), ctx.Identifier().getText());
} else {
return new FieldNode(null, createTypeNode(ctx.type().getText()), ctx.Identifier().getText());
}
}
@Override
@ -207,17 +201,10 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
crement = (IStatementNode) visit(ctx.statementExpression(i));
}
BlockNode forBlock = new BlockNode();
BlockNode forStatements = (BlockNode) visit(ctx.blockStatement());
if(forStatements != null) {
forBlock.addStatement((IStatementNode) forStatements);
}
BlockNode forBlock = (BlockNode) visit(ctx.blockStatement());
if(crement != null){
BlockNode forCrement = new BlockNode();
forCrement.addStatement((crement));
forBlock.addStatement(forCrement);
forBlock.addStatement((crement));
}
WhileNode While = new WhileNode(condition, forBlock);

View File

@ -5,11 +5,8 @@ import ast.ASTNode;
import ast.ClassNode;
import ast.ProgramNode;
import ast.members.ConstructorNode;
import ast.members.FieldNode;
import ast.members.MemberNode;
import ast.type.AccessModifierNode;
import ast.type.type.BaseType;
import ast.type.type.TypeEnum;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
@ -28,189 +25,140 @@ import static org.assertj.core.api.Assertions.assertThat;
class AstBuilderTest {
@Test
@DisplayName("Empty Class Test")
public void emptyClassTest(){
ClassNode emptyClass = Helper.generateEmptyClass("TestClass");
ProgramNode expected = new ProgramNode();
MemberNode constructor = new ConstructorNode(new AccessModifierNode("public"),"EmptyClass");
ClassNode emptyClass = new ClassNode("public", "EmptyClass");
emptyClass.addMember(constructor);
var expected = new ProgramNode();
expected.addClass(emptyClass);
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/EmptyClass.java");
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();
SimpleJavaParser parser = new SimpleJavaParser(tokenStream);
ParseTree parseTree = parser.program();
ASTBuilder astBuilder = new ASTBuilder();
var actual = astBuilder.visit(parseTree);
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
}
@Test
@DisplayName("Multiple Empty Classes Test")
public void multipleEmptyClassesTest() {
ClassNode classNode1 = Helper.generateEmptyClass("TestClass1");
ClassNode classNode2 = Helper.generateEmptyClass("TestClass2");
ProgramNode expected = new ProgramNode();
expected.addClass(classNode1);
expected.addClass(classNode2);
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/MultipleClasses.java");
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
}
@Test
@DisplayName("Empty Class Test with Constructor")
public void emptyClassWithConstructorTest() {
ClassNode classNode = Helper.generateEmptyClass("TestClass");
ProgramNode expected = new ProgramNode();
expected.addClass(classNode);
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/EmptyClassWithConstructor.java");
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
}
@Test
@DisplayName("Field Test")
public void fieldTest() {
ClassNode classNode = Helper.generateEmptyClass("TestClass");
classNode.addMember(new FieldNode(null, new BaseType(TypeEnum.INT), "a"));
ProgramNode expected = new ProgramNode();
expected.addClass(classNode);
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/Field.java");
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
}
@Test
@DisplayName("Field Test with Accessmodifier")
public void fieldTestWithModifier() {
ClassNode classNode = Helper.generateEmptyClass("TestClass");
classNode.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
ProgramNode expected = new ProgramNode();
expected.addClass(classNode);
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/FieldWithAccessModifier.java");
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
}
@Test
@DisplayName("Commments Ignore Test")
public void commmentsIgnoreTest(){
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
}
@Test
@DisplayName("Constructor Paramerter Test")
public void constructorParameterTest(){
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
}
@Test
@DisplayName("This Dot Test")
public void thisDotTest(){
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
}
@Test
@DisplayName("Constructor This Dot Test")
public void constructorThisDotTest(){
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
}
@Test
@DisplayName("Void Methoden Test")
public void voidMethodenTest(){
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
}
@Test
@DisplayName("Constructor Method call Test")
public void constructorMethodCallTest(){
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
}
@Test
@DisplayName("Constructor Method call Parameters Test")
public void constructorMethodCallParametersTest(){
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
}
@Test
@DisplayName("Char Test")
public void charTest(){
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
}
@Test
@DisplayName("Null Test")
public void nullTest(){
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
}
@Test
@DisplayName("Self Reference Test")
public void selfReferneceTest(){
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
}
@Test
@DisplayName("Variable Compare Test")
public void variableCompareTest(){
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
}
@Test
@DisplayName("Variable Calculation Test")
public void variableCalculationTest(){
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
}
@Test
@DisplayName("Main Method Test")
public void mainMethodTest(){
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
}
@Test
@DisplayName("While Test")
public void whileTest(){
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
}
@Test
@DisplayName("Do While Test")
public void doWhileTest(){
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
}
@Test
@DisplayName("For Test")
public void forTest(){
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
}
//Noch nicht speziell Increment nur zum Development Testen per Debug
@Test
@DisplayName("Increment Test")
public void incrementTest(){
ClassNode classNode = Helper.generateEmptyClass("TestClass");
classNode.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
ProgramNode expected = new ProgramNode();
expected.addClass(classNode);
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/Increment.java");
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
}

View File

@ -1,47 +0,0 @@
package parser;
import ast.ASTNode;
import ast.ClassNode;
import ast.ProgramNode;
import ast.members.ConstructorNode;
import ast.members.MemberNode;
import ast.statements.BlockNode;
import ast.statements.ReturnNode;
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 parser.astBuilder.ASTBuilder;
import parser.generated.SimpleJavaLexer;
import parser.generated.SimpleJavaParser;
import java.io.IOException;
public class Helper {
public static ASTNode generateAST(String filePath) {
CharStream testFile = null;
try {
testFile = CharStreams.fromFileName(filePath);
} catch (IOException e) {
throw new RuntimeException(e);
}
SimpleJavaLexer lexer = new SimpleJavaLexer(testFile);
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
tokenStream.fill();
SimpleJavaParser parser = new SimpleJavaParser(tokenStream);
ParseTree parseTree = parser.program();
ASTBuilder astBuilder = new ASTBuilder();
return astBuilder.visit(parseTree);
}
public static ClassNode generateEmptyClass(String className) {
BlockNode blockNode = new BlockNode();
blockNode.addStatement(new ReturnNode(null));
MemberNode constructor = new ConstructorNode("public",className, blockNode);
ClassNode classNode = new ClassNode("public", className);
classNode.addMember(constructor);
return classNode;
}
}

View File

@ -1 +1 @@
class TestClass {}
class EmptyClass { }

View File

@ -1,5 +0,0 @@
public class TestClass {
public TestClass() {
}
}

View File

@ -1,3 +0,0 @@
public class TestClass {
int a;
}

View File

@ -1,3 +0,0 @@
public class TestClass {
public int a;
}

View File

@ -1,12 +0,0 @@
public class Increment {
public int test;
public void increment(int p) {
test = p++;
for(int i = 1; i<=10, i++) {
int a = 5;
}
}
}

View File

@ -1,3 +1,7 @@
class TestClass1 {}
class EmptyClass1{
class TestClass2{}
}
class EmptyClass2{
}