Search in sources :

Example 1 with IContext

use of org.spoofax.interpreter.core.IContext in project spoofax by metaborg.

the class CheckSdf2TablePrimitive 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;
    }
    if (!languageSpec.config().sdfEnabled()) {
        env.setCurrent(env.getFactory().makeString("disabled"));
        return true;
    }
    env.setCurrent(env.getFactory().makeString(languageSpec.config().sdf2tableVersion().toString()));
    return true;
}
Also used : IContext(org.spoofax.interpreter.core.IContext) ISpoofaxLanguageSpec(org.metaborg.spoofax.meta.core.project.ISpoofaxLanguageSpec) ISpoofaxLanguageSpecService(org.metaborg.spoofax.meta.core.project.ISpoofaxLanguageSpecService) InterpreterException(org.spoofax.interpreter.core.InterpreterException) ConfigException(org.metaborg.core.config.ConfigException) FileObject(org.apache.commons.vfs2.FileObject) IProject(org.metaborg.core.project.IProject)

Example 2 with IContext

use of org.spoofax.interpreter.core.IContext in project spoofax by metaborg.

the class GetContextualGrammarPrimitive 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();
    String path = ((IStrategoString) tvars[0]).stringValue();
    ResourceService rs = context.injector().getInstance(ResourceService.class);
    FileObject fo = rs.resolve(path + "/target/metaborg/ctxgrammar.aterm");
    InputStream inputStream;
    String text = "";
    try {
        inputStream = fo.getContent().getInputStream();
        text = IOUtils.toString(inputStream, StandardCharsets.UTF_8.name());
    } catch (IOException e) {
        e.printStackTrace();
    }
    final ITermFactory tf = env.getFactory();
    env.setCurrent(tf.parseFromString(text));
    return true;
}
Also used : IContext(org.spoofax.interpreter.core.IContext) InputStream(java.io.InputStream) ResourceService(org.metaborg.core.resource.ResourceService) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IOException(java.io.IOException) FileObject(org.apache.commons.vfs2.FileObject) ITermFactory(org.spoofax.interpreter.terms.ITermFactory)

Example 3 with IContext

use of org.spoofax.interpreter.core.IContext 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 4 with IContext

use of org.spoofax.interpreter.core.IContext in project nabl by metaborg.

the class ScopeGraphContextPrimitive method call.

@Override
public final boolean call(IContext env, Strategy[] svars, IStrategoTerm[] tvars) throws InterpreterException {
    final Object contextObj = env.contextObject();
    if (contextObj == null) {
        logger.warn("Context is null.");
        return false;
    }
    if (!(contextObj instanceof IScopeGraphContext)) {
        throw new InterpreterException("Context does not implement IScopeGraphContext");
    }
    final IScopeGraphContext<?> context = (IScopeGraphContext<?>) env.contextObject();
    List<IStrategoTerm> termArgs = Arrays.asList(tvars);
    Optional<? extends IStrategoTerm> result;
    try (IClosableLock lock = context.guard()) {
        result = call(context, env.current(), termArgs, env.getFactory());
    }
    return result.map(t -> {
        env.setCurrent(t);
        return true;
    }).orElse(false);
}
Also used : Arrays(java.util.Arrays) ITerm(mb.nabl2.terms.ITerm) StrategoTerms(mb.nabl2.stratego.StrategoTerms) ITermFactory(org.spoofax.interpreter.terms.ITermFactory) IScopeGraphContext(mb.nabl2.spoofax.analysis.IScopeGraphContext) Collectors(java.util.stream.Collectors) IContext(org.spoofax.interpreter.core.IContext) List(java.util.List) Strategy(org.spoofax.interpreter.stratego.Strategy) InterpreterException(org.spoofax.interpreter.core.InterpreterException) ConstraintTerms(mb.nabl2.stratego.ConstraintTerms) Optional(java.util.Optional) LoggerUtils(org.metaborg.util.log.LoggerUtils) AbstractPrimitive(org.spoofax.interpreter.library.AbstractPrimitive) IClosableLock(org.metaborg.util.concurrent.IClosableLock) ILogger(org.metaborg.util.log.ILogger) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IScopeGraphContext(mb.nabl2.spoofax.analysis.IScopeGraphContext) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) InterpreterException(org.spoofax.interpreter.core.InterpreterException) IClosableLock(org.metaborg.util.concurrent.IClosableLock)

Aggregations

IContext (org.spoofax.interpreter.core.IContext)4 FileObject (org.apache.commons.vfs2.FileObject)3 InterpreterException (org.spoofax.interpreter.core.InterpreterException)3 ConfigException (org.metaborg.core.config.ConfigException)2 IProject (org.metaborg.core.project.IProject)2 ISpoofaxLanguageSpec (org.metaborg.spoofax.meta.core.project.ISpoofaxLanguageSpec)2 ISpoofaxLanguageSpecService (org.metaborg.spoofax.meta.core.project.ISpoofaxLanguageSpecService)2 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)2 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 IScopeGraphContext (mb.nabl2.spoofax.analysis.IScopeGraphContext)1 ConstraintTerms (mb.nabl2.stratego.ConstraintTerms)1 StrategoTerms (mb.nabl2.stratego.StrategoTerms)1 ITerm (mb.nabl2.terms.ITerm)1 ResourceService (org.metaborg.core.resource.ResourceService)1