/** * 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 protected KList apply(KList set, MetaData metaData) { return set.remove(length); } }
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))));
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; } }
@Test public void testKList() throws Exception { assertCleanup(Ambiguity.apply(KList.apply(ConsPStack.singleton(foo))), foo); }
/** * 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; } }