Merge mit Antlr

This commit is contained in:
JanUlrich 2017-04-20 18:55:34 +02:00
commit a9909d5f32
8 changed files with 51 additions and 30 deletions

24
src/build_compiler.sh Executable file
View File

@ -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:)."

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

@ -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<Class<?>> getClassesInPackage(String packageName) {
public static Set<Class<?>> getClassesInPackage(String packageName){
List<ClassLoader> classLoadersList = new LinkedList<ClassLoader>();
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())

View File

@ -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();

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.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<JavaClassName> 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<JavaClassName> 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<ClassOrInterface> classes = new ArrayList<>();
this.getNames(ctx);
this.setImports(ctx);
for(Java8Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()){
ClassOrInterface newClass;
if(typeDecl.classDeclaration() != null){

View File

@ -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");

View File

@ -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]));
}
}