Some cleanup to be able to recompile, i.e:

* Remove handling of superclasses which needs further debugging
* add missing exceptions
* remove ClassFinder as it is not used and needs debugging
This commit is contained in:
Jakob Herrmann 2017-04-20 17:15:52 +02:00
parent 5cc418371d
commit f05222fb56
4 changed files with 11 additions and 83 deletions

View File

@ -0,0 +1,4 @@
package de.dhbwstuttgart.parser;
public class ClassNotFoundException extends Exception{
}

View File

@ -1,4 +0,0 @@
package de.dhbwstuttgart.parser;
public class InvalidClassNameException extends Exception{
}

View File

@ -1,68 +0,0 @@
package de.dhbwstuttgart.parser;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.SyntaxTreeGenerator;
import de.dhbwstuttgart.parser.antlr.Java8Lexer;
import de.dhbwstuttgart.parser.antlr.Java8Parser;
import de.dhbwstuttgart.typecheck.JavaClassName;
import de.dhbwstuttgart.typecheck.JavaClassRegistry;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import de.dhbwstuttgart.syntaxtree.*;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
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){
try{
File file = new File(args[0]);
Scanner sc = new Scanner(file);
String inputString = sc.nextLine();
while(sc.hasNextLine()) inputString = inputString + sc.nextLine() + "\n";
InputStream stream = new ByteArrayInputStream(inputString.getBytes(StandardCharsets.UTF_8));
ANTLRInputStream input = new ANTLRInputStream(stream);
Java8Lexer lexer = new Java8Lexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
Java8Parser parser = new Java8Parser(tokens);
Java8Parser.CompilationUnitContext tree = parser.compilationUnit();
SyntaxTreeGenerator generator = new SyntaxTreeGenerator(new JavaClassRegistry(new ArrayList<>()));
generator.setImports(tree);
SourceFile f = generator.convert((Java8Parser.CompilationUnitContext) tree);
String pkgName = f.getPkgName();
System.out.println("package: " + pkgName);
System.out.println("Imports:");
for(JavaClassName c : f.getImports()){
System.out.println(c.toString());
}
System.out.println("classes:");
for(ClassOrInterface c : f.getClasses()){
int mod = c.getModifiers();
System.out.println(Modifier.toString(mod));
System.out.println(c.getClassName().toString());
System.out.println("{");
for(Field field : c.getFieldDecl()){
System.out.println(field.getName());
}
System.out.println("}");
}
}
catch(java.util.NoSuchElementException e){
System.out.println("Error: Source seems to be empty.");
}
catch(FileNotFoundException e){
System.out.println("File not found.");
}
catch(InvalidClassNameException e){
e.printStackTrace();
}
catch(IOException e){
System.out.println("An exception occured which is on our TODO list.");
e.printStackTrace();
}
}
}

View File

@ -1,7 +1,7 @@
package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.parser.InvalidClassNameException;
import de.dhbwstuttgart.parser.ClassNotFoundException;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.PackageCrawler;
import de.dhbwstuttgart.parser.antlr.Java8Parser;
@ -109,7 +109,7 @@ public class SyntaxTreeGenerator{
return ret;
}
public void setImports(Java8Parser.CompilationUnitContext ctx) throws InvalidClassNameException {
public void setImports(Java8Parser.CompilationUnitContext ctx) throws ClassNotFoundException {
List<JavaClassName> newImports = new ArrayList();
for(Java8Parser.ImportDeclarationContext importDeclCtx : ctx.importDeclaration()){
if(importDeclCtx.singleTypeImportDeclaration() != null){
@ -128,7 +128,7 @@ public class SyntaxTreeGenerator{
this.imports.addAll(newImports);
}
private JavaClassName convertSingleTypeImportDeclaration(Java8Parser.SingleTypeImportDeclarationContext ctx) throws InvalidClassNameException{
private JavaClassName convertSingleTypeImportDeclaration(Java8Parser.SingleTypeImportDeclarationContext ctx) throws ClassNotFoundException{
String typeName = convertTypeName(ctx.typeName());
String packageName = getPackageFromClass(typeName);
List<JavaClassName> classes = PackageCrawler.getClassNames(packageName);
@ -138,7 +138,7 @@ public class SyntaxTreeGenerator{
return ret;
}
else{
throw new InvalidClassNameException();
throw new ClassNotFoundException();
}
}
@ -164,9 +164,10 @@ public class SyntaxTreeGenerator{
return ret;
}
public SourceFile convert(Java8Parser.CompilationUnitContext ctx){
public SourceFile convert(Java8Parser.CompilationUnitContext ctx) throws ClassNotFoundException{
List<ClassOrInterface> classes = new ArrayList<>();
this.getNames(ctx);
this.setImports(ctx);
for(Java8Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()){
ClassOrInterface newClass;
if(typeDecl.classDeclaration() != null){
@ -205,12 +206,7 @@ public class SyntaxTreeGenerator{
List<Method> methods = convertMethods(ctx.classBody(), name);
Token offset = ctx.getStart();
RefType superClass ;
if(ctx.superclass() != null){
superClass = convert(ctx.superclass());
}else{
superClass = new ASTFactory(reg).createObjectClass().getType();
}
RefType superClass = null;
Boolean isInterface = false;
List<RefTypeOrTPHOrWildcardOrGeneric> implementedInterfaces = null;
return new ClassOrInterface(modifiers, name, fielddecl, methods, genericClassParameters, superClass, isInterface, implementedInterfaces, offset);