Search in sources :

Example 81 with IStrategoTerm

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;
}
Also used : IListTerm(mb.nabl2.terms.IListTerm) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) ImmutableClassToInstanceMap(com.google.common.collect.ImmutableClassToInstanceMap) IStrategoList(org.spoofax.interpreter.terms.IStrategoList)

Example 82 with IStrategoTerm

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)));
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl)

Aggregations

IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)82 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)31 FileObject (org.apache.commons.vfs2.FileObject)27 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)25 MetaborgException (org.metaborg.core.MetaborgException)19 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)17 HybridInterpreter (org.strategoxt.HybridInterpreter)17 IStrategoList (org.spoofax.interpreter.terms.IStrategoList)16 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)15 Nullable (javax.annotation.Nullable)14 ISourceRegion (org.metaborg.core.source.ISourceRegion)12 ICompletion (org.metaborg.core.completion.ICompletion)11 IStrategoTuple (org.spoofax.interpreter.terms.IStrategoTuple)10 IOException (java.io.IOException)9 IProject (org.metaborg.core.project.IProject)9 IMessage (org.metaborg.core.messages.IMessage)8 IToken (org.spoofax.jsglr.client.imploder.IToken)8 IContext (org.metaborg.core.context.IContext)7 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)7 ListImploderAttachment (org.spoofax.jsglr.client.imploder.ListImploderAttachment)7