Search in sources :

Example 1 with ContextException

use of org.metaborg.core.context.ContextException in project spoofax by metaborg.

the class HoverService method hover.

@Override
public Hover hover(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<HoverFacet> facetContrib = facet(langImpl);
    final HoverFacet 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, 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 : 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 2 with ContextException

use of org.metaborg.core.context.ContextException in project spoofax by metaborg.

the class Builder method updateLanguageResources.

private Collection<FileObject> updateLanguageResources(BuildInput input, ILanguageImpl language, LanguageBuildDiff diff, IBuildOutputInternal<P, A, AU, T> output, boolean pardoned, IProgress progress, ICancel cancel) throws InterruptedException {
    cancel.throwIfCancelled();
    final boolean analyze = input.analyze && analysisService.available(language);
    final boolean transform = input.transform;
    progress.setWorkRemaining(10 + (analyze ? 45 : 0) + (transform ? 45 : 0));
    final Iterable<IdentifiedResourceChange> sourceChanges = diff.sourceChanges;
    final Iterable<IdentifiedResourceChange> includeChanges = diff.includeChanges;
    final Set<FileName> includes = Sets.newHashSet();
    for (IdentifiedResourceChange includeChange : includeChanges) {
        includes.add(includeChange.change.resource.getName());
    }
    final FileObject location = input.project.location();
    final Collection<FileObject> changedSources = Sets.newHashSet();
    final Set<FileName> removedResources = Sets.newHashSet();
    final Collection<IMessage> extraMessages = Lists.newLinkedList();
    final RefBool success = new RefBool(true);
    logger.info("Building {} sources, {} includes of {}", Iterables.size(sourceChanges), Iterables.size(includeChanges), language);
    // Parse
    cancel.throwIfCancelled();
    final Collection<P> sourceParseUnits = parse(input, language, sourceChanges, pardoned, changedSources, removedResources, extraMessages, success, progress.subProgress(5), cancel);
    // GTODO: when a new context is created, all include files need to be parsed and analyzed in that context, this
    // approach does not do that!
    final Collection<P> includeParseUnits = parse(input, language, includeChanges, pardoned, changedSources, removedResources, extraMessages, success, progress.subProgress(5), cancel);
    final Iterable<P> allParseResults = Iterables.concat(sourceParseUnits, includeParseUnits);
    // Analyze
    cancel.throwIfCancelled();
    final Multimap<IContext, A> allAnalyzeUnits;
    final Collection<AU> allAnalyzeUpdates = Lists.newArrayList();
    if (analyze) {
        // Segregate by context
        final Multimap<IContext, P> parseUnitsPerContext = ArrayListMultimap.create();
        for (P parseResult : sourceParseUnits) {
            cancel.throwIfCancelled();
            final FileObject resource = parseResult.source();
            final ILanguageImpl langImpl = parseResult.input().langImpl();
            try {
                if (contextService.available(langImpl)) {
                    final IContext context = contextService.get(resource, input.project, langImpl);
                    parseUnitsPerContext.put(context, parseResult);
                }
            } catch (ContextException e) {
                final String message = String.format("Failed to retrieve context for parse result of %s", resource);
                printMessage(resource, message, e, input, pardoned);
                extraMessages.add(MessageFactory.newAnalysisErrorAtTop(resource, "Failed to retrieve context", e));
            }
        }
        // Run analysis
        cancel.throwIfCancelled();
        allAnalyzeUnits = analyze(input, language, location, parseUnitsPerContext, includeParseUnits, pardoned, allAnalyzeUpdates, removedResources, extraMessages, success, progress.subProgress(45), cancel);
    } else {
        allAnalyzeUnits = ArrayListMultimap.create();
    }
    // Transform
    cancel.throwIfCancelled();
    final Collection<T> allTransformUnits;
    if (transform) {
        allTransformUnits = transform(input, language, location, allAnalyzeUnits, includes, pardoned, removedResources, extraMessages, success, progress.subProgress(45), cancel);
    } else {
        allTransformUnits = Lists.newLinkedList();
    }
    printMessages(extraMessages, "Something", input, pardoned);
    output.add(success.get(), removedResources, includes, changedSources, allParseResults, allAnalyzeUnits.values(), allAnalyzeUpdates, allTransformUnits, extraMessages);
    final Collection<FileObject> newResources = Lists.newArrayList();
    for (T transformUnit : allTransformUnits) {
        for (ITransformOutput transformOutput : transformUnit.outputs()) {
            final FileObject outputFile = transformOutput.output();
            if (outputFile != null) {
                newResources.add(outputFile);
            }
        }
    }
    return newResources;
}
Also used : ITransformOutput(org.metaborg.core.transform.ITransformOutput) IContext(org.metaborg.core.context.IContext) FileName(org.apache.commons.vfs2.FileName) IdentifiedResourceChange(org.metaborg.core.resource.IdentifiedResourceChange) ContextException(org.metaborg.core.context.ContextException) FileObject(org.apache.commons.vfs2.FileObject) IMessage(org.metaborg.core.messages.IMessage) RefBool(org.metaborg.util.RefBool) ILanguageImpl(org.metaborg.core.language.ILanguageImpl)

Example 3 with ContextException

use of org.metaborg.core.context.ContextException 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 4 with ContextException

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

Aggregations

FileObject (org.apache.commons.vfs2.FileObject)4 ContextException (org.metaborg.core.context.ContextException)4 IContext (org.metaborg.core.context.IContext)4 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)4 Nullable (javax.annotation.Nullable)3 MetaborgException (org.metaborg.core.MetaborgException)3 MetaborgRuntimeException (org.metaborg.core.MetaborgRuntimeException)3 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)3 IProject (org.metaborg.core.project.IProject)3 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)3 HybridInterpreter (org.strategoxt.HybridInterpreter)3 ISourceRegion (org.metaborg.core.source.ISourceRegion)2 SourceRegion (org.metaborg.core.source.SourceRegion)2 TermWithRegion (org.metaborg.spoofax.core.tracing.TracingCommon.TermWithRegion)2 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)2 FileName (org.apache.commons.vfs2.FileName)1 IMessage (org.metaborg.core.messages.IMessage)1 IOutline (org.metaborg.core.outline.IOutline)1 IdentifiedResourceChange (org.metaborg.core.resource.IdentifiedResourceChange)1 ITransformOutput (org.metaborg.core.transform.ITransformOutput)1