private KLabel getUniqueLambdaLabel(String nameHint1, String nameHint2) { if (klabels.isEmpty()) { klabels.addAll(mutable(module.definedKLabels())); } int counter = 0; KLabel freezer; do { freezer = KLabel("#lambda" + nameHint1 + "_" + nameHint2 + "_" + (counter++ == 0 ? "" : counter)); } while (klabels.contains(freezer)); klabels.add(freezer); return freezer; }
private KLabel getUniqueFreezerLabel(Module input, String nameHint) { if (klabels.isEmpty()) { klabels.addAll(mutable(input.definedKLabels())); } int counter = 0; KLabel freezer; do { freezer = KLabel("#freezer" + nameHint + "_" + (counter++ == 0 ? "" : counter)); } while (klabels.contains(freezer)); klabels.add(freezer); return freezer; }
private void addOpaqueKLabels(Set<KLabel> klabels) { if (options.klabels == null) return; File definitionFile = files.resolveWorkingDirectory(options.klabels).getAbsoluteFile(); List<File> lookupDirectories = kompileOptions.outerParsing.includes.stream().map(files::resolveWorkingDirectory).collect(Collectors.toList()); lookupDirectories.add(Kompile.BUILTIN_DIRECTORY); java.util.Set<Module> mods = new ParserUtils(files::resolveWorkingDirectory, kem, globalOptions).loadModules( new HashSet<>(), "require " + StringUtil.enquoteCString(definitionFile.getPath()), Source.apply(definitionFile.getAbsolutePath()), definitionFile.getParentFile(), lookupDirectories, new HashSet<>(), false); mods.stream().forEach(m -> klabels.addAll(mutable(m.definedKLabels()))); }
public AddEmptyLists(Module m) { this.m = m; subsorts = m.subsorts(); listSorts = m.listSorts(); lists = UserList.getLists(mutable(m.sentences())).stream().collect(Collectors.groupingBy(p -> p.sort)); }
public static scala.collection.Set<UserList> apply(scala.collection.Set<Sentence> sentences) { return Collections.immutable(getLists(Collections.mutable(sentences))).toSet(); }
public static scala.collection.Set<UserList> apply(scala.collection.Set<Sentence> sentences) { return Collections.immutable(getLists(Collections.mutable(sentences))).toSet(); }
private Sort nthArgSort(KLabel klabel, int n) { java.util.Set<Tuple2<Seq<Sort>,Sort>> sigs = mutable(JavaConversions.mapAsJavaMap(module.signatureFor()).get(klabel)); if (sigs == null) { throw new IllegalArgumentException("Not found signature for label: " + klabel); } Sort sort = null; for (Tuple2<Seq<Sort>,Sort> sig : sigs) { List<Sort> sorts = JavaConversions.seqAsJavaList(sig._1()); if (n >= sorts.size()) continue; sort = sorts.get(n); if (!sort.equals(Sorts.K())) { return sort; } } return sort; } }
private java.util.Set<Production> getStreamProductions(Set<Sentence> sentences) { java.util.Set<Production> productions = new HashSet<>(); for (Sentence s : mutable(sentences)) { if (s instanceof Production) { Production p = (Production) s; if (p.att().getOption("stream").isDefined()) { checkStreamName(p.att().get("stream")); productions.add(p); } } } return productions; }
private void checkConfigVars(Set<KToken> inputConfigVars, CompiledDefinition compiledDef) { Set<KToken> defConfigVars = mutable(new ConfigurationInfoFromModule(compiledDef.kompiledDefinition.mainModule()).configVars()); for (KToken defConfigVar : defConfigVars) { if (!inputConfigVars.contains(defConfigVar)) { throw KEMException.compilerError("Configuration variable missing: " + defConfigVar.s()); } } for (KToken inputConfigVar : inputConfigVars) { if (!defConfigVars.contains(inputConfigVar)) { if (!inputConfigVar.s().equals("$STDIN") && !inputConfigVar.s().equals("$IO")) { kem.registerCompilerWarning("User specified configuration variable " + inputConfigVar.s() + " which does not exist."); } } } }
private void check(Production p) { if (p.att().contains(Attribute.CELL_KEY)) { for (ProductionItem i : mutable(p.items())) { if (i instanceof NonTerminal) { Sort sort = ((NonTerminal) i).sort(); if (sort.name().endsWith("Cell")) { if (cells.contains(sort)) { Production cell = new ConfigurationInfoFromModule(module).cellProductionsFor().get(sort).get().head(); errors.add(KEMException.compilerError("Cell " + cell.klabel().get() + " found twice in configuration.", p)); } cells.add(sort); } } } } } }
private Set<Sort> sort(K k, Rule r) { if (k instanceof KVariable) { return Collections.singleton(k.att().get(Sort.class)); } else if (k instanceof KToken) { return Collections.singleton(((KToken)k).sort()); } else if (k instanceof KApply) { KApply kapp = (KApply)k; if (kapp.klabel() instanceof KVariable) { throw KEMException.compilerError("Cannot compute macros with klabel variables.", r); } Set<Production> prods = new HashSet<>(mutable(mod.productionsFor().apply(kapp.klabel()))); prods.removeIf(p -> p.arity() != kapp.items().size()); for (int i = 0; i < kapp.items().size(); i++) { final int idx = i; Set<Sort> sorts = sort(kapp.items().get(idx), r); prods.removeIf(p -> sorts.stream().noneMatch(s -> mod.subsorts().lessThanEq(s, p.nonterminal(idx).sort()))); } Set<Sort> candidates = prods.stream().map(Production::sort).collect(Collectors.toSet()); return candidates; } else { throw KEMException.compilerError("Cannot compute macros with sort check on terms that are not KApply, KToken, or KVariable.", r); } }
List<KApply> topologicalSorted = mutable(TopologicalSort.tsort(immutable(edges)).toList()); return state.stream().sorted((k1, k2) -> (topologicalSorted.indexOf(k1) - topologicalSorted.indexOf(k2)));
private static void checkCircularModuleImports(Module mainModule, scala.collection.Seq<Module> visitedModules) { if (visitedModules.contains(mainModule)) { String msg = "Found circularity in module imports: "; for (Module m : mutable(visitedModules)) { // JavaConversions.seqAsJavaList(visitedModules) msg += m.getName() + " < "; } msg += visitedModules.head().getName(); throw KEMException.compilerError(msg); } }
@Override public Either<java.util.Set<ParseFailedException>, Term> apply(Ambiguity amb) { // if the ambiguity has rewrites at the top, prefer them, and eliminate the rest scala.collection.Set<Term> rewrites = amb.items().stream().filter(o -> o instanceof TermCons && ((TermCons) o).production().klabel().isDefined() && ((TermCons) o).production().klabel().get().name().equals("#KRewrite")).collect(Collections.toSet()); if (rewrites.size() == 0 || rewrites.size() == amb.items().size()) return super.apply(amb); if (rewrites.size() == 1) return Right.apply(rewrites.head()); return super.apply(Ambiguity.apply(mutable(rewrites))); }
@Override public Either<java.util.Set<ParseFailedException>, Term> apply(Ambiguity amb) { // if the ambiguity has KSeq at the top, prefer them, and eliminate the rest scala.collection.Set<Term> rewrites = amb.items().stream().filter(o -> o instanceof TermCons && ((TermCons) o).production().klabel().isDefined() && ((TermCons) o).production().klabel().get().name().equals("#KSequence")).collect(Collections.toSet()); if (rewrites.size() == 0 || rewrites.size() == amb.items().size()) return super.apply(amb); if (rewrites.size() == 1) return Right.apply(rewrites.head()); return super.apply(Ambiguity.apply(mutable(rewrites))); }
private void check(Production p) { if (p.att().contains("cell") && p.att().contains("stream")) { ProductionItem i = mutable(p.items()).get(1); if (i instanceof NonTerminal) { Sort sort = ((NonTerminal) i).sort(); if (!module.subsorts().lessThanEq(sort, Sorts.List())) { errors.add(KEMException.compilerError("Wrong sort in streaming cell. Expected List, but found " + sort.toString() + ".", p)); } } else { throw KEMException.internalError("Illegal arguments for stream cell."); } } } }
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 java.util.Set<Module> parseModules(CompiledDefinition definition, String mainModule, File definitionFile) { Definition def = parser.loadDefinition( mainModule, mutable(definition.getParsedDefinition().modules()), "require " + StringUtil.enquoteCString(definitionFile.getPath()), Source.apply(definitionFile.getAbsolutePath()), return mutable(def.entryModules());
polyKLabels.put(prod.klabel().get().name(), poly); List<Sort> params = new ArrayList<>(); List<NonTerminal> children = new ArrayList<>(mutable(prod.nonterminals())); Sort returnSort = prod.sort(); for (int i = 0; i < poly.size(); i++) {
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; }