Search in sources :

Example 6 with AnalysisException

use of org.metaborg.core.analysis.AnalysisException in project spoofax by metaborg.

the class StrategoAnalyzer method analyzeAll.

@Override
public ISpoofaxAnalyzeResults analyzeAll(Iterable<ISpoofaxParseUnit> inputs, IContext context, IProgress progress, ICancel cancel) throws AnalysisException, InterruptedException {
    cancel.throwIfCancelled();
    final ILanguageImpl language = context.language();
    final ITermFactory termFactory = termFactoryService.getGeneric();
    final FacetContribution<AnalysisFacet> facetContribution = language.facetContribution(AnalysisFacet.class);
    if (facetContribution == null) {
        logger.debug("No analysis required for {}", language);
        return new SpoofaxAnalyzeResults(context);
    }
    final AnalysisFacet facet = facetContribution.facet;
    cancel.throwIfCancelled();
    final HybridInterpreter runtime;
    try {
        runtime = runtimeService.runtime(facetContribution.contributor, context, true);
    } catch (MetaborgException e) {
        throw new AnalysisException(context, "Failed to get Stratego runtime", e);
    }
    final int size = Iterables.size(inputs);
    progress.setWorkRemaining(size);
    final Collection<ISpoofaxAnalyzeUnit> results = Lists.newArrayListWithCapacity(size);
    for (ISpoofaxParseUnit input : inputs) {
        cancel.throwIfCancelled();
        if (!input.valid()) {
            logger.warn("Parse input for {} is invalid, cannot analyze", input.source());
            // TODO: throw exception instead?
            progress.work(1);
            continue;
        }
        final ISpoofaxAnalyzeUnit result = analyze(input, context, runtime, facet.strategyName, termFactory);
        results.add(result);
        progress.work(1);
    }
    return new SpoofaxAnalyzeResults(results, context);
}
Also used : ISpoofaxAnalyzeResults(org.metaborg.spoofax.core.analysis.ISpoofaxAnalyzeResults) SpoofaxAnalyzeResults(org.metaborg.spoofax.core.analysis.SpoofaxAnalyzeResults) ISpoofaxParseUnit(org.metaborg.spoofax.core.unit.ISpoofaxParseUnit) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) MetaborgException(org.metaborg.core.MetaborgException) HybridInterpreter(org.strategoxt.HybridInterpreter) AnalysisException(org.metaborg.core.analysis.AnalysisException) AnalysisFacet(org.metaborg.spoofax.core.analysis.AnalysisFacet) ITermFactory(org.spoofax.interpreter.terms.ITermFactory) ISpoofaxAnalyzeUnit(org.metaborg.spoofax.core.unit.ISpoofaxAnalyzeUnit)

Example 7 with AnalysisException

use of org.metaborg.core.analysis.AnalysisException in project spoofax by metaborg.

the class AnalysisResultProcessor method request.

@Override
public Observable<A> request(final I input, final IContext context) {
    if (input.detached()) {
        throw new MetaborgRuntimeException("Cannot request updates for detached (no source) units");
    }
    final FileObject resource = input.source();
    return Observable.create(new OnSubscribe<A>() {

        @Override
        public void call(Subscriber<? super A> observer) {
            if (observer.isUnsubscribed()) {
                logger.trace("Unsubscribed from analysis result request for {}", resource);
                return;
            }
            final BehaviorSubject<AnalysisChange<A>> updates = getUpdates(input, context);
            final AnalysisChange<A> update = updates.toBlocking().first(new Func1<AnalysisChange<A>, Boolean>() {

                @Override
                public Boolean call(AnalysisChange<A> updateToFilter) {
                    final UpdateKind kind = updateToFilter.kind;
                    return kind != UpdateKind.Invalidate;
                }
            });
            if (observer.isUnsubscribed()) {
                logger.trace("Unsubscribed from analysis result request for {}", resource);
                return;
            }
            switch(update.kind) {
                case Update:
                    logger.trace("Returning cached analysis result for {}", resource);
                    observer.onNext(update.result);
                    observer.onCompleted();
                    break;
                case Error:
                    logger.trace("Returning analysis error for {}", resource);
                    observer.onError(update.exception);
                    break;
                case Remove:
                    {
                        final String message = String.format("Analysis result for %s was removed unexpectedly", resource);
                        logger.error(message);
                        observer.onError(new AnalysisException(context, message));
                        break;
                    }
                default:
                    {
                        final String message = String.format("Unexpected analysis update kind %s for %s", update.kind, resource);
                        logger.error(message);
                        observer.onError(new MetaborgRuntimeException(message));
                        break;
                    }
            }
        }
    });
}
Also used : MetaborgRuntimeException(org.metaborg.core.MetaborgRuntimeException) BehaviorSubject(rx.subjects.BehaviorSubject) UpdateKind(org.metaborg.core.build.UpdateKind) AnalysisException(org.metaborg.core.analysis.AnalysisException) FileObject(org.apache.commons.vfs2.FileObject) Func1(rx.functions.Func1)

Example 8 with AnalysisException

use of org.metaborg.core.analysis.AnalysisException in project spoofax by metaborg.

the class Builder method analyze.

private Multimap<IContext, A> analyze(BuildInput input, ILanguageImpl langImpl, FileObject location, Multimap<IContext, P> sourceParseUnits, Iterable<P> includeParseUnits, boolean pardoned, Collection<AU> analyzeUpdates, Set<FileName> removedResources, Collection<IMessage> extraMessages, RefBool success, IProgress progress, ICancel cancel) throws InterruptedException {
    final int size = sourceParseUnits.size() + Iterables.size(includeParseUnits);
    final Multimap<IContext, A> allAnalyzeUnits = ArrayListMultimap.create();
    if (size == 0) {
        return allAnalyzeUnits;
    }
    final Set<Entry<IContext, Collection<P>>> toAnalyze = sourceParseUnits.asMap().entrySet();
    final int toAnalyzeSize = toAnalyze.size();
    progress.setWorkRemaining(toAnalyzeSize);
    progress.setDescription("Analyzing " + size + " file(s) of " + langImpl.belongsTo().name());
    logger.debug("Analyzing {} parse results in {} context(s)", size, toAnalyzeSize);
    for (Entry<IContext, Collection<P>> entry : toAnalyze) {
        cancel.throwIfCancelled();
        final IContext context = entry.getKey();
        final Iterable<P> parseResults = Iterables.concat(entry.getValue(), includeParseUnits);
        try {
            try (IClosableLock lock = context.write()) {
                analysisResultUpdater.invalidate(parseResults);
                final IAnalyzeResults<A, AU> results = analysisService.analyzeAll(parseResults, context, progress.subProgress(1), cancel);
                for (A result : results.results()) {
                    cancel.throwIfCancelled();
                    final boolean noErrors = printMessages(result.messages(), "Analysis", input, pardoned);
                    success.and(noErrors);
                    analysisResultUpdater.update(result, removedResources);
                    allAnalyzeUnits.put(context, result);
                }
                analyzeUpdates.addAll(results.updates());
            } finally {
                context.persist();
            }
        } catch (AnalysisException e) {
            final String message = "Analysis failed unexpectedly";
            final boolean noErrors = printMessage(message, e, input, pardoned);
            success.and(noErrors);
            analysisResultUpdater.error(parseResults, e);
            extraMessages.add(MessageFactory.newAnalysisErrorAtTop(location, message, e));
        } catch (IOException e) {
            final String message = "Persisting analysis data failed unexpectedly";
            final boolean noErrors = printMessage(message, e, input, pardoned);
            success.and(noErrors);
            extraMessages.add(MessageFactory.newAnalysisErrorAtTop(location, message, e));
        }
    }
    return allAnalyzeUnits;
}
Also used : IContext(org.metaborg.core.context.IContext) IOException(java.io.IOException) Entry(java.util.Map.Entry) AnalysisException(org.metaborg.core.analysis.AnalysisException) Collection(java.util.Collection) IClosableLock(org.metaborg.util.concurrent.IClosableLock)

Example 9 with AnalysisException

use of org.metaborg.core.analysis.AnalysisException in project spoofax by metaborg.

the class StrategoAnalyzer method analyze.

private ISpoofaxAnalyzeUnit analyze(ISpoofaxParseUnit input, IContext context, HybridInterpreter runtime, String strategy, ITermFactory termFactory) throws AnalysisException {
    final FileObject source = input.source();
    final IStrategoString contextPath = strategoCommon.locationTerm(context.location());
    final IStrategoString resourcePath = strategoCommon.resourceTerm(source, context.location());
    final IStrategoTuple inputTerm = termFactory.makeTuple(input.ast(), resourcePath, contextPath);
    try {
        logger.trace("Analysing {}", source);
        final Timer timer = new Timer(true);
        final IStrategoTerm resultTerm = strategoCommon.invoke(runtime, inputTerm, strategy);
        final long duration = timer.stop();
        if (resultTerm == null) {
            logger.trace("Analysis for {} failed", source);
            return result(analysisCommon.analysisFailedMessage(runtime), input, context, null, duration);
        } else if (!(resultTerm instanceof IStrategoTuple)) {
            logger.trace("Analysis for {} has unexpected result, not a tuple", source);
            final String message = logger.format("Unexpected results from analysis {}", resultTerm);
            return result(message, input, context, null, duration);
        } else if (resultTerm.getSubtermCount() == 4) {
            logger.trace("Analysis for {} done", source);
            return result(resultTerm, input, context, duration);
        } else if (resultTerm.getSubtermCount() == 3) {
            logger.trace("Analysis for {} done", source);
            return resultNoAst(resultTerm, input, context, duration);
        } else {
            logger.trace("Analysis for {} has unexpected result; tuple with more than 4 or less than 2 elements", source);
            final String message = logger.format("Unexpected results from analysis {}", resultTerm);
            return result(message, input, context, null, duration);
        }
    } catch (MetaborgException e) {
        final String message = logger.format("Analysis for {} failed", source);
        logger.trace(message, e);
        throw new AnalysisException(context, message, e);
    }
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) Timer(org.metaborg.util.time.Timer) MetaborgException(org.metaborg.core.MetaborgException) AnalysisException(org.metaborg.core.analysis.AnalysisException) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoTuple(org.spoofax.interpreter.terms.IStrategoTuple) FileObject(org.apache.commons.vfs2.FileObject) IStrategoString(org.spoofax.interpreter.terms.IStrategoString)

Example 10 with AnalysisException

use of org.metaborg.core.analysis.AnalysisException in project spoofax by metaborg.

the class TaskEngineAnalyzer method analyzeAll.

@Override
public ISpoofaxAnalyzeResults analyzeAll(Iterable<ISpoofaxParseUnit> inputs, IContext context, IProgress progress, ICancel cancel) throws AnalysisException, InterruptedException {
    cancel.throwIfCancelled();
    final ILanguageImpl langImpl = context.language();
    final ITermFactory termFactory = termFactoryService.getGeneric();
    final FacetContribution<AnalysisFacet> facetContribution = langImpl.facetContribution(AnalysisFacet.class);
    if (facetContribution == null) {
        logger.debug("No analysis required for {}", langImpl);
        return new SpoofaxAnalyzeResults(context);
    }
    final AnalysisFacet facet = facetContribution.facet;
    cancel.throwIfCancelled();
    final HybridInterpreter runtime;
    try {
        runtime = runtimeService.runtime(facetContribution.contributor, context, false);
    } catch (MetaborgException e) {
        throw new AnalysisException(context, "Failed to get Stratego runtime", e);
    }
    cancel.throwIfCancelled();
    return analyzeAll(inputs, context, runtime, facet.strategyName, termFactory);
}
Also used : ISpoofaxAnalyzeResults(org.metaborg.spoofax.core.analysis.ISpoofaxAnalyzeResults) SpoofaxAnalyzeResults(org.metaborg.spoofax.core.analysis.SpoofaxAnalyzeResults) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) MetaborgException(org.metaborg.core.MetaborgException) HybridInterpreter(org.strategoxt.HybridInterpreter) AnalysisException(org.metaborg.core.analysis.AnalysisException) AnalysisFacet(org.metaborg.spoofax.core.analysis.AnalysisFacet) ITermFactory(org.spoofax.interpreter.terms.ITermFactory)

Aggregations

AnalysisException (org.metaborg.core.analysis.AnalysisException)11 MetaborgException (org.metaborg.core.MetaborgException)8 ISpoofaxAnalyzeResults (org.metaborg.spoofax.core.analysis.ISpoofaxAnalyzeResults)6 SpoofaxAnalyzeResults (org.metaborg.spoofax.core.analysis.SpoofaxAnalyzeResults)6 HybridInterpreter (org.strategoxt.HybridInterpreter)6 ISpoofaxAnalyzeUnit (org.metaborg.spoofax.core.unit.ISpoofaxAnalyzeUnit)5 ISpoofaxParseUnit (org.metaborg.spoofax.core.unit.ISpoofaxParseUnit)5 FileObject (org.apache.commons.vfs2.FileObject)4 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)4 AnalysisFacet (org.metaborg.spoofax.core.analysis.AnalysisFacet)4 Collection (java.util.Collection)3 Lists (com.google.common.collect.Lists)2 Sets (com.google.common.collect.Sets)2 Inject (com.google.inject.Inject)2 Map (java.util.Map)2 Optional (java.util.Optional)2 FixedPoint (mb.flowspec.runtime.solver.FixedPoint)2 NaBL2DebugConfig (mb.nabl2.config.NaBL2DebugConfig)2 ISolution (mb.nabl2.solver.ISolution)2 SolverException (mb.nabl2.solver.SolverException)2