Search in sources :

Example 1 with LanguageContributionIdentifier

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

the class LanguageComponentFactory method createConfig.

@Override
public ComponentCreationConfig createConfig(IComponentCreationConfigRequest configRequest) throws MetaborgException {
    final ComponentFactoryRequest request = (ComponentFactoryRequest) configRequest;
    final FileObject location = request.location();
    if (!request.valid()) {
        throw new MetaborgException(request.toString());
    }
    final ILanguageComponentConfig componentConfig = request.config();
    logger.debug("Creating language component for {}", location);
    final LanguageIdentifier identifier = componentConfig.identifier();
    final Collection<LanguageContributionIdentifier> langContribs = request.config().langContribs();
    if (langContribs.isEmpty()) {
        langContribs.add(new LanguageContributionIdentifier(identifier, componentConfig.name()));
    }
    final ComponentCreationConfig config = new ComponentCreationConfig(identifier, location, langContribs, componentConfig);
    final SyntaxFacet syntaxFacet;
    if (componentConfig.sdfEnabled()) {
        syntaxFacet = request.syntaxFacet();
        if (syntaxFacet != null) {
            config.addFacet(syntaxFacet);
        }
    } else {
        syntaxFacet = null;
    }
    final StrategoRuntimeFacet strategoRuntimeFacet = request.strategoRuntimeFacet();
    if (strategoRuntimeFacet != null) {
        config.addFacet(strategoRuntimeFacet);
    }
    final IStrategoAppl esvTerm = request.esvTerm();
    if (esvTerm != null) {
        final String[] extensions = extensions(esvTerm);
        if (extensions.length != 0) {
            final Iterable<String> extensionsIterable = Iterables2.from(extensions);
            final IdentificationFacet identificationFacet = new IdentificationFacet(new ResourceExtensionsIdentifier(extensionsIterable));
            config.addFacet(identificationFacet);
            final ResourceExtensionFacet resourceExtensionsFacet = new ResourceExtensionFacet(extensionsIterable);
            config.addFacet(resourceExtensionsFacet);
        }
        if (ParseFacetFromESV.hasParser(esvTerm)) {
            config.addFacet(ParseFacetFromESV.create(esvTerm));
        } else if (syntaxFacet != null) {
            config.addFacet(new ParseFacet("jsglr"));
        }
        final boolean hasContext = ContextFacetFromESV.hasContext(esvTerm);
        final boolean hasAnalysis = AnalysisFacetFromESV.hasAnalysis(esvTerm);
        final IContextFactory contextFactory;
        final ISpoofaxAnalyzer analyzer;
        final AnalysisFacet analysisFacet;
        if (hasAnalysis) {
            final String analysisType = AnalysisFacetFromESV.type(esvTerm);
            assert analysisType != null : "Analyzer type cannot be null because hasAnalysis is true, no null check is needed.";
            analyzer = analyzers.get(analysisType);
            analysisFacet = AnalysisFacetFromESV.create(esvTerm);
            final String contextType = hasContext ? ContextFacetFromESV.type(esvTerm) : null;
            if (hasContext && contextType == null) {
                contextFactory = null;
            } else {
                final String analysisContextType;
                switch(analysisType) {
                    default:
                    case StrategoAnalyzer.name:
                        analysisContextType = LegacyContextFactory.name;
                        break;
                    case TaskEngineAnalyzer.name:
                        analysisContextType = IndexTaskContextFactory.name;
                        break;
                    case ConstraintSingleFileAnalyzer.name:
                        analysisContextType = SingleFileScopeGraphContextFactory.name;
                        break;
                    case ConstraintMultiFileAnalyzer.name:
                        analysisContextType = MultiFileScopeGraphContextFactory.name;
                        break;
                }
                if (hasContext && !analysisContextType.equals(contextType)) {
                    logger.warn("Ignoring explicit context type {}, because it is incompatible with analysis {}.", contextType, analysisType);
                }
                contextFactory = contextFactory(analysisContextType);
            }
        } else if (hasContext) {
            final String type = ContextFacetFromESV.type(esvTerm);
            contextFactory = contextFactory(type);
            analyzer = null;
            analysisFacet = null;
        } else {
            contextFactory = contextFactory(LegacyContextFactory.name);
            analyzer = null;
            analysisFacet = null;
        }
        if (contextFactory != null) {
            final IContextStrategy contextStrategy = contextStrategy(ProjectContextStrategy.name);
            config.addFacet(new ContextFacet(contextFactory, contextStrategy));
        }
        if (analyzer != null) {
            config.addFacet(new AnalyzerFacet<>(analyzer));
        }
        if (analysisFacet != null) {
            config.addFacet(analysisFacet);
        }
        final ActionFacet menusFacet = ActionFacetFromESV.create(esvTerm);
        if (menusFacet != null) {
            config.addFacet(menusFacet);
        }
        final StylerFacet stylerFacet = StylerFacetFromESV.create(esvTerm);
        if (stylerFacet != null) {
            config.addFacet(stylerFacet);
        }
        final ResolverFacet resolverFacet = ResolverFacetFromESV.createResolver(esvTerm);
        if (resolverFacet != null) {
            config.addFacet(resolverFacet);
        }
        final HoverFacet hoverFacet = ResolverFacetFromESV.createHover(esvTerm);
        if (hoverFacet != null) {
            config.addFacet(hoverFacet);
        }
        final OutlineFacet outlineFacet = OutlineFacetFromESV.create(esvTerm);
        if (outlineFacet != null) {
            config.addFacet(outlineFacet);
        }
        final ShellFacet shellFacet = ShellFacetFromESV.create(esvTerm);
        if (shellFacet != null) {
            config.addFacet(shellFacet);
        }
    }
    return config;
}
Also used : ResourceExtensionsIdentifier(org.metaborg.core.language.ResourceExtensionsIdentifier) ResolverFacet(org.metaborg.spoofax.core.tracing.ResolverFacet) MetaborgException(org.metaborg.core.MetaborgException) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) LanguageContributionIdentifier(org.metaborg.core.language.LanguageContributionIdentifier) ParseFacet(org.metaborg.core.syntax.ParseFacet) ISpoofaxAnalyzer(org.metaborg.spoofax.core.analysis.ISpoofaxAnalyzer) IContextFactory(org.metaborg.core.context.IContextFactory) FileObject(org.apache.commons.vfs2.FileObject) StylerFacet(org.metaborg.spoofax.core.style.StylerFacet) ContextFacet(org.metaborg.core.context.ContextFacet) OutlineFacet(org.metaborg.spoofax.core.outline.OutlineFacet) ILanguageComponentConfig(org.metaborg.core.config.ILanguageComponentConfig) StrategoRuntimeFacet(org.metaborg.spoofax.core.stratego.StrategoRuntimeFacet) ShellFacet(org.metaborg.spoofax.core.shell.ShellFacet) SyntaxFacet(org.metaborg.spoofax.core.syntax.SyntaxFacet) IdentificationFacet(org.metaborg.core.language.IdentificationFacet) ActionFacet(org.metaborg.spoofax.core.action.ActionFacet) AnalysisFacet(org.metaborg.spoofax.core.analysis.AnalysisFacet) LanguageIdentifier(org.metaborg.core.language.LanguageIdentifier) IContextStrategy(org.metaborg.core.context.IContextStrategy) ResourceExtensionFacet(org.metaborg.core.language.ResourceExtensionFacet) ComponentCreationConfig(org.metaborg.core.language.ComponentCreationConfig) HoverFacet(org.metaborg.spoofax.core.tracing.HoverFacet)

Example 2 with LanguageContributionIdentifier

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

the class LanguageServiceTest method conflictingContributionNames.

/**
 * Add contributions iwth conflicting language names, assert that this throws an error.
 */
@Test(expected = IllegalStateException.class)
public void conflictingContributionNames() throws Exception {
    final String id = "org.metaborg.lang.entity";
    final String id1 = "org.metaborg.lang.entity.component1";
    final String id2 = "org.metaborg.lang.entity.component2";
    final LanguageVersion version = version(0, 0, 1);
    final LanguageIdentifier identifier = new LanguageIdentifier(groupId, id, version);
    final LanguageIdentifier identifier1 = new LanguageIdentifier(groupId, id1, version);
    final LanguageIdentifier identifier2 = new LanguageIdentifier(groupId, id2, version);
    final FileObject location1 = createDir("ram:///Entity1");
    final FileObject location2 = createDir("ram:///Entity2");
    final String name1 = "Entity1";
    final String name2 = "Entity2";
    language(identifier1, location1, new LanguageContributionIdentifier(identifier, name1));
    language(identifier2, location2, new LanguageContributionIdentifier(identifier, name2));
}
Also used : LanguageIdentifier(org.metaborg.core.language.LanguageIdentifier) LanguageVersion(org.metaborg.core.language.LanguageVersion) FileObject(org.apache.commons.vfs2.FileObject) LanguageContributionIdentifier(org.metaborg.core.language.LanguageContributionIdentifier) Test(org.junit.Test) MetaborgTest(org.metaborg.core.test.MetaborgTest)

Example 3 with LanguageContributionIdentifier

use of org.metaborg.core.language.LanguageContributionIdentifier 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 4 with LanguageContributionIdentifier

use of org.metaborg.core.language.LanguageContributionIdentifier 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 5 with LanguageContributionIdentifier

use of org.metaborg.core.language.LanguageContributionIdentifier 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)

Aggregations

LanguageContributionIdentifier (org.metaborg.core.language.LanguageContributionIdentifier)7 LanguageIdentifier (org.metaborg.core.language.LanguageIdentifier)7 FileObject (org.apache.commons.vfs2.FileObject)5 Test (org.junit.Test)4 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)4 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)4 LanguageVersion (org.metaborg.core.language.LanguageVersion)4 MetaborgTest (org.metaborg.core.test.MetaborgTest)4 ResourceExtensionFacet (org.metaborg.core.language.ResourceExtensionFacet)3 ILanguageComponentConfig (org.metaborg.core.config.ILanguageComponentConfig)2 ComponentCreationConfig (org.metaborg.core.language.ComponentCreationConfig)2 IFacet (org.metaborg.core.language.IFacet)2 IdentificationFacet (org.metaborg.core.language.IdentificationFacet)2 SyntaxFacet (org.metaborg.spoofax.core.syntax.SyntaxFacet)2 HierarchicalConfiguration (org.apache.commons.configuration2.HierarchicalConfiguration)1 ImmutableNode (org.apache.commons.configuration2.tree.ImmutableNode)1 MetaborgException (org.metaborg.core.MetaborgException)1 ContextFacet (org.metaborg.core.context.ContextFacet)1 IContextFactory (org.metaborg.core.context.IContextFactory)1 IContextStrategy (org.metaborg.core.context.IContextStrategy)1