diff --git a/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java b/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java index cfb59933..ce43a01f 100644 --- a/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java +++ b/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java @@ -8,14 +8,12 @@ import de.dhbwstuttgart.sat.asp.ASPStringConverter; import de.dhbwstuttgart.sat.asp.model.ASPRule; import de.dhbwstuttgart.sat.asp.parser.antlr.UnifyResultBaseListener; import de.dhbwstuttgart.sat.asp.parser.antlr.UnifyResultLexer; -import de.dhbwstuttgart.sat.asp.parser.antlr.UnifyResultListener; import de.dhbwstuttgart.sat.asp.parser.antlr.UnifyResultParser; import de.dhbwstuttgart.sat.asp.parser.model.ParsedType; import de.dhbwstuttgart.syntaxtree.type.*; import de.dhbwstuttgart.typeinference.result.*; import org.antlr.v4.runtime.CharStreams; import org.antlr.v4.runtime.CommonTokenStream; -import org.antlr.v4.runtime.TokenStream; import org.antlr.v4.runtime.tree.ParseTreeWalker; import javax.json.Json; @@ -36,11 +34,13 @@ import java.util.stream.Collectors; * -> Eigentlich nur die korrekten Namen der Typen und TPHs */ public class ASPParser extends UnifyResultBaseListener { - private final Collection originalTPHs; + private Collection originalTPHs; private ResultSet resultSet; private Map types = new HashMap<>(); private Set tphs = new HashSet<>(); private Map parameterLists = new HashMap<>(); + private Set equalsRelations = new HashSet<>(); + private Set smallerRelations = new HashSet<>(); /** * Parst clingo output welcher als JSON (option --outf=2) ausgibt @@ -53,36 +53,74 @@ public class ASPParser extends UnifyResultBaseListener { @Override public void enterParameter(UnifyResultParser.ParameterContext ctx) { + //Linked List (pointer, Type, nextPointer) + List params = parseParameterList(ctx.parameterList()); + parameterLists.put(params.get(0), new ParameterListNode(params.get(1), params.get(2))); + } + + private static class Relation { + public final String right; + public final String left; + + Relation(String leftType, String rightType){ + this.left = leftType; + this.right = rightType; + } + + @Override + public int hashCode() { + return (left+right).hashCode(); + } + + @Override + public boolean equals(Object obj) { + if(obj instanceof Relation) + return (right+left).equals(((Relation) obj).left+((Relation) obj).right); + return super.equals(obj); + } + } + + private List parseParameterList(UnifyResultParser.ParameterListContext ctx){ + return ctx.value().stream().map(v -> + //ASPStringConverter.fromConstant(v.getText()) + v.getText() + ).collect(Collectors.toList()); } @Override public void enterEquals(UnifyResultParser.EqualsContext ctx) { - List parameterList = ctx.parameterList().value().stream().map(v -> v.getText()).collect(Collectors.toList()); - if(parameterList.size()<2)throw new DebugException("Fehler in Regex"); + List parameterList = parseParameterList(ctx.parameterList()); + if(parameterList.size()<2)throw new DebugException("Fehler in Equals-Regel"); String ls = parameterList.get(0); String rs = parameterList.get(1); - /* - RefTypeOrTPHOrWildcardOrGeneric lsType = this.getType(ls); - RefTypeOrTPHOrWildcardOrGeneric rsType = this.getType(rs); - if(lsType instanceof TypePlaceholder && rsType instanceof RefType) - return new PairTPHequalRefTypeOrWildcardType((TypePlaceholder) lsType, rsType); - else if(lsType instanceof TypePlaceholder && rsType instanceof TypePlaceholder) - return new PairTPHEqualTPH((TypePlaceholder)lsType, (TypePlaceholder)rsType); - else throw new NotImplementedException(); - */ + equalsRelations.add(new Relation(ls, rs)); + } @Override public void enterSmaller(UnifyResultParser.SmallerContext ctx) { + List parameterList = parseParameterList(ctx.parameterList()); + if(parameterList.size()<2)throw new DebugException("Fehler in Smaller-Regel"); + String ls = parameterList.get(0); + String rs = parameterList.get(1); + smallerRelations.add(new Relation(ls, rs)); } @Override public void enterTypeVar(UnifyResultParser.TypeVarContext ctx) { + List parameterList = parseParameterList(ctx.parameterList()); + if(parameterList.size()<1)throw new DebugException("Fehler in typeVar-Regel"); + tphs.add(parameterList.get(0)); } @Override - public void enterType(UnifyResultParser.TypeContext ctx) { - super.enterType(ctx); + public void enterType(UnifyResultParser.TypeContext ctx){ + List parameterList = parseParameterList(ctx.parameterList()); + if(parameterList.size()<3)throw new DebugException("Fehler in Type-Regel"); + String name = parameterList.get(0); + String typeName = parameterList.get(1); + String paramPointer = parameterList.get(2); + types.put(name, new ParsedType(typeName, paramPointer)); } /* @@ -104,62 +142,46 @@ public class ASPParser extends UnifyResultBaseListener { } */ private ASPParser(String toParse, Collection oldPlaceholders){ + System.out.println(toParse); this.originalTPHs = oldPlaceholders; + + /* JsonObject jsonResult = Json.createReader(new StringReader(toParse)).readObject(); JsonArray results = jsonResult.getJsonArray("Call").getJsonObject(0). getJsonArray("Witnesses").getJsonObject(0). getJsonArray("Value"); + //Im ersten Schritt werden alle Regeln geparst + String completeResult = ""; + for(int i = 0; i ret = new HashSet<>(); //Zuerst die params und typeVars: - for(int i = 0; i parsedType.params).collect(Collectors.toList())){ } + //Dann die Equalsdot Statements - for(int i = 0; i params = new ArrayList<>(); ParsedType t = types.get(name); - for(String param : t.params){ + + for(String param : getParams(t.params)){ params.add(this.getType(param)); } return new RefType(new JavaClassName(ASPStringConverter.fromConstant(t.name)), params, new NullToken()); @@ -189,49 +212,6 @@ public class ASPParser extends UnifyResultBaseListener { this.nextNode = next; } } - 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(); - if(b){ - if(m.groupCount()<3)throw new DebugException("Fehler in Regex"); - String pointer = m.group(1); - String type = 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"); - } - this.parameterLists.put(pointer,new ParameterListNode(type, next)); - } - } - - private void parseTypeVar(String statement){ - Pattern p = Pattern.compile(ASPRule.ASP_TYPE_VAR+"\\(([^,]+)\\)"); - Matcher m = p.matcher(statement); - boolean b = m.matches(); - if(b){ - if(m.groupCount()<1)throw new DebugException("Fehler in Regex"); - String name = m.group(1); - this.tphs.add(name); - } - } - - private void parseType(String statement){ - //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()<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) { List params = new ArrayList<>(); @@ -239,6 +219,7 @@ public class ASPParser extends UnifyResultBaseListener { while(pointer != null){ if(!parameterLists.containsKey(pointer)) throw new DebugException("Fehler in Ergebnisparsen"); + //TODO: Fehler in ASP. Die adapt Regel muss erkennen, wenn die Parameterliste auf der linken Seite kürzer ist und diese Rechtzeitig beenden ParameterListNode param = parameterLists.get(pointer); pointer = param.nextNode; params.add(param.type); diff --git a/src/de/dhbwstuttgart/sat/asp/parser/model/ParsedType.java b/src/de/dhbwstuttgart/sat/asp/parser/model/ParsedType.java index c49ce831..d9e06e50 100644 --- a/src/de/dhbwstuttgart/sat/asp/parser/model/ParsedType.java +++ b/src/de/dhbwstuttgart/sat/asp/parser/model/ParsedType.java @@ -4,8 +4,8 @@ import java.util.List; public class ParsedType { public final String name; - public final List params; - public ParsedType(String name, List params){ + public final String params; + public ParsedType(String name, String params){ this.name = name; this.params = params; }