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);
}
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());
}
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);
}
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);
}
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);
}
}
Aggregations