Search in sources :

Example 31 with MetaborgException

use of org.metaborg.core.MetaborgException in project spoofax by metaborg.

the class StrategoRuntimeService method loadJars.

private void loadJars(HybridInterpreter runtime, Iterable<FileObject> jars) throws MetaborgException {
    try {
        final URL[] classpath = new URL[Iterables.size(jars)];
        int i = 0;
        for (FileObject jar : jars) {
            final File localJar = resourceService.localFile(jar);
            classpath[i] = localJar.toURI().toURL();
            ++i;
        }
        logger.trace("Loading jar files {}", (Object) classpath);
        final ClassLoader classLoader = new StrategoRuntimeClassLoader(additionalClassLoaders);
        runtime.loadJars(classLoader, classpath);
    } catch (IncompatibleJarException | IOException | MetaborgRuntimeException e) {
        throw new MetaborgException("Failed to load JAR", e);
    }
}
Also used : MetaborgRuntimeException(org.metaborg.core.MetaborgRuntimeException) IncompatibleJarException(org.strategoxt.IncompatibleJarException) MetaborgException(org.metaborg.core.MetaborgException) FileObject(org.apache.commons.vfs2.FileObject) IOException(java.io.IOException) File(java.io.File) URL(java.net.URL)

Example 32 with MetaborgException

use of org.metaborg.core.MetaborgException 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 33 with MetaborgException

use of org.metaborg.core.MetaborgException 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)

Example 34 with MetaborgException

use of org.metaborg.core.MetaborgException in project spoofax by metaborg.

the class OutlineService method outline.

@Override
public IOutline outline(ISpoofaxAnalyzeUnit result) throws MetaborgException {
    if (!result.valid() || !result.hasAst()) {
        return null;
    }
    final FileObject source = result.source();
    final IContext context = result.context();
    final ILanguageImpl language = context.language();
    final FacetContribution<OutlineFacet> facetContrib = facet(language);
    final OutlineFacet facet = facetContrib.facet;
    final ILanguageComponent contributor = facetContrib.contributor;
    final String strategy = facet.strategyName;
    try {
        final HybridInterpreter interpreter = strategoRuntimeService.runtime(contributor, context, true);
        final IStrategoTerm input = common.builderInputTerm(result.ast(), source, context.location());
        final IStrategoTerm outlineTerm = common.invoke(interpreter, input, strategy);
        if (outlineTerm == null) {
            return null;
        }
        final IOutline outline = toOutline(outlineTerm, facet.expandTo, contributor.location());
        return outline;
    } catch (MetaborgException e) {
        throw new MetaborgException("Creating outline failed", e);
    }
}
Also used : IContext(org.metaborg.core.context.IContext) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) IOutline(org.metaborg.core.outline.IOutline) MetaborgException(org.metaborg.core.MetaborgException) HybridInterpreter(org.strategoxt.HybridInterpreter) FileObject(org.apache.commons.vfs2.FileObject) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) ILanguageComponent(org.metaborg.core.language.ILanguageComponent)

Example 35 with MetaborgException

use of org.metaborg.core.MetaborgException in project spoofax by metaborg.

the class OutlineService method outline.

@Override
public IOutline outline(ISpoofaxParseUnit result) throws MetaborgException {
    if (!result.valid()) {
        return null;
    }
    final FileObject source = result.source();
    final IProject project = projectService.get(source);
    final ILanguageImpl langImpl = result.input().langImpl();
    @Nullable IContext context;
    if (project == null) {
        context = null;
    } else {
        try {
            context = contextService.get(source, project, langImpl);
        } catch (ContextException | MetaborgRuntimeException e) {
            // Failed to get a context, ignore and use the source file to get a stratego runtime later.
            context = null;
        }
    }
    final FacetContribution<OutlineFacet> facetContrib = facet(langImpl);
    final OutlineFacet facet = facetContrib.facet;
    final ILanguageComponent contributor = facetContrib.contributor;
    final String strategy = facet.strategyName;
    try {
        final HybridInterpreter interpreter;
        if (context == null) {
            interpreter = strategoRuntimeService.runtime(contributor, source, true);
        } else {
            interpreter = strategoRuntimeService.runtime(contributor, context, true);
        }
        final IStrategoTerm input = common.builderInputTerm(result.ast(), source, source);
        final IStrategoTerm outlineTerm = common.invoke(interpreter, input, strategy);
        if (outlineTerm == null) {
            return null;
        }
        final IOutline outline = toOutline(outlineTerm, facet.expandTo, contributor.location());
        return outline;
    } catch (MetaborgException e) {
        throw new MetaborgException("Creating outline failed", e);
    }
}
Also used : MetaborgRuntimeException(org.metaborg.core.MetaborgRuntimeException) IContext(org.metaborg.core.context.IContext) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IOutline(org.metaborg.core.outline.IOutline) MetaborgException(org.metaborg.core.MetaborgException) HybridInterpreter(org.strategoxt.HybridInterpreter) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IProject(org.metaborg.core.project.IProject) ContextException(org.metaborg.core.context.ContextException) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) FileObject(org.apache.commons.vfs2.FileObject) ILanguageComponent(org.metaborg.core.language.ILanguageComponent) Nullable(javax.annotation.Nullable)

Aggregations

MetaborgException (org.metaborg.core.MetaborgException)49 FileObject (org.apache.commons.vfs2.FileObject)25 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)19 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)17 HybridInterpreter (org.strategoxt.HybridInterpreter)14 IProject (org.metaborg.core.project.IProject)12 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)11 IOException (java.io.IOException)9 IContext (org.metaborg.core.context.IContext)9 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)9 Nullable (javax.annotation.Nullable)8 AnalysisException (org.metaborg.core.analysis.AnalysisException)8 FileSystemException (org.apache.commons.vfs2.FileSystemException)7 MetaborgRuntimeException (org.metaborg.core.MetaborgRuntimeException)7 ISpoofaxAnalyzeResults (org.metaborg.spoofax.core.analysis.ISpoofaxAnalyzeResults)6 SpoofaxAnalyzeResults (org.metaborg.spoofax.core.analysis.SpoofaxAnalyzeResults)6 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)6 ILanguage (org.metaborg.core.language.ILanguage)5 ISourceRegion (org.metaborg.core.source.ISourceRegion)5 ISpoofaxParseUnit (org.metaborg.spoofax.core.unit.ISpoofaxParseUnit)5