Search in sources :

Example 1 with IProject

use of org.metaborg.core.project.IProject in project spoofax by metaborg.

the class LanguageIncludeFilesPrimitive method call.

@Override
protected IStrategoTerm call(IStrategoTerm current, Strategy[] svars, IStrategoTerm[] tvars, ITermFactory factory, IContext context) throws MetaborgException {
    if (!Tools.isTermString(tvars[0])) {
        return null;
    }
    final IProject project = projectService.get(context.location());
    if (project == null) {
        return factory.makeList();
    }
    // GTODO: require language identifier instead of language name
    final String languageName = Tools.asJavaString(tvars[0]);
    final ILanguage language = languageService.getLanguage(languageName);
    if (language == null) {
        final String message = String.format("Getting include files for %s failed, language could not be found", languageName);
        throw new MetaborgException(message);
    }
    final ILanguageImpl impl = language.activeImpl();
    if (impl == null) {
        final String message = String.format("Getting include files for %s failed, no active language implementation could be found", languageName);
        throw new MetaborgException(message);
    }
    final Iterable<IdentifiedResource> includeFiles = languagePathService.includeFiles(project, impl);
    final List<IStrategoTerm> terms = Lists.newArrayList();
    for (IdentifiedResource includeFile : includeFiles) {
        terms.add(factory.makeString(includeFile.resource.getName().getURI()));
    }
    return factory.makeList(terms);
}
Also used : ILanguage(org.metaborg.core.language.ILanguage) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) MetaborgException(org.metaborg.core.MetaborgException) IdentifiedResource(org.metaborg.core.language.IdentifiedResource) IProject(org.metaborg.core.project.IProject)

Example 2 with IProject

use of org.metaborg.core.project.IProject in project spoofax by metaborg.

the class RelativeSourceOrIncludePath method call.

@Override
protected IStrategoTerm call(IStrategoTerm current, Strategy[] svars, IStrategoTerm[] tvars, ITermFactory factory, IContext context) {
    if (!Tools.isTermString(tvars[0])) {
        return null;
    }
    if (!Tools.isTermString(current)) {
        return null;
    }
    final String path = Tools.asJavaString(current);
    final FileObject resource = resourceService.resolve(context.project().location(), path);
    FileObject base = context.location();
    final IProject project = projectService.get(context.location());
    if (project != null) {
        // GTODO: require language identifier instead of language name
        final String languageName = Tools.asJavaString(tvars[0]);
        final Iterable<FileObject> sourceLocations = languagePathService.sourceAndIncludePaths(project, languageName);
        for (FileObject sourceLocation : sourceLocations) {
            if (sourceLocation.getName().isDescendent(resource.getName())) {
                base = sourceLocation;
                break;
            }
        }
    }
    final String relativePath = ResourceUtils.relativeName(resource.getName(), base.getName(), true);
    return factory.makeString(relativePath);
}
Also used : FileObject(org.apache.commons.vfs2.FileObject) IProject(org.metaborg.core.project.IProject)

Example 3 with IProject

use of org.metaborg.core.project.IProject in project spoofax by metaborg.

the class CLIUtils method getOrCreateProject.

/**
 * Get or create project for the given resource
 */
public IProject getOrCreateProject(FileObject resource) throws MetaborgException {
    final ISimpleProjectService projectService = spoofax.injector.getInstance(ISimpleProjectService.class);
    final IProject project = projectService.get(resource);
    if (project == null) {
        return projectService.create(resource);
    }
    return project;
}
Also used : ISimpleProjectService(org.metaborg.core.project.ISimpleProjectService) IProject(org.metaborg.core.project.IProject)

Example 4 with IProject

use of org.metaborg.core.project.IProject in project spoofax by metaborg.

the class LegacyForeignCallPrimitive method call.

@Override
protected IStrategoTerm call(IStrategoTerm current, Strategy[] svars, IStrategoTerm[] tvars, ITermFactory factory, IContext currentContext) throws MetaborgException {
    final String languageName = Tools.asJavaString(tvars[0]);
    final String strategyName = Tools.asJavaString(tvars[1]);
    // GTODO: require language identifier instead of language name
    final ILanguage language = languageService.getLanguage(languageName);
    if (language == null) {
        final String message = String.format("Stratego strategy call of '%s' into language %s failed, language could not be found", strategyName, languageName);
        throw new MetaborgException(message);
    }
    final ILanguageImpl activeImpl = language.activeImpl();
    if (activeImpl == null) {
        final String message = String.format("Stratego strategy call of '%s' into language %s failed, no active language implementation could be found", strategyName, languageName);
        throw new MetaborgException(message);
    }
    try {
        final IProject project = projectService.get(currentContext.location());
        IContext context = contextService.get(currentContext.location(), project, activeImpl);
        return common.invoke(activeImpl, context, current, strategyName);
    } catch (MetaborgException e) {
        final String message = String.format("Stratego strategy call of '%s' into language %s failed unexpectedly", strategyName, languageName);
        throw new MetaborgException(message, e);
    }
}
Also used : ILanguage(org.metaborg.core.language.ILanguage) IContext(org.metaborg.core.context.IContext) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) MetaborgException(org.metaborg.core.MetaborgException) IProject(org.metaborg.core.project.IProject)

Example 5 with IProject

use of org.metaborg.core.project.IProject 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)

Aggregations

IProject (org.metaborg.core.project.IProject)21 FileObject (org.apache.commons.vfs2.FileObject)13 MetaborgException (org.metaborg.core.MetaborgException)12 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)9 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)8 IContext (org.metaborg.core.context.IContext)6 HybridInterpreter (org.strategoxt.HybridInterpreter)6 ConfigException (org.metaborg.core.config.ConfigException)5 ISpoofaxLanguageSpec (org.metaborg.spoofax.meta.core.project.ISpoofaxLanguageSpec)5 ISpoofaxLanguageSpecService (org.metaborg.spoofax.meta.core.project.ISpoofaxLanguageSpecService)5 ISourceRegion (org.metaborg.core.source.ISourceRegion)4 SourceRegion (org.metaborg.core.source.SourceRegion)4 TermWithRegion (org.metaborg.spoofax.core.tracing.TracingCommon.TermWithRegion)4 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)4 Nullable (javax.annotation.Nullable)3 MetaborgRuntimeException (org.metaborg.core.MetaborgRuntimeException)3 ContextException (org.metaborg.core.context.ContextException)3 ILanguage (org.metaborg.core.language.ILanguage)3 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)3 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)3