public boolean collectionIsBag(KLabel collectionLabel) { return m.attributesFor().apply(collectionLabel).contains(Attribute.COMMUTATIVE_KEY) && !m.attributesFor().apply(collectionLabel).contains(Attribute.IDEMPOTENT_KEY) && m.attributesFor().apply(collectionLabel).contains(Att.bag()); }
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()); } }
JavaConversions.mapAsJavaMap(moduleWithPolyProds.signatureFor()).entrySet().stream().forEach(e -> { JavaConversions.setAsJavaSet(e.getValue()).stream().forEach(p -> { ImmutableList.Builder<Sort> sortsBuilder = ImmutableList.builder(); JavaConversions.mapAsJavaMap(module.attributesFor()).entrySet().stream().forEach(e -> { attributesBuilder.put(e.getKey().name(), e.getValue()); }); signaturesBuilder.build(), attributesBuilder.build(), JavaConverters.mapAsJavaMapConverter(module.freshFunctionFor()).asJava().entrySet().stream().collect(Collectors.toMap( e -> Sort.of(e.getKey()), e -> e.getValue())),
public void initialize() { disambModule.definedSorts(); disambModule.subsorts(); disambModule.priorities(); disambModule.leftAssoc(); disambModule.rightAssoc(); disambModule.productionsFor(); disambModule.overloads(); }
private boolean equalsSyntax(Module _this, Module that) { if (!_this.productions().equals(that.productions())) return false; if (!_this.priorities().equals(that.priorities())) return false; if (!_this.leftAssoc().equals(that.leftAssoc())) return false; if (!_this.rightAssoc().equals(that.rightAssoc())) return false; return _this.sortDeclarations().equals(that.sortDeclarations()); }
ListMultimap<KLabel, Rule> anywhereRules = ArrayListMultimap.create(); anywhereKLabels = new HashSet<>(); stream(mainModule.rules()).filter(r -> !r.att().contains(Attribute.MACRO_KEY) && !r.att().contains(Attribute.ALIAS_KEY)).forEach(r -> { K left = RewriteToTop.toLeft(r.body()); if (left instanceof KSequence) { if (kseq.items().size() == 1 && kseq.items().get(0) instanceof KApply) { KApply kapp = (KApply) kseq.items().get(0); if (mainModule.attributesFor().apply(kapp.klabel()).contains(Attribute.FUNCTION_KEY)) { functionRules.put(kapp.klabel(), r); for (Production p : iterable(mainModule.productions())) { if (p.att().contains(Attribute.FUNCTION_KEY)) { functions.add(p.klabel().get()); klabelsForEachPredicate = new HashMap<>(); for (KLabel functionLabel : rules.keySet()) { if (mainModule.attributesFor().get(functionLabel).getOrElse(() -> Att()).contains("klabelPredicate")) { klabelsForEachPredicate.put(functionLabel.name(), computeKLabelsForPredicate(functionRules.get(functionLabel))); 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()); String hook = mainModule.attributesFor().get(functionLabel).getOrElse(() -> Att()).<String>getOptional(Attribute.HOOK_KEY).orElse("."); if (hook.equals("KREFLECTION.fresh")) { sb.append(conn).append("freshFunction (sort: string) (config: k) (counter: Z.t) : k = match sort with \n"); for (Sort sort : iterable(mainModule.freshFunctionFor().keys())) { sb.append("| \"").append(sort.toString()).append("\" -> (");
Map<String, Boolean> attributes = new HashMap<>(); sb.append("\n"); for (Sort sort : iterable(module.definedSorts())) { Att att = module.sortAttributesFor().get(sort).getOrElse(() -> KORE.Att()); collectAttributes(attributes, att); for (Production prod : iterable(module.productions())) { Att att = prod.att(); collectAttributes(attributes, att); for (Rule r : iterable(module.rules())) { Att att = r.att(); collectAttributes(attributes, att); convert(module.name()); sb.append("\n\n// imports\n"); sb.append(" import K []\n\n// sorts\n"); collectionSorts.add("LIST.List"); collectionSorts.add("ARRAY.Array"); for (Sort sort : iterable(module.definedSorts())) { if (sort.equals(Sorts.K()) || sort.equals(Sorts.KItem())) { continue; Att att = module.sortAttributesFor().get(sort).getOrElse(() -> KORE.Att()); if (att.contains(Attribute.HOOK_KEY)) { if (collectionSorts.contains(att.get(Attribute.HOOK_KEY))) { att = att.remove(Attribute.HOOK_KEY);
public Module gen(Module mod) { this.mod = mod; predicateRules = stream(mod.rules()).filter(this::isPredicate).collect(Collectors.toSet()); return Module(mod.name(), mod.imports(), (Set<Sentence>) mod.localSentences().$bar(stream(mod.definedSorts()) .flatMap(this::gen).collect(Collections.toSet())), mod.att()); }
public Module resolve(Module m) { this.m = m; Set<Sentence> sentences = map(this::resolve, m.localSentences()); KToken counterCellLabel = KToken("generatedCounter", Sort("#CellName")); KApply freshCell = KApply(KLabel("#configCell"), counterCellLabel, KApply(KLabel("#cellPropertyListTerminator")), KToken("0", Sorts.Int()), counterCellLabel); if (m.equals(def.mainModule()) && kore) { if (!m.definedKLabels().contains(KLabels.GENERATED_TOP_CELL)) { RuleGrammarGenerator gen = new RuleGrammarGenerator(def); ParseInModule mod = RuleGrammarGenerator.getCombinedGrammar(gen.getConfigGrammar(m), true); 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")); if (kore && m.localKLabels().contains(KLabels.GENERATED_TOP_CELL)) { RuleGrammarGenerator gen = new RuleGrammarGenerator(def); ParseInModule mod = RuleGrammarGenerator.getCombinedGrammar(gen.getConfigGrammar(m), true); sentences = (Set<Sentence>) sentences.$bar(newSentences); if (sentences.equals(m.localSentences())) { return m; return new GenerateSortPredicateSyntax().gen(Module(m.name(), kore ? m.imports() : add(def.getModule("K-REFLECTION").get(), m.imports()), sentences, m.att()));
if (mod.importedModuleNames().contains(AUTO_CASTS)) { // create the diamond Set<Sentence> temp; for (Sort srt : iterable(mod.definedSorts())) { if (!isParserSort(srt)) { if (mod.importedModuleNames().contains(RECORD_PRODS)) { for (Production p : iterable(mod.productions())) { if (p.isPrefixProduction()) { prods.addAll(mutable(p.recordProductions())); for (Production p : iterable(mod.productions())) { if (p.att().contains("poly")) { List<Set<Integer>> positions = computePositions(p); if (mod.importedModuleNames().contains(RULE_CELLS)) { // prepare cell productions for rule parsing addRuleCells = mod.sentences().exists(p -> p instanceof Production && ((Production) p).att().contains("cell")); } else { addRuleCells = false; boolean addConfigCells = mod.importedModuleNames().contains(CONFIG_CELLS); Set<Sentence> parseProds; if (addRuleCells) { ConfigurationInfo cfgInfo = new ConfigurationInfoFromModule(mod); parseProds = Stream.concat(prods.stream(), stream(mod.sentences())).flatMap(s -> { if (s instanceof Production && s.att().contains("cellCollection")) { return Stream.empty(); parseProds = Stream.concat(prods.stream(), stream(mod.sentences()).filter(s -> !s.att().contains("cell"))).collect(Collectors.toSet());
if(mod.name().endsWith(POSTFIX)) { return mod; } else { Module newMod = ModuleTransformer.from(oldMod -> { Set<Module> imports = stream(oldMod.imports()).map(_import -> { if (_import.name().equals(ID)) { return baseK.getModule(ID_PROGRAM_PARSING).get(); } else if (_import.name().equals(ID_SYNTAX)) { return baseK.getModule(ID_PROGRAM_PARSING_SYNTAX).get(); } else { return Module.apply(oldMod.name(), immutable(imports), oldMod.localSentences(), oldMod.att()); }, "apply program parsing modules").apply(mod); if (! mod.definedSorts().contains(Sorts.Layout())) { modules.add(baseK.getModule(DEFAULT_LAYOUT).get()); return Module.apply(mod.name() + POSTFIX, immutable(modules), Set(), Att());
stream(mod.definedSorts()).filter(s -> mod.subsorts().lessThanEq(s, sort)).forEach(subsort -> { MutableBoolean isProtecting = new MutableBoolean(true); stream(mod.definedSorts()).filter(s -> mod.subsorts().lessThanEq(s, subsort)).forEach(candidateSort -> { if (predicateRules.stream().filter(r -> isPredicateFor(r, candidateSort)).findAny().isPresent()) { stream(mod.productions()).filter(p -> p.sort().equals(candidateSort) && p.klabel().isDefined()).forEach(candidateProduction -> { 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())) { stream(mod.productions()).filter(p -> mod.subsorts().lessThanEq(p.sort(), sort)).filter(p -> nonProtectingSubsorts.contains(p.sort())).distinct().forEach(p -> { if (p.klabel().isDefined() && !p.att().contains(Attribute.FUNCTION_KEY)) { List<K> klist = new ArrayList<>(); KVariable v = KVariable("K" + i++, Att().add(Sort.class, nt.sort())); klist.add(v); if (!mutable(mod.sortAttributesFor()).getOrDefault(sort, Att()).contains("flatPredicate")) { side.add(KApply(KLabel("is" + nt.sort().toString()), v)); stream(mod.definedSorts()).filter(s -> mod.subsorts().lessThanEq(s, sort)).distinct().forEach(s -> { res.add(Rule(KRewrite(KApply(KLabel("is" + sort.toString()), KApply(KLabel("#KToken"), KToken(s.toString(), Sorts.KString()), KVariable("_"))), BooleanUtils.TRUE), BooleanUtils.TRUE,
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()); } }
public String constants() { StringBuilder sb = new StringBuilder(); sb.append("type sort = \n"); Set<Sort> sorts = mutable(mainModule.definedSorts()); sorts.add(Sorts.Bool()); sorts.add(Sorts.Int()); Set<KLabel> klabels = mutable(mainModule.definedKLabels()); klabels.add(KLabel("#Bottom")); klabels.add(KLabel("littleEndianBytes")); encodeStringToIdentifier(sb, label); sb.append(" -> "); encodeStringToIdentifier(sb, KLabel(mainModule.attributesFor().apply(label).get(Attribute.UNIT_KEY))); sb.append("\n"); encodeStringToIdentifier(sb, label); sb.append(" -> "); encodeStringToIdentifier(sb, KLabel(mainModule.attributesFor().apply(label).get("element"))); sb.append("\n"); Set<Sort> arraySorts = sorts.stream().filter(s -> mainModule.sortAttributesFor().get(s).getOrElse(() -> Att()).<String>getOptional("hook") .orElse(".").equals("ARRAY.Array")).collect(Collectors.toSet()); for (Sort sort : arraySorts) { encodeStringToIdentifier(sb, sort); sb.append(" -> "); encodeStringToIdentifier(sb, KLabel(mainModule.sortAttributesFor().apply(sort).get("unit"))); sb.append("\n");
Function1<Module, Module> pipeline = preprocessKLabelPredicates .andThen(splitThreadCell) .andThen(mod -> Module(mod.name(), mod.imports(), Stream.concat(stream(mod.localSentences()), Stream.<Sentence>of(thread, bottom, threadLocal)).collect(org.kframework.Collections.toSet()), mod.att())) .andThen(convertLookups) .andThen(expandMacros) filteredMapConstructors = ConvertDataStructureToLookup.filteredMapConstructors(mainModule); Set<KLabel> threadKLabels = stream(mainModule.attributesFor()).filter(p -> p._2().contains("thread")).map(Tuple2::_1).collect(Collectors.toSet()); if (threadKLabels.size() == 0) { this.matchThreadSet = null; if (mainModule.definedKLabels().contains(stratCell)) { Rule makeStuck = Rule(IncompleteCellUtils.make(stratCell, false, KRewrite(KSequence(), KApply(KLabel("#STUCK"))), true), BooleanUtils.TRUE, BooleanUtils.TRUE); Rule makeUnstuck = Rule(IncompleteCellUtils.make(stratCell, false, KRewrite(KApply(KLabel("#STUCK")), KSequence()), true), BooleanUtils.TRUE, BooleanUtils.TRUE);
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(); } }
stream(module.rules()).filter(r -> !r.att().contains(Attribute.MACRO_KEY) && !r.att().contains(Attribute.ALIAS_KEY)).forEach(r -> { K left = RewriteToTop.toLeft(r.body()); if (left instanceof KApply) { String hook = module.attributesFor().apply(label).<String>getOptional(Attribute.HOOK_KEY).orElse("."); for (KLabel freshFunction : iterable(module.freshFunctionFor().values())) { dependencies.addEdge(new Object(), label, freshFunction);
public static Set<KLabel> filteredMapConstructors(Module m) { return stream(m.productions()) .filter(p -> p.att().contains(Attribute.ASSOCIATIVE_KEY) && p.att().contains("filterElement")) .map(p -> p.klabel().get()) .distinct() .collect(Collectors.toSet()); }
public Subsorts(Module module) { sorts = JavaConversions.asJavaCollection(module.definedSorts()).stream() .map(Sort::of) .collect(Collectors.toSet()); this.subsort = ArrayTable.create(sorts, sorts); for (org.kframework.kore.Sort sort1 : Collections.iterable(module.definedSorts())) { for (org.kframework.kore.Sort sort2 : Collections.iterable(module.definedSorts())) { subsort.put( Sort.of(sort1), Sort.of(sort2), module.subsorts().$greater(sort1, sort2)); } } }