Search in sources :

Example 6 with InterpreterException

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

the class LegacyParseFilePrimitive method call.

@Override
public boolean call(IContext env, Strategy[] strategies, IStrategoTerm[] terms) throws InterpreterException {
    if (!Tools.isTermString(terms[0]))
        return false;
    if (!Tools.isTermString(terms[3]))
        return false;
    try {
        final String pathOrInput = Tools.asJavaString(terms[0]);
        final String pathOrInput2 = Tools.asJavaString(terms[3]);
        FileObject resource;
        String text;
        try {
            resource = resourceService.resolve(pathOrInput);
            if (!resource.exists() || resource.getType() != FileType.FILE) {
                resource = resourceService.resolve(pathOrInput2);
                text = pathOrInput;
            } else {
                text = sourceTextService.text(resource);
            }
        } catch (MetaborgRuntimeException | IOException e) {
            resource = resourceService.resolve(pathOrInput2);
            text = pathOrInput;
        }
        if (resource.getType() != FileType.FILE) {
            return false;
        }
        final IdentifiedResource identifiedResource = languageIdentifierService.identifyToResource(resource);
        if (identifiedResource == null) {
            return false;
        }
        final ISpoofaxInputUnit input = unitService.inputUnit(resource, text, identifiedResource.language, identifiedResource.dialect);
        final ISpoofaxParseUnit result = syntaxService.parse(input);
        if (result.valid() && result.success()) {
            env.setCurrent(result.ast());
        } else {
            return false;
        }
    } catch (ParseException | IOException e) {
        throw new InterpreterException("Parsing failed unexpectedly", e);
    }
    return true;
}
Also used : ISpoofaxParseUnit(org.metaborg.spoofax.core.unit.ISpoofaxParseUnit) MetaborgRuntimeException(org.metaborg.core.MetaborgRuntimeException) ISpoofaxInputUnit(org.metaborg.spoofax.core.unit.ISpoofaxInputUnit) InterpreterException(org.spoofax.interpreter.core.InterpreterException) FileObject(org.apache.commons.vfs2.FileObject) IOException(java.io.IOException) ParseException(org.metaborg.core.syntax.ParseException) IdentifiedResource(org.metaborg.core.language.IdentifiedResource)

Example 7 with InterpreterException

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

Example 8 with InterpreterException

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

the class SG_get_resource_analysis method call.

@Override
public Optional<? extends IStrategoTerm> call(IScopeGraphContext<?> context, IStrategoTerm sterm, List<IStrategoTerm> sterms, ITermFactory factory) throws InterpreterException {
    if (!Tools.isTermString(sterm)) {
        throw new InterpreterException("Expect a resource path.");
    }
    String resource = Tools.asJavaString(sterm);
    final IScopeGraphUnit unit = context.unit(resource);
    return Optional.of(new StrategoBlob(unit));
}
Also used : InterpreterException(org.spoofax.interpreter.core.InterpreterException) IScopeGraphUnit(mb.nabl2.spoofax.analysis.IScopeGraphUnit) StrategoBlob(mb.nabl2.stratego.StrategoBlob)

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