From 6119bc92ed7824637f5a242c75cdb296c6d8551f Mon Sep 17 00:00:00 2001 From: luca9913 Date: Sat, 15 Jul 2023 22:05:34 +0200 Subject: [PATCH] Corrected ASTPrinter for RecordPattern --- .../SyntaxTreeGenerator/SyntaxTreeGenerator.java | 14 ++++++++------ .../syntaxtree/statement/RecordPattern.java | 6 ++++++ .../syntaxtree/visual/OutputGenerator.java | 15 ++++++++++++--- .../java/syntaxtreegenerator/TestNewFeatures.java | 10 ---------- 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java index 667c56db..baebce55 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java @@ -386,12 +386,14 @@ public class SyntaxTreeGenerator { } List permittedSubtypes = new ArrayList<>(); // Ist Bit für 'sealed'-Modifier gesetzt - if ((modifiers & 4096) != 0 && !Objects.isNull(ctx.PERMITS())) { - // permitted subtypes sind letzte typeList (siehe Grammatikregel 'classDeclaration') - permittedSubtypes.addAll(convert(ctx.typeList(ctx.typeList().size() - 1), generics)); - } else { - // falls sealed modifier ohne 'permits'-List oder umgekehrt - throw new NotImplementedException("Invalid sealed class declaration"); + if ((modifiers & 4096) != 0) { + if (!Objects.isNull(ctx.PERMITS())) { + // permitted subtypes sind letzte typeList (siehe Grammatikregel 'classDeclaration') + permittedSubtypes.addAll(convert(ctx.typeList(ctx.typeList().size() - 1), generics)); + } else { + // falls sealed modifier ohne 'permits'-List oder umgekehrt + throw new NotImplementedException("Invalid sealed class declaration"); + } } return new ClassOrInterface(modifiers, name, fields, Optional.empty(), methods, new ArrayList<>(), genericParams, superClass, true, extendedInterfaces, permittedSubtypes, ctx.getStart()); } diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/RecordPattern.java b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/RecordPattern.java index d10e7eb5..64b6d22b 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/RecordPattern.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/RecordPattern.java @@ -5,6 +5,7 @@ import java.util.List; import org.antlr.v4.runtime.Token; +import de.dhbwstuttgart.syntaxtree.StatementVisitor; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; public class RecordPattern extends Pattern { @@ -28,4 +29,9 @@ public class RecordPattern extends Pattern { this.subPattern.add(newPattern); } + @Override + public void accept(StatementVisitor visitor) { + visitor.visit(this); + } + } \ No newline at end of file diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java b/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java index 69078ccf..219c8172 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java @@ -7,6 +7,7 @@ import de.dhbwstuttgart.syntaxtree.type.*; import java.lang.reflect.Modifier; import java.util.Iterator; +import java.util.List; public class OutputGenerator implements ASTVisitor { private static final String TAB = " "; @@ -457,10 +458,18 @@ public class OutputGenerator implements ASTVisitor { @Override public void visit(RecordPattern aRecordPattern) { aRecordPattern.getType().accept(this); - for (Pattern subPattern : aRecordPattern.getSubPattern()) { - subPattern.accept(this); + out.append("("); + List subPatterns = aRecordPattern.getSubPattern(); + int i; + for (i = 0; i < subPatterns.size() - 1; i++) { + subPatterns.get(i).accept(this); + out.append(", "); } - out.append(aRecordPattern.getName()); + subPatterns.get(i).accept(this); + String name; + if ((name = aRecordPattern.getName()) != null) + out.append(name); + out.append(")"); } @Override diff --git a/src/test/java/syntaxtreegenerator/TestNewFeatures.java b/src/test/java/syntaxtreegenerator/TestNewFeatures.java index 03bcdeb1..d6717e46 100644 --- a/src/test/java/syntaxtreegenerator/TestNewFeatures.java +++ b/src/test/java/syntaxtreegenerator/TestNewFeatures.java @@ -106,14 +106,4 @@ public class TestNewFeatures { fail("An error occured while generating the AST for PatternMatching.jav"); } } -} - -class JavFilter implements FileFilter { - @Override - public boolean accept(File pathname) { - if (pathname.getName().contains(".jav")) - return true; - return false; - } - } \ No newline at end of file