Search in sources :

Example 31 with ILanguageImpl

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

the class DialectService method add.

@Override
public ILanguageImpl add(String name, FileObject location, ILanguageImpl base, IFacet syntaxFacet) {
    if (nameToDialect.containsKey(name)) {
        final String message = String.format("Dialect with name %s already exists", name);
        logger.error(message);
        throw new MetaborgRuntimeException(message);
    }
    logger.debug("Adding dialect {} from {} with {} as base", name, location, base);
    final ILanguageImpl dialect = createDialect(name, location, base, syntaxFacet, true, true);
    nameToDialect.put(name, dialect);
    dialectToBase.put(dialect, base);
    baseLanguageToDialects.put(base, dialect);
    return dialect;
}
Also used : MetaborgRuntimeException(org.metaborg.core.MetaborgRuntimeException) ILanguageImpl(org.metaborg.core.language.ILanguageImpl)

Example 32 with ILanguageImpl

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

the class DialectService method update.

@Override
public Iterable<ILanguageImpl> update(ILanguageImpl base) {
    final Collection<ILanguageImpl> dialects = baseLanguageToDialects.get(base);
    if (dialects.isEmpty()) {
        return dialects;
    }
    logger.debug("Updating base language for {} dialects", dialects.size());
    final Collection<ILanguageImpl> newDialects = Lists.newArrayListWithCapacity(dialects.size());
    for (ILanguageImpl dialect : dialects) {
        final String name = dialect.belongsTo().name();
        final FileObject location = Iterables.get(dialect.components(), 0).location();
        // HACK: assuming single syntax facet
        final IFacet syntaxFacet = dialect.facet(SyntaxFacet.class);
        final ILanguageImpl newDialect;
        try {
            // Add dialect before updating maps, adding can cause an exception; maps should not be updated.
            // Adding reloads the dialect because location is the same, no need to remove old dialect.
            // GTODO: what if id's or version change?
            newDialect = createDialect(name, location, base, syntaxFacet, true, true);
        } catch (IllegalStateException e) {
            final String message = String.format("Error updating dialect %s", name);
            logger.error(message, e);
            continue;
        }
        nameToDialect.put(name, newDialect);
        newDialects.add(newDialect);
    }
    return newDialects;
}
Also used : ILanguageImpl(org.metaborg.core.language.ILanguageImpl) IFacet(org.metaborg.core.language.IFacet) FileObject(org.apache.commons.vfs2.FileObject)

Example 33 with ILanguageImpl

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

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

the class DialectService method update.

@Override
public ILanguageImpl update(String name, IFacet syntaxFacet) {
    final ILanguageImpl dialect = nameToDialect.get(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("Updating syntax facet for dialect {}", name);
    final FileObject location = Iterables.get(dialect.components(), 0).location();
    final ILanguageImpl newDialect = createDialect(name, location, dialect, syntaxFacet, false, false);
    nameToDialect.put(name, newDialect);
    return newDialect;
}
Also used : MetaborgRuntimeException(org.metaborg.core.MetaborgRuntimeException) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) FileObject(org.apache.commons.vfs2.FileObject)

Example 35 with ILanguageImpl

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

the class LanguageSourceFilesPrimitive method call.

@Override
protected IStrategoTerm call(IStrategoTerm current, Strategy[] svars, IStrategoTerm[] tvars, ITermFactory factory, IContext context) throws MetaborgException {
    if (!Tools.isTermString(tvars[0])) {
        return null;
    }
    final IProject project = projectService.get(context.location());
    if (project == null) {
        return factory.makeList();
    }
    // GTODO: require language identifier instead of language name
    final String languageName = Tools.asJavaString(tvars[0]);
    final ILanguage language = languageService.getLanguage(languageName);
    if (language == null) {
        final String message = String.format("Getting include files for %s failed, language could not be found", languageName);
        throw new MetaborgException(message);
    }
    final ILanguageImpl impl = language.activeImpl();
    if (impl == null) {
        final String message = String.format("Getting include files for %s failed, no active language implementation could be found", languageName);
        throw new MetaborgException(message);
    }
    final Iterable<IdentifiedResource> sourceFiles = languagePathService.sourceFiles(project, impl);
    final List<IStrategoTerm> terms = Lists.newArrayList();
    for (IdentifiedResource sourceFile : sourceFiles) {
        terms.add(factory.makeString(sourceFile.resource.getName().getURI()));
    }
    return factory.makeList(terms);
}
Also used : ILanguage(org.metaborg.core.language.ILanguage) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) MetaborgException(org.metaborg.core.MetaborgException) IdentifiedResource(org.metaborg.core.language.IdentifiedResource) IProject(org.metaborg.core.project.IProject)

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