From dc8578632ca84645a53ca65d59bbfb3b80915566 Mon Sep 17 00:00:00 2001 From: luca9913 Date: Sat, 1 Apr 2023 15:40:12 +0200 Subject: [PATCH] Records in AST aufgenommen --- .../de/dhbwstuttgart/parser/JavaTXParser.java | 15 +- .../SyntaxTreeGenerator.java | 176 ++++++++++++------ .../SyntaxTreeGenerator/TypeGenerator.java | 59 ++---- .../dhbwstuttgart/syntaxtree/Constructor.java | 14 +- .../de/dhbwstuttgart/syntaxtree/Field.java | 13 +- .../syntaxtree/FormalParameter.java | 9 +- .../syntaxtree/ParameterList.java | 32 ++-- .../de/dhbwstuttgart/syntaxtree/Record.java | 16 ++ .../syntaxtree/statement/Assign.java | 7 +- .../syntaxtree/statement/AssignToField.java | 3 +- .../syntaxtree/statement/BinaryExpr.java | 13 +- .../syntaxtree/statement/InstanceOf.java | 9 +- .../syntaxtree/statement/LocalVar.java | 12 +- src/test/java/parser/NestedPattern.jav | 10 +- 14 files changed, 203 insertions(+), 185 deletions(-) create mode 100644 src/main/java/de/dhbwstuttgart/syntaxtree/Record.java diff --git a/src/main/java/de/dhbwstuttgart/parser/JavaTXParser.java b/src/main/java/de/dhbwstuttgart/parser/JavaTXParser.java index 4cbc5a40..eb5db14e 100644 --- a/src/main/java/de/dhbwstuttgart/parser/JavaTXParser.java +++ b/src/main/java/de/dhbwstuttgart/parser/JavaTXParser.java @@ -17,8 +17,7 @@ import java.util.ArrayList; import java.util.List; public class JavaTXParser { - public static Java17Parser.SourceFileContext parse(File source) - throws IOException, java.lang.ClassNotFoundException { + public static Java17Parser.SourceFileContext parse(File source) throws IOException, java.lang.ClassNotFoundException { InputStream stream = new FileInputStream(source); // DEPRECATED: ANTLRInputStream input = new ANTLRInputStream(stream); CharStream input = CharStreams.fromStream(stream); @@ -27,19 +26,11 @@ public class JavaTXParser { Java17Parser parser = new Java17Parser(tokens); return parser.sourceFile(); /* - * SyntaxTreeGenerator generator = new - * SyntaxTreeGenerator(environment.getRegistry(source)); - * return generator.convert(tree); + * SyntaxTreeGenerator generator = new SyntaxTreeGenerator(environment.getRegistry(source)); return generator.convert(tree); */ } /* - * Für das Typsystem ist es notwendig, dass sich der Source in einer Datei - * befindet: - * public SourceFile parse(String fileContent) throws IOException, - * java.lang.ClassNotFoundException { - * return this.parse(new - * ByteArrayInputStream(fileContent.getBytes(StandardCharsets.UTF_8))); - * } + * Für das Typsystem ist es notwendig, dass sich der Source in einer Datei befindet: public SourceFile parse(String fileContent) throws IOException, java.lang.ClassNotFoundException { return this.parse(new ByteArrayInputStream(fileContent.getBytes(StandardCharsets.UTF_8))); } */ } diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java index acf869cd..60045643 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java @@ -2,6 +2,7 @@ package de.dhbwstuttgart.parser.SyntaxTreeGenerator; import java.lang.reflect.Modifier; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -14,6 +15,8 @@ import java.util.stream.Collectors; import org.antlr.v4.runtime.CommonToken; import org.antlr.v4.runtime.Token; +import com.google.common.graph.ElementOrder.Type; + import de.dhbwstuttgart.environment.PackageCrawler; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.exceptions.TypeinferenceException; @@ -49,6 +52,8 @@ import de.dhbwstuttgart.parser.antlr.Java17Parser.MethodblockContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.MethoddeclContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.ModifierContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.NoclassorinterfaceContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.RecordComponentContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.RecordDeclarationContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.ReftypeContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.SrcfileContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.TypeArgumentsContext; @@ -59,17 +64,27 @@ import de.dhbwstuttgart.parser.scope.JavaClassRegistry; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.Constructor; import de.dhbwstuttgart.syntaxtree.Field; +import de.dhbwstuttgart.syntaxtree.FormalParameter; import de.dhbwstuttgart.syntaxtree.GenericDeclarationList; import de.dhbwstuttgart.syntaxtree.Method; import de.dhbwstuttgart.syntaxtree.ParameterList; +import de.dhbwstuttgart.syntaxtree.Record; import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; +import de.dhbwstuttgart.syntaxtree.statement.Assign; +import de.dhbwstuttgart.syntaxtree.statement.AssignLeftSide; +import de.dhbwstuttgart.syntaxtree.statement.AssignToField; import de.dhbwstuttgart.syntaxtree.statement.Block; +import de.dhbwstuttgart.syntaxtree.statement.FieldVar; +import de.dhbwstuttgart.syntaxtree.statement.LocalVar; +import de.dhbwstuttgart.syntaxtree.statement.Return; import de.dhbwstuttgart.syntaxtree.statement.Statement; +import de.dhbwstuttgart.syntaxtree.statement.This; import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.syntaxtree.type.Void; +import de.dhbwstuttgart.typeinference.constraints.GenericsResolver; public class SyntaxTreeGenerator { private JavaClassRegistry reg; @@ -114,9 +129,7 @@ public class SyntaxTreeGenerator { public String convertQualifiedName(Java17Parser.QualifiedNameContext ctx) { /* - * String ret = ""; for (Java17Parser.IdentifierContext ident : - * ctx.identifier()) { ret += ident.getText(); if - * (ctx.identifier().iterator().hasNext()) { ret += '.'; } } + * String ret = ""; for (Java17Parser.IdentifierContext ident : ctx.identifier()) { ret += ident.getText(); if (ctx.identifier().iterator().hasNext()) { ret += '.'; } } */ return ctx.getText(); } @@ -152,7 +165,8 @@ public class SyntaxTreeGenerator { newClass = convertClass(clsoif.classDeclaration(), modifiers); } else if (!Objects.isNull(clsoif.interfaceDeclaration())) { newClass = convertInterface(clsoif.interfaceDeclaration(), modifiers); - // TODO: else if(!Objects.isNull(clsoif.recordDeclaration())){ + } else if (!Objects.isNull(clsoif.recordDeclaration())) { + newClass = convertRecord(clsoif.recordDeclaration(), modifiers); } else { throw new NotImplementedException(); } @@ -196,62 +210,117 @@ public class SyntaxTreeGenerator { List implementedInterfaces = new ArrayList<>(); List permittedSubtypes = new ArrayList<>(); for (ClassBodyDeclarationContext clsbodydecl : ctx.classBody().classBodyDeclaration()) { - MemberdeclContext member; - // Statement-Blöcke und "leere Zeilen" (;) werden noch nicht berücksichtigt - if (clsbodydecl instanceof MemberdeclContext) { - member = (MemberdeclContext) clsbodydecl; - Integer membermodifiers = 0; - for (ModifierContext mod : member.modifier()) { - membermodifiers += allmodifiers.get(mod.getText()); - } - switch (member.memberDeclaration()) { - case MemberclassorinterfaceContext memberclsoif: { - break; - } - case MemberfieldContext memberfield: { - fielddecl.addAll(convert(memberfield.fieldDeclaration(), membermodifiers, generics)); - break; - } - case MembermethodContext membermethod: { - methods.add(convert(membermodifiers, membermethod.method(), name, superClass, generics)); - break; - } - case MemberconstructorContext memberconstructor: { - constructors.add(convert(membermodifiers, memberconstructor.constructor(), name, superClass, generics)); - break; - } - default: - break; - } - } else { - continue; + convert(clsbodydecl, fielddecl, constructors, methods, name, superClass, generics); + } + if (constructors.isEmpty()) { + constructors.add(generateStandardConstructor(ctx.identifier().getText(), name, superClass, genericClassParameters, offset)); + } + if (ctx.IMPLEMENTS() != null) { + implementedInterfaces.addAll(convert(ctx.typeList(0), generics)); + } + // Ist Bit für 'sealed'-Modifier gesetzt + if ((modifiers & 4096) != 0) { + switch (ctx.typeList().size()) { + case 1: { + permittedSubtypes.addAll(convert(ctx.typeList(0), generics)); + break; } - if (constructors.isEmpty()) { - constructors.add(generateStandardConstructor(ctx.identifier().getText(), name, superClass, genericClassParameters, offset)); + case 2: { + permittedSubtypes.addAll(convert(ctx.typeList(1), generics)); + break; } - if (ctx.IMPLEMENTS() != null) { - implementedInterfaces.addAll(convert(ctx.typeList(0), generics)); + default: { + break; } - // Ist Bit für 'sealed'-Modifier gesetzt - if ((modifiers & 4096) != 0) { - switch (ctx.typeList().size()) { - case 1: { - permittedSubtypes.addAll(convert(ctx.typeList(0), generics)); - break; - } - case 2: { - permittedSubtypes.addAll(convert(ctx.typeList(1), generics)); - break; - } - default: { - break; - } - } } } return new ClassOrInterface(modifiers, name, fielddecl, Optional.of(this.generatePseudoConstructor(ctx.identifier().getText(), name, superClass, genericClassParameters, offset)), methods, constructors, genericClassParameters, superClass, isInterface, implementedInterfaces, offset); } + private de.dhbwstuttgart.syntaxtree.Record convertRecord(RecordDeclarationContext recordDeclaration, int modifiers) { + String identifier = recordDeclaration.identifier().getText(); + String className = this.pkgName + (this.pkgName.length() > 0 ? "." : "") + identifier; + JavaClassName name = reg.getName(className); // Holt den Package Namen mit dazu + Token offset = recordDeclaration.getStart(); + GenericsRegistry generics = createGenerics(recordDeclaration.genericDeclarationList(), name, "", reg, new GenericsRegistry(globalGenerics)); + if (!name.toString().equals(className)) { // Kommt die Klasse schon in einem anderen Package vor? + throw new TypeinferenceException("Name " + className + " bereits vorhanden in " + reg.getName(className).toString(), recordDeclaration.getStart()); + } + GenericDeclarationList genericClassParameters; + if (recordDeclaration.genericDeclarationList() == null) { + genericClassParameters = new GenericDeclarationList(new ArrayList<>(), recordDeclaration.identifier().getStop()); + } else { + genericClassParameters = TypeGenerator.convert(recordDeclaration.genericDeclarationList(), name, "", reg, generics); + } + RefType superClass = new RefType(ASTFactory.createObjectClass().getClassName(), offset); + List fielddecl = new ArrayList<>(); + List methods = new ArrayList<>(); + List constructors = new ArrayList<>(); + Boolean isInterface = false; + List implementedInterfaces = new ArrayList<>(); + List constructorParameters = new ArrayList<>(); + List constructorStatements = new ArrayList<>(); + for (RecordComponentContext component : recordDeclaration.recordHeader().recordComponentList().recordComponent()) { + int fieldmodifiers = allmodifiers.get("private") + allmodifiers.get("final"); + String fieldname = component.identifier().getText(); + Token fieldoffset = component.getStart(); + RefTypeOrTPHOrWildcardOrGeneric fieldtype = null; + if (Objects.isNull(component.typeType())) { + fieldtype = TypePlaceholder.fresh(offset); + } else { + fieldtype = TypeGenerator.convert(component.typeType(), reg, generics); + } + fielddecl.add(new Field(fieldname, fieldtype, fieldmodifiers, fieldoffset)); + constructorParameters.add(new FormalParameter(fieldname, fieldtype, fieldoffset)); + FieldVar fieldvar = new FieldVar(new This(offset), fieldname, fieldtype, fieldoffset); + constructorStatements.add(new Assign(new AssignToField(fieldvar), new LocalVar(fieldname, fieldtype, fieldoffset), offset)); + Statement returnStatement = new Return(fieldvar, offset); + methods.add(new Method(allmodifiers.get("public"), fieldname, fieldtype, new ParameterList(new ArrayList<>(), offset), new Block(Arrays.asList(returnStatement), offset), new GenericDeclarationList(new ArrayList<>(), offset), offset)); + } + RefType classType = ClassOrInterface.generateTypeOfClass(reg.getName(className), genericClassParameters, offset); + Constructor implicitConstructor = new Constructor(allmodifiers.get("public"), identifier, classType, new ParameterList(constructorParameters, offset), new Block(constructorStatements, offset), genericClassParameters, offset); + Optional initializations = Optional.of(implicitConstructor); + constructors.add(implicitConstructor); + for (ClassBodyDeclarationContext bodyDeclaration : recordDeclaration.recordBody().classBodyDeclaration()) { + convert(bodyDeclaration, fielddecl, constructors, methods, name, superClass, generics); + } + if (!Objects.isNull(recordDeclaration.IMPLEMENTS())) { + implementedInterfaces.addAll(convert(recordDeclaration.typeList(), generics)); + } + return new Record(modifiers, name, fielddecl, initializations, methods, constructors, genericClassParameters, superClass, isInterface, implementedInterfaces, offset); + } + + private void convert(ClassBodyDeclarationContext classBody, List fields, List constructors, List methods, JavaClassName name, RefType superClass, GenericsRegistry generics) { + MemberdeclContext member; + // Statement-Blöcke und "leere Zeilen" (;) werden noch nicht berücksichtigt + if (classBody instanceof MemberdeclContext) { + member = (MemberdeclContext) classBody; + Integer membermodifiers = 0; + for (ModifierContext mod : member.modifier()) { + membermodifiers += allmodifiers.get(mod.getText()); + } + switch (member.memberDeclaration()) { + case MemberclassorinterfaceContext memberclsoif: { + break; + } + case MemberfieldContext memberfield: { + fields.addAll(convert(memberfield.fieldDeclaration(), membermodifiers, generics)); + break; + } + case MembermethodContext membermethod: { + methods.add(convert(membermodifiers, membermethod.method(), name, superClass, generics)); + break; + } + case MemberconstructorContext memberconstructor: { + constructors.add(convert(membermodifiers, memberconstructor.constructor(), name, superClass, generics)); + break; + } + default: + break; + } + } + } + private ClassOrInterface convertInterface(Java17Parser.InterfaceDeclarationContext ctx, int modifiers) { String className = this.pkgName.length() > 0 ? this.pkgName + "." : "" + ctx.identifier().getText(); JavaClassName name = reg.getName(className); // Holt den Package Namen mit dazu @@ -368,8 +437,7 @@ public class SyntaxTreeGenerator { } /* - * fieldInitializations werden in einem Psedokonstruktor in der abstrakten - * Syntax gespeichert + * fieldInitializations werden in einem Psedokonstruktor in der abstrakten Syntax gespeichert */ private Constructor generatePseudoConstructor(String className, JavaClassName parentClass, RefType superClass, GenericDeclarationList classGenerics, Token offset) { RefType classType = ClassOrInterface.generateTypeOfClass(reg.getName(className), classGenerics, offset); diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java index ba17193c..1e704d75 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java @@ -32,20 +32,11 @@ public class TypeGenerator { public static RefTypeOrTPHOrWildcardOrGeneric convert(Java17Parser.ClassOrInterfaceTypeContext classOrInterfaceTypeContext, JavaClassRegistry reg, GenericsRegistry generics) { Java17Parser.TypeArgumentsContext arguments = null; /* - * PL 2019-03-19 auskommentiert ANFANG if(unannClassOrInterfaceTypeContext. - * unannClassType_lfno_unannClassOrInterfaceType() != null){ arguments = - * unannClassOrInterfaceTypeContext. - * unannClassType_lfno_unannClassOrInterfaceType().typeArguments(); }else{// - * if(unannClassOrInterfaceTypeContext. - * unannInterfaceType_lfno_unannClassOrInterfaceType() != null){ arguments = - * unannClassOrInterfaceTypeContext. - * unannInterfaceType_lfno_unannClassOrInterfaceType(). - * unannClassType_lfno_unannClassOrInterfaceType().typeArguments(); } PL - * 2019-03-19 auskommentiert ENDE + * PL 2019-03-19 auskommentiert ANFANG if(unannClassOrInterfaceTypeContext. unannClassType_lfno_unannClassOrInterfaceType() != null){ arguments = unannClassOrInterfaceTypeContext. unannClassType_lfno_unannClassOrInterfaceType().typeArguments(); }else{// if(unannClassOrInterfaceTypeContext. unannInterfaceType_lfno_unannClassOrInterfaceType() != null){ arguments = unannClassOrInterfaceTypeContext. unannInterfaceType_lfno_unannClassOrInterfaceType(). + * unannClassType_lfno_unannClassOrInterfaceType().typeArguments(); } PL 2019-03-19 auskommentiert ENDE */ /** - * Problem sind hier die verschachtelten Typen mit verschachtelten Typargumenten - * Beispiel: Typ.InnererTyp + * Problem sind hier die verschachtelten Typen mit verschachtelten Typargumenten Beispiel: Typ.InnererTyp */ if (classOrInterfaceTypeContext.typeArguments().size() > 1) throw new NotImplementedException(); @@ -57,31 +48,9 @@ public class TypeGenerator { name += classOrInterfaceTypeContext.typeIdentifier().getText(); if (classOrInterfaceTypeContext.getStop().getText().equals(">")) { /* - * Fuer Debug-Zwecke unannClassOrInterfaceTypeContext. - * unannInterfaceType_lfno_unannClassOrInterfaceType(); - * unannClassOrInterfaceTypeContext. - * unannClassType_lfno_unannClassOrInterfaceType().getText(); - * //unannClassOrInterfaceTypeContext. - * unannInterfaceType_lfno_unannClassOrInterfaceType(). - * unannClassType_lfno_unannClassOrInterfaceType().typeArguments(); - * //UnannClassType_lfno_unannClassOrInterfaceTypeContext - * unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType( - * 0); - * unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType( - * 0).getText(); - * unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType( - * 1); - * unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType( - * 1).getText(); unannClassOrInterfaceTypeContext. - * unannClassType_lfno_unannClassOrInterfaceType().getText(); - * //unannClassOrInterfaceTypeContext. - * unannInterfaceType_lf_unannClassOrInterfaceType(); - * //unannClassOrInterfaceTypeContext. - * unannInterfaceType_lf_unannClassOrInterfaceType(0).getText(); - * //unannClassOrInterfaceTypeContext. - * unannInterfaceType_lf_unannClassOrInterfaceType(1).getText(); - * //unannClassOrInterfaceTypeContext. - * unannInterfaceType_lfno_unannClassOrInterfaceType().getText(); + * Fuer Debug-Zwecke unannClassOrInterfaceTypeContext. unannInterfaceType_lfno_unannClassOrInterfaceType(); unannClassOrInterfaceTypeContext. unannClassType_lfno_unannClassOrInterfaceType().getText(); //unannClassOrInterfaceTypeContext. unannInterfaceType_lfno_unannClassOrInterfaceType(). unannClassType_lfno_unannClassOrInterfaceType().typeArguments(); //UnannClassType_lfno_unannClassOrInterfaceTypeContext unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType( 0); + * unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType( 0).getText(); unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType( 1); unannClassOrInterfaceTypeContext.unannClassType_lf_unannClassOrInterfaceType( 1).getText(); unannClassOrInterfaceTypeContext. unannClassType_lfno_unannClassOrInterfaceType().getText(); //unannClassOrInterfaceTypeContext. unannInterfaceType_lf_unannClassOrInterfaceType(); //unannClassOrInterfaceTypeContext. + * unannInterfaceType_lf_unannClassOrInterfaceType(0).getText(); //unannClassOrInterfaceTypeContext. unannInterfaceType_lf_unannClassOrInterfaceType(1).getText(); //unannClassOrInterfaceTypeContext. unannInterfaceType_lfno_unannClassOrInterfaceType().getText(); */ List typeargs = classOrInterfaceTypeContext.typeArguments(); arguments = typeargs.size() != 0 ? classOrInterfaceTypeContext.typeArguments(0) : null; @@ -91,14 +60,12 @@ public class TypeGenerator { public static RefTypeOrTPHOrWildcardOrGeneric convert(Java17Parser.TypeTypeContext typeContext, JavaClassRegistry reg, GenericsRegistry genericsRegistry) { if (typeContext.primitiveType() != null) { - if (typeContext.primitiveType().getText().equals("boolean")) { + switch (typeContext.primitiveType().getText()) { + case "boolean": return new RefType(ASTFactory.createClass(Boolean.class).getClassName(), typeContext.getStart()); - } else { - /* - * Nicht benötigt, wenn danach eine Exception erstellt wird - * Java17Parser.NumericTypeContext numericType = - * typeContext.unannPrimitiveType().numericType(); - */ + case "int": + return new RefType(ASTFactory.createClass(Integer.class).getClassName(), typeContext.getStart()); + default: throw new NotImplementedException(); } } else if (!typeContext.LBRACK().isEmpty()) { // ArrayType über eckige Klammer prüfen @@ -106,9 +73,7 @@ public class TypeGenerator { throw new NotImplementedException(); } /* - * else if (typeContext.classOrInterfaceType() != null) { JavaClassName name = - * reg .getName(typeContext.classOrInterfaceType().typeIdentifier().getText()); - * return new RefType(name, typeContext.getStart()); } + * else if (typeContext.classOrInterfaceType() != null) { JavaClassName name = reg .getName(typeContext.classOrInterfaceType().typeIdentifier().getText()); return new RefType(name, typeContext.getStart()); } */ return TypeGenerator.convert(typeContext.classOrInterfaceType(), reg, genericsRegistry); } diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/Constructor.java b/src/main/java/de/dhbwstuttgart/syntaxtree/Constructor.java index 24b3bb81..1c955c81 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/Constructor.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/Constructor.java @@ -12,20 +12,16 @@ import java.util.List; public class Constructor extends Method { - - //TODO: Constructor braucht ein super-Statement - public Constructor(int modifier, String name, RefTypeOrTPHOrWildcardOrGeneric returnType, ParameterList parameterList, Block codeInsideConstructor, - GenericDeclarationList gtvDeclarations, Token offset /*, List fieldInitializations geloescht PL 2018-11-24 */) { - super(modifier, name, returnType, parameterList, /*codeInsideConstructor,*/ prepareBlock(codeInsideConstructor ) /*,fieldInitializations )geloescht PL 2018-11-24 )*/, gtvDeclarations, offset); + // TODO: Constructor braucht ein super-Statement + public Constructor(int modifier, String name, RefTypeOrTPHOrWildcardOrGeneric returnType, ParameterList parameterList, Block codeInsideConstructor, GenericDeclarationList gtvDeclarations, Token offset /* , List fieldInitializations geloescht PL 2018-11-24 */) { + super(modifier, name, returnType, parameterList, /* codeInsideConstructor, */ prepareBlock(codeInsideConstructor) /* ,fieldInitializations )geloescht PL 2018-11-24 ) */, gtvDeclarations, offset); } /** - * @param fieldInitializations - Das sind die Statements, - * welche die Felder der zugehörigen Klasse dieses - * Konstruktor initialisieren + * @param fieldInitializations - Das sind die Statements, welche die Felder der zugehörigen Klasse dieses Konstruktor initialisieren */ - protected static Block prepareBlock(Block constructorBlock /*, List fieldInitializations new ArrayList<>() geloescht PL 2018-11-24 */){ + protected static Block prepareBlock(Block constructorBlock /* , List fieldInitializations new ArrayList<>() geloescht PL 2018-11-24 */) { List statements = constructorBlock.getStatements(); statements.add(0, new SuperCall(null, null, constructorBlock.getOffset())); /* statements.addAll(fieldInitializations); geloescht PL 2018-11-24 */ diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/Field.java b/src/main/java/de/dhbwstuttgart/syntaxtree/Field.java index 8a4230e3..4afc3587 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/Field.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/Field.java @@ -5,22 +5,22 @@ import org.antlr.v4.runtime.Token; import java.util.ArrayList; -public class Field extends SyntaxTreeNode implements TypeScope{ +public class Field extends SyntaxTreeNode implements TypeScope { public final int modifier; private String name; private RefTypeOrTPHOrWildcardOrGeneric type; - - public Field(String name, RefTypeOrTPHOrWildcardOrGeneric type, int modifier, Token offset){ + + public Field(String name, RefTypeOrTPHOrWildcardOrGeneric type, int modifier, Token offset) { super(offset); this.name = name; this.type = type; this.modifier = modifier; } - public String getName(){ - return this.name; - } + public String getName() { + return this.name; + } public RefTypeOrTPHOrWildcardOrGeneric getType() { return type; @@ -41,4 +41,3 @@ public class Field extends SyntaxTreeNode implements TypeScope{ return type; } } - diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/FormalParameter.java b/src/main/java/de/dhbwstuttgart/syntaxtree/FormalParameter.java index 8e7e7983..58291293 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/FormalParameter.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/FormalParameter.java @@ -3,15 +3,14 @@ package de.dhbwstuttgart.syntaxtree; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import org.antlr.v4.runtime.Token; -public class FormalParameter extends SyntaxTreeNode -{ +public class FormalParameter extends SyntaxTreeNode { private RefTypeOrTPHOrWildcardOrGeneric type; private String name; - - public FormalParameter(String name, RefTypeOrTPHOrWildcardOrGeneric type, Token offset){ + + public FormalParameter(String name, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) { super(offset); this.name = name; - this.type = type; + this.type = type; } public RefTypeOrTPHOrWildcardOrGeneric getType() { diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/ParameterList.java b/src/main/java/de/dhbwstuttgart/syntaxtree/ParameterList.java index 2540f907..33a45c5d 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/ParameterList.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/ParameterList.java @@ -1,39 +1,33 @@ package de.dhbwstuttgart.syntaxtree; - - import org.antlr.v4.runtime.Token; import java.util.Iterator; import java.util.List; - -public class ParameterList extends SyntaxTreeNode implements Iterable -{ +public class ParameterList extends SyntaxTreeNode implements Iterable { private List formalparameter; - - public ParameterList(List params, Token offset){ + + public ParameterList(List params, Token offset) { super(offset); this.formalparameter = params; } - public FormalParameter getParameterAt(int i) - { - if (i >= formalparameter.size() ) return null; - + public FormalParameter getParameterAt(int i) { + if (i >= formalparameter.size()) + return null; + return formalparameter.get(i); } - - - public List getFormalparalist() - { + + public List getFormalparalist() { return formalparameter; } - @Override - public Iterator iterator() { - return formalparameter.iterator(); - } + @Override + public Iterator iterator() { + return formalparameter.iterator(); + } @Override public void accept(ASTVisitor visitor) { diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/Record.java b/src/main/java/de/dhbwstuttgart/syntaxtree/Record.java new file mode 100644 index 00000000..46f89f19 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/Record.java @@ -0,0 +1,16 @@ +package de.dhbwstuttgart.syntaxtree; + +import java.util.List; +import java.util.Optional; + +import org.antlr.v4.runtime.Token; + +import de.dhbwstuttgart.parser.scope.JavaClassName; +import de.dhbwstuttgart.syntaxtree.type.RefType; + +public class Record extends ClassOrInterface { + + public Record(int modifiers, JavaClassName name, List fielddecl, Optional fieldInitializations, List methods, List constructors, GenericDeclarationList genericClassParameters, RefType superClass, Boolean isInterface, List implementedInterfaces, Token offset) { + super(modifiers, name, fielddecl, fieldInitializations, methods, constructors, genericClassParameters, superClass, isInterface, implementedInterfaces, offset); + } +} diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/Assign.java b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/Assign.java index 3af2de6f..a827805e 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/Assign.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/Assign.java @@ -11,16 +11,15 @@ import org.antlr.v4.runtime.Token; Aufbau: rightSide = leftSide */ -public class Assign extends Statement -{ +public class Assign extends Statement { public final Expression rightSide; public final AssignLeftSide lefSide; public Assign(AssignLeftSide leftHandSide, Expression value, Token offset) { - super(leftHandSide.getType(), offset); + super(leftHandSide.getType(), offset); this.rightSide = value; this.lefSide = leftHandSide; - } + } @Override public void accept(StatementVisitor visitor) { diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/AssignToField.java b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/AssignToField.java index 931443d1..8ed8b95c 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/AssignToField.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/AssignToField.java @@ -2,8 +2,9 @@ package de.dhbwstuttgart.syntaxtree.statement; import de.dhbwstuttgart.syntaxtree.StatementVisitor; -public class AssignToField extends AssignLeftSide{ +public class AssignToField extends AssignLeftSide { public final FieldVar field; + public AssignToField(FieldVar fieldVar) { super(fieldVar.getType(), fieldVar.getOffset()); field = fieldVar; diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/BinaryExpr.java b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/BinaryExpr.java index 4fab15cc..cd93959c 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/BinaryExpr.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/BinaryExpr.java @@ -1,24 +1,22 @@ package de.dhbwstuttgart.syntaxtree.statement; - import de.dhbwstuttgart.syntaxtree.StatementVisitor; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import org.antlr.v4.runtime.Token; -public class BinaryExpr extends Expression -{ +public class BinaryExpr extends Expression { @Override public void accept(StatementVisitor visitor) { visitor.visit(this); } - public enum Operator{ + public enum Operator { ADD, // + SUB, // - MUL, // * MOD, // Modulo Operator % AND, // && - OR, // || + OR, // || DIV, // / LESSTHAN, // < BIGGERTHAN, // > @@ -32,9 +30,8 @@ public class BinaryExpr extends Expression public final Expression lexpr; public final Expression rexpr; - public BinaryExpr(Operator operation, RefTypeOrTPHOrWildcardOrGeneric type, Expression lexpr, Expression rexpr, Token offset) - { - super(type,offset); + public BinaryExpr(Operator operation, RefTypeOrTPHOrWildcardOrGeneric type, Expression lexpr, Expression rexpr, Token offset) { + super(type, offset); this.operation = operation; this.lexpr = lexpr; diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/InstanceOf.java b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/InstanceOf.java index 02fd667b..aea18dc3 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/InstanceOf.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/InstanceOf.java @@ -4,14 +4,11 @@ import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.syntaxtree.StatementVisitor; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; - -public class InstanceOf extends BinaryExpr -{ +public class InstanceOf extends BinaryExpr { public Expression expr; private RefTypeOrTPHOrWildcardOrGeneric reftype; - - public InstanceOf(int offset,int variableLength) - { + + public InstanceOf(int offset, int variableLength) { super(null, null, null, null, null); throw new NotImplementedException(); // #JB# 20.04.2005 diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/LocalVar.java b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/LocalVar.java index 91094a7a..75ac436e 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/LocalVar.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/LocalVar.java @@ -4,19 +4,17 @@ import de.dhbwstuttgart.syntaxtree.StatementVisitor; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import org.antlr.v4.runtime.Token; -public class LocalVar extends Statement{ +public class LocalVar extends Statement { public final String name; - public LocalVar(String n, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) - { - super(type,offset); + public LocalVar(String n, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) { + super(type, offset); this.name = n; } - public LocalVar(Expression e1, RefTypeOrTPHOrWildcardOrGeneric type, String access) - { - super(type,e1.getOffset()); + public LocalVar(Expression e1, RefTypeOrTPHOrWildcardOrGeneric type, String access) { + super(type, e1.getOffset()); this.name = access; } diff --git a/src/test/java/parser/NestedPattern.jav b/src/test/java/parser/NestedPattern.jav index e1961eb0..96c01181 100644 --- a/src/test/java/parser/NestedPattern.jav +++ b/src/test/java/parser/NestedPattern.jav @@ -1,18 +1,16 @@ import java.lang.String; record Point(int x, int y) {} -enum Color { RED, GREEN, BLUE } interface Shape {} -record ColoredPoint(Point pt, Color color) {} -record Rectangle(ColoredPoint upperLeft, ColoredPoint lowerRight) implements Shape {} +record Rectangle(Point upperLeft, Point lowerRight) implements Shape {} class PatternMatching { - void printColorOfUpperLeftPoint(Shape shape) + void printCoordinatesOfUpperLeftPoint(Shape shape) { switch (shape) { - case Rectangle(ColoredPoint(Point pt, Color color), ColoredPoint lowerRight) -> System.out.println("x: " + pt.x() + " / color: " + color + " / lowerRight: " + lowerRight); + case Rectangle(Point(int x, int y), Point lowerRight) -> System.out.println("x: " + x + " / y: " + y + " / lowerRight: " + lowerRight); default -> System.out.println("not a rectangle"); - } + } } } \ No newline at end of file