private void check(Production p) { check(p.sort(), p); }
private void functionalPattern(Production prod, Runnable functionPattern) { sb.append(" axiom"); convertParams(prod.klabel(), true); sb.append(" \\exists{R} (Val:"); convert(prod.sort(), prod); sb.append(", \\equals{"); convert(prod.sort(), prod); sb.append(", R} ("); sb.append("Val:"); convert(prod.sort(), prod); sb.append(", "); functionPattern.run(); sb.append("))"); }
public Term apply(Constant c) { if (c.production().sort().equals(Sorts.KVariable()) && !declaredNames.contains(getVarKey(c))) { if (vars.isEmpty()) vars.add(HashMultimap.<VarKey, Sort>create()); for (Multimap<VarKey, Sort> vars2 : vars) vars2.put(getVarKey(c), sort); } return c; } }
private Optional<KLabel> klabelFromTerm(Term labelTerm) { if (labelTerm instanceof Constant) { Constant labelCon = (Constant) labelTerm; if (labelCon.production().sort().equals(Sorts.KLabel())) { String labelVal = labelCon.value(); if (labelVal.charAt(0) == '`') { return Optional.of(KLabel(labelVal.substring(1, labelVal.length() - 1))); } else { return Optional.of(KLabel(labelVal)); } } } return Optional.empty(); }
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; }
@Override public K apply(KApply k) { if (k.klabel().name().startsWith("#SemanticCastTo")) { sort = k.att().get(Production.class).sort(); } return super.apply(k); }
/** * Gather sort information from a module. * Populates the close operators only for sorts which only have a single associative production. */ public static SortInfo fromModule(Module module) { Multimap<Sort, KLabel> joinOps = HashMultimap.create(); Collections.stream(module.productionsFor()).forEach(x -> { if (Collections.stream(x._2()).anyMatch(p -> p.att().contains("assoc") && Collections.stream(p.items()).filter(i -> i instanceof NonTerminal).count() == 2)) { joinOps.put(x._2().head().sort(), x._1()); } }); SortInfo info = new SortInfo(); joinOps.asMap().forEach((sort, labels) -> { info.closeOperators.put(sort, Iterators.getNext(labels.iterator(), null)); }); return info; } }
public Tuple2<Either<java.util.Set<ParseFailedException>, Term>, java.util.Set<VarInfo>> apply(Constant c) { if (c.production().sort().equals(Sorts.KVariable())) { return new Tuple2<>(Right.apply(c), Sets.newHashSet(new VarInfo(c, this.sort, varType))); } return simpleResult(c); } }
public Either<java.util.Set<ParseFailedException>, Term> apply(ProductionReference pr) { if (pr instanceof TermCons) { TermCons tc = (TermCons) pr; if (VariableTypeInferenceFilter.hasPolySort(tc)) { return VariableTypeInferenceFilter.visitPolyChildren(tc, this::apply); } } if (pr instanceof Constant && pr.production().sort().equals(Sorts.KVariable())) { // skip variables since they will always have a different sort at this stage. return Right.apply(pr); } if ((!strict && !subsorts.lessThanEq(pr.production().sort(), sort)) || (strict && !pr.production().sort().equals(sort))) { String msg = "Unexpected sort " + pr.production().sort() + " for term parsed as production " + pr.production() + ". Expected " + sort + "."; KException kex = new KException(KException.ExceptionType.ERROR, KException.KExceptionGroup.CRITICAL, msg, pr.source().orElse(null), pr.location().orElse(null)); return Left.apply(Sets.newHashSet(new VariableTypeClashException(kex))); } return Right.apply(pr); } }
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; }
@Override public Term apply(TermCons tc) { if (tc.production().sort().name().equals("RuleContent")) { Term t = new PushTopAmbiguityUp2().apply(tc.get(0)); if (t instanceof Ambiguity) { Ambiguity old = (Ambiguity)t; Set<Term> newTerms = new HashSet<>(); for (Term child : old.items()) { Term newTerm = tc.with(0, child); newTerms.add(newTerm); } return Ambiguity.apply(newTerms); } } return super.apply(tc); }
public static Sentence convertListItemToNonFunction(Module mod, Sentence sentence) { if (!(sentence instanceof Production)) { return sentence; } Production prod = (Production)sentence; if (prod.klabel().isDefined() && KLabels.ListItem.equals(prod.klabel().get())) { return Production(prod.sort(), prod.items(), prod.att().remove("function")); } return prod; }
@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); } }
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())); } }
public Either<java.util.Set<ParseFailedException>, Term> apply(Constant c) { if (c.production().sort().equals(Sorts.KVariable())) { Sort declared = decl.get(getVarKey(c)); if (declared != null && !(declared.equals(Sorts.K()) && subsorts.lessThanEq(sort, Sorts.KList()))) { // if the declared/inferred sort is K, make sure it can fit in the context (ex. is not a KLabel) if ((!strictSortEquality && !subsorts.lessThanEq(declared, sort)) || (strictSortEquality && !declared.equals(sort))) { String msg = "Unexpected sort " + declared + " for term " + c.value() + ". Expected " + sort + "."; KException kex = new KException(KException.ExceptionType.ERROR, KException.KExceptionGroup.CRITICAL, msg, c.source().orElse(null), c.location().orElse(null)); return Left.apply(Sets.newHashSet(new VariableTypeClashException(kex))); } return wrapTermWithCast(c, declared); } } return Right.apply(c); }
public static boolean isFunctionRule(TermCons tc) { if (tc.production().sort().name().equals("RuleContent")) { ProductionReference child = (ProductionReference) tc.get(0); if (child.production().klabel().isDefined() && child.production().klabel().get().equals(KLabels.KREWRITE)) { child = (ProductionReference)((TermCons)child).get(0); } return child.production().att().contains(Attribute.FUNCTION_KEY); } return false; }
@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); } }
private Production production(KApply term) { if (term.klabel().name().equals(KLabels.INJ)) return Production(INJ_PROD.klabel(), INJ_PROD.sort(), INJ_PROD.items(), Att.empty().add("originalPrd", Production.class, INJ_PROD)); scala.collection.Set<Production> prods = module.productionsFor().apply(((KApply) term).klabel()); assert(prods.size() == 1); return computePolyProd(prods.head()); }
private Sort sort(K k) { if (k instanceof KSequence) return Sorts.K(); if (k instanceof KAs) return sort(((KAs) k).pattern()); if (k instanceof InjectedKLabel) return Sorts.KItem(); if (k instanceof KToken) return ((KToken) k).sort(); if (k instanceof KApply) return k.att().get(Production.class).sort(); if (k instanceof KVariable) return Sorts.K(); throw KEMException.compilerError("Could not compute sort of term", k); }
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; }