Search in sources :

Example 11 with IProject

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;
}
Also used : IContext(org.spoofax.interpreter.core.IContext) InterpreterException(org.spoofax.interpreter.core.InterpreterException) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) ConfigException(org.metaborg.core.config.ConfigException) PlaceholderCharacters(org.metaborg.spoofax.meta.core.config.PlaceholderCharacters) IProject(org.metaborg.core.project.IProject) ISpoofaxLanguageSpec(org.metaborg.spoofax.meta.core.project.ISpoofaxLanguageSpec) ISpoofaxLanguageSpecService(org.metaborg.spoofax.meta.core.project.ISpoofaxLanguageSpecService) FileObject(org.apache.commons.vfs2.FileObject)

Example 12 with IProject

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();
}
Also used : FileSystemException(org.apache.commons.vfs2.FileSystemException) IOException(java.io.IOException) IProject(org.metaborg.core.project.IProject)

Example 13 with IProject

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;
}
Also used : HybridInterpreter(org.strategoxt.HybridInterpreter) IProject(org.metaborg.core.project.IProject)

Example 14 with IProject

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);
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) FileObject(org.apache.commons.vfs2.FileObject) IProject(org.metaborg.core.project.IProject)

Example 15 with IProject

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

IProject (org.metaborg.core.project.IProject)21 FileObject (org.apache.commons.vfs2.FileObject)13 MetaborgException (org.metaborg.core.MetaborgException)12 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)9 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)8 IContext (org.metaborg.core.context.IContext)6 HybridInterpreter (org.strategoxt.HybridInterpreter)6 ConfigException (org.metaborg.core.config.ConfigException)5 ISpoofaxLanguageSpec (org.metaborg.spoofax.meta.core.project.ISpoofaxLanguageSpec)5 ISpoofaxLanguageSpecService (org.metaborg.spoofax.meta.core.project.ISpoofaxLanguageSpecService)5 ISourceRegion (org.metaborg.core.source.ISourceRegion)4 SourceRegion (org.metaborg.core.source.SourceRegion)4 TermWithRegion (org.metaborg.spoofax.core.tracing.TracingCommon.TermWithRegion)4 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)4 Nullable (javax.annotation.Nullable)3 MetaborgRuntimeException (org.metaborg.core.MetaborgRuntimeException)3 ContextException (org.metaborg.core.context.ContextException)3 ILanguage (org.metaborg.core.language.ILanguage)3 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)3 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)3