Search in sources :

Example 1 with InterpreterException

use of org.spoofax.interpreter.core.InterpreterException 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 InterpreterException

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

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

the class AnalysisPrimitive method call.

@Override
public Optional<? extends IStrategoTerm> call(IScopeGraphContext<?> context, IStrategoTerm sterm, List<IStrategoTerm> sterms, ITermFactory factory) throws InterpreterException {
    if (sterms.size() < 1) {
        throw new IllegalArgumentException("Expected as first term argument: analysis");
    }
    final IStrategoTerm analysisTerm = sterms.get(0);
    final IScopeGraphUnit analysis;
    final Optional<IScopeGraphUnit> maybeAnalysis = StrategoBlob.match(analysisTerm, IScopeGraphUnit.class);
    if (maybeAnalysis.isPresent()) {
        analysis = maybeAnalysis.get();
    } else if (Tools.isTermAppl(analysisTerm) && Tools.hasConstructor((IStrategoAppl) analysisTerm, "AnalysisToken", 0)) {
        // TODO Remove legacy case after bootstrapping
        analysis = StrategoTermIndices.get(analysisTerm).map(idx -> context.unit(idx.getResource())).orElseThrow(() -> new IllegalArgumentException("Not a valid analysis term."));
    } else {
        throw new IllegalArgumentException("Not a valid analysis term.");
    }
    return call(analysis, sterm, sterms, factory);
}
Also used : NotImplementedException(org.spoofax.terms.util.NotImplementedException) ITerm(mb.nabl2.terms.ITerm) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) StrategoTerms(mb.nabl2.stratego.StrategoTerms) Tools(org.spoofax.interpreter.core.Tools) ITermFactory(org.spoofax.interpreter.terms.ITermFactory) IScopeGraphContext(mb.nabl2.spoofax.analysis.IScopeGraphContext) Collectors(java.util.stream.Collectors) List(java.util.List) StrategoTermIndices(mb.nabl2.stratego.StrategoTermIndices) InterpreterException(org.spoofax.interpreter.core.InterpreterException) ConstraintTerms(mb.nabl2.stratego.ConstraintTerms) StrategoBlob(mb.nabl2.stratego.StrategoBlob) Optional(java.util.Optional) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IScopeGraphUnit(mb.nabl2.spoofax.analysis.IScopeGraphUnit) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IScopeGraphUnit(mb.nabl2.spoofax.analysis.IScopeGraphUnit)

Example 4 with InterpreterException

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

the class StrategoRuntimeService method loadCtrees.

private static void loadCtrees(HybridInterpreter runtime, Iterable<FileObject> ctrees) throws MetaborgException {
    try {
        for (FileObject file : ctrees) {
            logger.trace("Loading ctree {}", file.getName());
            runtime.load(new BufferedInputStream(file.getContent().getInputStream()));
        }
    } catch (IOException | InterpreterException e) {
        throw new MetaborgException("Failed to load ctree", e);
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InterpreterException(org.spoofax.interpreter.core.InterpreterException) MetaborgException(org.metaborg.core.MetaborgException) FileObject(org.apache.commons.vfs2.FileObject) IOException(java.io.IOException)

Example 5 with InterpreterException

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

the class ASpoofaxPrimitive method call.

@Override
public boolean call(IContext context, Strategy[] svars, IStrategoTerm[] tvars) throws InterpreterException {
    final IStrategoTerm current = context.current();
    final ITermFactory factory = context.getFactory();
    try {
        final IStrategoTerm newCurrent = call(current, svars, tvars, factory, context);
        if (newCurrent != null) {
            context.setCurrent(newCurrent);
            return true;
        }
    } catch (MetaborgException | IOException e) {
        throw new InterpreterException("Executing primitive " + name + " failed unexpectedly", e);
    }
    return false;
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) InterpreterException(org.spoofax.interpreter.core.InterpreterException) MetaborgException(org.metaborg.core.MetaborgException) IOException(java.io.IOException) ITermFactory(org.spoofax.interpreter.terms.ITermFactory)

Aggregations

InterpreterException (org.spoofax.interpreter.core.InterpreterException)8 FileObject (org.apache.commons.vfs2.FileObject)4 IOException (java.io.IOException)3 IContext (org.spoofax.interpreter.core.IContext)3 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)3 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)3 List (java.util.List)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 IScopeGraphContext (mb.nabl2.spoofax.analysis.IScopeGraphContext)2 IScopeGraphUnit (mb.nabl2.spoofax.analysis.IScopeGraphUnit)2 ConstraintTerms (mb.nabl2.stratego.ConstraintTerms)2 StrategoBlob (mb.nabl2.stratego.StrategoBlob)2 StrategoTerms (mb.nabl2.stratego.StrategoTerms)2 ITerm (mb.nabl2.terms.ITerm)2 MetaborgException (org.metaborg.core.MetaborgException)2 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