Search in sources :

Example 51 with IStrategoTerm

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

the class OutlineService method outline.

@Override
public IOutline outline(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<OutlineFacet> facetContrib = facet(langImpl);
    final OutlineFacet facet = facetContrib.facet;
    final ILanguageComponent contributor = facetContrib.contributor;
    final String strategy = facet.strategyName;
    try {
        final HybridInterpreter interpreter;
        if (context == null) {
            interpreter = strategoRuntimeService.runtime(contributor, source, true);
        } else {
            interpreter = strategoRuntimeService.runtime(contributor, context, true);
        }
        final IStrategoTerm input = common.builderInputTerm(result.ast(), source, source);
        final IStrategoTerm outlineTerm = common.invoke(interpreter, input, strategy);
        if (outlineTerm == null) {
            return null;
        }
        final IOutline outline = toOutline(outlineTerm, facet.expandTo, contributor.location());
        return outline;
    } catch (MetaborgException e) {
        throw new MetaborgException("Creating outline failed", e);
    }
}
Also used : MetaborgRuntimeException(org.metaborg.core.MetaborgRuntimeException) IContext(org.metaborg.core.context.IContext) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IOutline(org.metaborg.core.outline.IOutline) MetaborgException(org.metaborg.core.MetaborgException) HybridInterpreter(org.strategoxt.HybridInterpreter) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IProject(org.metaborg.core.project.IProject) ContextException(org.metaborg.core.context.ContextException) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) FileObject(org.apache.commons.vfs2.FileObject) ILanguageComponent(org.metaborg.core.language.ILanguageComponent) Nullable(javax.annotation.Nullable)

Example 52 with IStrategoTerm

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

the class ASpoofaxPrimitive method call.

@Override
public boolean call(IContext context, Strategy[] svars, IStrategoTerm[] tvars) throws InterpreterException {
    final IStrategoTerm current = context.current();
    final ITermFactory factory = context.getFactory();
    try {
        final IStrategoTerm newCurrent = call(current, svars, tvars, factory, context);
        if (newCurrent != null) {
            context.setCurrent(newCurrent);
            return true;
        }
    } catch (MetaborgException | IOException e) {
        throw new InterpreterException("Executing primitive " + name + " failed unexpectedly", e);
    }
    return false;
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) InterpreterException(org.spoofax.interpreter.core.InterpreterException) MetaborgException(org.metaborg.core.MetaborgException) IOException(java.io.IOException) ITermFactory(org.spoofax.interpreter.terms.ITermFactory)

Example 53 with IStrategoTerm

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

the class Sdf2ParenthesizeStamper method stampOf.

@Override
public Stamp stampOf(File file) {
    if (!FileCommands.exists(file)) {
        return new ValueStamp<>(this, null);
    }
    final IStrategoTerm term;
    try {
        term = context.parse(file);
    } catch (ParseException | IOException e) {
        return LastModifiedStamper.instance.stampOf(file);
    }
    if (term == null) {
        return LastModifiedStamper.instance.stampOf(file);
    }
    final ParenExtractor parenExtractor = new ParenExtractor(context.termFactory());
    parenExtractor.visit(term);
    return new ValueStamp<>(this, Pair.create(parenExtractor.getRelevantProds(), parenExtractor.getPriorities()));
}
Also used : ValueStamp(build.pluto.stamp.ValueStamp) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) ParseException(org.metaborg.core.syntax.ParseException) IOException(java.io.IOException)

Example 54 with IStrategoTerm

use of org.spoofax.interpreter.terms.IStrategoTerm 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 55 with IStrategoTerm

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

IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)82 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)31 FileObject (org.apache.commons.vfs2.FileObject)27 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)25 MetaborgException (org.metaborg.core.MetaborgException)19 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)17 HybridInterpreter (org.strategoxt.HybridInterpreter)17 IStrategoList (org.spoofax.interpreter.terms.IStrategoList)16 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)15 Nullable (javax.annotation.Nullable)14 ISourceRegion (org.metaborg.core.source.ISourceRegion)12 ICompletion (org.metaborg.core.completion.ICompletion)11 IStrategoTuple (org.spoofax.interpreter.terms.IStrategoTuple)10 IOException (java.io.IOException)9 IProject (org.metaborg.core.project.IProject)9 IMessage (org.metaborg.core.messages.IMessage)8 IToken (org.spoofax.jsglr.client.imploder.IToken)8 IContext (org.metaborg.core.context.IContext)7 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)7 ListImploderAttachment (org.spoofax.jsglr.client.imploder.ListImploderAttachment)7