Search in sources :

Example 41 with MetaborgException

use of org.metaborg.core.MetaborgException in project spoofax by metaborg.

the class TaskEngineAnalyzer method analyzeAll.

private ISpoofaxAnalyzeResults analyzeAll(Iterable<ISpoofaxParseUnit> inputs, IContext context, HybridInterpreter runtime, String strategy, ITermFactory termFactory) throws AnalysisException {
    final Map<String, ISpoofaxParseUnit> inputsPerSource = Maps.newHashMap();
    int detachedCounter = 0;
    final Collection<IStrategoAppl> analysisInputs = Lists.newArrayList();
    for (ISpoofaxParseUnit input : inputs) {
        if (!input.valid()) {
            logger.warn("Parse result for {} is invalid, cannot analyze", input.source());
            continue;
        }
        final String pathString;
        if (input.detached()) {
            pathString = "detached-source-" + detachedCounter++;
            logger.debug("Parse input is detached, using '{}' as path", pathString);
        } else {
            pathString = input.source().getName().getURI();
        }
        inputsPerSource.put(pathString, input);
        final IStrategoString pathTerm = termFactory.makeString(pathString);
        final IStrategoReal durationTerm = termFactory.makeReal(input.duration());
        analysisInputs.add(termFactory.makeAppl(fileCons, pathTerm, input.ast(), durationTerm));
    }
    final IStrategoTerm inputTerm = termFactory.makeList(analysisInputs);
    logger.trace("Invoking {} strategy", strategy);
    final IStrategoTerm resultTerm;
    try {
        resultTerm = strategoCommon.invoke(runtime, inputTerm, strategy);
    } catch (MetaborgException e) {
        final String message = analysisCommon.analysisFailedMessage(runtime);
        throw new AnalysisException(context, message, e);
    }
    if (resultTerm == null) {
        final String message = analysisCommon.analysisFailedMessage(runtime);
        throw new AnalysisException(context, message);
    }
    if (!(resultTerm instanceof IStrategoAppl) || resultTerm.getSubtermCount() != 5) {
        final String message = logger.format("Unexpected results from analysis {}, expected 5-tuple", resultTerm);
        throw new AnalysisException(context, message);
    }
    final IStrategoTerm resultsTerm = resultTerm.getSubterm(0);
    final IStrategoTerm updateResultsTerm = resultTerm.getSubterm(1);
    final Collection<ISpoofaxAnalyzeUnit> fileResults = Lists.newArrayListWithCapacity(resultsTerm.getSubtermCount());
    for (IStrategoTerm result : resultsTerm) {
        // HACK: analysis duration per parse unit is unknown, pass -1 as duration.
        final ISpoofaxAnalyzeUnit fileResult = result(result, inputsPerSource, context, -1);
        if (fileResult == null) {
            continue;
        }
        fileResults.add(fileResult);
    }
    final Collection<ISpoofaxAnalyzeUnitUpdate> updateResults = Lists.newArrayListWithCapacity(updateResultsTerm.getSubtermCount());
    for (IStrategoTerm result : updateResultsTerm) {
        final ISpoofaxAnalyzeUnitUpdate updateResult = updateResult(result, context);
        if (updateResult == null) {
            continue;
        }
        updateResults.add(updateResult);
    }
    return new SpoofaxAnalyzeResults(fileResults, updateResults, context);
}
Also used : ISpoofaxParseUnit(org.metaborg.spoofax.core.unit.ISpoofaxParseUnit) ISpoofaxAnalyzeResults(org.metaborg.spoofax.core.analysis.ISpoofaxAnalyzeResults) SpoofaxAnalyzeResults(org.metaborg.spoofax.core.analysis.SpoofaxAnalyzeResults) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) ISpoofaxAnalyzeUnitUpdate(org.metaborg.spoofax.core.unit.ISpoofaxAnalyzeUnitUpdate) MetaborgException(org.metaborg.core.MetaborgException) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoReal(org.spoofax.interpreter.terms.IStrategoReal) AnalysisException(org.metaborg.core.analysis.AnalysisException) ISpoofaxAnalyzeUnit(org.metaborg.spoofax.core.unit.ISpoofaxAnalyzeUnit)

Example 42 with MetaborgException

use of org.metaborg.core.MetaborgException in project spoofax by metaborg.

the class HoverService method hover.

@Override
public Hover hover(int offset, ISpoofaxAnalyzeUnit result) throws MetaborgException {
    if (!result.valid() || !result.hasAst()) {
        return null;
    }
    final FileObject source = result.source();
    final IContext context = result.context();
    final ILanguageImpl language = context.language();
    final FacetContribution<HoverFacet> facetContrib = facet(language);
    final HoverFacet facet = facetContrib.facet;
    final String strategy = facet.strategyName;
    try {
        final IProject project = context.project();
        final ITermFactory termFactory = termFactoryService.get(facetContrib.contributor, project, true);
        final HybridInterpreter interpreter = strategoRuntimeService.runtime(facetContrib.contributor, context, true);
        final Iterable<IStrategoTerm> inRegion = tracingService.fragments(result, new SourceRegion(offset));
        final TermWithRegion tuple;
        try (IClosableLock lock = context.read()) {
            tuple = common.outputs(termFactory, interpreter, context.location(), source, result.ast(), inRegion, strategy);
        }
        return hover(tuple);
    } catch (MetaborgException e) {
        throw new MetaborgException("Getting hover tooltip information failed unexpectedly", e);
    }
}
Also used : IContext(org.metaborg.core.context.IContext) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) MetaborgException(org.metaborg.core.MetaborgException) HybridInterpreter(org.strategoxt.HybridInterpreter) ISourceRegion(org.metaborg.core.source.ISourceRegion) SourceRegion(org.metaborg.core.source.SourceRegion) IProject(org.metaborg.core.project.IProject) TermWithRegion(org.metaborg.spoofax.core.tracing.TracingCommon.TermWithRegion) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) FileObject(org.apache.commons.vfs2.FileObject) IClosableLock(org.metaborg.util.concurrent.IClosableLock) ITermFactory(org.spoofax.interpreter.terms.ITermFactory)

Example 43 with MetaborgException

use of org.metaborg.core.MetaborgException in project spoofax by metaborg.

the class ResolverService method resolve.

@Override
public Resolution resolve(int offset, ISpoofaxParseUnit result) throws MetaborgException {
    if (!result.valid()) {
        return null;
    }
    final FileObject source = result.source();
    final IProject project = projectService.get(source);
    final ILanguageImpl langImpl = result.input().langImpl();
    @Nullable IContext context;
    if (project == null) {
        context = null;
    } else {
        try {
            context = contextService.get(source, project, langImpl);
        } catch (ContextException | MetaborgRuntimeException e) {
            // Failed to get a context, ignore and use the source file to get a stratego runtime later.
            context = null;
        }
    }
    final FacetContribution<ResolverFacet> facetContrib = facet(langImpl);
    final ResolverFacet facet = facetContrib.facet;
    final ILanguageComponent contributor = facetContrib.contributor;
    final String strategy = facet.strategyName;
    try {
        final ITermFactory termFactory = termFactoryService.get(contributor, project, true);
        final HybridInterpreter interpreter;
        if (context == null) {
            interpreter = strategoRuntimeService.runtime(contributor, source, true);
        } else {
            interpreter = strategoRuntimeService.runtime(contributor, context, true);
        }
        final Iterable<IStrategoTerm> inRegion = tracingService.fragments(result, new SourceRegion(offset));
        final TermWithRegion tuple = common.outputs(termFactory, interpreter, source, source, result.ast(), inRegion, strategy);
        return resolve(tuple);
    } catch (MetaborgException e) {
        throw new MetaborgException("Reference resolution failed", e);
    }
}
Also used : MetaborgRuntimeException(org.metaborg.core.MetaborgRuntimeException) IContext(org.metaborg.core.context.IContext) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) MetaborgException(org.metaborg.core.MetaborgException) HybridInterpreter(org.strategoxt.HybridInterpreter) ISourceRegion(org.metaborg.core.source.ISourceRegion) SourceRegion(org.metaborg.core.source.SourceRegion) IProject(org.metaborg.core.project.IProject) ContextException(org.metaborg.core.context.ContextException) TermWithRegion(org.metaborg.spoofax.core.tracing.TracingCommon.TermWithRegion) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) FileObject(org.apache.commons.vfs2.FileObject) ILanguageComponent(org.metaborg.core.language.ILanguageComponent) ITermFactory(org.spoofax.interpreter.terms.ITermFactory) Nullable(javax.annotation.Nullable)

Example 44 with MetaborgException

use of org.metaborg.core.MetaborgException in project spoofax by metaborg.

the class StrategoTransformer method output.

private TransformOutput output(IStrategoTerm resourceTerm, IStrategoTerm contentTerm, FileObject location, ITransformConfig config) throws MetaborgException {
    if (!(resourceTerm instanceof IStrategoString)) {
        throw new MetaborgException("First term of result tuple {} is not a string, cannot write output file");
    } else {
        final String resourceString = Tools.asJavaString(resourceTerm);
        final String resultContents = common.toString(contentTerm);
        // writing to output file is allowed
        FileObject output;
        if (!config.dryRun()) {
            output = resourceService.resolve(location, resourceString);
            try (OutputStream stream = output.getContent().getOutputStream()) {
                IOUtils.write(resultContents, stream, Charset.defaultCharset());
            } catch (IOException e) {
                logger.error("Error occurred while writing output file", e);
            }
        } else {
            output = null;
        }
        return new TransformOutput(resourceString, output, contentTerm);
    }
}
Also used : TransformOutput(org.metaborg.spoofax.core.unit.TransformOutput) MetaborgException(org.metaborg.core.MetaborgException) OutputStream(java.io.OutputStream) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) FileObject(org.apache.commons.vfs2.FileObject) IOException(java.io.IOException)

Example 45 with MetaborgException

use of org.metaborg.core.MetaborgException 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)

Aggregations

MetaborgException (org.metaborg.core.MetaborgException)49 FileObject (org.apache.commons.vfs2.FileObject)25 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)19 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)17 HybridInterpreter (org.strategoxt.HybridInterpreter)14 IProject (org.metaborg.core.project.IProject)12 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)11 IOException (java.io.IOException)9 IContext (org.metaborg.core.context.IContext)9 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)9 Nullable (javax.annotation.Nullable)8 AnalysisException (org.metaborg.core.analysis.AnalysisException)8 FileSystemException (org.apache.commons.vfs2.FileSystemException)7 MetaborgRuntimeException (org.metaborg.core.MetaborgRuntimeException)7 ISpoofaxAnalyzeResults (org.metaborg.spoofax.core.analysis.ISpoofaxAnalyzeResults)6 SpoofaxAnalyzeResults (org.metaborg.spoofax.core.analysis.SpoofaxAnalyzeResults)6 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)6 ILanguage (org.metaborg.core.language.ILanguage)5 ISourceRegion (org.metaborg.core.source.ISourceRegion)5 ISpoofaxParseUnit (org.metaborg.spoofax.core.unit.ISpoofaxParseUnit)5