Search in sources :

Example 16 with MetaborgException

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

the class LanguageSpecificationPrimitive method call.

@Override
protected IStrategoTerm call(IStrategoTerm current, Strategy[] svars, IStrategoTerm[] tvars, ITermFactory factory, IContext context) throws MetaborgException {
    final IProject project = context.project();
    if (project == null) {
        return null;
    }
    if (languageSpecServiceProvider == null) {
        // Indicates that meta-Spoofax is not available (ISpoofaxLanguageSpecService cannot be injected), but this
        // should never happen because this primitive is inside meta-Spoofax. Check for null just in case.
        logger.error("Language specification service is not available; static injection failed");
        return null;
    }
    final ISpoofaxLanguageSpecService languageSpecService = languageSpecServiceProvider.get();
    if (!languageSpecService.available(project)) {
        return null;
    }
    final ISpoofaxLanguageSpec languageSpec;
    try {
        languageSpec = languageSpecService.get(project);
    } catch (ConfigException e) {
        throw new MetaborgException("Unable to get language specification configuration for " + project, e);
    }
    if (languageSpec == null) {
        return null;
    }
    final IStrategoString nameTerm = factory.makeString(languageSpec.config().name());
    final LanguageIdentifier id = languageSpec.config().identifier();
    final IStrategoString groupIdTerm = factory.makeString(id.groupId);
    final IStrategoString idTerm = factory.makeString(id.id);
    final IStrategoString versionTerm = factory.makeString(id.version.toString());
    final IStrategoString locationTerm = factory.makeString(languageSpec.location().getName().getURI());
    return factory.makeTuple(nameTerm, groupIdTerm, idTerm, versionTerm, locationTerm);
}
Also used : ISpoofaxLanguageSpec(org.metaborg.spoofax.meta.core.project.ISpoofaxLanguageSpec) LanguageIdentifier(org.metaborg.core.language.LanguageIdentifier) ISpoofaxLanguageSpecService(org.metaborg.spoofax.meta.core.project.ISpoofaxLanguageSpecService) MetaborgException(org.metaborg.core.MetaborgException) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) ConfigException(org.metaborg.core.config.ConfigException) IProject(org.metaborg.core.project.IProject)

Example 17 with MetaborgException

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

the class LegacyLanguageSpecNamePrimitive method call.

@Override
protected IStrategoTerm call(IStrategoTerm current, Strategy[] svars, IStrategoTerm[] tvars, ITermFactory factory, IContext context) throws MetaborgException {
    final FileObject location = context.location();
    final IProject project = projectService.get(location);
    if (project == null) {
        return null;
    }
    if (languageSpecServiceProvider == null) {
        // Indicates that meta-Spoofax is not available (ISpoofaxLanguageSpecService cannot be injected), but this
        // should never happen because this primitive is inside meta-Spoofax. Check for null just in case.
        logger.debug("Language specification service is not available; static injection failed");
        return null;
    }
    final ISpoofaxLanguageSpecService languageSpecService = languageSpecServiceProvider.get();
    if (!languageSpecService.available(project)) {
        return null;
    }
    final ISpoofaxLanguageSpec languageSpec;
    try {
        languageSpec = languageSpecService.get(project);
    } catch (ConfigException e) {
        throw new MetaborgException("Unable to get language specification name for " + location, e);
    }
    if (languageSpec == null) {
        return null;
    }
    return factory.makeString(languageSpec.config().name());
}
Also used : ISpoofaxLanguageSpec(org.metaborg.spoofax.meta.core.project.ISpoofaxLanguageSpec) ISpoofaxLanguageSpecService(org.metaborg.spoofax.meta.core.project.ISpoofaxLanguageSpecService) MetaborgException(org.metaborg.core.MetaborgException) ConfigException(org.metaborg.core.config.ConfigException) FileObject(org.apache.commons.vfs2.FileObject) IProject(org.metaborg.core.project.IProject)

Example 18 with MetaborgException

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

the class LanguageIdentifierService method identifyToResource.

@Override
@Nullable
public IdentifiedResource identifyToResource(FileObject resource, Iterable<? extends ILanguageImpl> impls) {
    // Ignore directories.
    try {
        if (resource.getType() == FileType.FOLDER) {
            return null;
        }
    } catch (FileSystemException e) {
        logger.error("Cannot identify {}, cannot determine its file type", e, resource);
        return null;
    }
    // Try to identify using the dialect identifier first.
    try {
        final IdentifiedDialect dialect = dialectIdentifier.identify(resource);
        if (dialect != null) {
            return new IdentifiedResource(resource, dialect);
        }
    } catch (MetaborgException e) {
        logger.error("Cannot identify dialect of {}", e, resource);
        return null;
    } catch (MetaborgRuntimeException e) {
    // Ignore
    }
    // Identify using identification facet.
    final Set<ILanguage> identifiedLanguages = Sets.newLinkedHashSet();
    ILanguageImpl identifiedImpl = null;
    for (ILanguageImpl impl : impls) {
        if (identify(resource, impl)) {
            identifiedLanguages.add(impl.belongsTo());
            identifiedImpl = impl;
        }
    }
    if (identifiedLanguages.size() > 1) {
        throw new IllegalStateException("Resource " + resource + " identifies to multiple languages: " + Joiner.on(", ").join(identifiedLanguages));
    }
    if (identifiedImpl == null) {
        return null;
    }
    return new IdentifiedResource(resource, null, identifiedImpl);
}
Also used : MetaborgRuntimeException(org.metaborg.core.MetaborgRuntimeException) FileSystemException(org.apache.commons.vfs2.FileSystemException) MetaborgException(org.metaborg.core.MetaborgException) IdentifiedDialect(org.metaborg.core.language.dialect.IdentifiedDialect) Nullable(javax.annotation.Nullable)

Example 19 with MetaborgException

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

the class Menu method action.

@Override
@Nullable
public IAction action(String name) throws MetaborgException {
    final List<IAction> actions = Lists.newLinkedList();
    for (IMenuItem item : items) {
        if (item instanceof IMenuAction && name.equals(item.name())) {
            final IMenuAction menuAction = (IMenuAction) item;
            final IAction action = menuAction.action();
            actions.add(action);
        }
    }
    final int size = actions.size();
    if (size == 0) {
        return null;
    } else if (size > 1) {
        final String message = String.format("Found multiple actions with name %s: %s", name, Joiner.on(", ").join(actions));
        throw new MetaborgException(message);
    }
    return actions.get(0);
}
Also used : IAction(org.metaborg.core.action.IAction) MetaborgException(org.metaborg.core.MetaborgException) Nullable(javax.annotation.Nullable)

Example 20 with MetaborgException

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

the class ServiceModulePluginLoader method modules.

@Override
public Iterable<Module> modules() throws MetaborgException {
    try {
        final ServiceLoader<T> modulePlugins = ServiceLoader.load(serviceClass);
        final Collection<Module> modules = Lists.newLinkedList();
        for (T plugin : modulePlugins) {
            Iterables.addAll(modules, plugin.modules());
        }
        return modules;
    } catch (Exception e) {
        throw new MetaborgException("Unhandled exception while loading module plugins with Java's ServiceLoader", e);
    }
}
Also used : MetaborgException(org.metaborg.core.MetaborgException) Module(com.google.inject.Module) MetaborgException(org.metaborg.core.MetaborgException)

Aggregations

MetaborgException (org.metaborg.core.MetaborgException)49 FileObject (org.apache.commons.vfs2.FileObject)25 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)19 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)17 HybridInterpreter (org.strategoxt.HybridInterpreter)14 IProject (org.metaborg.core.project.IProject)12 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)11 IOException (java.io.IOException)9 IContext (org.metaborg.core.context.IContext)9 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)9 Nullable (javax.annotation.Nullable)8 AnalysisException (org.metaborg.core.analysis.AnalysisException)8 FileSystemException (org.apache.commons.vfs2.FileSystemException)7 MetaborgRuntimeException (org.metaborg.core.MetaborgRuntimeException)7 ISpoofaxAnalyzeResults (org.metaborg.spoofax.core.analysis.ISpoofaxAnalyzeResults)6 SpoofaxAnalyzeResults (org.metaborg.spoofax.core.analysis.SpoofaxAnalyzeResults)6 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)6 ILanguage (org.metaborg.core.language.ILanguage)5 ISourceRegion (org.metaborg.core.source.ISourceRegion)5 ISpoofaxParseUnit (org.metaborg.spoofax.core.unit.ISpoofaxParseUnit)5