private int getArity(KLabel functionLabel) { Set<Integer> arities = stream(mainModule.productionsFor().apply(functionLabel)).map(Production::arity).collect(Collectors.toSet()); if (arities.size() > 1) { throw KEMException.compilerError("KLabel " + functionLabel + " has multiple productions with differing arities: " + mainModule.productionsFor().apply(functionLabel)); } assert arities.size() == 1; return arities.iterator().next(); }
private void forEachKLabel(Consumer<Tuple2<KLabel, Long>> action) { for (KLabel label : iterable(mainModule.definedKLabels())) { if (ConvertDataStructureToLookup.isLookupKLabel(label) || label.name().equals("#KToken")) continue; stream(mainModule.productionsFor().apply(label)).map(p -> Tuple2.apply(p.klabel().get(), stream(p.items()).filter(pi -> pi instanceof NonTerminal).count())).distinct().forEach(action); } }
/** * Gather sort information from a module. * Populates the close operators only for sorts which only have a single associative production. */ public static SortInfo fromModule(Module module) { Multimap<Sort, KLabel> joinOps = HashMultimap.create(); Collections.stream(module.productionsFor()).forEach(x -> { if (Collections.stream(x._2()).anyMatch(p -> p.att().contains("assoc") && Collections.stream(p.items()).filter(i -> i instanceof NonTerminal).count() == 2)) { joinOps.put(x._2().head().sort(), x._1()); } }); SortInfo info = new SortInfo(); joinOps.asMap().forEach((sort, labels) -> { info.closeOperators.put(sort, Iterators.getNext(labels.iterator(), null)); }); return info; } }
Term labelTerm = ((TermCons) child).get(0); Optional<KLabel> optLabel = klabelFromTerm(labelTerm); if (optLabel.isPresent() && m.productionsFor().contains(optLabel.get())) { Collection<Production> productions = mutable(m.productionsFor().get(optLabel.get()).get()); List<Term> rawArgs = lowerKList(((TermCons) child).get(1)); assert rawArgs.stream().allMatch(ProductionReference.class::isInstance);
private Set<Sort> sort(K k, Rule r) { if (k instanceof KVariable) { return Collections.singleton(k.att().get(Sort.class)); } else if (k instanceof KToken) { return Collections.singleton(((KToken)k).sort()); } else if (k instanceof KApply) { KApply kapp = (KApply)k; if (kapp.klabel() instanceof KVariable) { throw KEMException.compilerError("Cannot compute macros with klabel variables.", r); } Set<Production> prods = new HashSet<>(mutable(mod.productionsFor().apply(kapp.klabel()))); prods.removeIf(p -> p.arity() != kapp.items().size()); for (int i = 0; i < kapp.items().size(); i++) { final int idx = i; Set<Sort> sorts = sort(kapp.items().get(idx), r); prods.removeIf(p -> sorts.stream().noneMatch(s -> mod.subsorts().lessThanEq(s, p.nonterminal(idx).sort()))); } Set<Sort> candidates = prods.stream().map(Production::sort).collect(Collectors.toSet()); return candidates; } else { throw KEMException.compilerError("Cannot compute macros with sort check on terms that are not KApply, KToken, or KVariable.", r); } }
public void initialize() { disambModule.definedSorts(); disambModule.subsorts(); disambModule.priorities(); disambModule.leftAssoc(); disambModule.rightAssoc(); disambModule.productionsFor(); disambModule.overloads(); }
private Production production(KApply term) { scala.collection.Set<Production> prods = mod.productionsFor().apply(((KApply) term).klabel()); if (prods.size() != 1) { throw KEMException.compilerError("Could not find production for KApply with label " + term.klabel(), term); } return prods.head(); }
Set<KLabel> impurities = functions.stream().filter(lbl -> mainModule.attributesFor().apply(lbl).contains(Attribute.IMPURE_KEY)).collect(Collectors.toSet()); impurities.addAll(ancestors(impurities, dependencies)); constants = functions.stream().filter(lbl -> !impurities.contains(lbl) && stream(mainModule.productionsFor().apply(lbl)).filter(p -> p.arity() == 0).findAny().isPresent()).collect(Collectors.toSet());
private boolean isConstructorLike(KLabel klabel) { String labelName = klabel.name(); if (isInjectionLabel(labelName) || isBuiltinModuloConstructor(klabel)) return true; Set<Production> productionSet = module.productionsFor().apply(klabel); assert productionSet.size() == 1 : "Should not have more than one production"; Production production = productionSet.head(); return !production.att().contains(Att.Function()); }
private Production production(KApply term) { if (term.klabel().name().equals(KLabels.INJ)) return Production(INJ_PROD.klabel(), INJ_PROD.sort(), INJ_PROD.items(), Att.empty().add("originalPrd", Production.class, INJ_PROD)); scala.collection.Set<Production> prods = module.productionsFor().apply(((KApply) term).klabel()); assert(prods.size() == 1); return computePolyProd(prods.head()); }
if (!stream(mod.productionsFor().apply(candidateProduction.klabel().get())).filter(p -> p.sort().equals(sort) && p.arity() == candidateProduction.arity()).anyMatch(possibleParentProduction -> { for (int i = 0; i < candidateProduction.arity(); i++) { if (!mod.subsorts().lessThanEq(candidateProduction.nonterminal(i).sort(), possibleParentProduction.nonterminal(i).sort())) {
Sort.of(module.productionsFor().get(klabel).get().head().sort()), (KLabelConstant) convertedKLabel, KLabelConstant.of(org.kframework.backend.java.kil.KLabel.parse(module.attributesFor().get(klabel).get().<String>get(Att.unit())), global.getDefinition()), BuiltinList.Builder builder = BuiltinList.builder( Sort.of(stream(module.productionsFor().toStream()).filter(t -> t._1.equals(assocKLabelForUnit.get())).findAny().get()._2.head().sort()), KLabelConstant.of(assocKLabelForUnit.get(), global.getDefinition()), (KLabelConstant) convertedKLabel,
list = newDotVariable(m.productionsFor().get(collectionLabel).get().head().sort());
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;
String cellName = label.s(); Sort sort = Sort(getSortOfCell(cellName)); Option<Set<Production>> initializerProduction = m.productionsFor().get(KLabel(getInitLabel(sort))); if (initializerProduction.isDefined()) { Set<Production> realProds = stream(initializerProduction.get()).filter(p -> !p.att().contains("recordPrd", Production.class)).collect(Collections.toSet());
Sort topCellSort = configInfo.topCell(); KLabel topCellLabel = configInfo.getCellLabel(topCellSort); Production prod = m.productionsFor().apply(topCellLabel).head(); KToken cellName = KToken(prod.att().get("cellName"), Sort("#CellName"));
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(); } }