use of org.spoofax.interpreter.terms.IStrategoTerm in project nabl by metaborg.
the class StrategoTerms method toStrategoList.
// NB. This function does not preserve locks, it depends on toStratego for that.
private IStrategoTerm toStrategoList(IListTerm list) {
final LinkedList<IStrategoTerm> terms = Lists.newLinkedList();
final LinkedList<ImmutableClassToInstanceMap<Object>> attachments = Lists.newLinkedList();
while (list != null) {
attachments.push(list.getAttachments());
list = list.match(ListTerms.<IListTerm>cases(// @formatter:off
cons -> {
terms.push(toStratego(cons.getHead()));
return cons.getTail();
}, nil -> {
return null;
}, var -> {
throw new IllegalArgumentException("Cannot convert specialized terms to Stratego.");
}));
}
IStrategoList strategoList = termFactory.makeList();
putAttachments(strategoList, attachments.pop());
while (!terms.isEmpty()) {
strategoList = termFactory.makeListCons(terms.pop(), strategoList);
putAttachments(strategoList, attachments.pop());
}
return strategoList;
}
use of org.spoofax.interpreter.terms.IStrategoTerm in project nabl by metaborg.
the class StrategoTermIndices method match.
public static Optional<TermIndex> match(IStrategoTerm term) {
if (!(Tools.isTermAppl(term) && Tools.hasConstructor((IStrategoAppl) term, OP, ARITY))) {
return Optional.empty();
}
IStrategoTerm resourceTerm = term.getSubterm(0);
IStrategoTerm idTerm = term.getSubterm(1);
if (!(Tools.isTermString(resourceTerm) && Tools.isTermInt(idTerm))) {
return Optional.empty();
}
return Optional.of(ImmutableTermIndex.of(Tools.asJavaString(resourceTerm), Tools.asJavaInt(idTerm)));
}
Aggregations