Search in sources :

Example 11 with LanguageIdentifier

use of org.metaborg.core.language.LanguageIdentifier in project spoofax by metaborg.

the class LanguageServiceTest method languageChanges.

/**
 * Add/reload/remove components, assert change events correctness.
 */
@Test
public void languageChanges() throws Exception {
    final String id1 = "org.metaborg.lang.entity.component1";
    final String id2 = "org.metaborg.lang.entity.component2";
    final String id3 = "org.metaborg.lang.entity.component3";
    final LanguageVersion version = version(0, 0, 1);
    final LanguageIdentifier identifier1 = new LanguageIdentifier(groupId, id1, version);
    final LanguageIdentifier identifier2 = new LanguageIdentifier(groupId, id2, version);
    final LanguageIdentifier identifier3 = new LanguageIdentifier(groupId, id3, version);
    final FileObject location1 = createDir("ram:///Entity1");
    final FileObject location2 = createDir("ram:///Entity2");
    final FileObject location3 = createDir("ram:///Entity3");
    final String implId1 = "org.metaborg.lang.entity.impl1";
    final String implId2 = "org.metaborg.lang.entity.impl2";
    final String implId3 = "org.metaborg.lang.entity.impl3";
    final String name = "Entity";
    final LanguageIdentifier implIdentifier1 = new LanguageIdentifier(groupId, implId1, version);
    final LanguageIdentifier implIdentifier2 = new LanguageIdentifier(groupId, implId2, version);
    final LanguageIdentifier implIdentifier3 = new LanguageIdentifier(groupId, implId3, version);
    final LanguageContributionIdentifier requestIdentifier1 = new LanguageContributionIdentifier(implIdentifier1, name);
    final LanguageContributionIdentifier requestIdentifier2 = new LanguageContributionIdentifier(implIdentifier2, name);
    final LanguageContributionIdentifier requestIdentifier3 = new LanguageContributionIdentifier(implIdentifier3, name);
    final ITestableObserver<LanguageComponentChange> compObs = new TestableObserver<LanguageComponentChange>();
    languageService.componentChanges().subscribe(compObs);
    final ITestableObserver<LanguageImplChange> implObs = new TestableObserver<LanguageImplChange>();
    languageService.implChanges().subscribe(implObs);
    // Add component1 to impl1, expect component add, impl add
    final ILanguageComponent component1 = language(identifier1, location1, requestIdentifier1);
    final ILanguageImpl impl1 = languageService.getImpl(implIdentifier1);
    assertOnNext(new LanguageComponentChange(LanguageComponentChange.Kind.Add, null, component1), compObs);
    assertOnNext(new LanguageImplChange(LanguageImplChange.Kind.Add, impl1), implObs);
    // Add component2 to impl1, expect component2 add, impl1 reload
    final ILanguageComponent component2 = language(identifier2, location2, requestIdentifier1);
    assertOnNext(new LanguageComponentChange(LanguageComponentChange.Kind.Add, null, component2), compObs);
    assertOnNext(new LanguageImplChange(LanguageImplChange.Kind.Reload, impl1), implObs);
    // Add component3 to impl1, impl2, impl3, expect component add, [impl1 reload, impl2 add, impl3 add] (order
    // unknown)
    final ILanguageComponent component3 = language(identifier3, location3, requestIdentifier1, requestIdentifier2, requestIdentifier3);
    final ILanguageImpl impl2 = languageService.getImpl(implIdentifier2);
    final ILanguageImpl impl3 = languageService.getImpl(implIdentifier3);
    assertOnNext(new LanguageComponentChange(LanguageComponentChange.Kind.Add, null, component3), compObs);
    {
        final Iterable<LanguageImplChange> changes = Iterables2.from(new LanguageImplChange(LanguageImplChange.Kind.Reload, impl1), new LanguageImplChange(LanguageImplChange.Kind.Add, impl2), new LanguageImplChange(LanguageImplChange.Kind.Add, impl3));
        assertOnNext(changes, implObs);
        assertOnNext(changes, implObs);
        assertOnNext(changes, implObs);
    }
    // Remove component1, expect component1 remove, impl1 reload
    languageService.remove(component1);
    assertOnNext(new LanguageComponentChange(LanguageComponentChange.Kind.Remove, component1, null), compObs);
    assertOnNext(new LanguageImplChange(LanguageImplChange.Kind.Reload, impl1), implObs);
    // Reload component2, contribute to impl2 now, expect component2 reload, [impl1 reload, impl2 reload] (order
    // unknown)
    final ILanguageComponent component2Reload = language(identifier2, location2, requestIdentifier2);
    assertOnNext(new LanguageComponentChange(LanguageComponentChange.Kind.Reload, component2, component2Reload), compObs);
    {
        final Iterable<LanguageImplChange> changes = Iterables2.from(new LanguageImplChange(LanguageImplChange.Kind.Reload, impl1), new LanguageImplChange(LanguageImplChange.Kind.Reload, impl2));
        assertOnNext(changes, implObs);
        assertOnNext(changes, implObs);
    }
    // Remove component3, expect component3 remove, [impl1 remove, impl2 reload, impl3 remove] (order unknown)
    languageService.remove(component3);
    assertOnNext(new LanguageComponentChange(LanguageComponentChange.Kind.Remove, component3, null), compObs);
    {
        final Iterable<LanguageImplChange> changes = Iterables2.from(new LanguageImplChange(LanguageImplChange.Kind.Remove, impl3), new LanguageImplChange(LanguageImplChange.Kind.Reload, impl2), new LanguageImplChange(LanguageImplChange.Kind.Remove, impl1));
        assertOnNext(changes, implObs);
        assertOnNext(changes, implObs);
        assertOnNext(changes, implObs);
    }
    // Remove component2, expect component2 remove, impl2 remove
    languageService.remove(component2);
    assertOnNext(new LanguageComponentChange(LanguageComponentChange.Kind.Remove, component2, null), compObs);
    assertOnNext(new LanguageImplChange(LanguageImplChange.Kind.Remove, impl2), implObs);
    assertEmpty(compObs);
    assertEmpty(implObs);
}
Also used : ITestableObserver(org.metaborg.util.observable.ITestableObserver) TestableObserver(org.metaborg.util.observable.TestableObserver) LanguageImplChange(org.metaborg.core.language.LanguageImplChange) LanguageContributionIdentifier(org.metaborg.core.language.LanguageContributionIdentifier) LanguageComponentChange(org.metaborg.core.language.LanguageComponentChange) 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 12 with LanguageIdentifier

use of org.metaborg.core.language.LanguageIdentifier in project spoofax by metaborg.

the class DefaultDependencyService method checkDependencies.

@Override
public MissingDependencies checkDependencies(IProject project) {
    final IProjectConfig config = project.config();
    final Collection<LanguageIdentifier> compileDeps = config.compileDeps();
    final Collection<LanguageIdentifier> missingCompile = Lists.newLinkedList();
    for (LanguageIdentifier identifier : compileDeps) {
        if (languageService.getComponent(identifier) == null) {
            missingCompile.add(identifier);
        }
    }
    final Collection<LanguageIdentifier> sourceDeps = config.sourceDeps();
    final Collection<LanguageIdentifier> missingSource = Lists.newLinkedList();
    for (LanguageIdentifier identifier : sourceDeps) {
        if (languageService.getComponent(identifier) == null) {
            missingSource.add(identifier);
        }
    }
    return new MissingDependencies(missingCompile, missingSource);
}
Also used : LanguageIdentifier(org.metaborg.core.language.LanguageIdentifier) IProjectConfig(org.metaborg.core.config.IProjectConfig)

Example 13 with LanguageIdentifier

use of org.metaborg.core.language.LanguageIdentifier in project spoofax by metaborg.

the class LanguageComponentConfig method langContribs.

@Override
public Collection<LanguageContributionIdentifier> langContribs() {
    final List<HierarchicalConfiguration<ImmutableNode>> langContribConfigs = config.configurationsAt(PROP_LANGUAGE_CONTRIBUTIONS);
    final List<LanguageContributionIdentifier> langContribs = Lists.newArrayListWithCapacity(langContribConfigs.size());
    for (HierarchicalConfiguration<ImmutableNode> langContribConfig : langContribConfigs) {
        // HACK: for some reason get(LanguageIdentifier.class, "id") does not work here, it cannot convert to a
        // language identifier, do manually instead.
        final String idString = langContribConfig.getString("id");
        final LanguageIdentifier id = LanguageIdentifier.parse(idString);
        final String name = langContribConfig.getString("name");
        langContribs.add(new LanguageContributionIdentifier(id, name));
    }
    return langContribs;
}
Also used : ImmutableNode(org.apache.commons.configuration2.tree.ImmutableNode) LanguageIdentifier(org.metaborg.core.language.LanguageIdentifier) HierarchicalConfiguration(org.apache.commons.configuration2.HierarchicalConfiguration) LanguageContributionIdentifier(org.metaborg.core.language.LanguageContributionIdentifier)

Example 14 with LanguageIdentifier

use of org.metaborg.core.language.LanguageIdentifier in project spoofax by metaborg.

the class DialectService method createDialect.

private ILanguageImpl createDialect(String name, FileObject location, ILanguageImpl base, IFacet syntaxFacet, boolean replaceIdentification, boolean appendDialectName) {
    final LanguageIdentifier baseId = base.id();
    final String dialectId;
    if (appendDialectName) {
        dialectId = baseId.id + "-Dialect-" + name;
    } else {
        dialectId = baseId.id;
    }
    final LanguageIdentifier id = new LanguageIdentifier(baseId.groupId, dialectId, baseId.version);
    // HACK: use config of first component.
    final ILanguageComponentConfig config = Iterables.get(base.components(), 0).config();
    final ComponentCreationConfig creationConfig = languageService.create(id, location, Iterables2.singleton(new LanguageContributionIdentifier(id, name)), config);
    for (IFacet facet : base.facets()) {
        if (facet instanceof IdentificationFacet && replaceIdentification) {
            creationConfig.addFacet(new IdentificationFacet(new MetaFileIdentifier((IdentificationFacet) facet)));
        } else if (facet instanceof SyntaxFacet || facet instanceof ResourceExtensionFacet) {
        // Ignore
        } else {
            creationConfig.addFacet(facet);
        }
    }
    creationConfig.addFacet(syntaxFacet);
    final ILanguageComponent dialectComponent = languageService.add(creationConfig);
    final ILanguageImpl dialect = Iterables.get(dialectComponent.contributesTo(), 0);
    return dialect;
}
Also used : SyntaxFacet(org.metaborg.spoofax.core.syntax.SyntaxFacet) LanguageIdentifier(org.metaborg.core.language.LanguageIdentifier) IdentificationFacet(org.metaborg.core.language.IdentificationFacet) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) IFacet(org.metaborg.core.language.IFacet) ResourceExtensionFacet(org.metaborg.core.language.ResourceExtensionFacet) ComponentCreationConfig(org.metaborg.core.language.ComponentCreationConfig) ILanguageComponent(org.metaborg.core.language.ILanguageComponent) ILanguageComponentConfig(org.metaborg.core.config.ILanguageComponentConfig) LanguageContributionIdentifier(org.metaborg.core.language.LanguageContributionIdentifier)

Example 15 with LanguageIdentifier

use of org.metaborg.core.language.LanguageIdentifier in project spoofax by metaborg.

the class LanguageServiceTest method activeLowerVersion.

/**
 * Add an implementation with a lower version number, assert that the other implementation stays active.
 */
@Test
public void activeLowerVersion() throws Exception {
    final String id = "org.metaborg.lang.entity";
    final LanguageVersion version1 = version(0, 1, 0);
    final LanguageVersion version2 = version(0, 0, 1);
    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 1 with higher version number stays active.
    assertSame(component1, languageService.getComponent(location1.getName()));
    assertSame(component2, languageService.getComponent(location2.getName()));
    assertSame(impl1, languageService.getImpl(identifier1));
    assertSame(impl2, languageService.getImpl(identifier2));
    assertSame(impl1, 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)

Aggregations

LanguageIdentifier (org.metaborg.core.language.LanguageIdentifier)21 FileObject (org.apache.commons.vfs2.FileObject)11 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)11 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)11 LanguageVersion (org.metaborg.core.language.LanguageVersion)9 Test (org.junit.Test)8 MetaborgTest (org.metaborg.core.test.MetaborgTest)8 LanguageContributionIdentifier (org.metaborg.core.language.LanguageContributionIdentifier)7 ILanguage (org.metaborg.core.language.ILanguage)6 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)4 MetaborgException (org.metaborg.core.MetaborgException)3 ILanguageComponentConfig (org.metaborg.core.config.ILanguageComponentConfig)3 Nullable (javax.annotation.Nullable)2 IExportConfig (org.metaborg.core.config.IExportConfig)2 ComponentCreationConfig (org.metaborg.core.language.ComponentCreationConfig)2 IFacet (org.metaborg.core.language.IFacet)2 IdentificationFacet (org.metaborg.core.language.IdentificationFacet)2 ResourceExtensionFacet (org.metaborg.core.language.ResourceExtensionFacet)2 ISpoofaxLanguageSpecConfig (org.metaborg.spoofax.meta.core.config.ISpoofaxLanguageSpecConfig)2 ISpoofaxLanguageSpec (org.metaborg.spoofax.meta.core.project.ISpoofaxLanguageSpec)2