private static K makeBody(List<K> children) { if (children.size() == 1) { return children.get(0); } else { return KApply(KLabels.CELLS, KList(children)); } }
public static KApply make(KLabel label, boolean openLeft, K child, boolean openRight) { return KApply(label, KList(makeDots(openLeft), child, makeDots(openRight))); } public static KApply make(KLabel label, boolean openLeft, List<K> children, boolean openRight) {
@Override public K apply(KApply k) { ArrayList<K> newItems = new ArrayList<>(k.klist().items()); boolean change = false; for (int i = 0; i < newItems.size(); ++i) { K in = newItems.get(i); K out = apply(in); newItems.set(i, out); change = change || (in != out); } if (change) { return KApply(apply(k.klabel()), KList(newItems), k.att()); } else { return k; } }
@Override public K apply(KApply k) { List<K> children = new ArrayList<>(); for (K child : k.klist().items()) { K res = apply(child); if (res instanceof KSequence) { children.add(res); } else { children.add(KSequence(res)); } } return KApply(k.klabel(), KList(children), k.att()); }
private Rule funRule(KLabel fun, K k, Att att) { K resolved = transform(k); K withAnonVars = new ResolveAnonVar().resolveK(resolved); List<K> klist = new ArrayList<>(); klist.add(RewriteToTop.toLeft(withAnonVars)); klist.addAll(closure(k)); return Rule(KRewrite(KApply(fun, KList(klist)), RewriteToTop.toRight(withAnonVars)), BooleanUtils.TRUE, BooleanUtils.TRUE, att); }
public static KApply make(KLabel label, boolean openLeft, List<K> children, boolean openRight) { return KApply(label, KList(makeDots(openLeft), makeBody(children), makeDots(openRight))); } }
return KApply(k.klabel(), KList(ordered), k.att());
K concretize(K term) { if (term instanceof KApply) { KApply app = (KApply) term; KApply newTerm = KApply(app.klabel(), KList(app.klist().stream() .map(this::concretize).collect(Collectors.toList()))); if (cfg.isParentCell(newTerm.klabel())) { return concretizeCell(newTerm); } else { return newTerm; } } else if (term instanceof KRewrite) { KRewrite rew = (KRewrite) term; return KRewrite(concretize(rew.left()), concretize(rew.right())); } else if (term instanceof KSequence) { return KSequence(((KSequence) term).stream() .map(this::concretize).collect(Collectors.toList())); } else { return term; } }
klist.add(apply(arg)); klist.addAll(closure(body)); return KApply(fun, KList(klist));
@Override public K apply(KApply k0) { if (hasCells(k0)) { ArrayList<K> klist0 = new ArrayList<K>(Collections.nCopies(k0.klist().size(), null)); for (int idx = 0; idx < k0.klist().size(); idx++) { K item0 = k0.klist().items().get(idx); klist0.set(idx, item0); if (item0 instanceof KApply) { KApply k = (KApply) item0; if (k.klabel().name().equals("#cells")) { if (cellFragmentVarsCell.contains(k)) { Sort cellFragmentSort = nthArgSort(k0.klabel(), idx); if (cellFragmentSort == null) { throw new IllegalArgumentException("Not found " + idx + "th argument sort of " + k0.klabel()); } if (cellFragmentSort.name().endsWith("Fragment")) { Sort cellSort = Sort(cellFragmentSort.name().substring(0,cellFragmentSort.name().indexOf("Fragment"))); KLabel cellLabel = cfg.cfg.getCellLabel(cellSort); klist0.set(idx, KApply(cellLabel, KList(item0), Att().add("dummy_cell"))); } } } } } return KApply(k0.klabel(), KList(klist0), k0.att()); } return super.apply(k0); } @Override
return KApply(label, KList(contents)); } else { if (requiredLeft.equals(requiredRight)) { newItems.addAll(contents); newItems.add(newDotVariable(null)); return KApply(label, KList(newItems)); } else { newContents.add(cfg.getDefaultCell(reqChild)); return (KApply(label, KList(newContents))); return KApply(label, KList(contents.get(0))); newItems.add(newDotVariable(cellType)); return KApply(label, KList(KSequence(newItems))); } else { KLabel closeOperator = sortInfo.getCloseOperator(cellType); body = KApply(closeOperator, KList(body, newDotVariable(cellType))); body = KApply(closeOperator, KList(leftVar, body)); return KApply(label, KList(body));
requires = sideCondition.get(); Context ctx = Context(KApply(production.klabel().get(), KList(items)), requires, production.att()); sentences.add(ctx); K term = KApply(production.klabel().get(), KList(items)); Optional<KApply> sideCondition = strictnessPositions.stream().map(j -> KApply(KLabel("isKResult"), KVariable("K" + (j - 1)))).reduce(BooleanUtils::and); K requires;
requires = sideCondition.get(); Rule r = Rule(KRewrite(KApply(KLabel("is" + sort.toString()), KApply(p.klabel().get(), KList(klist))), BooleanUtils.TRUE), requires, BooleanUtils.TRUE); res.add(r);
return KSequence(visitChildren(term, Sorts.KItem(), actualSort)); } else { return KSequence(KApply(KLabel("inj", actualSort, Sorts.KItem()), KList(visitChildren(term, Sorts.KItem(), actualSort)), Att.empty().add(Sort.class, Sorts.KItem()))); if (collectionIsMap(collectionLabel)) { return KApply(elementLabel, KList(k.klist().items().get(0), visitChildren(k, expectedSort, actualSort)), Att.empty().add(Sort.class, expectedSort)); } else { return KApply(elementLabel, KList(visitChildren(k, expectedSort, actualSort)), Att.empty().add(Sort.class, expectedSort)); return KApply(KLabel("inj", actualSort, expectedSort), KList(visitChildren(term, expectedSort, actualSort)), Att.empty().add(Sort.class, expectedSort));
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()); }
children.add(addInjections(child, expectedSort)); return KApply(kapp.klabel(), KList(children), att); } else if (term instanceof KRewrite) { KRewrite rew = (KRewrite) term;
K configuration = cell("threads", Collections.emptyMap(), cell("thread", Collections.singletonMap("multiplicity", "*"), cells(cell("k", Collections.emptyMap(), KApply(KLabel("#SemanticCastToKItem"), KList(KToken("$PGM", Sorts.KConfigVar())), Att.empty().add(Production.class, prod2))), cell("opt", Collections.singletonMap("multiplicity", "?"), KApply(KLabel(".Opt"), KList(), Att.empty().add(Production.class, prod)))))); Module m1 = Module("CONFIG", Set(def.getModule("KSEQ").get()), Set(prod), Att()); RuleGrammarGenerator parserGen = new RuleGrammarGenerator(def);