Interfaces angefügt. Generierung von Generics im Parser geändert

This commit is contained in:
JanUlrich 2017-05-05 16:20:12 +02:00
parent 88ea14bbe8
commit 4261ce2603
13 changed files with 200 additions and 62 deletions

View File

@ -1,5 +1,6 @@
package de.dhbwstuttgart.core;
import de.dhbwstuttgart.parser.ClassNotFoundException;
import de.dhbwstuttgart.parser.JavaTXParser;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.SourceFile;

View File

@ -33,6 +33,7 @@ public class JavaTXParser {
}
//TODO: Wieso muss man das händisch anhängen?
ret.add("java.lang.Object");
return ret;
}
}

View File

@ -33,40 +33,7 @@ public class StatementGenerator {
this.generics = generics;
}
public Method convert(Java8Parser.MethodDeclarationContext methodDeclarationContext, JavaClassName parentClass) {
Java8Parser.MethodHeaderContext header = methodDeclarationContext.methodHeader();
String name = header.methodDeclarator().Identifier().getText();
GenericDeclarationList gtvDeclarations = new GenericDeclarationList(new ArrayList<>(), methodDeclarationContext.methodHeader().getStart());
if(methodDeclarationContext.methodHeader().typeParameters() != null){
gtvDeclarations = TypeGenerator.convert(methodDeclarationContext.methodHeader().typeParameters(), parentClass, name,reg, generics);
}
RefTypeOrTPHOrWildcardOrGeneric retType;
if(header.result() != null){
if(header.result().unannType() != null){
retType = TypeGenerator.convert(header.result().unannType(), reg, generics);
}
else retType = new de.dhbwstuttgart.syntaxtree.type.Void(header.result().getStart());
}else{
retType = TypePlaceholder.fresh(header.getStart());
}
int modifiers = SyntaxTreeGenerator.convert(methodDeclarationContext.methodModifier());
ParameterList parameterList = convert(header.methodDeclarator().formalParameterList());
Block block = null;
if(methodDeclarationContext.methodBody().block() == null){
if(! Modifier.isAbstract(modifiers)){
//TODO: Error! Abstrakte Methode ohne abstrakt Keyword
}
}else{
block = this.convert(methodDeclarationContext.methodBody().block());
}
if(parentClass.equals(new JavaClassName(name))){
return new Constructor(name, retType, modifiers, parameterList, block, gtvDeclarations, methodDeclarationContext.getStart());
}else{
return new Method(name, retType, modifiers, parameterList,block, gtvDeclarations, methodDeclarationContext.getStart());
}
}
private ParameterList convert(Java8Parser.FormalParameterListContext formalParameterListContext) {
public ParameterList convert(Java8Parser.FormalParameterListContext formalParameterListContext) {
List<FormalParameter> ret = new ArrayList<>();
List<Java8Parser.FormalParameterContext> fps = new ArrayList<>();
if(formalParameterListContext == null || formalParameterListContext.lastFormalParameter() == null)
@ -832,9 +799,9 @@ public class StatementGenerator {
funNParams.add(TypePlaceholder.fresh(expression.getStart()));//ret-Type
params.getFormalparalist().forEach(formalParameter -> //Für jeden Parameter einen TPH anfügen:
funNParams.add(TypePlaceholder.fresh(expression.getStart())));
RefType lambdaType = new RefType(reg.getName("Fun"+params.getFormalparalist().size()),
funNParams, expression.getStart());
RefTypeOrTPHOrWildcardOrGeneric lambdaType = TypePlaceholder.fresh(expression.getStart());
//RefType lambdaType = new RefType(reg.getName("Fun"+params.getFormalparalist().size()),
//funNParams, expression.getStart());
return new LambdaExpression(lambdaType, params, block, expression.getStart());
}
}

View File

@ -17,6 +17,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import jdk.internal.dynalink.support.TypeConverterFactory;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.TerminalNode;
@ -24,7 +25,6 @@ public class SyntaxTreeGenerator{
private JavaClassRegistry reg;
private String pkgName = "";
List<JavaClassName> imports = new ArrayList();
private GenericsRegistry generics = new GenericsRegistry();
public SyntaxTreeGenerator(JavaClassRegistry reg){
this.reg = reg;
@ -52,11 +52,13 @@ public class SyntaxTreeGenerator{
nameString = typeDecl.interfaceDeclaration().normalInterfaceDeclaration().Identifier().toString();
}
//Die Generic TypeParameter Definitionen Nicht! an die JavaClassName-Registry anfügen:
/* //Diese gelängen dadurch in den globalen Scope, was sie schließlich nicht sind
if(typeDecl.classDeclaration().normalClassDeclaration().typeParameters() != null){
for(Java8Parser.TypeParameterContext tp : typeDecl.classDeclaration().normalClassDeclaration().typeParameters().typeParameterList().typeParameter()){
//this.reg.add(tp.Identifier().toString());
}
}
*/
this.reg.add(nameString);
}
}
@ -68,12 +70,14 @@ public class SyntaxTreeGenerator{
else{
nameString = typeDecl.classDeclaration().normalClassDeclaration().Identifier().toString();
}
//Die Generic TypeParameter Definitionen ebenfalls an die JavaClassName-Registry anfügen:
//Die Generic TypeParameter Definitionen Nicht! an die JavaClassName-Registry anfügen:
/* //Diese gelängen dadurch in den globalen Scope, was sie schließlich nicht sind
if(typeDecl.classDeclaration().normalClassDeclaration().typeParameters() != null){
for(Java8Parser.TypeParameterContext tp : typeDecl.classDeclaration().normalClassDeclaration().typeParameters().typeParameterList().typeParameter()){
this.reg.add(tp.Identifier().toString());
}
}
*/
this.reg.add(nameString);
}
}
@ -180,6 +184,60 @@ public class SyntaxTreeGenerator{
return new SourceFile(this.pkgName, classes, this.imports);
}
public Method convert(Java8Parser.MethodDeclarationContext methodDeclarationContext, JavaClassName parentClass, GenericsRegistry generics) {
Java8Parser.MethodHeaderContext header = methodDeclarationContext.methodHeader();
int modifiers = SyntaxTreeGenerator.convert(methodDeclarationContext.methodModifier());
GenericsRegistry localGenerics = createGenerics(methodDeclarationContext.methodHeader().typeParameters(),
parentClass, header.methodDeclarator().Identifier().getText());
localGenerics.putAll(generics);
return convert(modifiers, header, methodDeclarationContext.methodBody(),parentClass, localGenerics);
}
public Method convert(Java8Parser.InterfaceMethodDeclarationContext ctx, JavaClassName parentClass, GenericsRegistry generics) {
Java8Parser.MethodHeaderContext header = ctx.methodHeader();
int modifiers = SyntaxTreeGenerator.convertInterfaceModifier(ctx.interfaceMethodModifier());
GenericsRegistry localGenerics = createGenerics(header.typeParameters(), parentClass, header.methodDeclarator().Identifier().getText());
localGenerics.putAll(generics);
return convert(modifiers, header, ctx.methodBody(),parentClass, localGenerics);
}
private Method convert(int modifiers, Java8Parser.MethodHeaderContext header, Java8Parser.MethodBodyContext body,
JavaClassName parentClass, GenericsRegistry localGenerics) {
StatementGenerator stmtGen = new StatementGenerator(reg, localGenerics);
String name = header.methodDeclarator().Identifier().getText();
GenericDeclarationList gtvDeclarations = new GenericDeclarationList(new ArrayList<>(), header.getStart());
if(header.typeParameters() != null){
gtvDeclarations = TypeGenerator.convert(header.typeParameters(), parentClass, name,reg, localGenerics);
}
RefTypeOrTPHOrWildcardOrGeneric retType;
if(header.result() != null){
if(header.result().unannType() != null){
retType = TypeGenerator.convert(header.result().unannType(), reg, localGenerics);
}
else retType = new de.dhbwstuttgart.syntaxtree.type.Void(header.result().getStart());
}else{
retType = TypePlaceholder.fresh(header.getStart());
}
ParameterList parameterList = stmtGen.convert(header.methodDeclarator().formalParameterList());
Block block = null;
if(body.block() == null){
if(! Modifier.isAbstract(modifiers)){
//TODO: Error! Abstrakte Methode ohne abstrakt Keyword
}
}else{
block = stmtGen.convert(body.block());
}
if(parentClass.equals(new JavaClassName(name))){
return new Constructor(name, retType, modifiers, parameterList, block, gtvDeclarations, header.getStart());
}else{
return new Method(name, retType, modifiers, parameterList,block, gtvDeclarations, header.getStart());
}
}
private ClassOrInterface convertClass(Java8Parser.ClassDeclarationContext ctx) {
ClassOrInterface newClass;
if(ctx.normalClassDeclaration() != null){
@ -200,10 +258,11 @@ public class SyntaxTreeGenerator{
}
}
JavaClassName name = reg.getName(ctx.Identifier().getText());
GenericsRegistry generics = createGenerics(ctx.typeParameters(), name, "");
Token offset = ctx.getStart();
GenericDeclarationList genericClassParameters = TypeGenerator.convert(ctx.typeParameters(), name, "",reg, generics);
List<Field> fielddecl = convertFields(ctx.classBody());
List<Method> methods = convertMethods(ctx.classBody(), name);
List<Field> fielddecl = convertFields(ctx.classBody(), generics);
List<Method> methods = convertMethods(ctx.classBody(), name, generics);
List<Constructor> konstruktoren = new ArrayList<>();
for(int i = 0; i<methods.size();i++){
Method m = methods.get(i);
@ -227,8 +286,22 @@ public class SyntaxTreeGenerator{
superClass = new ASTFactory(reg).createObjectClass().getType();
}
Boolean isInterface = false;
List<RefTypeOrTPHOrWildcardOrGeneric> implementedInterfaces = null;
return new ClassOrInterface(modifiers, name, fielddecl, methods, konstruktoren, genericClassParameters, superClass, isInterface, implementedInterfaces, offset);
List<RefTypeOrTPHOrWildcardOrGeneric> implementedInterfaces = convert(ctx.superinterfaces(), generics);
return new ClassOrInterface(modifiers, name, fielddecl, methods, konstruktoren, genericClassParameters, superClass,
isInterface, implementedInterfaces, offset);
}
private List<RefTypeOrTPHOrWildcardOrGeneric> convert(Java8Parser.SuperinterfacesContext ctx, GenericsRegistry generics) {
if(ctx == null)return new ArrayList<>();
return convert(ctx.interfaceTypeList(), generics);
}
private List<RefTypeOrTPHOrWildcardOrGeneric> convert(Java8Parser.InterfaceTypeListContext ctx, GenericsRegistry generics) {
List<RefTypeOrTPHOrWildcardOrGeneric> ret = new ArrayList<>();
for(Java8Parser.InterfaceTypeContext interfaceType : ctx.interfaceType()){
ret.add(TypeGenerator.convert(interfaceType.classType(), reg, generics));
}
return ret;
}
private Constructor generateStandardConstructor(String className, GenericDeclarationList classGenerics, Token offset){
@ -244,7 +317,8 @@ public class SyntaxTreeGenerator{
throw new NotImplementedException();
}
private List<Method> convertMethods(Java8Parser.ClassBodyContext classBodyContext, JavaClassName parentClass) {
private List<Method> convertMethods(Java8Parser.ClassBodyContext classBodyContext,
JavaClassName parentClass, GenericsRegistry generics) {
List<Method> ret = new ArrayList<>();
for(Java8Parser.ClassBodyDeclarationContext classMember : classBodyContext.classBodyDeclaration()){
if(classMember.classMemberDeclaration() != null){
@ -252,21 +326,21 @@ public class SyntaxTreeGenerator{
if(classMemberDeclarationContext.fieldDeclaration() != null){
//Do nothing!
}else if(classMemberDeclarationContext.methodDeclaration()!= null){
StatementGenerator stmtGen = new StatementGenerator(reg, generics);
ret.add(stmtGen.convert(classMemberDeclarationContext.methodDeclaration(), parentClass));
ret.add(this.convert(classMemberDeclarationContext.methodDeclaration(), parentClass, generics));
}
}
}
return ret;
}
private List<Field> convertFields(Java8Parser.ClassBodyContext classBodyContext) {
private List<Field> convertFields(Java8Parser.ClassBodyContext classBodyContext, GenericsRegistry generics) {
List<Field> ret = new ArrayList<>();
for(Java8Parser.ClassBodyDeclarationContext classMember : classBodyContext.classBodyDeclaration()){
if(classMember.classMemberDeclaration() != null){
Java8Parser.ClassMemberDeclarationContext classMemberDeclarationContext = classMember.classMemberDeclaration();
if(classMemberDeclarationContext.fieldDeclaration() != null){
ret.addAll(convert(classMember.classMemberDeclaration().fieldDeclaration()));
ret.addAll(convert(classMember.classMemberDeclaration().fieldDeclaration(), generics));
}else if(classMemberDeclarationContext.methodDeclaration()!= null){
//Do nothing!
}
@ -283,7 +357,15 @@ public class SyntaxTreeGenerator{
return ret;
}
private List<? extends Field> convert(Java8Parser.FieldDeclarationContext fieldDeclarationContext) {
public static int convertInterfaceModifier(List<Java8Parser.InterfaceMethodModifierContext> methodModifierContexts) {
int ret = 0;
for(Java8Parser.InterfaceMethodModifierContext mod : methodModifierContexts){
if(mod.annotation() == null)convertModifier(mod.getText());
}
return ret;
}
private List<? extends Field> convert(Java8Parser.FieldDeclarationContext fieldDeclarationContext, GenericsRegistry generics) {
List<Field> ret = new ArrayList<>();
int modifiers = 0;
for(Java8Parser.FieldModifierContext fieldModifierContext : fieldDeclarationContext.fieldModifier()){
@ -333,7 +415,7 @@ public class SyntaxTreeGenerator{
modifiers.put(Modifier.toString(Modifier.INTERFACE), Modifier.INTERFACE);
int ret = 0;
for(String m : modifiers.keySet()){
if(modifier.startsWith(m))ret+=modifiers.get(m);
if(modifier.contains(m))ret+=modifiers.get(m);
}
return ret;
}
@ -347,13 +429,86 @@ public class SyntaxTreeGenerator{
if(ctx.annotation() != null)return 0;
return convertModifier(ctx.getText());
}
private int convert(Java8Parser.InterfaceModifierContext ctx) {
if(ctx.annotation() != null)return 0;
return convertModifier(ctx.getText());
}
private ClassOrInterface convertEnum(Java8Parser.EnumDeclarationContext ctx){
return null;
}
private ClassOrInterface convertInterface(Java8Parser.InterfaceDeclarationContext ctx){
return null;
if(ctx.normalInterfaceDeclaration() != null){
return convertNormal(ctx.normalInterfaceDeclaration());
}else{
throw new NotImplementedException();
}
}
private ClassOrInterface convertNormal(Java8Parser.NormalInterfaceDeclarationContext ctx) {
int modifiers = 0;
if(ctx.interfaceModifier() != null){
for( Java8Parser.InterfaceModifierContext mod : ctx.interfaceModifier()){
int newModifier = convert(mod);
modifiers += newModifier;
}
}
JavaClassName name = reg.getName(ctx.Identifier().getText());
GenericsRegistry generics = createGenerics(ctx.typeParameters(), name, "");
List<Field> fields = convertFields(ctx.interfaceBody());
List<Method> methods = convertMethods(ctx.interfaceBody(), name, generics);
GenericDeclarationList genericParams = TypeGenerator.convert(ctx.typeParameters(), name, "",reg, generics);
RefType superClass = new ASTFactory(reg).createObjectClass().getType();
List<RefTypeOrTPHOrWildcardOrGeneric> extendedInterfaces = convert(ctx.extendsInterfaces(), generics);
return new ClassOrInterface(modifiers, name, fields, methods, new ArrayList<>(),
genericParams, superClass, true, extendedInterfaces, ctx.getStart());
}
private GenericsRegistry createGenerics(Java8Parser.TypeParametersContext ctx, JavaClassName parentClass, String parentMethod) {
GenericsRegistry ret = new GenericsRegistry();
if(ctx == null || ctx.typeParameterList() == null)return ret;
for(Java8Parser.TypeParameterContext tp : ctx.typeParameterList().typeParameter()){
TypeGenerator.convert(tp, parentClass, parentMethod, reg, ret);
}
return ret;
}
private List<RefTypeOrTPHOrWildcardOrGeneric> convert(Java8Parser.ExtendsInterfacesContext extendsInterfacesContext, GenericsRegistry generics) {
if(extendsInterfacesContext == null)return new ArrayList<>();
return convert(extendsInterfacesContext.interfaceTypeList(), generics);
}
private List<Method> convertMethods(Java8Parser.InterfaceBodyContext interfaceBodyContext,
JavaClassName parentClass, GenericsRegistry generics) {
List<Method> ret = new ArrayList<>();
for(Java8Parser.InterfaceMemberDeclarationContext member : interfaceBodyContext.interfaceMemberDeclaration()){
if(member.interfaceMethodDeclaration() != null){
ret.add(this.convert(member.interfaceMethodDeclaration(), parentClass, generics));
//new Method(name, type, modifier, params, null, genericDecls, member.interfaceMethodDeclaration().getStart());
}else{
throw new NotImplementedException();
}
}
return ret;
}
private List<Field> convertFields(Java8Parser.InterfaceBodyContext interfaceBodyContext) {
List<Field> ret = new ArrayList<>();
for(Java8Parser.InterfaceMemberDeclarationContext member : interfaceBodyContext.interfaceMemberDeclaration()){
if(member.constantDeclaration() != null){
//TODO: Erstelle hier ein Feld!
throw new NotImplementedException();
}
}
return ret;
}
}

View File

@ -125,4 +125,9 @@ public class TypeGenerator {
}
return ret;
}
public static RefTypeOrTPHOrWildcardOrGeneric convert(Java8Parser.ClassTypeContext ctx, JavaClassRegistry reg, GenericsRegistry generics) {
if(ctx.classOrInterfaceType() != null)throw new NotImplementedException();
return convertTypeName(ctx.Identifier().getText(), ctx.typeArguments(), ctx.getStart(), reg, generics);
}
}

View File

@ -99,7 +99,8 @@ public class UnifyTypeFactory {
}
public static UnifyType convert(GenericRefType t){
return new PlaceholderType(TypePlaceholder.fresh(t.getOffset()).getName());
throw new NotImplementedException();
//return new PlaceholderType(TypePlaceholder.fresh(t.getOffset()).getName());
}
public static UnifyType convert(WildcardType t){

View File

@ -48,7 +48,8 @@ public class LambdaExpression extends Expression implements TypeScope {
@Override
public RefTypeOrTPHOrWildcardOrGeneric getReturnType() {
RefType type = (RefType) this.getType();
return type.getParaList().get(0);
//RefType type = (RefType) this.getType();
//return type.getParaList().get(0);
return methodBody.getType();
}
}

View File

@ -4,9 +4,17 @@ String var;
methode(){
return () -> (f) -> {
f.apply(this,var);
f.apply(this, var);
return var;
};
}
}
interface Fun1<A,B>{
A apply(B b);
}
interface Fun2<A,B,C>{
A apply(B b, C c);
}

View File

@ -1,5 +1,6 @@
package parser;
import de.dhbwstuttgart.parser.ClassNotFoundException;
import de.dhbwstuttgart.parser.JavaTXParser;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.Field;

View File

@ -1,5 +1,6 @@
package parser;
import de.dhbwstuttgart.parser.ClassNotFoundException;
import de.dhbwstuttgart.parser.JavaTXParser;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.Field;
@ -14,7 +15,7 @@ public class FieldTest {
private static final String rootDirectory = System.getProperty("user.dir")+"/test/parser/";
@Test
public void test() throws IOException, ClassNotFoundException {
public void test() throws IOException, ClassNotFoundException, ClassNotFoundException {
JavaTXParser parser = new JavaTXParser();
SourceFile f = parser.parse(new File(rootDirectory + "FieldVarTest.jav"));

View File

@ -30,10 +30,7 @@ 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,7 +1,6 @@
package parser;
import de.dhbwstuttgart.parser.JavaTXParser;
import de.dhbwstuttgart.parser.RunParser;
import org.junit.Test;
import java.io.File;

View File

@ -1,6 +1,7 @@
package typeinference;
import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.parser.ClassNotFoundException;
import org.junit.Test;
import java.io.File;
@ -18,8 +19,8 @@ public class JavaTXCompilerTest {
JavaTXCompiler compiler = new JavaTXCompiler();
//compiler.parse(new File(rootDirectory+"Methods.jav"));
//compiler.parse(new File(rootDirectory+"Generics.jav"));
compiler.parse(new File(rootDirectory+"MethodsEasy.jav"));
//compiler.parse(new File(rootDirectory+"Lambda.jav"));
//compiler.parse(new File(rootDirectory+"MethodsEasy.jav"));
compiler.parse(new File(rootDirectory+"Lambda.jav"));
compiler.typeInference();
}
}