private static List<List<Sort>> makeAllSortTuples(int size, Module mod) { List<List<Sort>> res = new ArrayList<>(); List<Sort> allSorts = stream(mod.definedSorts()).filter(s -> !isParserSort(s)).collect(Collectors.toList()); makeAllSortTuples(size, size, allSorts, res, new int[size]); return res; }
public static Grammar getGrammar(Module module, Scanner scanner) { Automaton.setMinimization(Automaton.MINIMIZE_BRZOZOWSKI); Grammar grammar = new Grammar(); Set<String> rejects = new HashSet<>(); Set<Sort> sorts = Stream.concat(stream(module.definedSorts()), stream(module.usedCellSorts())).collect(Collectors.toSet()); // create a NonTerminal for every declared sort for (Sort sort : sorts) { grammar.add(grammar.new NonTerminal(sort.toString())); } stream(module.productions()).forEach(p -> collectRejects(p, rejects)); stream(module.productions()).collect(Collectors.groupingBy(p -> p.sort())).entrySet().stream().sorted(Comparator.comparing(e2 -> e2.getKey().toString())).forEach(e -> processProductions(e.getKey(), e.getValue(), grammar, rejects, scanner)); grammar.compile(scanner); return grammar; }
private int getArity(KLabel functionLabel) { Set<Integer> arities = stream(mainModule.productionsFor().apply(functionLabel)).map(Production::arity).collect(Collectors.toSet()); if (arities.size() > 1) { throw KEMException.compilerError("KLabel " + functionLabel + " has multiple productions with differing arities: " + mainModule.productionsFor().apply(functionLabel)); } assert arities.size() == 1; return arities.iterator().next(); }
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 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()); }
private void forEachKLabel(Consumer<Tuple2<KLabel, Long>> action) { for (KLabel label : iterable(mainModule.definedKLabels())) { if (ConvertDataStructureToLookup.isLookupKLabel(label) || label.name().equals("#KToken")) continue; stream(mainModule.productionsFor().apply(label)).map(p -> Tuple2.apply(p.klabel().get(), stream(p.items()).filter(pi -> pi instanceof NonTerminal).count())).distinct().forEach(action); } }
private Sort getPredicateSort(Sort s) { if (cfg.getMultiplicity(s) == Multiplicity.STAR) { scala.collection.Set<Sort> sorts = cfg.cfg.getCellBagSortsOfCell(s); if (sorts.size() != 1) { throw KEMException.compilerError("Expected exactly one cell collection sort for the sort " + s + "; found " + sorts); } return stream(sorts).findFirst().get(); } return s; }
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 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()); }
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); }
private Term getCanonicalKSeq(Term term) { return stream(Assoc.flatten(kSeqLabel, Seq(term), kDotLabel).reverse()) .map(Term.class::cast) .reduce((a, b) -> KItem.of(kSeqLabel, KList.concatenate(b, a), termContext.global())) .orElse(kDot); }
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 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 static Function1<Definition, Definition> excludeModulesByTag(Set<String> excludedModuleTags) { DefinitionTransformer dt = DefinitionTransformer.from(mod -> excludeModulesByTag(excludedModuleTags, mod), "remove modules based on attributes"); return dt.andThen(d -> Definition(d.mainModule(), immutable(stream(d.entryModules()).filter(mod -> excludedModuleTags.stream().noneMatch(tag -> mod.att().contains(tag))).collect(Collectors.toSet())), d.att())); }
private boolean containsThreadCell(CompiledDefinition def) { return stream(def.executionModule().productions()) .anyMatch(p -> (p.att().contains("thread") && p.att().contains("cell"))); } public void initialize(CompiledDefinition def) {
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 Map<KLabel, KLabel> collectionFor(Module m) { return stream(m.productions()).filter(p -> p.att().contains(Attribute.ASSOCIATIVE_KEY)).flatMap(p -> { Set<Tuple2<KLabel, KLabel>> set = new HashSet<>(); set.add(Tuple2.apply(p.klabel().get(), p.klabel().get())); if (p.att().contains(Attribute.UNIT_KEY)) { set.add(Tuple2.apply(KLabel(p.att().<String>get(Attribute.UNIT_KEY)), p.klabel().get())); } if (p.att().contains("element")) { set.add(Tuple2.apply(KLabel(p.att().<String>get("element")), p.klabel().get())); } if (p.att().contains("wrapElement")) { set.add(Tuple2.apply(KLabel(p.att().<String>get("wrapElement")), p.klabel().get())); } return set.stream(); }).distinct().collect(Collectors.toMap(Tuple2::_1, Tuple2::_2)); }
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; }
private Either<Set<ParseFailedException>, Term> wrapTermWithCast(Constant c, Sort declared) { Production cast; if (addCast) { cast = productions.apply(KLabel("#SemanticCastTo" + declared.toString())).head(); } else if (inferCasts && !hasCastAlready && productions.contains(KLabel("#SyntacticCast"))) { cast = stream(productions.apply(KLabel("#SyntacticCast"))).filter(p -> p.sort().equals(declared)).findAny().get(); } else { cast = null; } if (cast == null) { return Right.apply(c); } else { return Right.apply(TermCons.apply(ConsPStack.singleton(c), cast, c.location(), c.source())); } }
@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); } }