Search in sources :

Example 6 with ILanguage

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);
    }
}
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 7 with ILanguage

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);
}
Also used : ILanguage(org.metaborg.core.language.ILanguage) SyntaxFacet(org.metaborg.spoofax.core.syntax.SyntaxFacet) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) IdentificationFacet(org.metaborg.core.language.IdentificationFacet) FileObject(org.apache.commons.vfs2.FileObject) AnalysisFacet(org.metaborg.spoofax.core.analysis.AnalysisFacet) ILanguageComponent(org.metaborg.core.language.ILanguageComponent) StrategoRuntimeFacet(org.metaborg.spoofax.core.stratego.StrategoRuntimeFacet) Test(org.junit.Test) LanguageServiceTest(org.metaborg.core.test.language.LanguageServiceTest)

Example 8 with ILanguage

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());
}
Also used : ILanguage(org.metaborg.core.language.ILanguage) LanguageIdentifier(org.metaborg.core.language.LanguageIdentifier) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) LanguageVersion(org.metaborg.core.language.LanguageVersion) FileObject(org.apache.commons.vfs2.FileObject) ILanguageComponent(org.metaborg.core.language.ILanguageComponent) Test(org.junit.Test) MetaborgTest(org.metaborg.core.test.MetaborgTest)

Example 9 with ILanguage

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);
}
Also used : ILanguage(org.metaborg.core.language.ILanguage) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) LanguageVersion(org.metaborg.core.language.LanguageVersion) FileObject(org.apache.commons.vfs2.FileObject) ILanguageComponent(org.metaborg.core.language.ILanguageComponent) Test(org.junit.Test) MetaborgTest(org.metaborg.core.test.MetaborgTest)

Example 10 with ILanguage

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

Aggregations

ILanguage (org.metaborg.core.language.ILanguage)14 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)14 FileObject (org.apache.commons.vfs2.FileObject)10 Test (org.junit.Test)7 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)7 LanguageVersion (org.metaborg.core.language.LanguageVersion)7 MetaborgException (org.metaborg.core.MetaborgException)6 LanguageIdentifier (org.metaborg.core.language.LanguageIdentifier)6 MetaborgTest (org.metaborg.core.test.MetaborgTest)6 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)4 MetaborgRuntimeException (org.metaborg.core.MetaborgRuntimeException)3 IProject (org.metaborg.core.project.IProject)3 IContext (org.metaborg.core.context.IContext)2 IdentificationFacet (org.metaborg.core.language.IdentificationFacet)2 IdentifiedResource (org.metaborg.core.language.IdentifiedResource)2 IdentifiedDialect (org.metaborg.core.language.dialect.IdentifiedDialect)2 SyntaxFacet (org.metaborg.spoofax.core.syntax.SyntaxFacet)2 IOException (java.io.IOException)1 Nullable (javax.annotation.Nullable)1 FileSystemException (org.apache.commons.vfs2.FileSystemException)1