Begin rewriting of convert for TypeDecl.

This commit is contained in:
Jakob Herrmann 2017-01-16 23:32:12 +01:00
parent 4c79023889
commit 0db15bffa8
3 changed files with 54 additions and 25 deletions

View File

@ -10,6 +10,7 @@ import de.dhbwstuttgart.typecheck.*;
import java.util.Scanner; import java.util.Scanner;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.InputStream; import java.io.InputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
public class RunParser{ public class RunParser{
public static void main(String[] args){ public static void main(String[] args){
@ -24,14 +25,18 @@ public class RunParser{
Java8Parser parser = new Java8Parser(tokens); Java8Parser parser = new Java8Parser(tokens);
Java8Parser.CompilationUnitContext tree = parser.compilationUnit(); Java8Parser.CompilationUnitContext tree = parser.compilationUnit();
SyntaxTreeGenerator generator = new SyntaxTreeGenerator(); SyntaxTreeGenerator generator = new SyntaxTreeGenerator();
generator.getNames(tree);
SourceFile f = generator.convert((Java8Parser.CompilationUnitContext) tree); SourceFile f = generator.convert((Java8Parser.CompilationUnitContext) tree);
String pkgName = f.getPkgName();
System.out.println(pkgName);
for(ClassOrInterface c : f.KlassenVektor){ for(ClassOrInterface c : f.KlassenVektor){
System.out.println(c.getClassName().toString()); System.out.println(c.getClassName().toString());
} }
} }
catch(Exception e){ catch(java.util.NoSuchElementException e){
System.out.println("An exception occured which is unknown and on our TODO list."); System.out.println("Error: Source seems to be empty.");
}
catch(IOException e){
System.out.println("An exception occured which is on our TODO list.");
e.printStackTrace(); e.printStackTrace();
} }
} }

View File

@ -1,29 +1,33 @@
package de.dhbwstuttgart.parser; package de.dhbwstuttgart.parser;
import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.syntaxtree.*;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.modifier.*;
import de.dhbwstuttgart.syntaxtree.statement.Block;
import de.dhbwstuttgart.syntaxtree.statement.Expr;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.typecheck.*; import de.dhbwstuttgart.typecheck.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.antlr.v4.runtime.tree.TerminalNode; import org.antlr.v4.runtime.tree.TerminalNode;
public class SyntaxTreeGenerator{ public class SyntaxTreeGenerator{
JavaClassRegistry reg = new JavaClassRegistry(); JavaClassRegistry reg = new JavaClassRegistry();
String packageDecl = ""; String pkgName = null;
List<JavaClassName> imports = null;
public void getNames(Java8Parser.CompilationUnitContext ctx){ public void getNames(Java8Parser.CompilationUnitContext ctx){
if(ctx.packageDeclaration() != null){ if(ctx.packageDeclaration() != null){
this.pkgName = "";
for(TerminalNode t : ctx.packageDeclaration().Identifier()){ for(TerminalNode t : ctx.packageDeclaration().Identifier()){
this.packageDecl = this.packageDecl + "." + t.toString(); this.pkgName = this.pkgName + "." + t.toString();
} }
this.packageDecl = this.packageDecl.substring(1); this.pkgName = this.pkgName.substring(1);
} }
String nameString = ""; String nameString = "";
for (Java8Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()){ for (Java8Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()){
if(typeDecl.interfaceDeclaration() != null){ if(typeDecl.interfaceDeclaration() != null){
if(typeDecl.interfaceDeclaration().normalInterfaceDeclaration() != null){ if(typeDecl.interfaceDeclaration().normalInterfaceDeclaration() != null){
if(packageDecl != ""){ if(this.pkgName != null){
nameString = packageDecl + "." + typeDecl.interfaceDeclaration().normalInterfaceDeclaration().Identifier().toString(); nameString = this.pkgName + "." + typeDecl.interfaceDeclaration().normalInterfaceDeclaration().Identifier().toString();
} }
else{ else{
nameString = typeDecl.interfaceDeclaration().normalInterfaceDeclaration().Identifier().toString(); nameString = typeDecl.interfaceDeclaration().normalInterfaceDeclaration().Identifier().toString();
@ -33,8 +37,8 @@ public class SyntaxTreeGenerator{
} }
else{ else{
if(typeDecl.classDeclaration().normalClassDeclaration() != null){ if(typeDecl.classDeclaration().normalClassDeclaration() != null){
if(packageDecl != ""){ if(this.pkgName != ""){
nameString = packageDecl + "." + typeDecl.classDeclaration().normalClassDeclaration().Identifier().toString(); nameString = this.pkgName + "." + typeDecl.classDeclaration().normalClassDeclaration().Identifier().toString();
} }
else{ else{
nameString = typeDecl.classDeclaration().normalClassDeclaration().Identifier().toString(); nameString = typeDecl.classDeclaration().normalClassDeclaration().Identifier().toString();
@ -47,23 +51,39 @@ public class SyntaxTreeGenerator{
public SourceFile convert(Java8Parser.CompilationUnitContext ctx){ public SourceFile convert(Java8Parser.CompilationUnitContext ctx){
List<ClassOrInterface> classes = new ArrayList<>(); List<ClassOrInterface> classes = new ArrayList<>();
this.getNames(ctx);
for(Java8Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()){ for(Java8Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()){
ClassOrInterface newClass = convert(typeDecl.classDeclaration()); ClassOrInterface newClass = convert(typeDecl);
classes.add(newClass); classes.add(newClass);
} }
return new SourceFile(classes); return new SourceFile(this.pkgName, classes, this.imports);
} }
private ClassOrInterface convert(Java8Parser.ClassDeclarationContext ctx) { private ClassOrInterface convert(Java8Parser.TypeDeclarationContext ctx) {
ClassOrInterface newClass = new ClassOrInterface(); Java8Parser.ClassDeclarationContext cl = ctx.classDeclaration();
String name = ""; Java8Parser.InterfaceDeclarationContext id = ctx.interfaceDeclaration();
if(this.packageDecl != ""){ if(cl != null){
name = packageDecl + "." + ctx.normalClassDeclaration().Identifier().toString(); Java8Parser.NormalClassDeclarationContext nc = cl.normalClassDeclaration();
if(nc != null){
Modifiers modifiers = null;
JavaClassName name = null;
if(this.pkgName != null){
name = new JavaClassName(this.pkgName +"."+ nc.Identifier().toString());
}
else{
name = new JavaClassName(nc.Identifier().toString());
}
System.out.println("Created name" + name);
Block class_block = null;
List<Field> fielddecl = null;
GenericDeclarationList genericClassParameters = null;
int offset = 0;
RefType superClass = null;
Boolean isInterface = false;
List<RefType> implementedInterfaces = null;
return new ClassOrInterface(modifiers, name, class_block, fielddecl, genericClassParameters, offset, superClass, isInterface, implementedInterfaces);
}
} }
else{ return null;
name = ctx.normalClassDeclaration().Identifier().toString();
}
newClass.setClassName(new JavaClassName(name));
return newClass;
} }
} }

View File

@ -36,4 +36,8 @@ public class SourceFile extends SyntaxTreeNode{
public SourceFile(List<ClassOrInterface> classDefinitions, List<JavaClassName> imports){ public SourceFile(List<ClassOrInterface> classDefinitions, List<JavaClassName> imports){
this(null, classDefinitions, imports); this(null, classDefinitions, imports);
} }
public String getPkgName(){
return this.pkgName;
}
} }