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; } }
/** * Add to this function the mappings resulting from composing mappings in call with the mappings in exit. * * This is used when the child (a {@link NonTerminal}) of a {@link NonTerminalState} finishes parsing. * In that case 'call' is the Function for the {@link StateCall} for that {@link NonTerminalState} and * 'exit' is the Function for the {@link StateReturn} of the {@link ExitState} in the {@link NonTerminal}. * @param call The base function onto which 'exit' should be appended * @param exit The function to append on 'call' * @return 'true' iff the mapping in this function changed */ boolean addNTCall(Function call, final Function exit) { return addAux(call, set -> { Set<Term> result = new HashSet<>(); if (!exit.values.isEmpty()) { // if we found some, make an amb node and append them to the KList for (Term context : set) { if (exit.values.size() == 1) { result.add(((KList)context).add(exit.values.iterator().next())); } else { result.add(((KList) context).add(Ambiguity.apply(exit.values))); } } } return result; }); }
@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); }
@Test public void testAmb2() throws Exception { Ambiguity two = Ambiguity.apply(foo, bar); assertCleanup(two, two); }
@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))); }
@Test public void testAmb() throws Exception { assertCleanup(Ambiguity.apply(foo), foo); }
@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); } }
for(StateReturn stateReturn : s.ntCalls.computeIfAbsent(new NonTerminalCall.Key(nt, position), NonTerminalCall.Key::create).exitStateReturns) { if (stateReturn.key.stateEnd == s.input.length) { resultSet.add(KList.apply(ConsPStack.singleton(Ambiguity.apply(stateReturn.function.values)))); Ambiguity result = Ambiguity.apply(resultSet);
@Test public void testKList() throws Exception { assertCleanup(Ambiguity.apply(KList.apply(ConsPStack.singleton(foo))), foo); }