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;
}
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;
}
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;
}
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;
}
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);
}
Aggregations