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.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class RunParser{
public static void main(String[] args){
@ -24,14 +25,18 @@ public class RunParser{
Java8Parser parser = new Java8Parser(tokens);
Java8Parser.CompilationUnitContext tree = parser.compilationUnit();
SyntaxTreeGenerator generator = new SyntaxTreeGenerator();
generator.getNames(tree);
SourceFile f = generator.convert((Java8Parser.CompilationUnitContext) tree);
String pkgName = f.getPkgName();
System.out.println(pkgName);
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.");
catch(java.util.NoSuchElementException e){
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();
}
}

View File

@ -1,29 +1,33 @@
package de.dhbwstuttgart.parser;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.*;
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 java.util.ArrayList;
import java.util.List;
import org.antlr.v4.runtime.tree.TerminalNode;
public class SyntaxTreeGenerator{
JavaClassRegistry reg = new JavaClassRegistry();
String packageDecl = "";
String pkgName = null;
List<JavaClassName> imports = null;
public void getNames(Java8Parser.CompilationUnitContext ctx){
if(ctx.packageDeclaration() != null){
this.pkgName = "";
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 = "";
for (Java8Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()){
if(typeDecl.interfaceDeclaration() != null){
if(typeDecl.interfaceDeclaration().normalInterfaceDeclaration() != null){
if(packageDecl != ""){
nameString = packageDecl + "." + typeDecl.interfaceDeclaration().normalInterfaceDeclaration().Identifier().toString();
if(this.pkgName != null){
nameString = this.pkgName + "." + typeDecl.interfaceDeclaration().normalInterfaceDeclaration().Identifier().toString();
}
else{
nameString = typeDecl.interfaceDeclaration().normalInterfaceDeclaration().Identifier().toString();
@ -33,8 +37,8 @@ public class SyntaxTreeGenerator{
}
else{
if(typeDecl.classDeclaration().normalClassDeclaration() != null){
if(packageDecl != ""){
nameString = packageDecl + "." + typeDecl.classDeclaration().normalClassDeclaration().Identifier().toString();
if(this.pkgName != ""){
nameString = this.pkgName + "." + typeDecl.classDeclaration().normalClassDeclaration().Identifier().toString();
}
else{
nameString = typeDecl.classDeclaration().normalClassDeclaration().Identifier().toString();
@ -47,23 +51,39 @@ public class SyntaxTreeGenerator{
public SourceFile convert(Java8Parser.CompilationUnitContext ctx){
List<ClassOrInterface> classes = new ArrayList<>();
this.getNames(ctx);
for(Java8Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()){
ClassOrInterface newClass = convert(typeDecl.classDeclaration());
ClassOrInterface newClass = convert(typeDecl);
classes.add(newClass);
}
return new SourceFile(classes);
return new SourceFile(this.pkgName, classes, this.imports);
}
private ClassOrInterface convert(Java8Parser.ClassDeclarationContext ctx) {
ClassOrInterface newClass = new ClassOrInterface();
String name = "";
if(this.packageDecl != ""){
name = packageDecl + "." + ctx.normalClassDeclaration().Identifier().toString();
private ClassOrInterface convert(Java8Parser.TypeDeclarationContext ctx) {
Java8Parser.ClassDeclarationContext cl = ctx.classDeclaration();
Java8Parser.InterfaceDeclarationContext id = ctx.interfaceDeclaration();
if(cl != null){
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{
name = ctx.normalClassDeclaration().Identifier().toString();
}
newClass.setClassName(new JavaClassName(name));
return newClass;
return null;
}
}

View File

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