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()); } }
private Rule upRule(K contents) { KApply ruleContents = (KApply) contents; List<org.kframework.kore.K> items = ruleContents.klist().items(); switch (ruleContents.klabel().name()) { case "#ruleNoConditions": return Rule(items.get(0), BooleanUtils.TRUE, BooleanUtils.TRUE, ruleContents.att()); case "#ruleRequires": return Rule(items.get(0), items.get(1), BooleanUtils.TRUE, ruleContents.att()); case "#ruleEnsures": return Rule(items.get(0), BooleanUtils.TRUE, items.get(1), ruleContents.att()); case "#ruleRequiresEnsures": return Rule(items.get(0), items.get(1), items.get(2), ruleContents.att()); default: throw new AssertionError("Wrong KLabel for rule content"); } }
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; } }
public Module resolve(Module input) { Set<Sentence> rulesToAdd = stream(input.sentences()) .filter(s -> s instanceof Context) .map(s -> (Context) s) .flatMap(c -> this.resolve(c, input)).collect(Collectors.toCollection(HashSet::new)); if (!rulesToAdd.isEmpty()) { rulesToAdd.add(SyntaxSort(Sorts.K())); } return Module(input.name(), input.imports(), (scala.collection.Set<Sentence>) stream(input.localSentences()).filter(s -> !(s instanceof Context)).collect(Collections.toSet()).$bar(immutable(rulesToAdd)), input.att()); }
res.add(SyntaxSort(sort, convertAttributes(s))); return res; .getProductions().stream().filter(p -> p.getKLabel(kore) != null).map(p -> Tag(p.getKLabel(kore))) .collect(Collectors.toSet())); res.add(SyntaxPriority(immutable(s.getPriorityBlocks().stream().map(applyToTags) .collect(Collectors.toList())))); if (!b.getAssoc().equals("")) { Value assoc = applyAssoc(b.getAssoc()); res.add(SyntaxAssociativity(assoc, applyToTags.apply(b))); if (it instanceof NonTerminal) { NonTerminal nt = (NonTerminal)it; items.add(NonTerminal(nt.getSort(), nt.getName())); } else if (it instanceof UserList) { throw new AssertionError("Lists should have applied before."); items.add(Terminal(((Terminal) it).getTerminal())); } else { throw new AssertionError("Unhandled case"); prod = Production( sort, immutable(items), "" + System.identityHashCode(p))); else
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()); } }
.map(p -> Production(Sorts.Cell(), Seq(NonTerminal(p.sort())))).collect(Collections.toSet()); Module module = Module(inputModule.name(), (Set<Module>) inputModule.imports(), (Set<Sentence>) inputModule.localSentences().$bar(importedConfigurationSortsSubsortedToCell), inputModule.att()); switch (configContents.klabel().name()) { case "#ruleNoConditions": return Configuration(items.get(0), BooleanUtils.TRUE, configContents.att()); case "#ruleEnsures": return Configuration(items.get(0), items.get(1), configContents.att()); default: throw KEMException.compilerError("Illegal configuration with requires clause detected.", configContents); throw KEMException.compilerError("Module Map must be visible at the configuration declaration, in module " + module.name()); return Module(module.name(), (Set<Module>) module.imports().$bar(Set(mapModule)), (Set<Sentence>) module.localSentences().$bar(configDeclSyntax), module.att()); throw KEMException.compilerError("Module Map must be visible at the configuration declaration, in module " + module.name()); return Module(module.name(), (Set<Module>) module.imports().$bar(Set(mapModule)), (Set<Sentence>) module.localSentences().$bar(configDeclRules), module.att());
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 Module resolve(Module m) { module = m; funProds.clear(); funRules.clear(); Set<Sentence> newSentences = stream(m.localSentences()).map(this::resolve).collect(Collectors.toSet()); newSentences.addAll(funProds); newSentences.addAll(funRules); return Module(m.name(), m.imports(), immutable(newSentences), m.att()); } }
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; }
requires = sideCondition.get(); Context ctx = Context(KApply(production.klabel().get(), KList(items)), requires, production.att()); sentences.add(ctx); requires = sideCondition.get(); Rule hybrid = Rule(KRewrite(KApply(KLabel("isKResult"), term), BooleanUtils.TRUE), requires, BooleanUtils.TRUE); sentences.add(hybrid);
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()); }
public org.kframework.definition.Bubble apply(StringSentence sentence) { return Bubble(sentence.getType(), sentence.getContent(), convertAttributes(sentence) .add("contentStartLine", Integer.class, sentence.getContentStartLine()) .add("contentStartColumn", Integer.class, sentence.getContentStartColumn())); }
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")); }
returnSort = sort; } else { children.set(j - 1, NonTerminal(sort)); return Production(KLabel(prod.klabel().get().name(), immutable(params)), returnSort, immutable(items), prod.att().add("originalPrd", Production.class, prod));
private static Module excludeModulesByTag(Set<String> excludedModuleTags, Module mod) { Set<Module> newImports = stream(mod.imports()).filter(_import -> excludedModuleTags.stream().noneMatch(tag -> _import.att().contains(tag))).collect(Collectors.toSet()); return Module(mod.name(), immutable(newImports), mod.localSentences(), mod.att()); }
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; }
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); }
private Rule getExitCodeRule(Definition parsedDefinition) { Module mainMod = parsedDefinition.mainModule(); Set<Production> exitProds = stream(mainMod.productions()).filter(p -> p.att().contains("exit")).collect(Collectors.toSet()); if (exitProds.size() == 0) { return null; } else if (exitProds.size() > 1) { throw KEMException.compilerError("Found more than one or zero productions with 'exit' attribute. Exactly one production, a cell, must have this attribute, designating the exit code of krun. Found:\n" + exitProds); } Production exitProd = exitProds.iterator().next(); return Rule(IncompleteCellUtils.make(exitProd.klabel().get(), false, KApply(KLabel("#SemanticCastToInt"), KVariable("_")), false), BooleanUtils.TRUE, BooleanUtils.TRUE); }
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); }