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; } }
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()); }
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())); } }
TermCons terminator = TermCons.apply(ConsPStack.empty(), ulTerm.pTerminator, child.location(), child.source()); TermCons newTc = TermCons.apply(ConsPStack.from(Arrays.asList(terminator, child)), ul.pList, child.location(), child.source()); newItems.add(newTc); changed = true; tc = TermCons.apply(ConsPStack.from(newItems), tc.production(), tc.location(), tc.source());
@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); } }
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; }
@Test(expected = ParseFailedException.class) public void testNoKLabel() throws Exception { throwFirstLeftException(TermCons.apply(ConsPStack.from(Arrays.asList(bar, foo)), noKLabelProduction, new Location(0, 0, 0, 0), new Source(""))); }