From 40d0d6b63eddef94f922ea2491ebeacbfbdd968f Mon Sep 17 00:00:00 2001 From: luca9913 Date: Sun, 5 Feb 2023 09:16:59 +0100 Subject: [PATCH] =?UTF-8?q?TypeGenerator=20f=C3=BCr=20Java17=20Grammatik?= =?UTF-8?q?=20angepasst?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SyntaxTreeGenerator.java | 10 ++ .../SyntaxTreeGenerator/TypeGenerator.java | 113 ++++-------------- 2 files changed, 32 insertions(+), 91 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java index 15280703..4e1790c3 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java @@ -33,6 +33,16 @@ import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.tree.ParseTree; import org.antlr.v4.runtime.tree.TerminalNode; +/* + * TODO: Für additional type bounds mit '&', muss im SyntaxTreeGenerator + * abgefangen + * werden + * private static RefTypeOrTPHOrWildcardOrGeneric + * convert(Java17Parser.InterfaceTypeContext interfaceTypeContext) { + * throw new NotImplementedException(); + * } + */ + public class SyntaxTreeGenerator { private JavaClassRegistry reg; private final GenericsRegistry globalGenerics; diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java index a18202b7..7798c188 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java @@ -3,6 +3,9 @@ package de.dhbwstuttgart.parser.SyntaxTreeGenerator; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.exceptions.TypeinferenceException; import de.dhbwstuttgart.parser.antlr.Java17Parser; +import de.dhbwstuttgart.parser.antlr.Java17Parser.IdentifierContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.TypeArgumentsContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.TypeTypeContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.WildcardTypeContext; import de.dhbwstuttgart.parser.scope.GenericsRegistry; import de.dhbwstuttgart.parser.scope.JavaClassName; @@ -27,7 +30,7 @@ import java.util.regex.Pattern; public class TypeGenerator { public static RefTypeOrTPHOrWildcardOrGeneric convert( - Java17Parser.ClassOrInterfaceTypeContext unannClassOrInterfaceTypeContext, JavaClassRegistry reg, + Java17Parser.ClassOrInterfaceTypeContext classOrInterfaceTypeContext, JavaClassRegistry reg, GenericsRegistry generics) { Java17Parser.TypeArgumentsContext arguments = null; /* @@ -48,9 +51,15 @@ public class TypeGenerator { * Problem sind hier die verschachtelten Typen mit verschachtelten Typargumenten * Beispiel: Typ.InnererTyp */ - String name = unannClassOrInterfaceTypeContext.getText(); - if (name.contains("<")) { - name = name.split("<")[0]; // Der Typ ist alles vor den ersten Argumenten + if (classOrInterfaceTypeContext.typeArguments().size() > 1) + throw new NotImplementedException(); + + String name = ""; + for (IdentifierContext id : classOrInterfaceTypeContext.identifier()) { + name += id.getText(); + } + name += classOrInterfaceTypeContext.typeIdentifier(); + if (classOrInterfaceTypeContext.getStop().getText().equals(">")) { /* * Fuer Debug-Zwecke * unannClassOrInterfaceTypeContext. @@ -80,61 +89,11 @@ public class TypeGenerator { * //unannClassOrInterfaceTypeContext. * unannInterfaceType_lfno_unannClassOrInterfaceType().getText(); */ - int lastElement = new ArrayList<>( - unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType()).size() - 1; - if (lastElement >= 0) {// qualifizierter Name z.B.: java.util.Vector - arguments = unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType(lastElement) - .typeArguments(); - } else { // unqualifizierter Name z.B.: Vector - arguments = unannClassOrInterfaceTypeContext.unannClassType_lfno_unannClassOrInterfaceType() - .typeArguments(); - } + List typeargs = classOrInterfaceTypeContext.typeArguments(); + arguments = typeargs.size() != 0 ? classOrInterfaceTypeContext.typeArguments(0) : null; } - return convertTypeName(name, arguments, unannClassOrInterfaceTypeContext.getStart(), reg, generics); + return convertTypeName(name, arguments, classOrInterfaceTypeContext.getStart(), reg, generics); } - // TODO: Merge with method above - /* - * ClassType und UnannClassTypes waren in alter Grammatik getrennt, jetzt - * zusammen unter ClassOrInterfaceType - * - * private static RefTypeOrTPHOrWildcardOrGeneric convert( - * Java17Parser.ClassOrInterfaceTypeContext classOrInterfaceTypeContext, - * JavaClassRegistry reg, - * GenericsRegistry generics) { - * Java17Parser.ClassType_lfno_classOrInterfaceTypeContext ctx = - * classOrInterfaceTypeContext - * .classType_lfno_classOrInterfaceType(); - * if (ctx.typeArguments() != null && - * classOrInterfaceTypeContext.classType_lf_classOrInterfaceType().size() > 0) - * throw new NotImplementedException(); - * String typeName = ctx.Identifier().toString(); - * Java17Parser.ClassType_lf_classOrInterfaceTypeContext nextCtx = null; - * for (Java17Parser.ClassType_lf_classOrInterfaceTypeContext forEachCtx : - * classOrInterfaceTypeContext - * .classType_lf_classOrInterfaceType()) { - * nextCtx = forEachCtx; - * typeName += "." + forEachCtx.Identifier().toString(); - * } - * Java17Parser.TypeArgumentsContext arguments = nextCtx != null ? - * nextCtx.typeArguments() : ctx.typeArguments(); - * return convertTypeName(typeName, arguments, - * classOrInterfaceTypeContext.getStart(), reg, generics); - * } - * - * private static RefTypeOrTPHOrWildcardOrGeneric - * convert(Java17Parser.InterfaceTypeContext interfaceTypeContext) { - * throw new NotImplementedException(); - * } - * - * public static RefTypeOrTPHOrWildcardOrGeneric - * convert(Java17Parser.ClassTypeContext ctx, JavaClassRegistry reg, - * GenericsRegistry generics) { - * if (ctx.classOrInterfaceType() != null) - * throw new NotImplementedException(); - * return convertTypeName(ctx.Identifier().getText(), ctx.typeArguments(), - * ctx.getStart(), reg, generics); - * } - */ public static RefTypeOrTPHOrWildcardOrGeneric convert(Java17Parser.TypeTypeContext typeContext, JavaClassRegistry reg, GenericsRegistry genericsRegistry) { @@ -160,26 +119,6 @@ public class TypeGenerator { return TypeGenerator.convert(typeContext.classOrInterfaceType(), reg, genericsRegistry); } - // TODO: Merge with method above - /* - * public static RefTypeOrTPHOrWildcardOrGeneric - * convert(Java17Parser.ReferenceTypeContext referenceTypeContext, - * JavaClassRegistry reg, GenericsRegistry generics) { - * if (referenceTypeContext.classOrInterfaceType() != null) { - * if (referenceTypeContext.classOrInterfaceType(). - * classType_lfno_classOrInterfaceType() != null) { - * return convert(referenceTypeContext.classOrInterfaceType(), reg, generics); - * // return - * // convertTypeName(referenceTypeContext.getText(), - * // ctx.typeArguments(),referenceTypeContext.getStart(), reg, generics); - * } else { - * throw new NotImplementedException(); - * } - * } else { - * throw new NotImplementedException(); - * } - * } - */ public static GenericDeclarationList convert(Java17Parser.GenericDeclarationListContext typeParametersContext, JavaClassName parentClass, String parentMethod, JavaClassRegistry reg, GenericsRegistry generics) { @@ -208,25 +147,17 @@ public class TypeGenerator { public static List convert(Java17Parser.TypeBoundContext typeBoundContext, JavaClassRegistry reg, GenericsRegistry generics) { List ret = new ArrayList<>(); - if (typeBoundContext == null) { + if (Objects.isNull(typeBoundContext)) { ret.add(ASTFactory.createObjectType()); return ret; } - if (typeBoundContext.typeVariable() != null) { - ret.add(convertTypeName(typeBoundContext.typeVariable().Identifier().getText(), null, - typeBoundContext.typeVariable().getStart(), reg, generics)); + if (typeBoundContext.typeType().size() > 0) { + for (TypeTypeContext tt : typeBoundContext.typeType()) { + ret.add(convert(tt, reg, generics)); + } return ret; } - if (typeBoundContext.classOrInterfaceType() != null) { - ret.add(convert(typeBoundContext.classOrInterfaceType(), reg, generics)); - if (typeBoundContext.additionalBound() != null) - for (Java17Parser.AdditionalBoundContext addCtx : typeBoundContext.additionalBound()) { - ret.add(convert(addCtx.interfaceType())); - } - return ret; - } else { - throw new NotImplementedException(); - } + throw new NotImplementedException(); } public static RefTypeOrTPHOrWildcardOrGeneric convert(Java17Parser.WildcardTypeContext wildcardContext,