static Term visitPolyChildrenSafe(TermCons tc, Function<Term, Term> apply) { for (int i : getPolyChildren(tc)) { apply.apply(tc.get(i - 1)); } return tc; }
static Either<Set<ParseFailedException>,Term> visitPolyChildren(TermCons tc, Function<Term, Either<Set<ParseFailedException>, Term>> apply) { Set<ParseFailedException> errors = new HashSet<>(); for (int i : getPolyChildren(tc)) { Either<Set<ParseFailedException>, Term> res = apply.apply(tc.get(i - 1)); if (res.isLeft()) { errors.addAll(res.left().get()); } } if (errors.isEmpty()) return Right.apply(tc); else return Left.apply(errors); }
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(); } }
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); } }
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; }
static Tuple2<Either<Set<ParseFailedException>, Term>, Set<VarInfo>> visitPolyChildrenSets(TermCons tc, Function<Term, Tuple2<Either<Set<ParseFailedException>, Term>, Set<VarInfo>>> apply) { Set<ParseFailedException> errors = new HashSet<>(); Set<VarInfo> info = new HashSet<>(); for (int i : getPolyChildren(tc)) { Tuple2<Either<Set<ParseFailedException>, Term>, Set<VarInfo>> res = apply.apply(tc.get(i - 1)); info.addAll(res._2()); if (res._1().isLeft()) errors.addAll(res._1().left().get()); } if (errors.isEmpty()) return Tuple2.apply(Right.apply(tc), info); else return Tuple2.apply(Left.apply(errors), info); }
@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 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 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); }
@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); } }
@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 static Sort getSortOfCast(TermCons tc) { switch (tc.production().klabel().get().name()) { case "#ruleNoConditions": case "#ruleRequires": case "#ruleEnsures": case "#ruleRequiresEnsures": { 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().sort(); } case "#SyntacticCast": case "#OuterCast": return tc.production().sort(); case "#InnerCast": return ((NonTerminal)tc.production().items().apply(1)).sort(); default: if (tc.production().klabel().get().name().startsWith("#SemanticCastTo")) { return tc.production().sort(); } throw new AssertionError("Unexpected cast type"); } }
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); }
public Tuple2<Either<java.util.Set<ParseFailedException>, Term>, java.util.Set<VarInfo>> apply(TermCons tc) { // TODO: (Radu) if this is cast, take the sort from annotations? Set<VarInfo> collector = Sets.newHashSet(); 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"))) { Term t = tc.get(0); collector = new CollectVariables2(getSortOfCast(tc), VarType.USER).apply(t)._2(); } else if (tc.production().klabel().isDefined() && isFunctionRule(tc) && j == 0) { Term t = tc.get(0); collector = new CollectVariables2(getSortOfCast(tc), VarType.CONTEXT).apply(t)._2(); j++; } else { Term t = tc.get(j); Set<VarInfo> vars = new CollectVariables2(((NonTerminal) tc.production().items().apply(i)).sort(), VarType.CONTEXT).apply(t)._2(); collector.addAll(vars); j++; } } } Tuple2<Either<java.util.Set<ParseFailedException>, Term>, java.util.Set<VarInfo>> rez = super.apply(tc); return new Tuple2<>(Right.apply(rez._1().right().get()), mergeWarnings(collector, rez._2())); }
@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; }
|| tc.production().klabel().get().name().equals("#InnerCast")) || (VariableTypeInferenceFilter.isFunctionRule(tc) && j == 0)) { Term t = tc.get(0); boolean strict = tc.production().klabel().get().name().equals("#SyntacticCast") || tc.production().klabel().get().name().equals("#InnerCast"); Either<Set<ParseFailedException>, Term> rez = new ApplyTypeCheck2(VariableTypeInferenceFilter.getSortOfCast(tc), strict).apply(t); j++; } else { Term t = tc.get(j); Sort s = ((NonTerminal) tc.production().items().apply(i)).sort(); Either<Set<ParseFailedException>, Term> rez = new ApplyTypeCheck2(s).apply(t);
|| tc.production().klabel().get().name().equals("#InnerCast") || (isFunctionRule(tc)) && j == 0)) { Term t = tc.get(0); boolean strictSortEquality = tc.production().klabel().get().name().equals("#SyntacticCast") || tc.production().klabel().get().name().equals("#InnerCast"); Either<Set<ParseFailedException>, Term> rez = new ApplyTypeCheck2(getSortOfCast(tc), true, strictSortEquality, strictSortEquality && inferSortChecks).apply(t); j++; } else { Term t = tc.get(j); Sort s = ((NonTerminal) tc.production().items().apply(i)).sort(); Either<Set<ParseFailedException>, Term> rez = new ApplyTypeCheck2(s, false, false, inferSortChecks).apply(t);
@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() && !exceptions.contains(tc.production().klabel().get().name())) { // match only on the outermost elements if (tc.production().items().apply(0) instanceof NonTerminal) { 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()); } if (tc.production().items().apply(tc.production().items().size() - 1) instanceof NonTerminal) { int last = tc.items().size() - 1; Either<java.util.Set<ParseFailedException>, Term> rez = new PriorityVisitor2(tc).apply(tc.get(last)); if (rez.isLeft()) return rez; tc = tc.with(last, rez.right().get()); } } return super.apply(tc); }
@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() && !exceptions.contains(tc.production().klabel().get().name())) { // match only on the outermost elements if (tc.production().items().apply(0) instanceof NonTerminal) { 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()); } if (tc.production().items().apply(tc.production().items().size() - 1) instanceof NonTerminal) { int last = tc.items().size() - 1; Either<java.util.Set<ParseFailedException>, Term> rez = new PriorityVisitor2(tc).apply(tc.get(last)); if (rez.isLeft()) return rez; tc = tc.with(last, rez.right().get()); } } return super.apply(tc); }
@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().att().contains("bracket")) { // match only on the outermost elements if (tc.production().items().apply(0) instanceof NonTerminal) { Either<java.util.Set<ParseFailedException>, Term> rez = new PriorityVisitor2(tc, PriorityVisitor2.Side.LEFT, priorities, leftAssoc, rightAssoc).apply(tc.get(0)); if (rez.isLeft()) return rez; tc = tc.with(0, rez.right().get()); } if (tc.production().items().apply(tc.production().items().size() - 1) instanceof NonTerminal) { int last = tc.items().size() - 1; Either<java.util.Set<ParseFailedException>, Term> rez = new PriorityVisitor2(tc, PriorityVisitor2.Side.RIGHT, priorities, leftAssoc, rightAssoc).apply(tc.get(last)); if (rez.isLeft()) return rez; tc = tc.with(last, rez.right().get()); } } return super.apply(tc); }