diff --git a/src/de/dhbwstuttgart/parser/RunParser.java b/src/de/dhbwstuttgart/parser/RunParser.java index c1d2af50..5ee9606f 100644 --- a/src/de/dhbwstuttgart/parser/RunParser.java +++ b/src/de/dhbwstuttgart/parser/RunParser.java @@ -24,12 +24,11 @@ public class RunParser{ Java8Parser parser = new Java8Parser(tokens); Java8Parser.CompilationUnitContext tree = parser.compilationUnit(); SyntaxTreeGenerator generator = new SyntaxTreeGenerator(); - // generator.convert((Java8Parser.CompilationUnitContext) tree); generator.getNames(tree); - for(JavaClassName name : generator.reg.existingClasses){ - System.out.println(name.toString()); - } - System.out.println(generator.reg.existingClasses.size()); + SourceFile f = generator.convert((Java8Parser.CompilationUnitContext) tree); + for(ClassOrInterface c : f.KlassenVektor){ + System.out.println(c.getClassName().toString()); + } } catch(Exception e){ System.out.println("An exception occured which is unknown and on our TODO list."); diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator.java index 38486593..bc9c773a 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator.java @@ -7,17 +7,16 @@ import de.dhbwstuttgart.typecheck.*; import java.util.ArrayList; import java.util.List; import org.antlr.v4.runtime.tree.TerminalNode; - public class SyntaxTreeGenerator{ JavaClassRegistry reg = new JavaClassRegistry(); + String packageDecl = ""; public void getNames(Java8Parser.CompilationUnitContext ctx){ - String packageDecl = ""; if(ctx.packageDeclaration() != null){ for(TerminalNode t : ctx.packageDeclaration().Identifier()){ - packageDecl = packageDecl + "." + t.toString(); + this.packageDecl = this.packageDecl + "." + t.toString(); } - packageDecl = packageDecl.substring(1); + this.packageDecl = this.packageDecl.substring(1); } String nameString = ""; for (Java8Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()){ @@ -46,18 +45,25 @@ public class SyntaxTreeGenerator{ } } - - public SourceFile convert(Java8Parser.CompilationUnitContext ctx){ - List classes = new ArrayList<>(); - for(Java8Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()){ - ClassOrInterface newClass = convert(typeDecl.classDeclaration()); - classes.add(newClass); - } - return new SourceFile(classes); + public SourceFile convert(Java8Parser.CompilationUnitContext ctx){ + List classes = new ArrayList<>(); + for(Java8Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()){ + ClassOrInterface newClass = convert(typeDecl.classDeclaration()); + classes.add(newClass); } + return new SourceFile(classes); + } - private ClassOrInterface convert(Java8Parser.ClassDeclarationContext ctx) { - //TODO: Implement - return null; + private ClassOrInterface convert(Java8Parser.ClassDeclarationContext ctx) { + ClassOrInterface newClass = new ClassOrInterface(); + String name = ""; + if(this.packageDecl != ""){ + name = packageDecl + "." + ctx.normalClassDeclaration().Identifier().toString(); } + else{ + name = ctx.normalClassDeclaration().Identifier().toString(); + } + newClass.setClassName(new JavaClassName(name)); + return newClass; + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java b/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java index 2d1af147..d785d4d1 100755 --- a/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java +++ b/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java @@ -13,18 +13,24 @@ import java.util.List; /** * Stellt jede Art von Klasse dar. Auch abstrakte Klassen und Interfaces */ -public class ClassOrInterface extends GTVDeclarationContext implements IItemWithOffset, Generic -{ - - protected Modifiers modifiers; - protected JavaClassName name; - private Block class_block; - private List fielddecl = new ArrayList<>(); - private GenericDeclarationList genericClassParameters; - private int offset; - private RefType superClass; - protected boolean isInterface; - private List implementedInterfaces; - - +public class ClassOrInterface extends GTVDeclarationContext implements IItemWithOffset, Generic{ + protected Modifiers modifiers; + protected JavaClassName name; + private Block class_block; + private List fielddecl = new ArrayList<>(); + private GenericDeclarationList genericClassParameters; + private int offset; + private RefType superClass; + protected boolean isInterface; + private List implementedInterfaces; + + // Gets class name + public JavaClassName getClassName(){ + return this.name; + } + + // Sets class name. + public void setClassName(JavaClassName name){ + this.name = name; + } }