use of org.metaborg.core.language.ILanguage 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);
}
}
use of org.metaborg.core.language.ILanguage in project spoofax by metaborg.
the class SpoofaxLanguageTest method discoverLanguage.
/**
* The 'res:' filesystem redirects to resources inside the tests' JAR file or class file location, which are copied
* to the class file location from src/test/resources by Maven. The binary files of the Entity language are located
* in the resources to test language discovery.
*/
@Test
public void discoverLanguage() throws Exception {
final FileObject location = resourceService.resolve("res:");
final Iterable<ILanguageComponent> languages = languageDiscoveryService.discover(languageDiscoveryService.request(location));
assertEquals(1, Iterables.size(languages));
final ILanguageComponent component = Iterables.get(languages, 0);
final ILanguageImpl impl = Iterables.get(component.contributesTo(), 0);
final ILanguage language = impl.belongsTo();
assertEquals("Entity", language.name());
assertEquals(resourceService.resolve("res:Entity"), component.location());
final IdentificationFacet identificationFacet = impl.facet(IdentificationFacet.class);
assertTrue(identificationFacet.identify(resourceService.resolve("ram:///Entity/test.ent")));
final SyntaxFacet syntaxFacet = impl.facet(SyntaxFacet.class);
assertEquals(resourceService.resolve("res:Entity/target/metaborg/sdf.tbl"), syntaxFacet.parseTable);
assertIterableEquals(syntaxFacet.startSymbols, "Start");
final StrategoRuntimeFacet strategoFacet = impl.facet(StrategoRuntimeFacet.class);
assertIterableEquals(strategoFacet.ctreeFiles, resourceService.resolve("res:Entity/target/metaborg/stratego.ctree"));
assertIterableEquals(strategoFacet.jarFiles, resourceService.resolve("res:Entity/target/metaborg/stratego-javastrat.jar"));
final AnalysisFacet analysisFacet = impl.facet(AnalysisFacet.class);
assertEquals("editor-analyze", analysisFacet.strategyName);
}
use of org.metaborg.core.language.ILanguage in project spoofax by metaborg.
the class LanguageServiceTest method activeHigherVersion.
/**
* Add an implementation with a higher version number, assert that the newer implementation becomes active.
*/
@Test
public void activeHigherVersion() throws Exception {
final String id = "org.metaborg.lang.entity";
final LanguageVersion version1 = version(0, 0, 1);
final LanguageVersion version2 = version(0, 1, 0);
final LanguageIdentifier identifier1 = new LanguageIdentifier(groupId, id, version1);
final LanguageIdentifier identifier2 = new LanguageIdentifier(groupId, id, version2);
final FileObject location1 = createDir("ram:///Entity1");
final FileObject location2 = createDir("ram:///Entity2");
final String name = "Entity";
final ILanguageComponent component1 = language(identifier1, location1, name);
final ILanguageImpl impl1 = Iterables.get(component1.contributesTo(), 0);
final ILanguage lang = impl1.belongsTo();
assertSame(component1, languageService.getComponent(location1.getName()));
assertSame(impl1, languageService.getImpl(identifier1));
assertSame(impl1, lang.activeImpl());
assertSame(lang, languageService.getLanguage(name));
final ILanguageComponent component2 = language(identifier2, location2, name);
final ILanguageImpl impl2 = Iterables.get(component2.contributesTo(), 0);
// Language 2 with higher version number becomes active.
assertSame(component1, languageService.getComponent(location1.getName()));
assertSame(component2, languageService.getComponent(location2.getName()));
assertSame(impl1, languageService.getImpl(identifier1));
assertSame(impl2, languageService.getImpl(identifier2));
assertSame(impl2, lang.activeImpl());
assertSame(lang, languageService.getLanguage(name));
assertSize(1, impl1.components());
assertSize(1, impl2.components());
assertSize(2, lang.impls());
assertSize(2, languageService.getAllComponents());
assertSize(2, languageService.getAllImpls());
assertSize(1, languageService.getAllLanguages());
}
use of org.metaborg.core.language.ILanguage in project spoofax by metaborg.
the class LanguageServiceTest method reloadComponent.
/**
* Reload a single component, assert that it was reloaded and its implementations and language stays the same.
*/
@Test
public void reloadComponent() throws Exception {
final String id = "org.metaborg.lang.entity";
final LanguageVersion version = version(0, 0, 1);
final FileObject location = createDir("ram:///");
final String name = "Entity";
// GTODO: test multiple contributing components
final ILanguageComponent componentBefore = language(groupId, id, version, location, name);
final ILanguageImpl implBefore = Iterables.get(componentBefore.contributesTo(), 0);
final ILanguage langBefore = implBefore.belongsTo();
assertSame(componentBefore, languageService.getComponent(location.getName()));
final ILanguageComponent componentAfter = language(groupId, id, version, location, name);
final ILanguageImpl implAfter = Iterables.get(componentAfter.contributesTo(), 0);
final ILanguage langAfter = implAfter.belongsTo();
// Before components are equal, but not the same object, since they are re-created.
assertEquals(componentBefore, languageService.getComponent(location.getName()));
assertNotSame(componentBefore, languageService.getComponent(location.getName()));
assertEquals(componentBefore, componentAfter);
assertNotSame(componentBefore, componentAfter);
assertSame(componentAfter, languageService.getComponent(location.getName()));
assertSame(implBefore, implAfter);
assertSame(langBefore, langAfter);
}
use of org.metaborg.core.language.ILanguage in project spoofax by metaborg.
the class LanguageSourceFilesPrimitive 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> sourceFiles = languagePathService.sourceFiles(project, impl);
final List<IStrategoTerm> terms = Lists.newArrayList();
for (IdentifiedResource sourceFile : sourceFiles) {
terms.add(factory.makeString(sourceFile.resource.getName().getURI()));
}
return factory.makeList(terms);
}
Aggregations