private void applyPattern(Production prod, String varName) { convert(prod.klabel().get(), prod); sb.append("("); String conn = ""; for (int i = 0; i < prod.arity(); i++) { sb.append(conn); sb.append(varName).append(i).append(":"); convert(prod.nonterminal(i).sort(), prod); conn = ", "; } sb.append(')'); }
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()); }
public Either<java.util.Set<ParseFailedException>, Term> apply(TermCons tc) { if (tc.production().klabel().isDefined() && tc.production().klabel().get().name().equals("#KRewrite")) { String msg = "Rewrite is not allowed to be an immediate child of " + parent.production().klabel().get() + " Use parentheses: (x)=>(y) to set the proper scope of the operations."; KException kex = new KException(KException.ExceptionType.ERROR, KException.KExceptionGroup.CRITICAL, msg, tc.source().get(), tc.location().get()); return Left.apply(Sets.newHashSet(new PriorityException(kex))); } return Right.apply(tc); } }
public Either<java.util.Set<ParseFailedException>, Term> apply(TermCons tc) { if (tc.production().klabel().isDefined() && tc.production().klabel().get().name().equals("#KSequence")) { String msg = "~> is not allowed to be an immediate child of " + parent.production().klabel().get() + " Use parentheses: (x)~>(y) to set the proper scope of the operations."; KException kex = new KException(KException.ExceptionType.ERROR, KException.KExceptionGroup.CRITICAL, msg, tc.source().get(), tc.location().get()); return Left.apply(Sets.newHashSet(new PriorityException(kex))); } return Right.apply(tc); } }
@Override public Term apply(TermCons tc) { if (tc.production().att().contains("bracket") || tc.production().klabel().get().name().equals("#SyntacticCast") || tc.production().klabel().get().name().equals("#InnerCast") || tc.production().klabel().get().name().equals("#OuterCast")) { return apply(tc.get(0)); } return super.apply(tc); } }
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("))"); }
private boolean isRightAssoc(ProductionReference outer, ProductionReference inner) { Tag parentLabel = new Tag(outer.production().klabel().get().name()); Tag localLabel = new Tag(inner.production().klabel().get().name()); if (m.rightAssoc().contains(new Tuple2<>(parentLabel, localLabel))) { return true; } return false; }
private boolean isLeftAssoc(ProductionReference outer, ProductionReference inner) { Tag parentLabel = new Tag(outer.production().klabel().get().name()); Tag localLabel = new Tag(inner.production().klabel().get().name()); if (m.leftAssoc().contains(new Tuple2<>(parentLabel, localLabel))) { return true; } return false; }
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 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); }
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 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 boolean isPriorityWrong(ProductionReference outer, ProductionReference inner) { Tag parentLabel = new Tag(outer.production().klabel().get().name()); Tag localLabel = new Tag(inner.production().klabel().get().name()); if (m.priorities().lessThan(parentLabel, localLabel)) { return true; } if (m.leftAssoc().contains(new Tuple2<>(parentLabel, localLabel))) { return true; } if (m.rightAssoc().contains(new Tuple2<>(parentLabel, localLabel))) { return true; } return false; }
public WrapLabelRule(Production label) { assert label != null; this.label = label; this.isToken = label.att().contains("token"); this.needsLabel = label.klabel().isDefined() || !label.isSyntacticSubsort(); } protected Term apply(KList klist, MetaData metaData) {
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; }
public Set<Sentence> resolve(Set<Production> strictProductions) { Set<Sentence> sentences = new HashSet<>(); for (Production prod : strictProductions) { if (!prod.klabel().isDefined()) { throw KEMException.compilerError("Only productions with a KLabel can be strict.", prod); } if (prod.att().contains(Attribute.STRICT_KEY)) { sentences.addAll(resolve(prod, false)); } if (prod.att().contains(Attribute.SEQSTRICT_KEY)) { sentences.addAll(resolve(prod, true)); } } return sentences; }
public Either<java.util.Set<ParseFailedException>, Term> apply(TermCons tc) { if (tc.production().items().apply(tc.production().items().size() - 1) instanceof NonTerminal) { String msg = parent.production().klabel().get() + " is not allowed to be an immediate child of cast." + " Use parentheses: (x):Sort to set the proper scope of the operations."; KException kex = new KException(KException.ExceptionType.ERROR, KException.KExceptionGroup.CRITICAL, msg, tc.source().get(), tc.location().get()); return Left.apply(Sets.newHashSet(new PriorityException(kex))); } return Right.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()); }