[PLUGIN]: Filter type insert points after insertion.

This commit is contained in:
Michael Uhl 2019-04-18 09:42:34 +02:00
parent e53c29f582
commit 905d9e25a2
5 changed files with 502 additions and 429 deletions

View File

@ -10,6 +10,7 @@ import java.util.stream.Collectors;
public class TypeInsertPoint { public class TypeInsertPoint {
public Token point; public Token point;
private String insertString; private String insertString;
private int extraOffset = 0;
public TypeInsertPoint(Token point, String toInsert){ public TypeInsertPoint(Token point, String toInsert){
this.point = point; this.point = point;
@ -21,19 +22,15 @@ public class TypeInsertPoint {
//token.point.getLine() != point.getLine() && token.point.getCharPositionInLine() <= point.getCharPositionInLine())) //token.point.getLine() != point.getLine() && token.point.getCharPositionInLine() <= point.getCharPositionInLine()))
token.point.getStartIndex() <= point.getStartIndex())) token.point.getStartIndex() <= point.getStartIndex()))
.mapToInt((typeInsertPoint -> typeInsertPoint.insertString.length())).sum(); .mapToInt((typeInsertPoint -> typeInsertPoint.insertString.length())).sum();
return new StringBuilder(intoSource).insert(point.getStartIndex()+offset, insertString).toString(); return new StringBuilder(intoSource).insert(point.getStartIndex()+offset+extraOffset, insertString).toString();
} }
public String getInsertString() { public String getInsertString() {
return insertString; return insertString;
} }
public Token getToken() { public void addExtraOffset(int toAdd) {
return this.point; this.extraOffset += toAdd;
}
public void setToken(Token point) {
this.point = point;
} }
/* PL 2018-06-19 /* PL 2018-06-19

View File

@ -27,4 +27,36 @@ public abstract class ResultPair<A extends RefTypeOrTPHOrWildcardOrGeneric,B ext
public String toString() { public String toString() {
return "(" + left.toString() + ", " + right.toString() + ")"; return "(" + left.toString() + ", " + right.toString() + ")";
} }
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((left == null) ? 0 : left.getOffset().hashCode());
result = prime * result + ((right == null) ? 0 : right.getOffset().hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ResultPair<?,?> other = (ResultPair<?,?>) obj;
if (left == null) {
if (other.left != null)
return false;
} else if (!left.getOffset().equals(other.left.getOffset()))
return false;
if (right == null) {
if (other.right != null)
return false;
} else if (!right.getOffset().equals(other.right.getOffset()))
return false;
return true;
}
} }

View File

@ -0,0 +1,25 @@
package de.dhbwstuttgart.typeinference.result;
import java.util.HashMap;
import java.util.Map;
import org.antlr.v4.runtime.Token;
public class ResultPairMap {
private Map<Token, ResultPair<?,?>> indexMap = new HashMap<>();
public static final ResultPairMap RESULT_PAIRS = new ResultPairMap();
private ResultPairMap() {
// Nothing to do here.
}
public void put(Token tkn, ResultPair<?, ?> resultPair) {
indexMap.put(tkn, resultPair);
}
public ResultPair<?,?> get(Token tkn) {
return indexMap.get(tkn);
}
}

View File

@ -1,16 +1,31 @@
package de.dhbwstuttgart.typeinference.result; package de.dhbwstuttgart.typeinference.result;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.type.*;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType;
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import static de.dhbwstuttgart.typeinference.result.ResultPairMap.RESULT_PAIRS;
@SuppressWarnings("rawtypes")
public class ResultSet { public class ResultSet {
public final Set<ResultPair> results; public final Set<ResultPair> results;
public ResultSet(Set<ResultPair> results){ public ResultSet(Set<ResultPair> set){
this.results = results; this.results = set;
}
public boolean contains(ResultPair toCheck) {
return this.results.contains(toCheck);
}
public void remove(ResultPair toCheck) {
results.remove(toCheck);
} }
public ResolvedType resolveType(RefTypeOrTPHOrWildcardOrGeneric type) { public ResolvedType resolveType(RefTypeOrTPHOrWildcardOrGeneric type) {
@ -49,12 +64,14 @@ class Resolver implements ResultSetVisitor {
toResolve = tph; toResolve = tph;
resolved = null; resolved = null;
System.out.println(tph.toString()); System.out.println(tph.toString());
for(ResultPair resultPair : result.results){ for(ResultPair<?,?> resultPair : result.results) {
RESULT_PAIRS.put(resultPair.getRight().getOffset(), resultPair);
RESULT_PAIRS.put(resultPair.getLeft().getOffset(), resultPair);
if(resultPair instanceof PairTPHEqualTPH && ((PairTPHEqualTPH) resultPair).getLeft().equals(toResolve)){ if(resultPair instanceof PairTPHEqualTPH && ((PairTPHEqualTPH) resultPair).getLeft().equals(toResolve)){
return resolve(((PairTPHEqualTPH) resultPair).getRight()); return resolve(((PairTPHEqualTPH) resultPair).getRight());
} }
} }
for(ResultPair resultPair : result.results){ for(ResultPair<?,?> resultPair : result.results){
resultPair.accept(this); resultPair.accept(this);
} }
if(resolved==null){//TPH kommt nicht im Result vor: if(resolved==null){//TPH kommt nicht im Result vor:
@ -121,6 +138,7 @@ class Resolver implements ResultSetVisitor {
/** /**
* Sucht aus dem Result Set den Sub/supertyp für einen TPH * Sucht aus dem Result Set den Sub/supertyp für einen TPH
*/ */
@SuppressWarnings("rawtypes")
class TPHResolver implements ResultSetVisitor { class TPHResolver implements ResultSetVisitor {
private final TypePlaceholder tph; private final TypePlaceholder tph;
@ -189,6 +207,7 @@ class TPHResolver implements ResultSetVisitor {
} }
} }
@SuppressWarnings("rawtypes")
class RelatedTypeWalker implements ResultSetVisitor { class RelatedTypeWalker implements ResultSetVisitor {
final Set<GenericInsertPair> relatedTPHs = new HashSet<>(); final Set<GenericInsertPair> relatedTPHs = new HashSet<>();