static Term visitPolyChildrenSafe(TermCons tc, Function<Term, Term> apply) { for (int i : getPolyChildren(tc)) { apply.apply(tc.get(i - 1)); } return tc; }
public Either<java.util.Set<ParseFailedException>, Term> apply(TermCons tc) { if (hasPolySort(tc)) { return visitPolyChildren(tc, this::apply); } return Right.apply(tc); }
public Term apply(TermCons tc) { if (hasPolySort(tc)) { return visitPolyChildrenSafe(tc, this::apply); } return tc; }
public Tuple2<Either<java.util.Set<ParseFailedException>, Term>, java.util.Set<VarInfo>> apply(TermCons tc) { if (hasPolySort(tc)) { return visitPolyChildrenSets(tc, this::apply); } return simpleResult(tc); }
public Term apply(TermCons tc) { for (int i = 0, j = 0; i < tc.production().items().size(); i++) { if (tc.production().items().apply(i) instanceof NonTerminal) { if (tc.production().klabel().isDefined() && (tc.production().klabel().get().name().equals("#SyntacticCast") || tc.production().klabel().get().name().startsWith("#SemanticCastTo") || tc.production().klabel().get().name().equals("#InnerCast")) || (isFunctionRule(tc) && j == 0)) { Term t = tc.get(0); new CollectUndeclaredVariables2(getSortOfCast(tc)).apply(t); j++; } else { Term t = tc.get(j); new CollectUndeclaredVariables2(((NonTerminal) tc.production().items().apply(i)).sort()).apply(t); j++; } } } return super.apply(tc); }
return simpleError(Sets.newHashSet(new VariableTypeClashException(kex))); for (VarKey key : variant.keySet()) { Collection<Sort> values = variant.get(key); Set<Sort> mins = lowerBounds(values); if (mins.size() == 0) { fails = key; String msg = "Could not infer a sort for variable " + fails + " to match every location."; KException kex = new KException(ExceptionType.ERROR, KExceptionGroup.CRITICAL, msg, loc.source().orElse(null), loc.location().orElse(null)); return simpleError(Sets.newHashSet(new VariableTypeClashException(kex))); return simpleError(Sets.newHashSet(new ParseFailedException(kex))); msg = msg.substring(0, msg.length() - 2); KException kex = new KException(ExceptionType.ERROR, KExceptionGroup.CRITICAL, msg, loc.source().orElse(null), loc.location().orElse(null)); return simpleError(Sets.newHashSet(new VariableTypeClashException(kex))); Set<VarKey> reportVars = hittingSet(solution.keySet(), badVars); String msg = "Could not infer unique sorts. Each variable has a unique greatest possible sort," +" but these cannot all be assigned simultaneously: "; return simpleError(Sets.newHashSet(new VariableTypeClashException(kex))); } else {
if (rez.isLeft()) return new Tuple2<>(rez, warn); Tuple2<Either<Set<ParseFailedException>, Term>, Set<ParseFailedException>> rez2 = new VariableTypeInferenceFilter(disambModule.subsorts(), disambModule.definedSorts(), disambModule.productionsFor(), strict && inferSortChecks, true).apply(rez.right().get()); if (rez2._1().isLeft()) return rez2;
public Tuple2<Either<java.util.Set<ParseFailedException>, Term>, java.util.Set<VarInfo>> apply(TermCons tc) { // TODO: (Radu) if this is cast, take the sort from annotations? Set<VarInfo> collector = Sets.newHashSet(); for (int i = 0, j = 0; i < tc.production().items().size(); i++) { if (tc.production().items().apply(i) instanceof NonTerminal) { if (tc.production().klabel().isDefined() && (tc.production().klabel().get().name().equals("#SyntacticCast") || tc.production().klabel().get().name().startsWith("#SemanticCastTo") || tc.production().klabel().get().name().equals("#InnerCast"))) { Term t = tc.get(0); collector = new CollectVariables2(getSortOfCast(tc), VarType.USER).apply(t)._2(); } else if (tc.production().klabel().isDefined() && isFunctionRule(tc) && j == 0) { Term t = tc.get(0); collector = new CollectVariables2(getSortOfCast(tc), VarType.CONTEXT).apply(t)._2(); j++; } else { Term t = tc.get(j); Set<VarInfo> vars = new CollectVariables2(((NonTerminal) tc.production().items().apply(i)).sort(), VarType.CONTEXT).apply(t)._2(); collector.addAll(vars); j++; } } } Tuple2<Either<java.util.Set<ParseFailedException>, Term>, java.util.Set<VarInfo>> rez = super.apply(tc); return new Tuple2<>(Right.apply(rez._1().right().get()), mergeWarnings(collector, rez._2())); }
public static Term disambiguateForUnparse(Module mod, Term ambiguity) { Term rez3 = new PushTopAmbiguityUp().apply(ambiguity); Either<Set<ParseFailedException>, Term> rez = new ApplyTypeCheckVisitor(mod.subsorts()).apply(rez3); Tuple2<Either<Set<ParseFailedException>, Term>, Set<ParseFailedException>> rez2; if (rez.isLeft()) { rez2 = new AmbFilter(false).apply(rez3); return rez2._1().right().get(); } rez2 = new VariableTypeInferenceFilter(mod.subsorts(), mod.definedSorts(), mod.productionsFor(), false, false).apply(rez.right().get()); if (rez2._1().isLeft()) { rez2 = new AmbFilter(false).apply(rez.right().get()); return rez2._1().right().get(); } rez3 = new PushAmbiguitiesDownAndPreferAvoid(mod.overloads()).apply(rez2._1().right().get()); rez2 = new AmbFilter(false).apply(rez3); return rez2._1().right().get(); } }
|| tc.production().klabel().get().name().startsWith("#SemanticCastTo") || tc.production().klabel().get().name().equals("#InnerCast") || (isFunctionRule(tc)) && j == 0)) { Term t = tc.get(0); boolean strictSortEquality = tc.production().klabel().get().name().equals("#SyntacticCast") || tc.production().klabel().get().name().equals("#InnerCast"); Either<Set<ParseFailedException>, Term> rez = new ApplyTypeCheck2(getSortOfCast(tc), true, strictSortEquality, strictSortEquality && inferSortChecks).apply(t); if (rez.isLeft()) return rez;
public Either<java.util.Set<ParseFailedException>, Term> apply(ProductionReference pr) { if (pr instanceof TermCons) { TermCons tc = (TermCons) pr; if (VariableTypeInferenceFilter.hasPolySort(tc)) { return VariableTypeInferenceFilter.visitPolyChildren(tc, this::apply); } } if (pr instanceof Constant && pr.production().sort().equals(Sorts.KVariable())) { // skip variables since they will always have a different sort at this stage. return Right.apply(pr); } if ((!strict && !subsorts.lessThanEq(pr.production().sort(), sort)) || (strict && !pr.production().sort().equals(sort))) { String msg = "Unexpected sort " + pr.production().sort() + " for term parsed as production " + pr.production() + ". Expected " + sort + "."; KException kex = new KException(KException.ExceptionType.ERROR, KException.KExceptionGroup.CRITICAL, msg, pr.source().orElse(null), pr.location().orElse(null)); return Left.apply(Sets.newHashSet(new VariableTypeClashException(kex))); } return Right.apply(pr); } }
static Either<Set<ParseFailedException>,Term> visitPolyChildren(TermCons tc, Function<Term, Either<Set<ParseFailedException>, Term>> apply) { Set<ParseFailedException> errors = new HashSet<>(); for (int i : getPolyChildren(tc)) { Either<Set<ParseFailedException>, Term> res = apply.apply(tc.get(i - 1)); if (res.isLeft()) { errors.addAll(res.left().get()); } } if (errors.isEmpty()) return Right.apply(tc); else return Left.apply(errors); }
|| tc.production().klabel().get().name().startsWith("#SemanticCastTo") || tc.production().klabel().get().name().equals("#InnerCast")) || (VariableTypeInferenceFilter.isFunctionRule(tc) && j == 0)) { Term t = tc.get(0); boolean strict = tc.production().klabel().get().name().equals("#SyntacticCast") || tc.production().klabel().get().name().equals("#InnerCast"); Either<Set<ParseFailedException>, Term> rez = new ApplyTypeCheck2(VariableTypeInferenceFilter.getSortOfCast(tc), strict).apply(t); if (rez.isLeft()) return rez;
static Tuple2<Either<Set<ParseFailedException>, Term>, Set<VarInfo>> visitPolyChildrenSets(TermCons tc, Function<Term, Tuple2<Either<Set<ParseFailedException>, Term>, Set<VarInfo>>> apply) { Set<ParseFailedException> errors = new HashSet<>(); Set<VarInfo> info = new HashSet<>(); for (int i : getPolyChildren(tc)) { Tuple2<Either<Set<ParseFailedException>, Term>, Set<VarInfo>> res = apply.apply(tc.get(i - 1)); info.addAll(res._2()); if (res._1().isLeft()) errors.addAll(res._1().left().get()); } if (errors.isEmpty()) return Tuple2.apply(Right.apply(tc), info); else return Tuple2.apply(Left.apply(errors), info); }