Adding EmptyClass Test AST
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Has been cancelled

This commit is contained in:
Purplumbi504 2024-06-27 21:14:02 +02:00
parent 85ae06048c
commit 27f50b9c66
11 changed files with 66 additions and 103 deletions

View File

@ -15,30 +15,20 @@ public class ClassNode implements ASTNode, Visitable {
public AccessModifierNode accessType;
public String identifier;
public List<MemberNode> 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<MethodNode> getMethods(){
List<MethodNode> methods = new ArrayList<>();
for (MemberNode member : members) {

View File

@ -44,11 +44,28 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@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;
}

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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);
}

View File

@ -0,0 +1 @@
class EmptyClass { }

View File

@ -0,0 +1,7 @@
class EmptyClass1{
}
class EmptyClass2{
}