From dff72b0c97d7a59382cfb046ea00a4a5d972d151 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Mon, 12 Mar 2018 00:38:02 +0100 Subject: [PATCH] ResultSetOutputGenerator --- .../sat/asp/parser/ASPParser.java | 21 ++++++------ .../visual/ResultSetOutputGenerator.java | 34 +++++++++++++++++++ .../syntaxtree/visual/ResultSetPrinter.java | 20 +++++++++++ test/asp/UnifyWithoutWildcards.java | 3 +- 4 files changed, 66 insertions(+), 12 deletions(-) create mode 100644 src/de/dhbwstuttgart/syntaxtree/visual/ResultSetOutputGenerator.java create mode 100644 src/de/dhbwstuttgart/syntaxtree/visual/ResultSetPrinter.java diff --git a/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java b/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java index 75acfe90..9aa86891 100644 --- a/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java +++ b/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java @@ -130,6 +130,7 @@ public class ASPParser { } } private void parseParameter(String statement){ + //param(pointer, typ, nextPointer Pattern p = Pattern.compile(ASPRule.ASP_PARAMLIST_NAME+"\\(([^,]+),([^,]+),([^,]+)\\)"); Matcher m = p.matcher(statement); boolean b = m.matches(); @@ -137,7 +138,7 @@ public class ASPParser { if(m.groupCount()<3)throw new DebugException("Fehler in Regex"); String pointer = m.group(1); String type = m.group(2); - String next = m.group(2); + String next = m.group(3); if(next.equals(ASPRule.ASP_PARAMLIST_END_POINTER.toString()))next = null; if(this.parameterLists.containsKey(pointer)){ throw new DebugException("Fehler in Ergebnisparsen"); @@ -158,20 +159,18 @@ public class ASPParser { } private void parseType(String statement){ - //TODO: Diese Regel muss 3 Parameter parsen (TypPointer, TypName, ParameterlistenPointer) - throw new NotImplementedException(); - /* - Pattern p = Pattern.compile(ASPRule.ASP_TYPE+"\\(([^,]+),([^,]+)\\)"); + //Diese Regel muss 3 Parameter parsen (TypPointer, TypName, ParameterlistenPointer) + Pattern p = Pattern.compile(ASPRule.ASP_TYPE+"\\(([^,]+),([^,]+),([^,]+)\\)"); Matcher m = p.matcher(statement); boolean b = m.matches(); if(b){ - if(m.groupCount()<2)throw new DebugException("Fehler in Regex"); - String ls = m.group(1); - String rs = m.group(2); - List params = this.getParams(rs); - this.types.put(ls,new ParsedType(ls, params)); + if(m.groupCount()<3)throw new DebugException("Fehler in Regex"); + String pointer = m.group(1); + String name = m.group(2); + String paraList = m.group(3); + List params = this.getParams(paraList); + this.types.put(pointer,new ParsedType(name, params)); } - */ } private List getParams(String pointer) { diff --git a/src/de/dhbwstuttgart/syntaxtree/visual/ResultSetOutputGenerator.java b/src/de/dhbwstuttgart/syntaxtree/visual/ResultSetOutputGenerator.java new file mode 100644 index 00000000..f50b05a6 --- /dev/null +++ b/src/de/dhbwstuttgart/syntaxtree/visual/ResultSetOutputGenerator.java @@ -0,0 +1,34 @@ +package de.dhbwstuttgart.syntaxtree.visual; + +import de.dhbwstuttgart.syntaxtree.type.*; +import de.dhbwstuttgart.typeinference.result.*; + +public class ResultSetOutputGenerator extends OutputGenerator implements ResultSetVisitor{ + + public ResultSetOutputGenerator(StringBuilder out) { + super(out); + } + + @Override + public void visit(PairTPHsmallerTPH p) { + print(p, "<"); + } + + @Override + public void visit(PairTPHequalRefTypeOrWildcardType p) { + print(p, "=."); + } + + @Override + public void visit(PairTPHEqualTPH p) { + print(p, "=."); + } + + private void print(ResultPair p , String operator){ + out.append("("); + p.getLeft().accept((ResultSetVisitor) this); + out.append(" "+operator+" "); + p.getRight().accept((ResultSetVisitor) this); + out.append(")"); + } +} diff --git a/src/de/dhbwstuttgart/syntaxtree/visual/ResultSetPrinter.java b/src/de/dhbwstuttgart/syntaxtree/visual/ResultSetPrinter.java new file mode 100644 index 00000000..fc67a723 --- /dev/null +++ b/src/de/dhbwstuttgart/syntaxtree/visual/ResultSetPrinter.java @@ -0,0 +1,20 @@ +package de.dhbwstuttgart.syntaxtree.visual; + +import de.dhbwstuttgart.syntaxtree.SourceFile; +import de.dhbwstuttgart.typeinference.result.ResultPair; +import de.dhbwstuttgart.typeinference.result.ResultSet; + +import java.util.Set; + +public class ResultSetPrinter { + + public static String print(ResultSet toPrint){ + StringBuilder output = new StringBuilder(); + for(ResultPair p : toPrint.results){ + p.accept(new ResultSetOutputGenerator(output)); + output.append("\n"); + } + return output.toString(); + } + +} diff --git a/test/asp/UnifyWithoutWildcards.java b/test/asp/UnifyWithoutWildcards.java index 1b33cbd5..eadca649 100644 --- a/test/asp/UnifyWithoutWildcards.java +++ b/test/asp/UnifyWithoutWildcards.java @@ -8,6 +8,7 @@ import de.dhbwstuttgart.sat.asp.writer.ASPFactory; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; import de.dhbwstuttgart.syntaxtree.type.*; +import de.dhbwstuttgart.syntaxtree.visual.ResultSetPrinter; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.constraints.Pair; import de.dhbwstuttgart.typeinference.result.ResultSet; @@ -33,7 +34,7 @@ public class UnifyWithoutWildcards { RefType t2 = new RefType(new JavaClassName("java.util.HashMap"), list2, new NullToken()); testSet.addUndConstraint(new Pair(t1, t2, PairOperator.SMALLERDOT)); ResultSet resultSet = run(testSet); - System.out.println(resultSet.results); + System.out.println(ResultSetPrinter.print(resultSet)); assert resultSet.results.size() > 0; }