Search in sources :

Example 1 with StrategoBlob

use of mb.nabl2.stratego.StrategoBlob in project nabl by metaborg.

the class StrategoTerms method toStratego.

// to
public IStrategoTerm toStratego(ITerm term) {
    IStrategoTerm strategoTerm = term.match(Terms.cases(// @formatter:off
    appl -> {
        List<IStrategoTerm> args = appl.getArgs().stream().map(arg -> toStratego(arg)).collect(Collectors.toList());
        IStrategoTerm[] argArray = args.toArray(new IStrategoTerm[args.size()]);
        return appl.getOp().equals(Terms.TUPLE_OP) ? termFactory.makeTuple(argArray) : termFactory.makeAppl(termFactory.makeConstructor(appl.getOp(), appl.getArity()), argArray);
    }, list -> toStrategoList(list), string -> termFactory.makeString(string.getValue()), integer -> termFactory.makeInt(integer.getValue()), blob -> new StrategoBlob(blob.getValue()), var -> {
        throw new IllegalArgumentException("Cannot convert specialized terms to Stratego.");
    }));
    strategoTerm = putAttachments(strategoTerm, term.getAttachments());
    return strategoTerm;
}
Also used : B(mb.nabl2.terms.build.TermBuild.B) Arrays(java.util.Arrays) IStrategoInt(org.spoofax.interpreter.terms.IStrategoInt) Function1(org.metaborg.util.functions.Function1) ListTerms(mb.nabl2.terms.ListTerms) Builder(com.google.common.collect.ImmutableClassToInstanceMap.Builder) ITerm(mb.nabl2.terms.ITerm) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) ITermFactory(org.spoofax.interpreter.terms.ITermFactory) IStrategoTuple(org.spoofax.interpreter.terms.IStrategoTuple) Collectors(java.util.stream.Collectors) IStrategoList(org.spoofax.interpreter.terms.IStrategoList) IStrategoReal(org.spoofax.interpreter.terms.IStrategoReal) List(java.util.List) Terms(mb.nabl2.terms.Terms) Lists(com.google.common.collect.Lists) IListTerm(mb.nabl2.terms.IListTerm) Optional(java.util.Optional) LinkedList(java.util.LinkedList) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) ImmutableClassToInstanceMap(com.google.common.collect.ImmutableClassToInstanceMap) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IStrategoList(org.spoofax.interpreter.terms.IStrategoList) List(java.util.List) LinkedList(java.util.LinkedList)

Example 2 with StrategoBlob

use of mb.nabl2.stratego.StrategoBlob in project nabl by metaborg.

the class SG_get_resource_analysis method call.

@Override
public Optional<? extends IStrategoTerm> call(IScopeGraphContext<?> context, IStrategoTerm sterm, List<IStrategoTerm> sterms, ITermFactory factory) throws InterpreterException {
    if (!Tools.isTermString(sterm)) {
        throw new InterpreterException("Expect a resource path.");
    }
    String resource = Tools.asJavaString(sterm);
    final IScopeGraphUnit unit = context.unit(resource);
    return Optional.of(new StrategoBlob(unit));
}
Also used : InterpreterException(org.spoofax.interpreter.core.InterpreterException) IScopeGraphUnit(mb.nabl2.spoofax.analysis.IScopeGraphUnit) StrategoBlob(mb.nabl2.stratego.StrategoBlob)

Aggregations

ImmutableClassToInstanceMap (com.google.common.collect.ImmutableClassToInstanceMap)1 Builder (com.google.common.collect.ImmutableClassToInstanceMap.Builder)1 Lists (com.google.common.collect.Lists)1 Arrays (java.util.Arrays)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 IScopeGraphUnit (mb.nabl2.spoofax.analysis.IScopeGraphUnit)1 StrategoBlob (mb.nabl2.stratego.StrategoBlob)1 IListTerm (mb.nabl2.terms.IListTerm)1 ITerm (mb.nabl2.terms.ITerm)1 ListTerms (mb.nabl2.terms.ListTerms)1 Terms (mb.nabl2.terms.Terms)1 B (mb.nabl2.terms.build.TermBuild.B)1 Function1 (org.metaborg.util.functions.Function1)1 InterpreterException (org.spoofax.interpreter.core.InterpreterException)1 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)1 IStrategoInt (org.spoofax.interpreter.terms.IStrategoInt)1 IStrategoList (org.spoofax.interpreter.terms.IStrategoList)1