Search in sources :

Example 1 with ILanguageImpl

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

the class DialectIdentifier method identify.

@Override
public IdentifiedDialect identify(FileObject resource) throws MetaborgException {
    final ILanguage strategoLanguage = languageService.getLanguage(SpoofaxConstants.LANG_STRATEGO_NAME);
    if (strategoLanguage == null) {
        final String message = logger.format("Could not find Stratego language, Stratego dialects cannot be identified for resource: {}", resource);
        throw new MetaborgRuntimeException(message);
    }
    // GTODO: use identifier service instead, but that introduces a cyclic dependency. Could use a provider.
    final ILanguageImpl strategoImpl = strategoLanguage.activeImpl();
    if (strategoImpl == null) {
        return null;
    }
    // HACK: assuming single identification facet
    final IdentificationFacet facet = strategoImpl.facet(IdentificationFacet.class);
    if (facet == null || !facet.identify(resource)) {
        return null;
    }
    try {
        final FileObject metaResource = metaResource(resource);
        if (metaResource == null) {
            return null;
        }
        final TermReader termReader = new TermReader(termFactoryService.getGeneric());
        final IStrategoTerm term = termReader.parseFromStream(metaResource.getContent().getInputStream());
        final String name = getSyntaxName(term.getSubterm(0));
        if (name == null) {
            return null;
        }
        final ILanguageImpl dialect = dialectService.getDialect(name);
        if (dialect == null) {
            final String message = String.format("Resource %s requires dialect %s, but that dialect does not exist", resource, name);
            throw new MetaborgException(message);
        }
        final ILanguageImpl base = dialectService.getBase(dialect);
        return new IdentifiedDialect(dialect, base);
    } catch (ParseError | IOException e) {
        throw new MetaborgException("Unable to open or parse .meta file", e);
    }
}
Also used : MetaborgRuntimeException(org.metaborg.core.MetaborgRuntimeException) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IdentificationFacet(org.metaborg.core.language.IdentificationFacet) MetaborgException(org.metaborg.core.MetaborgException) IOException(java.io.IOException) ILanguage(org.metaborg.core.language.ILanguage) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) ParseError(org.spoofax.terms.ParseError) FileObject(org.apache.commons.vfs2.FileObject) IdentifiedDialect(org.metaborg.core.language.dialect.IdentifiedDialect) TermReader(org.spoofax.terms.io.binary.TermReader)

Example 2 with ILanguageImpl

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

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

the class DialectService method remove.

@Override
public Iterable<ILanguageImpl> remove(ILanguageImpl base) {
    final Collection<ILanguageImpl> dialects = baseLanguageToDialects.get(base);
    if (dialects.isEmpty()) {
        return dialects;
    }
    logger.debug("Removing {} dialects for base language {}", dialects.size(), base);
    final Collection<ILanguageImpl> removedDialects = Lists.newArrayListWithCapacity(dialects.size());
    for (ILanguageImpl dialect : dialects) {
        final String name = dialect.belongsTo().name();
        nameToDialect.remove(name);
        dialectToBase.remove(dialect);
        baseLanguageToDialects.remove(base, dialect);
        try {
            // Remove dialect after updating maps, exception indicates that dialect has already been removed.
            final ILanguageComponent dialectComponent = Iterables.get(dialect.components(), 0);
            languageService.remove(dialectComponent);
        } catch (IllegalStateException e) {
            final String message = String.format("Error removing dialect %s", name);
            logger.error(message, e);
            continue;
        }
        removedDialects.add(dialect);
    }
    return removedDialects;
}
Also used : ILanguageImpl(org.metaborg.core.language.ILanguageImpl) ILanguageComponent(org.metaborg.core.language.ILanguageComponent)

Example 4 with ILanguageImpl

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

the class DialectService method remove.

@Override
public ILanguageImpl remove(String name) {
    final ILanguageImpl dialect = nameToDialect.remove(name);
    if (dialect == null) {
        final String message = String.format("Dialect with name %s does not exist", name);
        logger.error(message);
        throw new MetaborgRuntimeException(message);
    }
    logger.debug("Removing dialect {}", name);
    final ILanguageImpl base = dialectToBase.remove(dialect);
    baseLanguageToDialects.remove(base, dialect);
    try {
        // Remove dialect after updating maps, exception indicates that dialect has already been removed.
        final ILanguageComponent dialectComponent = Iterables.get(dialect.components(), 0);
        languageService.remove(dialectComponent);
    } catch (IllegalStateException e) {
        final String message = String.format("Error removing dialect %s", name);
        logger.error(message, e);
    }
    return dialect;
}
Also used : MetaborgRuntimeException(org.metaborg.core.MetaborgRuntimeException) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) ILanguageComponent(org.metaborg.core.language.ILanguageComponent)

Example 5 with ILanguageImpl

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

the class CallStrategyPrimitive 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]);
    final Iterable<ILanguageComponent> compileDeps = dependencyService.compileDeps(currentContext.project());
    final Iterable<ILanguageImpl> impls = LanguageUtils.toImpls(compileDeps);
    final List<ILanguageImpl> selectedImpls = Lists.newArrayList();
    for (ILanguageImpl impl : impls) {
        if (impl.belongsTo().name().equals(languageName)) {
            selectedImpls.add(impl);
        }
    }
    if (selectedImpls.isEmpty()) {
        final String message = logger.format("Stratego strategy call of '{}' into language '{}' failed, no language implementation found", strategyName, languageName);
        throw new MetaborgException(message);
    } else if (selectedImpls.size() > 1) {
        final String message = logger.format("Stratego strategy call of '{}' into language '{}' failed, multiple language implementations found: {}", strategyName, languageName, Joiner.on(", ").join(selectedImpls));
        throw new MetaborgException(message);
    }
    final ILanguageImpl impl = selectedImpls.get(0);
    final IContext context = contextService.get(currentContext.location(), currentContext.project(), impl);
    return common.invoke(impl, context, current, strategyName);
}
Also used : IContext(org.metaborg.core.context.IContext) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) MetaborgException(org.metaborg.core.MetaborgException) ILanguageComponent(org.metaborg.core.language.ILanguageComponent)

Aggregations

ILanguageImpl (org.metaborg.core.language.ILanguageImpl)53 FileObject (org.apache.commons.vfs2.FileObject)32 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)24 MetaborgException (org.metaborg.core.MetaborgException)17 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)15 ILanguage (org.metaborg.core.language.ILanguage)14 HybridInterpreter (org.strategoxt.HybridInterpreter)13 LanguageIdentifier (org.metaborg.core.language.LanguageIdentifier)11 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)11 Test (org.junit.Test)10 IContext (org.metaborg.core.context.IContext)10 LanguageVersion (org.metaborg.core.language.LanguageVersion)10 MetaborgRuntimeException (org.metaborg.core.MetaborgRuntimeException)9 MetaborgTest (org.metaborg.core.test.MetaborgTest)9 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)9 IProject (org.metaborg.core.project.IProject)8 Nullable (javax.annotation.Nullable)6 ISpoofaxParseUnit (org.metaborg.spoofax.core.unit.ISpoofaxParseUnit)6 IdentifiedResource (org.metaborg.core.language.IdentifiedResource)5 ISourceRegion (org.metaborg.core.source.ISourceRegion)5