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 Context upContext(K contents) { KApply ruleContents = (KApply) contents; List<K> items = ruleContents.klist().items(); switch (ruleContents.klabel().name()) { case "#ruleNoConditions": return Context(items.get(0), BooleanUtils.TRUE, ruleContents.att()); case "#ruleRequires": return Context(items.get(0), items.get(1), ruleContents.att()); default: throw KEMException.criticalError("Illegal context with ensures clause detected.", contents); } }
@Override public K apply(KApply k) { ArrayList<K> newItems = new ArrayList<>(k.klist().items()); boolean change = false; for (int i = 0; i < newItems.size(); ++i) { K in = newItems.get(i); K out = apply(in); newItems.set(i, out); change = change || (in != out); } if (change) { return KApply(apply(k.klabel()), KList(newItems), k.att()); } else { return k; } }
@Override public K apply(KApply k) { List<K> children = new ArrayList<>(); for (K child : k.klist().items()) { K res = apply(child); if (res instanceof KSequence) { children.add(res); } else { children.add(KSequence(res)); } } return KApply(k.klabel(), KList(children), k.att()); }
@Override public K apply(KApply k) { if (k.klabel() instanceof KVariable && predicates.containsKey(k.klabel())) { return KApply(apply(k.klabel()), k.klist().items().stream().map(this::apply).collect(org.kframework.Collections.toList()), k.att()); } return super.apply(k); }
@Override public K apply(KApply k) { if (k.klabel().name().startsWith("#SemanticCastTo")) { sort = k.att().get(Production.class).sort(); } return super.apply(k); }
private KLabel computePolyKLabel(KApply k) { List<Set<Integer>> poly = polyKLabels.get(k.klabel().name()); List<Sort> params = new ArrayList<>(); for (Set<Integer> positions : poly) { int pos = positions.iterator().next(); Sort sort; if (pos == 0) { sort = k.att().get(Sort.class); } else { sort = k.items().get(pos-1).att().get(Sort.class); } params.add(sort); } return KLabel(k.klabel().name(), immutable(params)); }
return KApply(k.klabel(), KList(ordered), k.att());
@Override public void apply(KApply k) { if (k.klabel().name().equals(userCellLabel.name())) { String sort = wellformedAndGetSortNameOfCast(k.klist()); if (!sort.isEmpty()) { sorts.add(sort); } else { if (k.att().getOption(Location.class).isDefined()) { // warning only for user-provided rules kem.registerCompilerWarning("Unsupported matching pattern in stdin stream cell." + "\nThe currently supported pattern is: <in> ListItem(V:Sort) => .List ... </in>", k); } } } super.apply(k); }
@Override public K apply(KApply k) { k = (KApply) super.apply(k); return KApply(apply(k.klabel()), k.klist(), k.att()); }
funRules.add(funRule(fun, body, k.att())); List<K> klist = new ArrayList<>(); klist.add(apply(arg));
private K transform(K requires, Att att) { if (att.contains("cool") && !modes.contains(Mode.COOL_RESULT_CONDITION)) { return requires; } String sort = att.<String>getOptional("result").orElse("KResult"); KApply predicate = KApply(KLabel("is" + sort), KVariable("HOLE")); if (att.contains("heat")) { return BooleanUtils.and(requires, BooleanUtils.not(predicate)); } else if (att.contains("cool")) { if (transitions.stream().anyMatch(att::contains)) { // if the cooling rule is a super strict, then tag the isKResult predicate and drop it during search predicate = KApply(predicate.klabel(), predicate.klist(), predicate.att().add(Att.transition())); } return BooleanUtils.and(requires, predicate); } throw new AssertionError("unreachable"); }
@Override public void apply(KApply k) { if (k.klabel().name().startsWith("#SemanticCastTo")) { Sort savedContext = context; context = k.att().get(Production.class).sort(); apply(k.items().get(0)); context = savedContext; return; } if (k.klabel() instanceof KVariable) { apply((KVariable) k.klabel()); } super.apply(k); } }
@Override public K apply(KApply k0) { if (hasCells(k0)) { ArrayList<K> klist0 = new ArrayList<K>(Collections.nCopies(k0.klist().size(), null)); for (int idx = 0; idx < k0.klist().size(); idx++) { K item0 = k0.klist().items().get(idx); klist0.set(idx, item0); if (item0 instanceof KApply) { KApply k = (KApply) item0; if (k.klabel().name().equals("#cells")) { if (cellFragmentVarsCell.contains(k)) { Sort cellFragmentSort = nthArgSort(k0.klabel(), idx); if (cellFragmentSort == null) { throw new IllegalArgumentException("Not found " + idx + "th argument sort of " + k0.klabel()); } if (cellFragmentSort.name().endsWith("Fragment")) { Sort cellSort = Sort(cellFragmentSort.name().substring(0,cellFragmentSort.name().indexOf("Fragment"))); KLabel cellLabel = cfg.cfg.getCellLabel(cellSort); klist0.set(idx, KApply(cellLabel, KList(item0), Att().add("dummy_cell"))); } } } } } return KApply(k0.klabel(), KList(klist0), k0.att()); } return super.apply(k0); } @Override
private Stream<? extends K> performParse(Map<String, ParsedSentence> cache, ParseInModule parser, Scanner scanner, Bubble b) { int startLine = b.att().get("contentStartLine", Integer.class); int startColumn = b.att().get("contentStartColumn", Integer.class); Source source = b.att().get(Source.class); Tuple2<Either<java.util.Set<ParseFailedException>, K>, java.util.Set<ParseFailedException>> result; if (cache.containsKey(b.contents())) { ParsedSentence parse = cache.get(b.contents()); Optional<Source> cacheSource = parse.getParse().source(); //Cache might contain content from an identical file but another source path. //The content will have wrong Source attribute and must be invalidated. if (cacheSource.isPresent() && cacheSource.get().equals(source)) { cachedBubbles.getAndIncrement(); kem.addAllKException(parse.getWarnings().stream().map(e -> e.getKException()).collect(Collectors.toList())); return Stream.of(parse.getParse()); } } result = parser.parseString(b.contents(), START_SYMBOL, scanner, source, startLine, startColumn, !b.att().contains("macro") && !b.att().contains("alias")); parsedBubbles.getAndIncrement(); kem.addAllKException(result._2().stream().map(e -> e.getKException()).collect(Collectors.toList())); if (result._1().isRight()) { KApply k = (KApply) new TreeNodesToKORE(Outer::parseSort, isStrict).down(result._1().right().get()); k = KApply(k.klabel(), k.klist(), k.att().addAll(b.att().remove("contentStartLine").remove("contentStartColumn").remove(Source.class).remove(Location.class))); cache.put(b.contents(), new ParsedSentence(k, new HashSet<>(result._2()))); return Stream.of(k); } else { errors.addAll(result._1().left().get()); return Stream.empty(); } } }
@Override public K apply(KApply k) { if (k.klabel().name().equals("#SemanticCastToString") && k.klist().size() == 1) { K i = k.klist().items().get(0); if (i instanceof KVariable) { KVariable x = (KVariable) i; switch (x.name()) { case "?Sort": return KToken("\"" + sort + "\"", Sorts.String()); case "?Delimiters": // TODO(Daejun): support `delimiter` attribute in stream cell return KToken("\" \\n\\t\\r\"", Sorts.String()); default: // fall through } } } k = (KApply) super.apply(k); return KApply(apply(k.klabel()), k.klist(), k.att()); }
public static K flattenTerm(Module mod, KApply kapp) { List<K> items = new ArrayList<>(); Att att = mod.attributesFor().apply(KLabel(kapp.klabel().name())); if (att.contains("assoc") && att.contains("unit")) { items = Assoc.flatten(kapp.klabel(), kapp.klist().items(), KLabel(att.get("unit"))); } else { items = kapp.klist().items(); } return KApply(kapp.klabel(), KList(items), kapp.att()); }
private Rule resolveInitRule(Production streamProduction, Rule rule) { Sort streamSort = streamProduction.sort(); // InCell, OutCell String initLabel = GenerateSentencesFromConfigDecl.getInitLabel(streamSort); // initInCell, initOutCell KLabel cellLabel = streamProduction.klabel().get(); // <in>, <out> // rule initInCell(Init) => <in> ... </in> if (isInitRule(initLabel, cellLabel.name(), rule)) { KRewrite body = (KRewrite) rule.body(); KApply right = (KApply) body.right(); KList klist = getContentsOfInitRule(streamProduction); right = KApply(right.klabel(), klist, right.att()); body = KRewrite(body.left(), right, body.att()); return Rule(body, rule.requires(), rule.ensures(), rule.att()); } return rule; }