use of org.metaborg.core.project.IProject in project spoofax by metaborg.
the class PlaceholderCharsPrimitive method call.
@Override
public boolean call(IContext env, Strategy[] svars, IStrategoTerm[] tvars) throws InterpreterException {
org.metaborg.core.context.IContext context = (org.metaborg.core.context.IContext) env.contextObject();
final FileObject location = context.location();
final IProject project = projectService.get(location);
if (project == null) {
return false;
}
if (languageSpecServiceProvider == null) {
// Indicates that meta-Spoofax is not available (ISpoofaxLanguageSpecService cannot be injected), but this
// should never happen because this primitive is inside meta-Spoofax. Check for null just in case.
logger.error("Language specification service is not available; static injection failed");
return false;
}
final ISpoofaxLanguageSpecService languageSpecService = languageSpecServiceProvider.get();
if (!languageSpecService.available(project)) {
return false;
}
final ISpoofaxLanguageSpec languageSpec;
try {
languageSpec = languageSpecService.get(project);
} catch (ConfigException e) {
throw new InterpreterException("Unable to get language specification name for " + location, e);
}
if (languageSpec == null) {
return false;
}
PlaceholderCharacters placeholderChars = languageSpec.config().placeholderChars();
IStrategoString prefix = env.getFactory().makeString(placeholderChars.prefix);
IStrategoString suffix = null;
if (placeholderChars.suffix != null) {
suffix = env.getFactory().makeString(placeholderChars.suffix);
} else {
suffix = env.getFactory().makeString("");
}
env.setCurrent(env.getFactory().makeTuple(prefix, suffix));
return true;
}
use of org.metaborg.core.project.IProject in project spoofax by metaborg.
the class WithLocationStreamMessagePrinter method print.
private void print(String message, boolean pardoned, @Nullable FileObject sourceFile, @Nullable MessageSeverity severity, @Nullable ISourceRegion sourceRegion) {
if (pardoned) {
return;
}
final StringBuilder sb = new StringBuilder();
// if available, print filename, line number, and column
if (sourceFile != null) {
try {
// source file name relative to project location (so that we don't output long absolute paths)
final IProject project = projectService.get(sourceFile);
String relativeSourceFilename = sourceFile.getName().getBaseName();
if (// projectService.get() is @Nullable
project != null && // don't "relativize" if project == source
project.location().getName() != sourceFile.getName()) {
relativeSourceFilename = project.location().getName().getRelativeName(sourceFile.getName());
}
sb.append(relativeSourceFilename);
sb.append(':');
if (sourceRegion != null && sourceRegion.startRow() != -1 && sourceRegion.startColumn() != -1) {
// startRow() is in [0, #lines)
sb.append(sourceRegion.startRow() + 1);
sb.append(':');
sb.append(sourceRegion.startColumn() + 1);
sb.append(':');
}
sb.append(' ');
} catch (FileSystemException ignored) {
}
}
// print severity and message
if (severity != null) {
sb.append(severity.name().toLowerCase());
sb.append(": ");
}
sb.append(message);
sb.append('\n');
// if available, pretty-print source code
if (sourceFile != null && sourceRegion != null) {
try {
final String affectedSource = AffectedSourceHelper.affectedSourceText(sourceRegion, sourceTextService.text(sourceFile), " ");
if (affectedSource != null) {
// affectedSourceText() is @Nullable
sb.append(affectedSource);
}
} catch (IOException ignored) {
}
}
outputStream.print(sb.toString());
outputStream.flush();
}
use of org.metaborg.core.project.IProject in project spoofax by metaborg.
the class StrategoRuntimeService method runtime.
@Override
public HybridInterpreter runtime(ILanguageComponent component, FileObject location, boolean typesmart) throws MetaborgException {
HybridInterpreter prototype = prototypes.get(component);
if (prototype == null) {
prototype = createPrototype(component);
}
final IProject project = projectService.get(location);
final HybridInterpreter runtime = clone(prototype, location, component, project, typesmart);
return runtime;
}
use of org.metaborg.core.project.IProject in project spoofax by metaborg.
the class LanguageSourceDirectoriesPrimitive method call.
@Override
protected IStrategoTerm call(IStrategoTerm current, Strategy[] svars, IStrategoTerm[] tvars, ITermFactory factory, IContext context) {
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 Iterable<FileObject> sourceLocations = languagePathService.sourcePaths(project, languageName);
final List<IStrategoTerm> terms = Lists.newArrayList();
for (FileObject sourceLocation : sourceLocations) {
terms.add(factory.makeString(sourceLocation.getName().getURI()));
}
return factory.makeList(terms);
}
use of org.metaborg.core.project.IProject 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