TypeGenerator für Java17 Grammatik angepasst

This commit is contained in:
luca9913 2023-02-05 09:16:59 +01:00
parent a77970b5e7
commit 40d0d6b63e
2 changed files with 32 additions and 91 deletions

View File

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

View File

@ -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<String>.InnererTyp<Integer>
*/
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<TypeArgumentsContext> 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<RefTypeOrTPHOrWildcardOrGeneric> convert(Java17Parser.TypeBoundContext typeBoundContext,
JavaClassRegistry reg, GenericsRegistry generics) {
List<RefTypeOrTPHOrWildcardOrGeneric> 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,