Adding EmptyClass Test AST
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Has been cancelled
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Has been cancelled
This commit is contained in:
parent
85ae06048c
commit
27f50b9c66
@ -15,30 +15,20 @@ public class ClassNode implements ASTNode, Visitable {
|
|||||||
public AccessModifierNode accessType;
|
public AccessModifierNode accessType;
|
||||||
public String identifier;
|
public String identifier;
|
||||||
public List<MemberNode> members = new ArrayList<>();
|
public List<MemberNode> members = new ArrayList<>();
|
||||||
public boolean hasConstructor;
|
|
||||||
|
|
||||||
public ClassNode() {}
|
public ClassNode(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public ClassNode(String accessType, String identifier){
|
public ClassNode(String accessType, String identifier){
|
||||||
this.accessType = new AccessModifierNode(accessType);
|
this.accessType = new AccessModifierNode(accessType);
|
||||||
this.identifier = identifier;
|
this.identifier = identifier;
|
||||||
hasConstructor = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addMember(MemberNode member) {
|
public void addMember(MemberNode member) {
|
||||||
if (member instanceof ConstructorNode) {
|
|
||||||
this.hasConstructor = true;
|
|
||||||
}
|
|
||||||
members.add(member);
|
members.add(member);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ensureConstructor(){
|
|
||||||
if(!hasConstructor) {
|
|
||||||
ConstructorNode constructor = new ConstructorNode(new AccessModifierNode("public"), identifier);
|
|
||||||
members.addFirst(constructor);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<MethodNode> getMethods(){
|
public List<MethodNode> getMethods(){
|
||||||
List<MethodNode> methods = new ArrayList<>();
|
List<MethodNode> methods = new ArrayList<>();
|
||||||
for (MemberNode member : members) {
|
for (MemberNode member : members) {
|
||||||
|
@ -44,11 +44,28 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ASTNode visitClassDeclaration(SimpleJavaParser.ClassDeclarationContext ctx) {
|
public ASTNode visitClassDeclaration(SimpleJavaParser.ClassDeclarationContext ctx) {
|
||||||
ClassNode classNode = new ClassNode(ctx.AccessModifier().getText(), ctx.Identifier().getText());
|
ClassNode classNode;
|
||||||
for (SimpleJavaParser.MemberDeclarationContext member : ctx.memberDeclaration()) {
|
if(ctx.AccessModifier() != null){
|
||||||
classNode.addMember((MemberNode) visit(member));
|
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;
|
return classNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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;
|
package parser.generated;
|
||||||
|
|
||||||
import org.antlr.v4.runtime.ParserRuleContext;
|
import org.antlr.v4.runtime.ParserRuleContext;
|
||||||
|
@ -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;
|
package parser.generated;
|
||||||
import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor;
|
import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor;
|
||||||
|
|
||||||
|
@ -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;
|
package parser.generated;
|
||||||
import org.antlr.v4.runtime.Lexer;
|
import org.antlr.v4.runtime.Lexer;
|
||||||
import org.antlr.v4.runtime.CharStream;
|
import org.antlr.v4.runtime.CharStream;
|
||||||
|
@ -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;
|
package parser.generated;
|
||||||
import org.antlr.v4.runtime.tree.ParseTreeListener;
|
import org.antlr.v4.runtime.tree.ParseTreeListener;
|
||||||
|
|
||||||
|
@ -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;
|
package parser.generated;
|
||||||
import org.antlr.v4.runtime.atn.*;
|
import org.antlr.v4.runtime.atn.*;
|
||||||
import org.antlr.v4.runtime.dfa.DFA;
|
import org.antlr.v4.runtime.dfa.DFA;
|
||||||
|
@ -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;
|
package parser.generated;
|
||||||
import org.antlr.v4.runtime.tree.ParseTreeVisitor;
|
import org.antlr.v4.runtime.tree.ParseTreeVisitor;
|
||||||
|
|
||||||
|
@ -1,103 +1,51 @@
|
|||||||
package parser;
|
package parser;
|
||||||
|
|
||||||
|
|
||||||
|
import ast.ASTNode;
|
||||||
import ast.ClassNode;
|
import ast.ClassNode;
|
||||||
import ast.ProgramNode;
|
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.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;
|
||||||
import org.antlr.v4.runtime.tree.ParseTree;
|
import org.antlr.v4.runtime.tree.ParseTree;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import parser.astBuilder.ASTBuilder;
|
import parser.astBuilder.ASTBuilder;
|
||||||
import parser.generated.SimpleJavaLexer;
|
import parser.generated.SimpleJavaLexer;
|
||||||
import parser.generated.SimpleJavaParser;
|
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
|
@Test
|
||||||
public void astBuilderTest() {
|
public void emptyClassTest(){
|
||||||
// ---------------- Leere Klasse nachgebaut ----------------
|
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();
|
CharStream testFile = null;
|
||||||
|
try {
|
||||||
// public class Name {}
|
testFile = CharStreams.fromFileName("src/test/resources/input/javaCases/EmptyClass.java");
|
||||||
ClassNode nameClass = new ClassNode("public", "Name");
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
expectedASTEmptyClass.addClass(nameClass);
|
}
|
||||||
|
SimpleJavaLexer lexer = new SimpleJavaLexer(testFile);
|
||||||
|
|
||||||
// ---------------- Leere Klasse erzeugt ----------------
|
|
||||||
|
|
||||||
// init
|
|
||||||
CharStream inputCharStream = CharStreams.fromString("public class Name {}");
|
|
||||||
SimpleJavaLexer lexer = new SimpleJavaLexer(inputCharStream);
|
|
||||||
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
|
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
|
||||||
tokenStream.fill();
|
tokenStream.fill();
|
||||||
|
|
||||||
/* Parser -> Parsetree */
|
|
||||||
SimpleJavaParser parser = new SimpleJavaParser(tokenStream);
|
SimpleJavaParser parser = new SimpleJavaParser(tokenStream);
|
||||||
ParseTree parseTreeEmptyClass = parser.program(); // parse the input
|
ParseTree parseTree = parser.program();
|
||||||
|
|
||||||
/* AST builder -> AST */
|
|
||||||
ASTBuilder astBuilder = new ASTBuilder();
|
ASTBuilder astBuilder = new ASTBuilder();
|
||||||
ProgramNode actualASTEmptyClass = (ProgramNode) new ASTBuilder().visit(parseTreeEmptyClass);
|
var actual = astBuilder.visit(parseTree);
|
||||||
|
|
||||||
|
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
// ---------------- Vergleichen ----------------
|
|
||||||
|
|
||||||
String expectedASTasString = expectedASTEmptyClass.toString();
|
|
||||||
String actualASTasString = new ASTBuilder().visit(parseTreeEmptyClass).toString();
|
|
||||||
|
|
||||||
// Wie vergleiche ich das?
|
|
||||||
assertEquals(expectedASTasString, actualASTasString);
|
|
||||||
assertEquals(expectedASTEmptyClass, actualASTEmptyClass);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ---------------- 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);
|
|
||||||
}
|
|
1
src/test/resources/input/javaCases/EmptyClass.java
Normal file
1
src/test/resources/input/javaCases/EmptyClass.java
Normal file
@ -0,0 +1 @@
|
|||||||
|
class EmptyClass { }
|
7
src/test/resources/input/javaCases/MultipleClasses.java
Normal file
7
src/test/resources/input/javaCases/MultipleClasses.java
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
class EmptyClass1{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class EmptyClass2{
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user