public static List<Set<Integer>> computePositions(Production p) { return (List<Set<Integer>>) Arrays.asList(p.att().get("poly").split(";")) .stream().map(l -> Arrays.asList(l.split(",")).stream() .map(s -> Integer.valueOf(s.trim())).collect(Collectors.toSet())).collect(Collectors.toList()); }
private java.util.Set<Production> getStreamProductions(Set<Sentence> sentences) { java.util.Set<Production> productions = new HashSet<>(); for (Sentence s : mutable(sentences)) { if (s instanceof Production) { Production p = (Production) s; if (p.att().getOption("stream").isDefined()) { checkStreamName(p.att().get("stream")); productions.add(p); } } } return productions; }
/** * Searches for and retrieves (if found) a meta binder map for this label * See {@link org.kframework.kil.Production#getBinderMap()} * * @return the binder map for this label (or {@code null} if no meta binder map was defined. */ public Multimap<Integer, Integer> getMetaBinderMap() { if (isMetaBinder()) { return productionAttributes.get("metabinder", Multimap.class); } else { return null; } }
private Optional<KLabel> getAssocKLabelForUnit(KLabel klabel) { return definition.kLabelAttributes().entrySet().stream() .filter(e -> effectivelyAssocAttributes(e.getValue()) && e.getValue().get(Att.unit()).equals(klabel.name())) .map(e -> KORE.KLabel(e.getKey())) .findAny(); }
public Sort getCellSort(K k) { if (k instanceof KApply) { return labels.getCodomain(((KApply) k).klabel()); } else if (k instanceof KVariable) { return k.att().get(Sort.class); } else { throw new AssertionError("expected KApply or KVariable, found " + k.getClass().getSimpleName()); } }
@Override public void apply(KRewrite k) { sb.append("\\rewrites{"); convert(k.att().get(Sort.class), false); sb.append("}("); apply(k.left()); sb.append(","); apply(k.right()); sb.append(")"); }
@Override public void apply(KAs k) { Sort sort = k.att().get(Sort.class); sb.append("\\and{"); convert(sort, false); sb.append("}("); apply(k.pattern()); sb.append(","); apply(k.alias()); sb.append(")"); }
private KLabel computePolyKLabel(KApply k) { List<Set<Integer>> poly = polyKLabels.get(k.klabel().name()); List<Sort> params = new ArrayList<>(); for (Set<Integer> positions : poly) { int pos = positions.iterator().next(); Sort sort; if (pos == 0) { sort = k.att().get(Sort.class); } else { sort = k.items().get(pos-1).att().get(Sort.class); } params.add(sort); } return KLabel(k.klabel().name(), immutable(params)); }
private boolean isFunction(Production prod) { Production realProd = prod.att().get("originalPrd", Production.class); if (!realProd.att().contains(Attribute.FUNCTION_KEY)) { return false; } return true; }
private KLabel getWrapElement(KLabel collectionLabel) { return KLabel(m.attributesFor().apply(collectionLabel).get("wrapElement")); }
private KLabel getWrapElement(KLabel collectionLabel) { return KLabel(m.attributesFor().apply(collectionLabel).get("wrapElement")); }
@Override public void apply(KVariable k) { if (k.att().contains("denormal")) { normalization.put(KVariable(k.att().get("denormal")), k.name()); } } }.apply(normal);
@Override public K apply(KApply k) { if (k.klabel().name().startsWith("#SemanticCastTo")) { sort = k.att().get(Production.class).sort(); } return super.apply(k); }
private String getVarName(KVariable var) { if (var.att().contains("denormal")) { return var.att().get("denormal"); } return var.name(); }
@Override public K apply(K k) { if (isRHS() && !isLHS()) { if (cache.containsKey(k)) { return cache.get(k); } } if (isLHS() && !isRHS()) { if (usedOnRhs.contains(k)) { return KAs(super.apply(k), cache.get(k), Att.empty().add(Sort.class, cache.get(k).att().get(Sort.class))); } } return super.apply(k); } }.apply(term);
private static void color(Indenter indenter, Production p, int offset, ColorSetting colorize) { if (p.att().contains("color")) { indenter.append(ColorUtil.RgbToAnsi(p.att().get("color"), colorize)); } if (p.att().contains("colors")) { try { String color = p.att().get("colors").split(",")[offset].trim(); indenter.append(ColorUtil.RgbToAnsi(color, colorize)); } catch (ArrayIndexOutOfBoundsException e) { throw KEMException.compilerError("Invalid colors attribute. Must be a comma separated list with exactly one element per terminal.", e, p); } } }
public static Definition addSemanticsModule(Definition d) { java.util.Set<Module> allModules = mutable(d.modules()); Module languageParsingModule = Constructors.Module("LANGUAGE-PARSING", Set(d.mainModule(), d.getModule(d.att().get(Att.syntaxModule())).get(), d.getModule("K-TERM").get(), d.getModule(RuleGrammarGenerator.ID_PROGRAM_PARSING).get()), Set(), Att()); allModules.add(languageParsingModule); return Constructors.Definition(d.mainModule(), immutable(allModules), d.att()); }
@Override public void apply(KApply k) { if (k.klabel().name().startsWith("#SemanticCastTo")) { Sort savedContext = context; context = k.att().get(Production.class).sort(); apply(k.items().get(0)); context = savedContext; return; } if (k.klabel() instanceof KVariable) { apply((KVariable) k.klabel()); } super.apply(k); } }
private Sort sort(K k) { if (k instanceof KSequence) return Sorts.K(); if (k instanceof KAs) return sort(((KAs) k).pattern()); if (k instanceof InjectedKLabel) return Sorts.KItem(); if (k instanceof KToken) return ((KToken) k).sort(); if (k instanceof KApply) return k.att().get(Production.class).sort(); if (k instanceof KVariable) return Sorts.K(); throw KEMException.compilerError("Could not compute sort of term", k); }
public static K flattenTerm(Module mod, KApply kapp) { List<K> items = new ArrayList<>(); Att att = mod.attributesFor().apply(KLabel(kapp.klabel().name())); if (att.contains("assoc") && att.contains("unit")) { items = Assoc.flatten(kapp.klabel(), kapp.klist().items(), KLabel(att.get("unit"))); } else { items = kapp.klist().items(); } return KApply(kapp.klabel(), KList(items), kapp.att()); }