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);
}
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;
}
}
}
});
}
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;
}
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);
}
}
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);
}
Aggregations