Search in sources :

Example 11 with MetaborgException

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

the class LanguageSpecBuilder method initialize.

public void initialize(LanguageSpecBuildInput input) throws MetaborgException {
    final SpoofaxCommonPaths paths = new SpoofaxLangSpecCommonPaths(input.languageSpec().location());
    try {
        paths.srcGenDir().createFolder();
        paths.targetMetaborgDir().createFolder();
    } catch (FileSystemException e) {
        throw new MetaborgException("Initializing directories failed", e);
    }
    for (IBuildStep buildStep : buildSteps) {
        buildStep.execute(LanguageSpecBuildPhase.initialize, input);
    }
}
Also used : FileSystemException(org.apache.commons.vfs2.FileSystemException) MetaborgException(org.metaborg.core.MetaborgException) SpoofaxCommonPaths(org.metaborg.spoofax.core.build.SpoofaxCommonPaths)

Example 12 with MetaborgException

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

the class LanguageSpecBuilder method generateSourcesBuilderInput.

private GenerateSourcesBuilder.Input generateSourcesBuilderInput(LanguageSpecBuildInput input) throws FileSystemException, MetaborgException {
    final ISpoofaxLanguageSpec languageSpec = input.languageSpec();
    final ISpoofaxLanguageSpecConfig config = languageSpec.config();
    final FileObject baseLoc = input.languageSpec().location();
    final SpoofaxLangSpecCommonPaths paths = new SpoofaxLangSpecCommonPaths(baseLoc);
    final FileObject buildInfoLoc = paths.plutoBuildInfoDir();
    final SpoofaxContext context = new SpoofaxContext(baseLoc, buildInfoLoc);
    // SDF
    final Boolean sdfEnabled = config.sdfEnabled();
    final String sdfModule = config.sdfName();
    final FileObject sdfFileCandidate;
    final SdfVersion sdfVersion = config.sdfVersion();
    final Sdf2tableVersion sdf2tableVersion = config.sdf2tableVersion();
    switch(sdfVersion) {
        case sdf2:
            final Iterable<FileObject> sdfRoots = languagePathService.sourcePaths(input.project(), SpoofaxConstants.LANG_SDF_NAME);
            sdfFileCandidate = paths.findSyntaxMainFile(sdfRoots, sdfModule);
            break;
        case sdf3:
            sdfFileCandidate = paths.syntaxSrcGenMainFile(sdfModule);
            break;
        default:
            throw new MetaborgException("Unknown SDF version: " + sdfVersion);
    }
    @Nullable final File sdfFile;
    if (sdfFileCandidate != null && sdfFileCandidate.exists()) {
        sdfFile = resourceService.localPath(sdfFileCandidate);
    } else {
        sdfFile = null;
    }
    @Nullable final File sdfExternalDef;
    final String sdfExternalDefStr = config.sdfExternalDef();
    if (sdfExternalDefStr != null) {
        final FileObject sdfExternalDefLoc = resourceService.resolve(sdfExternalDefStr);
        if (!sdfExternalDefLoc.exists()) {
            throw new MetaborgException("External SDF definition at " + sdfExternalDefLoc + " does not exist");
        }
        sdfExternalDef = resourceService.localFile(sdfExternalDefLoc);
    } else {
        sdfExternalDef = null;
    }
    final Iterable<FileObject> sdfIncludePaths = languagePathService.sourceAndIncludePaths(languageSpec, SpoofaxConstants.LANG_SDF_NAME);
    final FileObject packSdfIncludesReplicateDir = paths.replicateDir().resolveFile("pack-sdf-includes");
    packSdfIncludesReplicateDir.delete(new AllFileSelector());
    final List<File> packSdfIncludePaths = Lists.newArrayList();
    for (FileObject path : sdfIncludePaths) {
        if (!path.exists()) {
            continue;
        }
        packSdfIncludePaths.add(resourceService.localFile(path, packSdfIncludesReplicateDir));
    }
    final Arguments packSdfArgs = config.sdfArgs();
    // SDF completions
    final String sdfCompletionModule = config.sdfName() + "-completion-insertions";
    @Nullable final File sdfCompletionFile;
    FileObject sdfCompletionFileCandidate = null;
    if (sdf2tableVersion == Sdf2tableVersion.c) {
        sdfCompletionFileCandidate = paths.syntaxCompletionMainFile(sdfCompletionModule);
    } else if (sdf2tableVersion == Sdf2tableVersion.java || sdf2tableVersion == Sdf2tableVersion.dynamic) {
        sdfCompletionFileCandidate = paths.syntaxCompletionMainFileNormalized(sdfCompletionModule);
    }
    boolean dataDependent = config.dataDependent();
    if (sdfCompletionFileCandidate != null && sdfCompletionFileCandidate.exists()) {
        sdfCompletionFile = resourceService.localPath(sdfCompletionFileCandidate);
    } else {
        sdfCompletionFile = null;
    }
    // Meta-SDF
    final Iterable<FileObject> sdfRoots = languagePathService.sourcePaths(input.project(), SpoofaxConstants.LANG_SDF_NAME);
    final String sdfMetaModule = config.metaSdfName();
    final FileObject sdfMetaFileCandidate = paths.findSyntaxMainFile(sdfRoots, sdfMetaModule);
    @Nullable final File sdfMetaFile;
    if (sdfMetaFileCandidate != null && sdfMetaFileCandidate.exists()) {
        sdfMetaFile = resourceService.localPath(sdfMetaFileCandidate);
    } else {
        sdfMetaFile = null;
    }
    // Stratego
    final String strModule = config.strategoName();
    final Iterable<FileObject> strRoots = languagePathService.sourcePaths(input.project(), SpoofaxConstants.LANG_STRATEGO_NAME);
    final FileObject strFileCandidate = paths.findStrMainFile(strRoots, strModule);
    @Nullable final File strFile;
    if (strFileCandidate != null && strFileCandidate.exists()) {
        strFile = resourceService.localPath(strFileCandidate);
    } else {
        strFile = null;
    }
    final String strStratPkg = paths.strJavaTransPkg(config.identifier().id);
    final String strJavaStratPkg = paths.strJavaStratPkg(config.identifier().id);
    final FileObject strJavaStratFileCandidate = paths.strMainJavaStratFile(config.identifier().id);
    @Nullable final File strJavaStratFile;
    if (strJavaStratFileCandidate.exists()) {
        strJavaStratFile = resourceService.localPath(strJavaStratFileCandidate);
    } else {
        strJavaStratFile = null;
    }
    final StrategoFormat strFormat = config.strFormat();
    @Nullable final File strExternalJar;
    final String strExternalJarStr = config.strExternalJar();
    if (strExternalJarStr != null) {
        final FileObject strExternalJarLoc = resourceService.resolve(strExternalJarStr);
        if (!strExternalJarLoc.exists()) {
            throw new MetaborgException("External Stratego JAR at " + strExternalJarLoc + " does not exist");
        }
        strExternalJar = resourceService.localFile(strExternalJarLoc);
    } else {
        strExternalJar = null;
    }
    final String strExternalJarFlags = config.strExternalJarFlags();
    final Iterable<FileObject> strIncludePaths = languagePathService.sourceAndIncludePaths(languageSpec, SpoofaxConstants.LANG_STRATEGO_NAME);
    final FileObject strjIncludesReplicateDir = paths.replicateDir().resolveFile("strj-includes");
    strjIncludesReplicateDir.delete(new AllFileSelector());
    final List<File> strjIncludeDirs = Lists.newArrayList();
    final List<File> strjIncludeFiles = Lists.newArrayList();
    for (FileObject path : strIncludePaths) {
        if (!path.exists()) {
            continue;
        }
        if (path.isFolder()) {
            strjIncludeDirs.add(resourceService.localFile(path, strjIncludesReplicateDir));
        }
        if (path.isFile()) {
            strjIncludeFiles.add(resourceService.localFile(path, strjIncludesReplicateDir));
        }
    }
    final Arguments strjArgs = config.strArgs();
    return new GenerateSourcesBuilder.Input(context, config.identifier().id, config.sourceDeps(), sdfEnabled, sdfModule, sdfFile, sdfVersion, sdf2tableVersion, dataDependent, sdfExternalDef, packSdfIncludePaths, packSdfArgs, sdfCompletionModule, sdfCompletionFile, sdfMetaModule, sdfMetaFile, strFile, strStratPkg, strJavaStratPkg, strJavaStratFile, strFormat, strExternalJar, strExternalJarFlags, strjIncludeDirs, strjIncludeFiles, strjArgs);
}
Also used : StrategoFormat(org.metaborg.spoofax.meta.core.config.StrategoFormat) ISpoofaxLanguageSpecConfig(org.metaborg.spoofax.meta.core.config.ISpoofaxLanguageSpecConfig) MetaborgException(org.metaborg.core.MetaborgException) Arguments(org.metaborg.util.cmd.Arguments) BuildInput(org.metaborg.core.build.BuildInput) ISpoofaxLanguageSpec(org.metaborg.spoofax.meta.core.project.ISpoofaxLanguageSpec) SdfVersion(org.metaborg.spoofax.meta.core.config.SdfVersion) SpoofaxContext(org.metaborg.spoofax.meta.core.pluto.SpoofaxContext) AllFileSelector(org.apache.commons.vfs2.AllFileSelector) Sdf2tableVersion(org.metaborg.core.config.Sdf2tableVersion) FileObject(org.apache.commons.vfs2.FileObject) File(java.io.File) Nullable(javax.annotation.Nullable)

Example 13 with MetaborgException

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

the class HoverService method hover.

@Override
public Hover hover(int offset, 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<HoverFacet> facetContrib = facet(langImpl);
    final HoverFacet facet = facetContrib.facet;
    final ILanguageComponent contributor = facetContrib.contributor;
    final String strategy = facet.strategyName;
    try {
        final ITermFactory termFactory = termFactoryService.get(contributor, project, true);
        final HybridInterpreter interpreter;
        if (context == null) {
            interpreter = strategoRuntimeService.runtime(contributor, source, true);
        } else {
            interpreter = strategoRuntimeService.runtime(contributor, context, true);
        }
        final Iterable<IStrategoTerm> inRegion = tracingService.fragments(result, new SourceRegion(offset));
        final TermWithRegion tuple = common.outputs(termFactory, interpreter, context.location(), source, result.ast(), inRegion, strategy);
        return hover(tuple);
    } catch (MetaborgException e) {
        throw new MetaborgException("Getting hover tooltip information failed unexpectedly", e);
    }
}
Also used : MetaborgRuntimeException(org.metaborg.core.MetaborgRuntimeException) IContext(org.metaborg.core.context.IContext) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) MetaborgException(org.metaborg.core.MetaborgException) HybridInterpreter(org.strategoxt.HybridInterpreter) ISourceRegion(org.metaborg.core.source.ISourceRegion) SourceRegion(org.metaborg.core.source.SourceRegion) IProject(org.metaborg.core.project.IProject) ContextException(org.metaborg.core.context.ContextException) TermWithRegion(org.metaborg.spoofax.core.tracing.TracingCommon.TermWithRegion) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) FileObject(org.apache.commons.vfs2.FileObject) ILanguageComponent(org.metaborg.core.language.ILanguageComponent) ITermFactory(org.spoofax.interpreter.terms.ITermFactory) Nullable(javax.annotation.Nullable)

Example 14 with MetaborgException

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

the class ResolverService method resolve.

@Override
public Resolution resolve(int offset, ISpoofaxAnalyzeUnit result) throws MetaborgException {
    if (!result.valid() || !result.hasAst()) {
        return null;
    }
    final FileObject source = result.source();
    final IContext context = result.context();
    final IProject project = context.project();
    final ILanguageImpl language = context.language();
    final FacetContribution<ResolverFacet> facetContrib = facet(language);
    final ResolverFacet facet = facetContrib.facet;
    final String strategy = facet.strategyName;
    try {
        final ITermFactory termFactory = termFactoryService.get(facetContrib.contributor, project, true);
        final HybridInterpreter interpreter = strategoRuntimeService.runtime(facetContrib.contributor, context, true);
        final Iterable<IStrategoTerm> inRegion = tracingService.fragments(result, new SourceRegion(offset));
        final TermWithRegion tuple;
        try (IClosableLock lock = context.read()) {
            tuple = common.outputs(termFactory, interpreter, source, source, result.ast(), inRegion, strategy);
        }
        return resolve(tuple);
    } catch (MetaborgException e) {
        throw new MetaborgException("Reference resolution failed", e);
    }
}
Also used : IContext(org.metaborg.core.context.IContext) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) MetaborgException(org.metaborg.core.MetaborgException) HybridInterpreter(org.strategoxt.HybridInterpreter) ISourceRegion(org.metaborg.core.source.ISourceRegion) SourceRegion(org.metaborg.core.source.SourceRegion) IProject(org.metaborg.core.project.IProject) TermWithRegion(org.metaborg.spoofax.core.tracing.TracingCommon.TermWithRegion) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) FileObject(org.apache.commons.vfs2.FileObject) IClosableLock(org.metaborg.util.concurrent.IClosableLock) ITermFactory(org.spoofax.interpreter.terms.ITermFactory)

Example 15 with MetaborgException

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

the class LanguageSpecSrcGenDirectory method call.

@Override
protected IStrategoTerm call(IStrategoTerm current, Strategy[] svars, IStrategoTerm[] tvars, ITermFactory factory, IContext context) throws MetaborgException {
    final IProject project = context.project();
    if (project == null) {
        return null;
    }
    final CommonPaths paths = new CommonPaths(project.location());
    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 null;
    }
    if (!Tools.isTermString(current)) {
        throw new MetaborgException("Expect a string as argument term, got " + current);
    }
    String name = Tools.asJavaString(current);
    final String srcGenDir;
    try {
        srcGenDir = ResourceUtils.relativeName(paths.srcGenDir().resolveFile(name).getName(), project.location().getName(), true);
    } catch (FileSystemException e) {
        return null;
    }
    return factory.makeString(srcGenDir);
}
Also used : FileSystemException(org.apache.commons.vfs2.FileSystemException) CommonPaths(org.metaborg.core.build.CommonPaths) MetaborgException(org.metaborg.core.MetaborgException) IProject(org.metaborg.core.project.IProject)

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