Search in sources :

Example 1 with SyntaxFacet

use of org.metaborg.spoofax.core.syntax.SyntaxFacet 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 SyntaxFacet

use of org.metaborg.spoofax.core.syntax.SyntaxFacet in project spoofax by metaborg.

the class DialectProcessor method update.

@Override
public void update(FileObject location, Iterable<ResourceChange> changes) {
    final int numChanges = Iterables.size(changes);
    if (numChanges == 0) {
        return;
    }
    final ILanguage strategoLanguage = languageService.getLanguage(SpoofaxConstants.LANG_STRATEGO_NAME);
    if (strategoLanguage == null) {
        logger.debug("Could not find Stratego language, Stratego dialects cannot be updated");
        return;
    }
    final ILanguageImpl strategoImpl = strategoLanguage.activeImpl();
    if (strategoImpl == null) {
        logger.debug("Could not find active Stratego language implementation, " + "Stratego dialects cannot be updated");
        return;
    }
    logger.debug("Processing dialect updates for {}", location);
    // HACK: assuming single syntax facet
    final SyntaxFacet baseFacet = strategoImpl.facet(SyntaxFacet.class);
    if (baseFacet == null) {
        logger.debug("Active Stratego language implementation has no syntax facet, " + "Stratego dialects cannot be updated");
        return;
    }
    for (ResourceChange change : changes) {
        final FileObject resource = change.resource;
        try {
            if (!FileSelectorUtils.include(selector, resource, location)) {
                continue;
            }
        } catch (FileSystemException e) {
            continue;
        }
        final String fileName = FilenameUtils.getBaseName(resource.getName().getBaseName());
        final SyntaxFacet newFacet = new SyntaxFacet(resource, baseFacet.completionParseTable, baseFacet.startSymbols, baseFacet.singleLineCommentPrefixes, baseFacet.multiLineCommentCharacters, baseFacet.fenceCharacters);
        final ResourceChangeKind changeKind = change.kind;
        try {
            switch(changeKind) {
                case Create:
                    add(fileName, resource, strategoImpl, newFacet);
                    break;
                case Delete:
                    remove(fileName, resource);
                    break;
                case Modify:
                    update(fileName, resource, newFacet);
                    break;
                case Rename:
                    if (change.from != null) {
                        remove(fileName, resource);
                    }
                    if (change.to != null) {
                        add(fileName, resource, strategoImpl, newFacet);
                    }
                    break;
                case Copy:
                    if (change.to != null) {
                        add(fileName, resource, strategoImpl, newFacet);
                    }
                    break;
                default:
                    logger.error("Unhandled resource change kind {}", changeKind);
                    break;
            }
        } catch (MetaborgRuntimeException e) {
            logger.error("Failed to update dialect", e);
        }
    }
}
Also used : ILanguage(org.metaborg.core.language.ILanguage) MetaborgRuntimeException(org.metaborg.core.MetaborgRuntimeException) SyntaxFacet(org.metaborg.spoofax.core.syntax.SyntaxFacet) FileSystemException(org.apache.commons.vfs2.FileSystemException) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) ResourceChangeKind(org.metaborg.core.resource.ResourceChangeKind) FileObject(org.apache.commons.vfs2.FileObject) ResourceChange(org.metaborg.core.resource.ResourceChange)

Example 3 with SyntaxFacet

use of org.metaborg.spoofax.core.syntax.SyntaxFacet 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 4 with SyntaxFacet

use of org.metaborg.spoofax.core.syntax.SyntaxFacet in project spoofax by metaborg.

the class LanguageComponentFactory method request.

private IComponentCreationConfigRequest request(FileObject root) throws MetaborgException {
    final Collection<String> errors = Lists.newLinkedList();
    final Collection<Throwable> exceptions = Lists.newLinkedList();
    final ConfigRequest<ILanguageComponentConfig> configRequest = componentConfigService.get(root);
    if (!configRequest.valid()) {
        for (IMessage message : configRequest.errors()) {
            errors.add(message.message());
            final Throwable exception = message.exception();
            if (exception != null) {
                exceptions.add(exception);
            }
        }
    }
    final ILanguageComponentConfig config = configRequest.config();
    if (config == null) {
        final String message = logger.format("Cannot retrieve language component configuration at {}", root);
        errors.add(message);
        return new ComponentFactoryRequest(root, errors, exceptions);
    }
    final IStrategoAppl esvTerm;
    try {
        final FileObject esvFile = root.resolveFile("target/metaborg/editor.esv.af");
        if (!esvFile.exists()) {
            esvTerm = null;
        } else {
            esvTerm = esvTerm(root, esvFile);
        }
    } catch (ParseError | IOException | MetaborgException e) {
        exceptions.add(e);
        return new ComponentFactoryRequest(root, errors, exceptions);
    }
    SyntaxFacet syntaxFacet = null;
    StrategoRuntimeFacet strategoRuntimeFacet = null;
    if (esvTerm != null) {
        try {
            syntaxFacet = SyntaxFacetFromESV.create(esvTerm, root);
            if (syntaxFacet != null) {
                Iterables.addAll(errors, syntaxFacet.available());
            }
        } catch (FileSystemException e) {
            exceptions.add(e);
        }
        try {
            strategoRuntimeFacet = StrategoRuntimeFacetFromESV.create(esvTerm, root);
            if (strategoRuntimeFacet != null) {
                Iterables.addAll(errors, strategoRuntimeFacet.available(resourceService));
            }
        } catch (IOException e) {
            exceptions.add(e);
        }
    }
    final ComponentFactoryRequest request;
    if (errors.isEmpty() && exceptions.isEmpty()) {
        request = new ComponentFactoryRequest(root, config, esvTerm, syntaxFacet, strategoRuntimeFacet);
    } else {
        request = new ComponentFactoryRequest(root, errors, exceptions);
    }
    return request;
}
Also used : SyntaxFacet(org.metaborg.spoofax.core.syntax.SyntaxFacet) IMessage(org.metaborg.core.messages.IMessage) MetaborgException(org.metaborg.core.MetaborgException) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) IOException(java.io.IOException) FileSystemException(org.apache.commons.vfs2.FileSystemException) ParseError(org.spoofax.terms.ParseError) FileObject(org.apache.commons.vfs2.FileObject) ILanguageComponentConfig(org.metaborg.core.config.ILanguageComponentConfig) StrategoRuntimeFacet(org.metaborg.spoofax.core.stratego.StrategoRuntimeFacet)

Example 5 with SyntaxFacet

use of org.metaborg.spoofax.core.syntax.SyntaxFacet 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

SyntaxFacet (org.metaborg.spoofax.core.syntax.SyntaxFacet)5 FileObject (org.apache.commons.vfs2.FileObject)4 ILanguageComponentConfig (org.metaborg.core.config.ILanguageComponentConfig)3 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)3 IdentificationFacet (org.metaborg.core.language.IdentificationFacet)3 StrategoRuntimeFacet (org.metaborg.spoofax.core.stratego.StrategoRuntimeFacet)3 FileSystemException (org.apache.commons.vfs2.FileSystemException)2 MetaborgException (org.metaborg.core.MetaborgException)2 ComponentCreationConfig (org.metaborg.core.language.ComponentCreationConfig)2 ILanguage (org.metaborg.core.language.ILanguage)2 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)2 LanguageContributionIdentifier (org.metaborg.core.language.LanguageContributionIdentifier)2 LanguageIdentifier (org.metaborg.core.language.LanguageIdentifier)2 ResourceExtensionFacet (org.metaborg.core.language.ResourceExtensionFacet)2 AnalysisFacet (org.metaborg.spoofax.core.analysis.AnalysisFacet)2 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)2 IOException (java.io.IOException)1 Test (org.junit.Test)1 MetaborgRuntimeException (org.metaborg.core.MetaborgRuntimeException)1 ContextFacet (org.metaborg.core.context.ContextFacet)1