Search in sources :

Example 1 with HybridInterpreter

use of org.strategoxt.HybridInterpreter in project spoofax by metaborg.

the class StrategoRuntimeService method createPrototype.

private HybridInterpreter createPrototype(ILanguageComponent component) throws MetaborgException {
    logger.debug("Creating prototype runtime for {}", component);
    final ITermFactory termFactory = termFactoryService.get(component, null, false);
    final HybridInterpreter runtime = createNew(termFactory);
    loadFiles(runtime, component);
    prototypes.put(component, runtime);
    return runtime;
}
Also used : HybridInterpreter(org.strategoxt.HybridInterpreter) ITermFactory(org.spoofax.interpreter.terms.ITermFactory)

Example 2 with HybridInterpreter

use of org.strategoxt.HybridInterpreter in project spoofax by metaborg.

the class StrategoRuntimeService method runtime.

@Override
public HybridInterpreter runtime(ILanguageComponent component, IContext context, boolean typesmart) throws MetaborgException {
    HybridInterpreter prototype = prototypes.get(component);
    if (prototype == null) {
        prototype = createPrototype(component);
    }
    final HybridInterpreter runtime = clone(prototype, context.location(), component, context.project(), typesmart);
    runtime.getContext().setContextObject(context);
    runtime.getCompiledContext().setContextObject(context);
    return runtime;
}
Also used : HybridInterpreter(org.strategoxt.HybridInterpreter)

Example 3 with HybridInterpreter

use of org.strategoxt.HybridInterpreter 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 4 with HybridInterpreter

use of org.strategoxt.HybridInterpreter in project spoofax by metaborg.

the class ResolverService method resolve.

@Override
public Resolution resolve(int offset, ISpoofaxAnalyzeUnit result) throws MetaborgException {
    if (!result.valid() || !result.hasAst()) {
        return null;
    }
    final FileObject source = result.source();
    final IContext context = result.context();
    final IProject project = context.project();
    final ILanguageImpl language = context.language();
    final FacetContribution<ResolverFacet> facetContrib = facet(language);
    final ResolverFacet facet = facetContrib.facet;
    final String strategy = facet.strategyName;
    try {
        final ITermFactory termFactory = termFactoryService.get(facetContrib.contributor, project, true);
        final HybridInterpreter interpreter = strategoRuntimeService.runtime(facetContrib.contributor, context, true);
        final Iterable<IStrategoTerm> inRegion = tracingService.fragments(result, new SourceRegion(offset));
        final TermWithRegion tuple;
        try (IClosableLock lock = context.read()) {
            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 : 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) TermWithRegion(org.metaborg.spoofax.core.tracing.TracingCommon.TermWithRegion) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) FileObject(org.apache.commons.vfs2.FileObject) IClosableLock(org.metaborg.util.concurrent.IClosableLock) ITermFactory(org.spoofax.interpreter.terms.ITermFactory)

Example 5 with HybridInterpreter

use of org.strategoxt.HybridInterpreter in project spoofax by metaborg.

the class AbstractConstraintAnalyzer method analyzeAll.

@SuppressWarnings("unchecked")
@Override
public ISpoofaxAnalyzeResults analyzeAll(Iterable<ISpoofaxParseUnit> inputs, IContext genericContext, IProgress progress, ICancel cancel) throws AnalysisException {
    C context;
    try {
        context = (C) genericContext;
    } catch (ClassCastException ex) {
        throw new AnalysisException(genericContext, "Scope graph context required for constraint analysis.", ex);
    }
    final ILanguageImpl langImpl = context.language();
    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;
    final HybridInterpreter runtime;
    try {
        runtime = runtimeService.runtime(facetContribution.contributor, context, false);
    } catch (MetaborgException e) {
        throw new AnalysisException(context, "Failed to get Stratego runtime", e);
    }
    Map<String, ISpoofaxParseUnit> changed = Maps.newHashMap();
    Set<String> removed = Sets.newHashSet();
    for (ISpoofaxParseUnit input : inputs) {
        final String source;
        if (input.detached()) {
            source = "detached-" + UUID.randomUUID().toString();
        } else {
            source = resource(input.source(), context);
        }
        if (!input.valid() || isEmptyAST(input.ast())) {
            removed.add(source);
        } else {
            changed.put(source, input);
        }
    }
    return analyzeAll(changed, removed, context, runtime, facet.strategyName, progress, cancel);
}
Also used : ISpoofaxAnalyzeResults(org.metaborg.spoofax.core.analysis.ISpoofaxAnalyzeResults) SpoofaxAnalyzeResults(org.metaborg.spoofax.core.analysis.SpoofaxAnalyzeResults) ISpoofaxParseUnit(org.metaborg.spoofax.core.unit.ISpoofaxParseUnit) MetaborgException(org.metaborg.core.MetaborgException) HybridInterpreter(org.strategoxt.HybridInterpreter) AnalysisFacet(org.metaborg.spoofax.core.analysis.AnalysisFacet) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) AnalysisException(org.metaborg.core.analysis.AnalysisException)

Aggregations

HybridInterpreter (org.strategoxt.HybridInterpreter)26 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)17 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)17 MetaborgException (org.metaborg.core.MetaborgException)14 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)13 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)12 FileObject (org.apache.commons.vfs2.FileObject)11 ICompletion (org.metaborg.core.completion.ICompletion)8 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)8 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)7 IStrategoTuple (org.spoofax.interpreter.terms.IStrategoTuple)7 AnalysisException (org.metaborg.core.analysis.AnalysisException)6 StrategoAppl (org.spoofax.terms.StrategoAppl)6 ISourceRegion (org.metaborg.core.source.ISourceRegion)5 SourceRegion (org.metaborg.core.source.SourceRegion)5 ISpoofaxAnalyzeResults (org.metaborg.spoofax.core.analysis.ISpoofaxAnalyzeResults)5 SpoofaxAnalyzeResults (org.metaborg.spoofax.core.analysis.SpoofaxAnalyzeResults)5 IContext (org.metaborg.core.context.IContext)4 IProject (org.metaborg.core.project.IProject)4 AnalysisFacet (org.metaborg.spoofax.core.analysis.AnalysisFacet)4