From f05222fb563282945ba395047f54f6b3059b10c5 Mon Sep 17 00:00:00 2001 From: Jakob Herrmann Date: Thu, 20 Apr 2017 17:15:52 +0200 Subject: [PATCH] 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 --- .../parser/ClassNotFoundException.java | 4 ++ .../parser/InvalidClassNameException.java | 4 -- src/de/dhbwstuttgart/parser/RunParser.java | 68 ------------------- .../SyntaxTreeGenerator.java | 18 ++--- 4 files changed, 11 insertions(+), 83 deletions(-) create mode 100644 src/de/dhbwstuttgart/parser/ClassNotFoundException.java delete mode 100644 src/de/dhbwstuttgart/parser/InvalidClassNameException.java delete mode 100644 src/de/dhbwstuttgart/parser/RunParser.java diff --git a/src/de/dhbwstuttgart/parser/ClassNotFoundException.java b/src/de/dhbwstuttgart/parser/ClassNotFoundException.java new file mode 100644 index 00000000..08609467 --- /dev/null +++ b/src/de/dhbwstuttgart/parser/ClassNotFoundException.java @@ -0,0 +1,4 @@ +package de.dhbwstuttgart.parser; +public class ClassNotFoundException extends Exception{ + +} diff --git a/src/de/dhbwstuttgart/parser/InvalidClassNameException.java b/src/de/dhbwstuttgart/parser/InvalidClassNameException.java deleted file mode 100644 index 97115416..00000000 --- a/src/de/dhbwstuttgart/parser/InvalidClassNameException.java +++ /dev/null @@ -1,4 +0,0 @@ -package de.dhbwstuttgart.parser; -public class InvalidClassNameException extends Exception{ - -} diff --git a/src/de/dhbwstuttgart/parser/RunParser.java b/src/de/dhbwstuttgart/parser/RunParser.java deleted file mode 100644 index 4480eb48..00000000 --- a/src/de/dhbwstuttgart/parser/RunParser.java +++ /dev/null @@ -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(); - } - } -} diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java index 68b67fe5..c6f8bd54 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java @@ -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 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 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 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 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 implementedInterfaces = null; return new ClassOrInterface(modifiers, name, fielddecl, methods, genericClassParameters, superClass, isInterface, implementedInterfaces, offset);