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); } }
public Term apply(Ambiguity a) { Set<Production> productions = new HashSet<>(); for (Term t : a.items()) { if (t instanceof TermCons) { TermCons tc = (TermCons)t; productions.add(tc.production()); } else { return a; } } Set<Production> candidates = overloads.minimal(productions); Ambiguity result = Ambiguity.apply(a.items().stream().filter(t -> candidates.contains(((ProductionReference)t).production())).collect(Collectors.toSet())); if (result.items().size() == 1) { return result.items().iterator().next(); } return result; } }
private boolean isRightAssoc(ProductionReference outer, ProductionReference inner) { Tag parentLabel = new Tag(outer.production().klabel().get().name()); Tag localLabel = new Tag(inner.production().klabel().get().name()); if (m.rightAssoc().contains(new Tuple2<>(parentLabel, localLabel))) { return true; } return false; }
if (listSorts.contains(expectedSort) && !(subsorts.lessThanEq(childSort, expectedSort) && listSorts.contains(childSort))) { final boolean isBracket = child.production().att().contains("bracket"); if (isBracket || (child.production().klabel().isDefined() && child.production().klabel().get().name().equals("#KRewrite"))) { newItems.add(child); } else if (childSort.equals(Sorts.K()) || !subsorts.lessThan(childSort, expectedSort)) { String msg = "Found sort '" + childSort + "' where list sort '" + expectedSort + "' was expected. Moving on."; warnings.add(new ParseFailedException( new KException(KException.ExceptionType.HIDDENWARNING, KException.KExceptionGroup.LISTS, msg, child.source().get(), child.location().get()))); newItems.add(child); } else { TermCons terminator = TermCons.apply(ConsPStack.empty(), ulTerm.pTerminator, child.location(), child.source()); TermCons newTc = TermCons.apply(ConsPStack.from(Arrays.asList(terminator, child)), ul.pList, child.location(), child.source()); newItems.add(newTc); changed = true;
private boolean isLeftAssoc(ProductionReference outer, ProductionReference inner) { Tag parentLabel = new Tag(outer.production().klabel().get().name()); Tag localLabel = new Tag(inner.production().klabel().get().name()); if (m.leftAssoc().contains(new Tuple2<>(parentLabel, localLabel))) { return true; } return false; }
ProductionReference getRightCapture(ProductionReference previousRightCapture, ProductionReference outer, ProductionReference inner) { EnumSet<Fixity> fixity = getFixity(outer); int position = getPosition(inner, outer); if (position == outer.production().items().size() - 1 && fixity.contains(Fixity.BARE_RIGHT)) { return previousRightCapture; } else { return outer; } }
public static boolean isFunctionRule(TermCons tc) { if (tc.production().sort().name().equals("RuleContent")) { ProductionReference child = (ProductionReference) tc.get(0); if (child.production().klabel().isDefined() && child.production().klabel().get().equals(KLabels.KREWRITE)) { child = (ProductionReference)((TermCons)child).get(0); } return child.production().att().contains(Attribute.FUNCTION_KEY); } return false; }
private EnumSet<Fixity> getFixity(ProductionReference t) { Production p = t.production(); EnumSet<Fixity> set = EnumSet.noneOf(Fixity.class); if (t instanceof Constant) { return set; } if (p.items().apply(0) instanceof NonTerminal) set.add(Fixity.BARE_LEFT); if (p.items().apply(p.items().size() - 1) instanceof NonTerminal) set.add(Fixity.BARE_RIGHT); return set; }
public Term preferAvoid(Ambiguity amb) { List<Term> prefer = new ArrayList<>(); List<Term> avoid = new ArrayList<>(); for (Term t : amb.items()) { if (t instanceof ProductionReference) { if (((ProductionReference) t).production().att().contains("prefer")) { prefer.add(t); } else if (((ProductionReference) t).production().att().contains("avoid")) { avoid.add(t); } } } Term result = amb; if (!prefer.isEmpty()) { if (prefer.size() == 1) { result = prefer.get(0); } else { amb.replaceChildren(prefer); } } else if (!avoid.isEmpty()) { if (avoid.size() < amb.items().size()) { amb.items().removeAll(avoid); if (amb.items().size() == 1) result = amb.items().iterator().next(); } } return result; }
private Sort getSort(ProductionReference child) { if ((child instanceof TermCons) && child.production().klabel().isDefined()) { KLabel label = child.production().klabel().get(); if (label.name().equals("#KApply")) { Term labelTerm = ((TermCons) child).get(0); return child.production().sort();
private boolean isPriorityWrong(ProductionReference outer, ProductionReference inner) { Tag parentLabel = new Tag(outer.production().klabel().get().name()); Tag localLabel = new Tag(inner.production().klabel().get().name()); if (m.priorities().lessThan(parentLabel, localLabel)) { return true; } if (m.leftAssoc().contains(new Tuple2<>(parentLabel, localLabel))) { return true; } if (m.rightAssoc().contains(new Tuple2<>(parentLabel, localLabel))) { return true; } return false; }
private boolean isPriorityWrong(ProductionReference outer, ProductionReference inner, int position) { if (outer.production().klabel().isEmpty() || inner.production().klabel().isEmpty()) { return false; } Tag parentLabel = new Tag(outer.production().klabel().get().name()); Tag localLabel = new Tag(inner.production().klabel().get().name()); if (!m.subsorts().lessThanEq(inner.production().sort(), ((NonTerminal)outer.production().items().apply(position)).sort())) { return true; } if (m.priorities().lessThan(parentLabel, localLabel)) { return true; } if (m.leftAssoc().contains(new Tuple2<>(parentLabel, localLabel)) && position == outer.production().items().size() - 1) { return true; } if (m.rightAssoc().contains(new Tuple2<>(parentLabel, localLabel)) && position == 0) { return true; } return false; }
EnumSet<Fixity> fixity = getFixity(inner, outer); EnumSet<Fixity> innerFixity = getFixity(inner); if (inner.production().klabel().equals(outer.production().klabel()) && inner.production().klabel().isDefined() && m.attributesFor().apply(inner.production().klabel().get()).contains(Attribute.ASSOCIATIVE_KEY)) return false; if (inner instanceof Constant) if (priority) return true; if (inner.production().isSyntacticSubsort()) return false; inversePriority = isPriorityWrong(inner, rightCapture, inner.production().items().size() - 1); EnumSet<Fixity> rightCaptureFixity = getFixity(rightCapture); if (!inversePriority && rightCaptureFixity.contains(Fixity.BARE_LEFT)) {
public static Sort getSortOfCast(TermCons tc) { switch (tc.production().klabel().get().name()) { case "#ruleNoConditions": case "#ruleRequires": case "#ruleEnsures": case "#ruleRequiresEnsures": { ProductionReference child = (ProductionReference) tc.get(0); if (child.production().klabel().isDefined() && child.production().klabel().get().equals(KLabels.KREWRITE)) { child = (ProductionReference)((TermCons)child).get(0); } return child.production().sort(); } case "#SyntacticCast": case "#OuterCast": return tc.production().sort(); case "#InnerCast": return ((NonTerminal)tc.production().items().apply(1)).sort(); default: if (tc.production().klabel().get().name().startsWith("#SemanticCastTo")) { return tc.production().sort(); } throw new AssertionError("Unexpected cast type"); } }
if (elem instanceof ProductionReference) { ProductionReference tc = (ProductionReference) elem; msg += tc.production().toString();
prod = ref.production(); if (ref instanceof TermCons) { arity = ((TermCons) ref).items().size(); } else if (!prod.equals(ref.production())) { return a;
public ProductionReference addBrackets(ProductionReference inner, TermCons outer, ProductionReference leftCapture, ProductionReference rightCapture) { if (requiresBracketWithSimpleAlgorithm(outer, leftCapture, rightCapture, inner)) { int position = getPosition(inner, outer); Sort outerSort = ((NonTerminal)outer.production().items().apply(position)).sort(); Sort innerSort = inner.production().sort(); for (Tuple2<Sort, scala.collection.immutable.List<Production>> sort : iterable(m.bracketProductionsFor())) { boolean isCorrectOuterSort = m.subsorts().lessThanEq(sort._1(), outerSort); if (isCorrectOuterSort) { for (Production p : mutable(sort._2())) { boolean isCorrectInnerSort = stream(p.items()) .filter(i -> i instanceof NonTerminal) .map(i -> (NonTerminal) i) .map(NonTerminal::sort) .filter(s -> m.subsorts().lessThanEq(innerSort, s)) .findAny().isPresent(); if (isCorrectInnerSort) { return TermCons.apply(ConsPStack.singleton(inner), p); } } } } return TermCons.apply(ConsPStack.singleton(inner), Production(Sorts.KBott(), Seq(NonTerminal(Sorts.K())))); } return inner; }