Merge branch 'NewParser' into code-generator
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run

This commit is contained in:
i22007 2024-06-30 19:28:01 -04:00
commit 093beb9ef1
10 changed files with 175 additions and 49 deletions

View File

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

View File

@ -5,8 +5,11 @@ 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;
@ -25,140 +28,189 @@ import static org.assertj.core.api.Assertions.assertThat;
class AstBuilderTest {
@Test
@DisplayName("Empty Class Test")
public void emptyClassTest(){
MemberNode constructor = new ConstructorNode(new AccessModifierNode("public"),"EmptyClass");
ClassNode emptyClass = new ClassNode("public", "EmptyClass");
emptyClass.addMember(constructor);
var expected = new ProgramNode();
ClassNode emptyClass = Helper.generateEmptyClass("TestClass");
ProgramNode expected = new ProgramNode();
expected.addClass(emptyClass);
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);
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/EmptyClass.java");
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

@ -0,0 +1,47 @@
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 EmptyClass { }
class TestClass {}

View File

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

View File

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

View File

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

View File

@ -0,0 +1,12 @@
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,7 +1,3 @@
class EmptyClass1{
class TestClass1 {}
}
class EmptyClass2{
}
class TestClass2{}