private static Set<Sentence> makeCasts(Sort outerSort, Sort innerSort, Sort castSort) { Set<Sentence> prods = new HashSet<>(); Att attrs1 = Att().add(Sort.class, castSort); prods.add(Production(KLabel("#SyntacticCast"), castSort, Seq(NonTerminal(castSort), Terminal("::" + castSort.toString())), attrs1)); prods.add(Production(KLabel("#SemanticCastTo" + castSort.toString()), castSort, Seq(NonTerminal(castSort), Terminal(":" + castSort.toString())), attrs1)); prods.add(Production(KLabel("#InnerCast"), outerSort, Seq(Terminal("{"), NonTerminal(castSort), Terminal("}"), Terminal("<:" + castSort.toString())), attrs1)); prods.add(Production(KLabel("#OuterCast"), castSort, Seq(Terminal("{"), NonTerminal(innerSort), Terminal("}"), Terminal(":>" + castSort.toString())), attrs1)); return prods; } }
private static Production resolve(Production prod) { if (prod.klabel().isDefined() && prod.klabel().get().equals(KLabels.GENERATED_TOP_CELL)) { List<Integer> cellPositions = new ArrayList<Integer>(); int i = 1; for (ProductionItem p: JavaConverters.seqAsJavaList(prod.items())) { if (p instanceof NonTerminal) { NonTerminal nt = (NonTerminal) p; if (! nt.sort().equals(Sorts.GeneratedCounterCell())) { cellPositions.add(i); } } i++; } StringBuilder format = new StringBuilder(); if (cellPositions.size() == 1) { format.append("%").append(cellPositions.get(0)); } else { format.append("%1%i"); int j; for (j = 0; j < cellPositions.size(); j++) { format.append("%n%").append(cellPositions.get(j)); } format.append("%d%n%").append(cellPositions.get(j - 1) + 1); } return Production(prod.klabel(), prod.sort(), prod.items(), prod.att().add("format", format.toString())); } return prod; }
private Production funProd(KLabel fun, K k) { List<ProductionItem> pis = new ArrayList<>(); K left = RewriteToTop.toLeft(k); K right = RewriteToTop.toRight(k); pis.add(Terminal(fun.name())); pis.add(Terminal("(")); pis.add(NonTerminal(sort(left))); for (KVariable var : closure(k)) { pis.add(Terminal(",")); pis.add(NonTerminal(var.att().getOptional(Sort.class).orElse(Sorts.K()))); } pis.add(Terminal(")")); return Production(fun, sort(right), immutable(pis), Att().add("function")); }
private Production resolve(Production prod) { if (kore && prod.klabel().isDefined() && prod.klabel().get().equals(KLabels.GENERATED_TOP_CELL)) { List<ProductionItem> pis = stream(prod.items()).collect(Collectors.toCollection(ArrayList::new)); int idx = 0; int i = 0; for (ProductionItem pi : pis) { if (pi instanceof NonTerminal) { idx = i; } i++; } pis.add(idx, NonTerminal(Sorts.GeneratedCounterCell())); return Production(prod.klabel().get(), prod.sort(), immutable(pis), prod.att()); } return prod; }
public static Sentence convertListItemToNonFunction(Module mod, Sentence sentence) { if (!(sentence instanceof Production)) { return sentence; } Production prod = (Production)sentence; if (prod.klabel().isDefined() && KLabels.ListItem.equals(prod.klabel().get())) { return Production(prod.sort(), prod.items(), prod.att().remove("function")); } return prod; }
return Production(KLabel(prod.klabel().get().name(), immutable(params)), returnSort, immutable(items), prod.att().add("originalPrd", Production.class, prod));
public static Module subsortKItem(Module module) { java.util.Set<Sentence> prods = new HashSet<>(); for (Sort srt : iterable(module.definedSorts())) { if (!RuleGrammarGenerator.isParserSort(srt)) { // KItem ::= Sort Production prod = Production(Sorts.KItem(), Seq(NonTerminal(srt)), Att()); if (!module.sentences().contains(prod)) { prods.add(prod); } } } if (prods.isEmpty()) { return module; } else { return Module(module.name(), module.imports(), Stream.concat(stream(module.localSentences()), prods.stream()) .collect(org.kframework.Collections.toSet()), module.att()); } }
@Override public Either<Set<ParseFailedException>, Term> apply(TermCons tc) { if (overloads.elements().contains(tc.production()) && tc.items().isEmpty()) { Set<Production> candidates = stream(overloads.elements()).filter(p -> p.klabel().isDefined() && p.klabelAtt().equals(tc.production().klabelAtt()) && overloads.lessThanEq(p, tc.production())).collect(Collectors.toSet()); candidates = overloads.minimal(candidates); if (candidates.size() != 1) { KException ex = new KException(KException.ExceptionType.ERROR, KException.KExceptionGroup.INNER_PARSER, "Overloaded term does not have a least sort. Possible sorts: " + candidates, tc.source().orElse(null), tc.location().orElse(null)); return Left.apply(Sets.newHashSet(new ParseFailedException(ex))); } Production prod = candidates.iterator().next(); prod = Production(prod.klabel(), prod.sort(), prod.items(), prod.att().add("originalPrd", Production.class, tc.production())); return super.apply(TermCons.apply(tc.items(), prod, tc.location(), tc.source())); } return super.apply(tc); } }
public void applyUserList(Set<org.kframework.definition.Sentence> res, org.kframework.kore.Sort sort, Production p, UserList userList) { // Transform list declarations of the form Es ::= List{E, ","} into something representable in kore org.kframework.kore.Sort elementSort = userList.getSort(); org.kframework.attributes.Att attrs = convertAttributes(p).add(Att.userList(), userList.getListType()); String kilProductionId = "" + System.identityHashCode(p); Att attrsWithKilProductionId = attrs.add(PRODUCTION_ID, kilProductionId); org.kframework.definition.Production prod1, prod3; // Es ::= E "," Es prod1 = Production(KLabel(p.getKLabel(kore)), sort, Seq(NonTerminal(elementSort), Terminal(userList.getSeparator()), NonTerminal(sort)), attrsWithKilProductionId.add("right")); // Es ::= ".Es" prod3 = Production(KLabel(p.getTerminatorKLabel(kore)), sort, Seq(Terminal("." + sort.toString())), attrsWithKilProductionId.remove("format").remove("strict").add("klabel", p.getTerminatorKLabel(false))); res.add(prod1); res.add(prod3); }
public Module gen(Module mod) { Set<Sentence> res = new HashSet<>(); for (Sort sort : iterable(mod.definedSorts())) { Production prod = Production(KLabel("is" + sort.toString()), Sorts.Bool(), Seq(Terminal("is" + sort.toString()), Terminal("("), NonTerminal(Sorts.K()), Terminal(")")), Att().add(Attribute.FUNCTION_KEY).add(Attribute.PREDICATE_KEY, Sort.class, sort)); if (!mod.productions().contains(prod)) res.add(prod); } if (!res.isEmpty()) { res.add(SyntaxSort(Sorts.K())); } return Module(mod.name(), mod.imports(), (scala.collection.Set<Sentence>) mod.localSentences().$bar(immutable(res)), mod.att()); } }
prod = Production( sort, immutable(items), "" + System.identityHashCode(p))); else prod = Production( KLabel(p.getKLabel(kore)), sort,
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()); }
Production prod = Production(KLabel("is" + sort.toString()), Sorts.Bool(), Seq(Terminal("is" + sort.toString()), Terminal("("), NonTerminal(Sorts.K()), Terminal(")")), Att().add(Attribute.FUNCTION_KEY).add(Attribute.PREDICATE_KEY, Sort.class, sort));
Production freezer = Production(freezerLabel, Sorts.KItem(), immutable(items), Att()); K frozen = KApply(freezerLabel, vars.values().stream().collect(Collections.toList())); return Stream.of(freezer,
.map(p -> Production(Sorts.Cell(), Seq(NonTerminal(p.sort())))).collect(Collections.toSet());
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; }
prods.add(Production(p.klabel(), returnSort, immutable(pis), p.att().add(Constants.ORIGINAL_PRD, Production.class, p))); Production p1 = Production(p.klabel().get(), p.sort(), pi, p.att()); Production p2 = Production(Sorts.Cell(), Seq(NonTerminal(p.sort()))); return Stream.of(p1, p2); Production p1 = Production(Sorts.Cell(), Seq(NonTerminal(p.sort()))); return Stream.of(p, p1); p = Production(p.klabel().get(), p.sort(), p.items(), p.att()); else p = Production(p.sort(), p.items(), p.att()); return p; prods3.add(Production(Sorts.K(), Seq(NonTerminal(srt)), Att())); prod1 = Production(ul.terminatorKLabel, Sort(ul.sort.name() + "#Terminator", ul.sort.params()), Seq(Terminal("")), newAtts.remove("format").add(Constants.ORIGINAL_PRD, Production.class, ul.pTerminator)); prod2 = Production(ul.klabel, Sort("Ne#" + ul.sort.name(), ul.sort.params()), Seq(NonTerminal(ul.childSort), Terminal(ul.separator), NonTerminal(Sort("Ne#" + ul.sort.name(), ul.sort.params()))), newAtts.add(Constants.ORIGINAL_PRD, Production.class, ul.pList)); prod3 = Production(ul.klabel, Sort("Ne#" + ul.sort.name(), ul.sort.params()), Seq(NonTerminal(ul.childSort), Terminal(""), NonTerminal(Sort(ul.sort.name() + "#Terminator", ul.sort.params()))), newAtts.add(Constants.ORIGINAL_PRD, Production.class, ul.pList)); prod4 = Production(ul.sort, Seq(NonTerminal(Sort("Ne#" + ul.sort.name(), ul.sort.params()))));
ModuleTransformer.fromSentenceTransformer(s -> s, "identity function -- no transformation"); ModuleTransformer preprocessKLabelPredicates = ModuleTransformer.fromSentenceTransformer(new PreprocessKLabelPredicates(def.executionModule())::convert, "preprocess klabel predicates"); Sentence thread = Production(KLabel("#Thread"), Sorts.KItem(), Seq( Terminal("#Thread"), Terminal("("), NonTerminal(Sorts.K()), Terminal(","), NonTerminal(Sorts.K()), Terminal(","), NonTerminal(Sorts.K()), Terminal(")"))); Sentence bottom = Production(KLabel("#Bottom"), Sorts.KItem(), Seq(Terminal("#Bottom"))); Sentence threadLocal = Production(KLabel("#ThreadLocal"), Sorts.KItem(), Seq(Terminal("#ThreadLocal"))); Function1<Module, Module> pipeline = preprocessKLabelPredicates .andThen(splitThreadCell)
private static Set<Sentence> genProjection(Sort sort, Module m) { KLabel lbl = getProjectLbl(sort, m); KVariable var = KVariable("K", Att.empty().add(Sort.class, sort)); Rule r = Rule(KRewrite(KApply(lbl, var), var), BooleanUtils.TRUE, BooleanUtils.TRUE, Att().add("projection")); if (m.definedKLabels().contains(lbl)) { return Set(r); } return Set(Production(lbl, sort, Seq(Terminal(lbl.name()), Terminal("("), NonTerminal(Sorts.K()), Terminal(")")), Att().add("function").add("projection")), r); }
@Test public void testSingleTop() { Production prod = Production(KLabel(".Opt"), Sort("OptCellContent"), Seq(Terminal(""))); Production prod2 = Production(KLabel("#SemanticCastToKItem"), Sort("KItem"), Seq(NonTerminal(Sort("KItem")))); K configuration = cell("threads", Collections.emptyMap(), cell("thread", Collections.singletonMap("multiplicity", "*"), Att initializerAtts = Att().add("initializer"); Att productionAtts = initializerAtts.add("function").add("noThread"); Set<Sentence> reference = Set(Production(KLabel("<threads>"), Sort("ThreadsCell"), Seq(Terminal("<threads>"), NonTerminal(Sort("ThreadCellBag")), Terminal("</threads>")), Att().add("cell").add("cellName", "threads").add("topcell").add("format", "%1%i%n%2%d%n%3")), SyntaxSort(Sort("ThreadCellBag"), Att().add("hook", "BAG.Bag")), Production(KLabel("_ThreadCellBag_"), Sort("ThreadCellBag"), Seq(NonTerminal(Sort("ThreadCellBag")), NonTerminal(Sort("ThreadCellBag"))), Att().add("assoc","").add("comm","").add("unit",".ThreadCellBag") .add("element","ThreadCellBagItem").add("wrapElement","<thread>") .add("function").add("avoid").add("bag").add("cellCollection").add("hook","BAG.concat")), Production(KLabel(".ThreadCellBag"), Sort("ThreadCellBag"), Seq(Terminal(".ThreadCellBag")), Att().add("function").add("hook","BAG.unit")), Production(Sort("ThreadCellBag"), Seq(NonTerminal(Sort("ThreadCell")))), Production(KLabel("ThreadCellBagItem"), Sort("ThreadCellBag"), Seq(Terminal("ThreadCellBagItem"), Terminal("("), NonTerminal(Sort("ThreadCell")), Terminal(")")), Att().add("function").add("hook","BAG.element")), Production(KLabel("<thread>"), Sort("ThreadCell"), Seq(Terminal("<thread>"), NonTerminal(Sort("KCell")), NonTerminal(Sort("OptCell")), Terminal("</thread>")), Att().add("cell").add("cellName", "thread").add("multiplicity","*").add("format", "%1%i%n%2%n%3%d%n%4")), Production(KLabel("<k>"), Sort("KCell"),