Merge branch 'antlr' of ssh://gohorb.ba-horb.de/bahome/projekt/git/JavaCompilerCore into antlr

This commit is contained in:
JanUlrich 2016-12-15 15:35:26 +01:00
commit 5c6b001ae1
4 changed files with 58 additions and 148 deletions

View File

@ -0,0 +1,24 @@
package de.dhbwstuttgart.parser;
import org.antlr.v4.runtime.tree.*;
import de.dhbwstuttgart.syntaxtree.*;
public class CompilationUnitEvaluator extends Java8BaseListener{
@Override
public void enterCompilationUnit(Java8Parser.CompilationUnitContext ctx) {
System.out.println("SourceFile(");
}
@Override
public void exitCompilationUnit(Java8Parser.CompilationUnitContext ctx) {
System.out.println(")");
}
@Override
public void enterPackageDeclaration(Java8Parser.PackageDeclarationContext ctx) {
System.out.println("package(");
}
@Override
public void exitPackageDeclaration(Java8Parser.PackageDeclarationContext ctx) {
System.out.println(ctx.Identifier());
System.out.println(")");
}
}

View File

@ -1,130 +0,0 @@
package de.dhbwstuttgart.parser;
import de.dhbwstuttgart.typeinference.Menge;
import de.dhbwstuttgart.syntaxtree.type.RefType;
/**
* Stellt den Namen einer Java Klasse dar.
* Dieser kann auch den Packagenamen mit beinhalten: de.dhbwstuttgart.typeinference.Menge
*
* @author janulrich
*
*
*/
public class JavaClassName {
private String name;
private PackageName packageName;
/**
* TODO: JavaClassName sollten aus den Assumptions generiert werden.
* Diese wissen, welche Typen und Typnamen existieren und können direkt auf Korrektheit prüfen.
*/
JavaClassName(String name){
if(name == null)throw new NullPointerException();
String[] names = name.split("[.]");
boolean match = true;
if(names.length == 1){
//packageName = new PackageName();
this.name = name;
}else {
name = names[names.length-1];
Menge<String> packageNames = new Menge<String>();
for(int i = 0; i<names.length-1;i++){
packageNames.add(names[i]);
}
packageName = new PackageName(packageNames);
this.name = names[names.length-1];
}
}
//Auto-generated hashCode:
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
//result = prime * result
// + ((packageName == null) ? 0 : packageName.hashCode()); //PackageName does not infect hashCode
return result;
}
/**
* Namen sind nur gleich, wenn bei den beiden zu vergleichenden JavaClassNames auch das Package angegeben ist
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if(obj instanceof String)
return this.toString().equals(obj) || (this.name != null && this.name.equals(obj)); //Auch mit Strings als Klassennamen kompatibel TODO: sollte bald obsolet sein
if (getClass() != obj.getClass())
return false;
JavaClassName other = (JavaClassName) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (packageName != null && other.packageName != null) {
if (!packageName.equals(other.packageName))
return false;//Spezialfall, nicht beide Typen ¼ssen eindeutig mit Packagenamen angegeben werden
}
return true;
}
@Override
public String toString() {
return (packageName!=null ? packageName.toString() : "") + name;
}
}
class PackageName{
Menge<String> names = new Menge<String>();
public PackageName(Menge<String> packageNames) {
names = packageNames;
}
public PackageName() {
//Do nothing
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((names == null) ? 0 : names.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PackageName other = (PackageName) obj;
if (names == null) {
if (other.names != null)
return false;
} else if (!names.equals(other.names))
return false;
return true;
}
@Override
public String toString() {
String ret = "";
if(names == null)return "";
for(String n : names)ret+=n+".";
return ret;
}
}

View File

@ -1,18 +0,0 @@
package de.dhbwstuttgart.parser;
import java.util.ArrayList;
import java.util.List;
/**
* Speichert die Klassen im aktuellen Projektscope
*/
public class JavaClassRegistry {
public List<JavaClassName> existingClasses = new ArrayList<>();
public JavaClassName getName(String className) {
for(JavaClassName name : existingClasses){
if(name.toString().equals(className))return name;
}
throw new TypeNotPresentException(className, new Throwable());
}
}

View File

@ -0,0 +1,34 @@
package de.dhbwstuttgart.parser;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import de.dhbwstuttgart.syntaxtree.*;
import java.util.Scanner;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
public class RunParser{
public static void main(String[] args){
try{
Scanner sc = new Scanner(System.in);
String inputString = sc.nextLine();
while(sc.hasNextLine()) inputString = inputString + sc.nextLine();
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);
ParserRuleContext tree = parser.compilationUnit();
ParseTreeWalker walker = new ParseTreeWalker();
CompilationUnitEvaluator eval = new CompilationUnitEvaluator();
walker.walk(eval, tree);
}
catch(Exception e){
System.out.println("An exception occured which is unknown and on our TODO list.");
e.printStackTrace();
}
}
}