@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 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); }
@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; }
/** * Remove KList artifacts from parsing only when it contains a single element. */ @Override public Either<Set<ParseFailedException>, Term> apply(KList node) { Either<Set<ParseFailedException>, Term> res = super.apply(node); if (res.isRight() && ((KList) res.right().get()).items().size() == 1) return Right.apply(((KList) res.right().get()).items().get(0)); else return res; } }
Either<java.util.Set<ParseFailedException>, Term> rez = super.apply(tc); if (rez.isLeft()) return rez;
Either<java.util.Set<ParseFailedException>, Term> rez = super.apply(tc); if (rez.isLeft()) return rez;
@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); }