use of org.metaborg.core.language.ResourceExtensionFacet 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;
}
use of org.metaborg.core.language.ResourceExtensionFacet 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;
}
use of org.metaborg.core.language.ResourceExtensionFacet in project spoofax by metaborg.
the class LanguageServiceTest method implementationFacets.
/**
* Add multiple components with facets to a single implementation, assert correctness of returned facets.
*/
@Test
public void implementationFacets() throws Exception {
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 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 implId = "org.metaborg.lang.entity.impl1";
final String name = "Entity";
final LanguageIdentifier implIdentifier = new LanguageIdentifier(groupId, implId, version);
final LanguageContributionIdentifier requestIdentifier = new LanguageContributionIdentifier(implIdentifier, name);
final DescriptionFacet facet1 = new DescriptionFacet("Component1", null);
final ResourceExtensionFacet facet2 = new ResourceExtensionFacet(Iterables2.singleton("com"));
final DescriptionFacet facet3 = new DescriptionFacet("Component2", null);
final ILanguageComponent component1 = language(identifier1, location1, requestIdentifier, facet1, facet2);
final ILanguageComponent component2 = language(identifier2, location2, requestIdentifier, facet3);
final ILanguageImpl impl = languageService.getImpl(implIdentifier);
assertContains(facet1, component1.facets());
assertContains(facet2, component1.facets());
assertContains(facet3, component2.facets());
assertSize(2, component1.facets());
assertSize(1, component2.facets());
assertContains(facet1, component1.facets(DescriptionFacet.class));
assertSame(facet1, component1.facet(DescriptionFacet.class));
assertSize(1, component1.facets(DescriptionFacet.class));
assertContains(facet2, component1.facets(ResourceExtensionFacet.class));
assertSame(facet2, component1.facet(ResourceExtensionFacet.class));
assertSize(1, component1.facets(ResourceExtensionFacet.class));
assertContains(facet3, component2.facets(DescriptionFacet.class));
assertSame(facet3, component2.facet(DescriptionFacet.class));
assertSize(1, component2.facets(DescriptionFacet.class));
assertContains(facet1, impl.facets());
assertContains(facet2, impl.facets());
assertContains(facet3, impl.facets());
assertSize(3, impl.facets());
assertContains(facet1, impl.facets(DescriptionFacet.class));
assertContains(facet2, impl.facets(ResourceExtensionFacet.class));
assertContains(facet3, impl.facets(DescriptionFacet.class));
assertSize(2, impl.facets(DescriptionFacet.class));
assertSize(1, impl.facets(ResourceExtensionFacet.class));
for (FacetContribution<IFacet> facetContribution : component1.facetContributions()) {
assertSame(facetContribution.contributor, component1);
}
assertSame(component1, component1.facetContribution(DescriptionFacet.class).contributor);
assertSame(facet1, component1.facetContribution(DescriptionFacet.class).facet);
assertSize(1, component1.facetContributions(DescriptionFacet.class));
assertSame(component1, component1.facetContribution(ResourceExtensionFacet.class).contributor);
assertSame(facet2, component1.facetContribution(ResourceExtensionFacet.class).facet);
assertSize(1, component1.facetContributions(ResourceExtensionFacet.class));
for (FacetContribution<IFacet> facetContribution : component2.facetContributions()) {
assertSame(facetContribution.contributor, component2);
}
assertSame(component2, component2.facetContribution(DescriptionFacet.class).contributor);
assertSame(facet3, component2.facetContribution(DescriptionFacet.class).facet);
assertSize(1, component2.facetContributions(DescriptionFacet.class));
for (FacetContribution<IFacet> facetContribution : impl.facetContributions()) {
final ILanguageComponent contributor = facetContribution.contributor;
final IFacet facet = facetContribution.facet;
if (facet.equals(facet1)) {
assertSame(component1, contributor);
} else if (facet.equals(facet2)) {
assertSame(component1, contributor);
} else if (facet.equals(facet3)) {
assertSame(component2, contributor);
} else {
fail("Facet does not equals any created facet");
}
}
}
Aggregations