Search in sources :

Example 16 with IStrategoList

use of org.spoofax.interpreter.terms.IStrategoList in project spoofax by metaborg.

the class StrategoTransformer method transform.

private <I extends IUnit> ISpoofaxTransformUnit<I> transform(I input, IContext context, TransformActionContrib actionContribution, FileObject source, IStrategoTerm term, ITransformConfig config) throws TransformException {
    final FileObject location = context.location();
    final ILanguageComponent component = actionContribution.contributor;
    final TransformAction action = action(actionContribution.action);
    // Get input term
    final IStrategoTerm inputTerm = common.builderInputTerm(term, source, location);
    // Get Stratego runtime
    final HybridInterpreter runtime;
    try {
        runtime = strategoRuntimeService.runtime(component, context, true);
    } catch (MetaborgException e) {
        throw new TransformException("Transformation failed unexpectedly; cannot get Stratego interpreter", e);
    }
    // Transform
    logger.debug("Transforming {} with '{}'", source, action.name);
    final Timer timer = new Timer(true);
    final IStrategoTerm outputTerm;
    try {
        outputTerm = common.invoke(runtime, inputTerm, action.strategy);
    } catch (MetaborgException e) {
        throw new TransformException(e.getMessage(), e.getCause());
    }
    final long duration = timer.stop();
    if (outputTerm == null) {
        final String message = logger.format("Invoking Stratego strategy {} failed", action.strategy);
        throw new TransformException(message);
    }
    // Get the result and, if allowed and required, write to file
    List<TransformOutput> outputs;
    IStrategoTerm resultTerm;
    if (outputTerm.getSubtermCount() == 2 && (outputTerm instanceof IStrategoTuple)) {
        final IStrategoTerm resourceTerm = outputTerm.getSubterm(0);
        final IStrategoTerm contentTerm = outputTerm.getSubterm(1);
        try {
            if (resourceTerm instanceof IStrategoString) {
                resultTerm = contentTerm;
                outputs = Lists.newArrayList(output(resourceTerm, contentTerm, location, config));
            } else if (resourceTerm instanceof IStrategoList) {
                if (!(contentTerm instanceof IStrategoList) || resourceTerm.getSubtermCount() != contentTerm.getSubtermCount()) {
                    logger.error("List of terms does not match list of file names, cannot write to file.");
                    resultTerm = null;
                    outputs = Collections.emptyList();
                } else {
                    outputs = Lists.newArrayListWithExpectedSize(resourceTerm.getSubtermCount());
                    for (int i = 0; i < resourceTerm.getSubtermCount(); i++) {
                        outputs.add(output(resourceTerm.getSubterm(i), contentTerm.getSubterm(i), location, config));
                    }
                    resultTerm = resourceTerm.getSubtermCount() == 1 ? resourceTerm.getSubterm(0) : null;
                }
            } else {
                logger.error("First term of result tuple {} is neither a string, nor a list, cannot write output file", resourceTerm);
                resultTerm = null;
                outputs = Collections.emptyList();
            }
        } catch (MetaborgException ex) {
            resultTerm = null;
            outputs = Collections.emptyList();
        }
    } else {
        resultTerm = outputTerm;
        outputs = Collections.emptyList();
    }
    // Open editor
    if (action.flags.openEditor) {
        List<FileObject> resources = Lists.newArrayListWithExpectedSize(outputs.size());
        for (TransformOutput output : outputs) {
            if (output.resource != null) {
                resources.add(output.resource);
            }
        }
        editorRegistry.open(resources, context.project());
    }
    // Return result
    final TransformContrib contrib = new TransformContrib(resultTerm != null || !Iterables.isEmpty(outputs), true, resultTerm, outputs, Iterables2.<IMessage>empty(), duration);
    return unitService.transformUnit(input, contrib, context, actionContribution);
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) TransformOutput(org.metaborg.spoofax.core.unit.TransformOutput) MetaborgException(org.metaborg.core.MetaborgException) TransformAction(org.metaborg.spoofax.core.action.TransformAction) ITransformAction(org.metaborg.core.action.ITransformAction) HybridInterpreter(org.strategoxt.HybridInterpreter) TransformException(org.metaborg.core.transform.TransformException) IStrategoTuple(org.spoofax.interpreter.terms.IStrategoTuple) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoList(org.spoofax.interpreter.terms.IStrategoList) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) Timer(org.metaborg.util.time.Timer) FileObject(org.apache.commons.vfs2.FileObject) TransformContrib(org.metaborg.spoofax.core.unit.TransformContrib) ILanguageComponent(org.metaborg.core.language.ILanguageComponent)

Example 17 with IStrategoList

use of org.spoofax.interpreter.terms.IStrategoList 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)

Aggregations

IStrategoList (org.spoofax.interpreter.terms.IStrategoList)17 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)16 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)12 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)8 ICompletion (org.metaborg.core.completion.ICompletion)7 IToken (org.spoofax.jsglr.client.imploder.IToken)7 ITokens (org.spoofax.jsglr.client.imploder.ITokens)5 ListImploderAttachment (org.spoofax.jsglr.client.imploder.ListImploderAttachment)5 StrategoTerm (org.spoofax.terms.StrategoTerm)5 Nullable (javax.annotation.Nullable)4 FileObject (org.apache.commons.vfs2.FileObject)4 Completion (org.metaborg.core.completion.Completion)4 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)4 IStrategoTuple (org.spoofax.interpreter.terms.IStrategoTuple)4 ImploderAttachment (org.spoofax.jsglr.client.imploder.ImploderAttachment)4 HybridInterpreter (org.strategoxt.HybridInterpreter)4 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)3 MetaborgException (org.metaborg.core.MetaborgException)2 CompletionKind (org.metaborg.core.completion.CompletionKind)2 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)2