private static List<Integer> getPolyChildren(TermCons tc) { return RuleGrammarGenerator.computePositions(tc.production()).stream().filter(s -> s.contains(0)).findAny() .orElse(Collections.emptySet()).stream().filter(j -> j != 0).collect(Collectors.toList()); }
static boolean hasPolySort(TermCons tc) { if (!tc.production().att().contains("poly")) return false; List<Set<Integer>> positions = RuleGrammarGenerator.computePositions(tc.production()); return positions.stream().anyMatch(s -> s.contains(0)); }
@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); } }
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); } }
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); } }
public Term apply(Ambiguity a) { Set<Production> productions = new HashSet<>(); for (Term t : a.items()) { if (t instanceof TermCons) { TermCons tc = (TermCons)t; productions.add(tc.production()); } else { return a; } } Set<Production> candidates = overloads.minimal(productions); Ambiguity result = Ambiguity.apply(a.items().stream().filter(t -> candidates.contains(((ProductionReference)t).production())).collect(Collectors.toSet())); if (result.items().size() == 1) { return result.items().iterator().next(); } return result; } }
@Override public Either<java.util.Set<ParseFailedException>, Term> apply(TermCons tc) { assert tc.production() != null : this.getClass() + ":" + " production not found." + tc; if (!tc.production().isSyntacticSubsort() && tc.production().klabel().isDefined() && (tc.production().klabel().get().name().equals("#SyntacticCast") || tc.production().klabel().get().name().startsWith("#SemanticCastTo"))) { // match only on the outermost elements Either<java.util.Set<ParseFailedException>, Term> rez = new PriorityVisitor2(tc).apply(tc.get(0)); if (rez.isLeft()) return rez; tc = tc.with(0, rez.right().get()); } return super.apply(tc); }
public Term apply(TermCons tc) { for (int i = 0, j = 0; i < tc.production().items().size(); i++) { if (tc.production().items().apply(i) instanceof NonTerminal) { if (tc.production().klabel().isDefined() && (tc.production().klabel().get().name().equals("#SyntacticCast") || tc.production().klabel().get().name().startsWith("#SemanticCastTo") || tc.production().klabel().get().name().equals("#InnerCast")) || (isFunctionRule(tc) && j == 0)) { Term t = tc.get(0); new CollectUndeclaredVariables2(getSortOfCast(tc)).apply(t); j++; } else { Term t = tc.get(j); new CollectUndeclaredVariables2(((NonTerminal) tc.production().items().apply(i)).sort()).apply(t); j++; } } } return super.apply(tc); }
private int getPosition(ProductionReference inner, ProductionReference outer) { EnumSet<Fixity> set = EnumSet.noneOf(Fixity.class); assert outer instanceof TermCons; TermCons tc = (TermCons)outer; Production p = tc.production(); for (int i = 0, j = 0; i < p.items().size(); i++) { if (p.items().apply(i) instanceof NonTerminal) { if (tc.get(j) == inner) { return i; } j++; } } throw new AssertionError(); } }
@Override public Term apply(TermCons tc) { if (tc.production().klabel().isDefined() && tc.production().klabel().get().equals(KLabels.KREWRITE)) { Term t = 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); } }
private void lowerKListAcc(Term term, List<Term> items) { if (term instanceof TermCons) { TermCons cons = (TermCons) term; if (cons.production().klabel().isDefined()) { String labelName = cons.production().klabel().get().name(); if (labelName.equals("#KList")) { assert cons.items().size() == 2; lowerKListAcc(cons.get(0), items); lowerKListAcc(cons.get(1), items); return; } else if (labelName.equals("#EmptyKList")) { return; } } } items.add(term); } }
@Override public Either<Set<ParseFailedException>, Term> apply(TermCons tc) { Either<Set<ParseFailedException>, Term> vis; if (tc.production().isSyntacticSubsort() && tc.production().klabel().isEmpty()) { // eliminating syntactic subsort vis = apply(tc.get(0)); } else if (!tc.production().att().contains("bracket") && tc.production().klabel().isEmpty()) { return Left.apply(Sets.newHashSet(new ParseFailedException(new KException( KException.ExceptionType.ERROR, KException.KExceptionGroup.INNER_PARSER, "Only subsort productions are allowed to have no #klabel attribute", tc.source().get(), tc.location().get())))); } else { // invalidate the hashCode cache vis = super.apply(tc); } return vis; }
private EnumSet<Fixity> getFixity(ProductionReference inner, ProductionReference outer) { assert outer instanceof TermCons; TermCons tc = (TermCons)outer; int i; for (i = 0; i < tc.items().size(); i++) { if (tc.get(i) == inner) break; } Production p = tc.production(); EnumSet<Fixity> set = EnumSet.noneOf(Fixity.class); int position = getPosition(inner, outer); if (!hasTerminalAtIdx(p, position+1)) { set.add(Fixity.BARE_RIGHT); } if (!hasTerminalAtIdx(p, position-1)) { set.add(Fixity.BARE_LEFT); } return set; }
@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))); }
@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); } }
@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 ProductionReference addBrackets(ProductionReference t, ProductionReference previousLeftCapture, ProductionReference previousRightCapture) { if (t instanceof Constant) { return t; } TermCons outer = (TermCons)t; List<Term> newItems = new ArrayList<>(); for (Term t2 : outer.items()) { ProductionReference inner = (ProductionReference) t2; EnumSet<Fixity> fixity = getFixity(outer); int position = getPosition(inner, outer); ProductionReference leftCapture = getLeftCapture(previousLeftCapture, outer, inner); ProductionReference rightCapture = getRightCapture(previousRightCapture, outer, inner); ProductionReference newInner = addBrackets(inner, outer, leftCapture, rightCapture); newInner = addBrackets(newInner, leftCapture, rightCapture); newItems.add(newInner); } return TermCons.apply(ConsPStack.from(newItems), outer.production()); }
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; }