private Module getMainModule(String mainModuleName, Set<Module> modules) { Optional<Module> opt = modules.stream().filter(m -> m.name().equals(mainModuleName)).findFirst(); if (!opt.isPresent()) { throw KEMException.compilerError("Could not find main module with name " + mainModuleName + " in definition. Use --main-module to specify one."); } return opt.get(); } }
private ParseCache loadCache(Module parser) { ParseCache cachedParser = caches.get(parser.name()); if (cachedParser == null || !equalsSyntax(cachedParser.getModule(), parser) || cachedParser.isStrict() != isStrict) { cachedParser = new ParseCache(parser, isStrict, java.util.Collections.synchronizedMap(new HashMap<>())); caches.put(parser.name(), cachedParser); } return cachedParser; }
private Parser(Module module) { // TODO: remove hack once the frontend is cleaner; also remove the IOException once the hack is cleared if (module.name().endsWith(RuleGrammarGenerator.RULE_CELLS)) { this.parseInModule = RuleGrammarGenerator.getCombinedGrammar(module, true); } else { this.parseInModule = new ParseInModule(module); } }
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()); } }
public Set<Module> loadModules( Set<Module> previousModules, String definitionText, Source source, File currentDirectory, List<File> lookupDirectories, Set<File> requiredFiles, boolean kore) { List<org.kframework.kil.Module> kilModules = slurp(definitionText, source, currentDirectory, lookupDirectories, requiredFiles); Definition def = new Definition(); def.setItems((List<DefinitionItem>) (Object) kilModules); Context context = new Context(); new CollectProductionsVisitor(kore, context).visit(def); KILtoKORE kilToKore = new KILtoKORE(context, false, kore); HashMap<String, Module> koreModules = new HashMap<>(); koreModules.putAll(previousModules.stream().collect(Collectors.toMap(Module::name, m -> m))); HashSet<org.kframework.kil.Module> kilModulesSet = new HashSet<>(kilModules); return kilModules.stream().map(m -> kilToKore.apply(m, kilModulesSet, koreModules)).flatMap(m -> Stream.concat(Stream.of(m), Stream.of(koreModules.get(m.name() + "$SYNTAX")))).collect(Collectors.toSet()); }
public String convertSpecificationModule(Module definition, Module spec) { ConfigurationInfoFromModule configInfo = new ConfigurationInfoFromModule(definition); Sort topCell = configInfo.getRootCell(); sb.append("[]\n"); sb.append("module "); convert(spec.name()); sb.append("\n\n// imports\n"); sb.append("import "); convert(definition.name()); sb.append(" []\n"); sb.append("\n\n// claims\n"); for (Sentence sentence : iterable(spec.sentencesExcept(definition))) { assert sentence instanceof Rule || sentence instanceof ModuleComment : "Unexpected non-rule claim " + sentence.toString(); if (sentence instanceof Rule) { convertRule((Rule) sentence, false, topCell, new HashMap<>(), HashMultimap.create(), true); } } sb.append("endmodule "); convert(new HashMap<>(), spec.att()); sb.append("\n"); return sb.toString(); }
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 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()); }
public static Module resolve(Module m) { Set<Sentence> newSentences = JavaConverters.asScalaSet(stream(m.localSentences()).map(s -> s instanceof Production ? resolve((Production) s) : s).collect(Collectors.toSet())); return Module(m.name(), m.imports(), newSentences, m.att()); }
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()); }
/** * Creates the seed module that can be used to parse rules. * Imports module markers RULE-CELLS and K found in /include/kast.k. * @param mod The user defined module from which to start. * @return a new module which imports the original user module and a set of marker modules. */ public Module getRuleGrammar(Module mod) { // import RULE-CELLS in order to parse cells specific to rules Module newM = new Module( mod.name() + "-" + RULE_CELLS , Set(mod, baseK.getModule(K).get(), baseK.getModule(RULE_CELLS).get(), baseK.getModule(DEFAULT_LAYOUT).get()) , Set() , Att() ); return newM; }
/** * Creates the seed module that can be used to parse configurations. * Imports module markers CONFIG-CELLS and K found in /include/kast.k. * @param mod The user defined module from which to start. * @return a new module which imports the original user module and a set of marker modules. */ public Module getConfigGrammar(Module mod) { // import CONFIG-CELLS in order to parse cells specific to configurations Module newM = new Module( mod.name() + "-" + CONFIG_CELLS , Set(mod, baseK.getModule(K).get(), baseK.getModule(CONFIG_CELLS).get(), baseK.getModule(DEFAULT_LAYOUT).get()) , Set() , Att() ); return newM; }
public Module resolve(Module input) { Set<Sentence> contextsToAdd = resolve(stream(input.localSentences()) .filter(s -> s instanceof Production) .map(s -> (Production) s) .filter(p -> p.att().contains("strict") || p.att().contains("seqstrict")).collect(Collectors.toSet())); return Module(input.name(), input.imports(), (scala.collection.Set<Sentence>) input.localSentences().$bar(immutable(contextsToAdd)), input.att()); } }
public static Tuple2<Definition, Module> getProofDefinition(File proofFile, String defModuleName, String specModuleName, CompiledDefinition compiledDefinition, Backend backend, FileUtil files, KExceptionManager kem, Stopwatch sw) { Kompile kompile = new Kompile(compiledDefinition.kompileOptions, files, kem, sw, true); if (defModuleName == null) { defModuleName = compiledDefinition.kompiledDefinition.mainModule().name(); } if (specModuleName == null) { specModuleName = FilenameUtils.getBaseName(proofFile.getName()).toUpperCase(); } java.util.Set<Module> modules = kompile.parseModules(compiledDefinition, defModuleName, files.resolveWorkingDirectory(proofFile).getAbsoluteFile()); Map<String, Module> modulesMap = new HashMap<>(); modules.forEach(m -> modulesMap.put(m.name(), m)); Module defModule = getModule(defModuleName, modulesMap, compiledDefinition.getParsedDefinition()); Module specModule = getModule(specModuleName, modulesMap, compiledDefinition.getParsedDefinition()); specModule = backend.specificationSteps(compiledDefinition.kompiledDefinition).apply(specModule); specModule = spliceModule(specModule, compiledDefinition.kompiledDefinition); Definition combinedDef = Definition.apply(defModule, compiledDefinition.getParsedDefinition().entryModules(), compiledDefinition.getParsedDefinition().att()); Definition compiled = compileDefinition(backend, combinedDef); return Tuple2.apply(compiled, specModule); }
/** * @return the module used for generating the program (i.e. ground) parser for the module named moduleName * It automatically generates this module unless the user has already defined a module postfixed with * {@link RuleGrammarGenerator#POSTFIX}. In latter case, it uses the user-defined module. */ public Option<Module> programParsingModuleFor(String moduleName, KExceptionManager kem) { RuleGrammarGenerator gen = new RuleGrammarGenerator(parsedDefinition); Option<Module> userProgramParsingModule = parsedDefinition.getModule(moduleName + RuleGrammarGenerator.POSTFIX); if (userProgramParsingModule.isDefined()) { kem.registerInternalHiddenWarning("Module " + userProgramParsingModule.get().name() + " is user-defined."); return userProgramParsingModule; } else { Option<Module> moduleOption = parsedDefinition.getModule(moduleName); Option<Module> programParsingModuleOption = moduleOption.isDefined() ? Option.apply(gen.getProgramsGrammar(moduleOption.get())) : Option.empty(); if (programParsingModuleOption.isDefined()) { kem.registerInternalHiddenWarning("Module " + programParsingModuleOption.get().name() + " has been automatically generated."); } return programParsingModuleOption; } }
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 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()); } }
public int run(CompiledDefinition compiledDef, KRunOptions options, Function<Module, Rewriter> rewriterGenerator, ExecutionMode executionMode) { String pgmFileName = options.configurationCreation.pgm(); K program; if (options.configurationCreation.term()) { program = externalParse(options.configurationCreation.parser(compiledDef.executionModule().name(), files), pgmFileName, compiledDef.programStartSymbol, Source.apply("<parameters>"), compiledDef); } else { program = parseConfigVars(options, compiledDef); } // store initial configuration in single mutable reference so that we can make sure it can be garbage collected // down the stack. InitialConfiguration config = new InitialConfiguration(program); program = null; Tuple2<K, Integer> result = executionMode.execute(config, rewriterGenerator, compiledDef); if (result != null) { kprint.prettyPrint(compiledDef.getParsedDefinition(), compiledDef.languageParsingModule(), s -> kprint.outputFile(s), result._1()); return result._2(); } return 0; }