diff --git a/src/build_compiler.sh b/src/build_compiler.sh new file mode 100755 index 00000000..6ccc2dc4 --- /dev/null +++ b/src/build_compiler.sh @@ -0,0 +1,24 @@ +#!/bin/bash +# A basic script to compile the necessary packages and their subpackages to work with the parser. +# Messages are logged to stderr. +>&2 echo "Building de.dhbwstuttgart.typecheck..." +javac -d ../bin ./de/dhbwstuttgart/typecheck/*.java +>&2 echo "Building de.dhbwstuttgart.syntaxtree..." +javac -d ../bin ./de/dhbwstuttgart/syntaxtree/*.java +>&2 echo "Building de.dhbwstuttgart.syntaxtree.factory..." +javac -d ../bin ./de/dhbwstuttgart/syntaxtree/factory/*.java +>&2 echo "Building de.dhbwstuttgart.syntaxtree.operator..." +javac -d ../bin ./de/dhbwstuttgart/syntaxtree/operator/*.java +>&2 echo "Building de.dhbwstuttgart.syntaxtree.statement..." +javac -d ../bin ./de/dhbwstuttgart/syntaxtree/statement/*.java +>&2 echo "Building de.dhbwstuttgart.syntaxtree.statement.literal..." +javac -d ../bin ./de/dhbwstuttgart/syntaxtree/statement/literal/*.java +>&2 echo "Building de.dhbwstuttgart.syntaxtree.type..." +javac -d ../bin ./de/dhbwstuttgart/syntaxtree/type/*.java +>&2 echo "Building de.dhbwstuttgart.parser..." +javac -d ../bin ./de/dhbwstuttgart/parser/*.java +>&2 echo "Building de.dhbwstuttgart.parser.SyntaxTreeGenerator..." +javac -d ../bin ./de/dhbwstuttgart/parser/SyntaxTreeGenerator/*.java +>&2 echo "Building de.dhbwstuttgart.parser.antlr..." +javac -d ../bin ./de/dhbwstuttgart/parser/antlr/*.java +echo "Done. Now its your turn to debug:)." 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/PackageCrawler.java b/src/de/dhbwstuttgart/parser/PackageCrawler.java index 18bcd444..7028c8e7 100644 --- a/src/de/dhbwstuttgart/parser/PackageCrawler.java +++ b/src/de/dhbwstuttgart/parser/PackageCrawler.java @@ -12,7 +12,7 @@ import java.util.LinkedList; import java.util.List; import java.util.ArrayList; import java.util.Set; - +import java.lang.InterruptedException; /** * Hilft beim Durchsuchen von Packages * Benutzt die Reflections-Library (https://github.com/ronmamo/reflections) @@ -20,10 +20,11 @@ import java.util.Set; */ public class PackageCrawler { - public static Set> getClassesInPackage(String packageName) { + public static Set> getClassesInPackage(String packageName){ List classLoadersList = new LinkedList(); - classLoadersList.add(ClasspathHelper.contextClassLoader()); + classLoadersList.add(Thread.currentThread().getContextClassLoader()); classLoadersList.add(ClasspathHelper.staticClassLoader()); + classLoadersList.add(Thread.currentThread().getContextClassLoader().getParent()); Reflections reflections = new Reflections(new ConfigurationBuilder() .setScanners(new SubTypesScanner(false /* don't exclude Object.class */), new ResourcesScanner()) diff --git a/src/de/dhbwstuttgart/parser/RunParser.java b/src/de/dhbwstuttgart/parser/RunTXParser.java similarity index 65% rename from src/de/dhbwstuttgart/parser/RunParser.java rename to src/de/dhbwstuttgart/parser/RunTXParser.java index 4480eb48..8d4c6d9e 100644 --- a/src/de/dhbwstuttgart/parser/RunParser.java +++ b/src/de/dhbwstuttgart/parser/RunTXParser.java @@ -17,22 +17,11 @@ import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; -public class RunParser{ +public class RunTXParser{ 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); + JavaTXParser parser = new JavaTXParser(); + SourceFile f = parser.parse(new File(args[0])); String pkgName = f.getPkgName(); System.out.println("package: " + pkgName); System.out.println("Imports:"); @@ -54,12 +43,12 @@ public class RunParser{ catch(java.util.NoSuchElementException e){ System.out.println("Error: Source seems to be empty."); } + catch(ClassNotFoundException e){ + System.out.println("Class not found."); + } 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 35c0d94e..5b1235b5 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.PackageCrawler; import de.dhbwstuttgart.parser.antlr.Java8Parser; import de.dhbwstuttgart.syntaxtree.*; @@ -108,7 +108,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){ @@ -127,7 +127,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); @@ -137,7 +137,7 @@ public class SyntaxTreeGenerator{ return ret; } else{ - throw new InvalidClassNameException(); + throw new ClassNotFoundException(); } } @@ -163,9 +163,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){ diff --git a/test/parser/GeneralParserTest.java b/test/parser/GeneralParserTest.java index 53cb6f7b..fc293d13 100644 --- a/test/parser/GeneralParserTest.java +++ b/test/parser/GeneralParserTest.java @@ -30,7 +30,10 @@ public class GeneralParserTest{ filenames.add("ImportTest.jav"); filenames.add("CastTest.jav"); filenames.add("StatementsTest.jav"); +<<<<<<< HEAD //filenames.add("Methods.jav"); +======= +>>>>>>> f05222fb563282945ba395047f54f6b3059b10c5 filenames.add("ImportTestGeneric.jav"); filenames.add("CastTest.jav"); //filenames.add("BoundedParameter.jav"); diff --git a/test/parser/RunParserTest.java b/test/parser/RunParserTest.java index dd8db38e..5effe244 100644 --- a/test/parser/RunParserTest.java +++ b/test/parser/RunParserTest.java @@ -1,8 +1,11 @@ package parser; +import de.dhbwstuttgart.parser.JavaTXParser; import de.dhbwstuttgart.parser.RunParser; import org.junit.Test; +import java.io.File; + import static org.junit.Assert.*; public class RunParserTest { @@ -13,6 +16,6 @@ public class RunParserTest { public void testMain() throws Exception { String[] args = new String[1]; args[0] = rootDirectory+"ImportTest2.jav"; - RunParser.main(args); + new JavaTXParser().parse(new File(args[0])); } } \ No newline at end of file