@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() && !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); }
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); } }
@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 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 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<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; }
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()); }
prod = ref.production(); if (ref instanceof TermCons) { arity = ((TermCons) ref).items().size(); TermCons tc = (TermCons)t; if (sameTerm == null) { sameTerm = tc.get(i); } else if (!sameTerm.equals(tc.get(i))) { sameAtIdx = false; return apply(first.with(i, new Ambiguity(a.items().stream().map(t -> (TermCons)t).map(t -> t.get(idx)).collect(Collectors.toSet()))));
public ProductionReference addBrackets(ProductionReference inner, TermCons outer, ProductionReference leftCapture, ProductionReference rightCapture) { if (requiresBracketWithSimpleAlgorithm(outer, leftCapture, rightCapture, inner)) { int position = getPosition(inner, outer); Sort outerSort = ((NonTerminal)outer.production().items().apply(position)).sort(); Sort innerSort = inner.production().sort(); for (Tuple2<Sort, scala.collection.immutable.List<Production>> sort : iterable(m.bracketProductionsFor())) { boolean isCorrectOuterSort = m.subsorts().lessThanEq(sort._1(), outerSort); if (isCorrectOuterSort) { for (Production p : mutable(sort._2())) { boolean isCorrectInnerSort = stream(p.items()) .filter(i -> i instanceof NonTerminal) .map(i -> (NonTerminal) i) .map(NonTerminal::sort) .filter(s -> m.subsorts().lessThanEq(innerSort, s)) .findAny().isPresent(); if (isCorrectInnerSort) { return TermCons.apply(ConsPStack.singleton(inner), p); } } } } return TermCons.apply(ConsPStack.singleton(inner), Production(Sorts.KBott(), Seq(NonTerminal(Sorts.K())))); } return inner; }
protected Term apply(KList klist, MetaData metaData) { Term term; Location loc = new Location(metaData.start.line, metaData.start.column, metaData.end.line, metaData.end.column); Source source = metaData.source; if (isToken) { String value = metaData.input.subSequence(metaData.start.position, metaData.end.position).toString(); term = Constant.apply(value, label, Optional.of(loc), Optional.of(source)); } else if (needsLabel) { term = TermCons.apply(klist.items(), label, loc, source); } else { return klist; } return term; } }
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 Term visitPolyChildrenSafe(TermCons tc, Function<Term, Term> apply) { for (int i : getPolyChildren(tc)) { apply.apply(tc.get(i - 1)); } return 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 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); } }
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 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())); } }
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)); }
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); }