Search in sources :

Example 6 with SpoofaxAnalyzeResults

use of org.metaborg.spoofax.core.analysis.SpoofaxAnalyzeResults 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)

Aggregations

MetaborgException (org.metaborg.core.MetaborgException)6 AnalysisException (org.metaborg.core.analysis.AnalysisException)6 ISpoofaxAnalyzeResults (org.metaborg.spoofax.core.analysis.ISpoofaxAnalyzeResults)6 SpoofaxAnalyzeResults (org.metaborg.spoofax.core.analysis.SpoofaxAnalyzeResults)6 ISpoofaxParseUnit (org.metaborg.spoofax.core.unit.ISpoofaxParseUnit)5 HybridInterpreter (org.strategoxt.HybridInterpreter)5 ISpoofaxAnalyzeUnit (org.metaborg.spoofax.core.unit.ISpoofaxAnalyzeUnit)4 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)3 AnalysisFacet (org.metaborg.spoofax.core.analysis.AnalysisFacet)3 Lists (com.google.common.collect.Lists)2 Sets (com.google.common.collect.Sets)2 Inject (com.google.inject.Inject)2 Collection (java.util.Collection)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 IMessages (mb.nabl2.solver.messages.IMessages)2