Search in sources :

Example 36 with ILanguageImpl

use of org.metaborg.core.language.ILanguageImpl 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 37 with ILanguageImpl

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

Example 38 with ILanguageImpl

use of org.metaborg.core.language.ILanguageImpl in project spoofax by metaborg.

the class LanguageChangeProcessor method reloadedImpl.

/**
 * Implementation was reloaded
 *
 * @param impl
 *            Reloaded implementation
 */
protected void reloadedImpl(ILanguageImpl impl) {
    for (ILanguageCache languageCache : languageCaches) {
        languageCache.invalidateCache(impl);
    }
    parseResultProcessor.invalidate(impl);
    analysisResultProcessor.invalidate(impl);
    final Iterable<IEditor> editors = editorRegistry.openEditors();
    for (IEditor editor : editors) {
        final ILanguageImpl editorLanguage = editor.language();
        if (editorLanguage == null || impl.equals(editorLanguage)) {
            editor.reconfigure();
            editor.forceUpdate();
        }
    }
}
Also used : IEditor(org.metaborg.core.editor.IEditor) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) ILanguageCache(org.metaborg.core.language.ILanguageCache)

Example 39 with ILanguageImpl

use of org.metaborg.core.language.ILanguageImpl in project spoofax by metaborg.

the class ParseFile method build.

@Override
protected Out<IStrategoTerm> build(Input input) throws Throwable {
    requireBuild(input.requiredUnits);
    require(input.file);
    if (!FileCommands.exists(input.file)) {
        return null;
    }
    final FileObject resource = context.resourceService().resolve(input.file);
    final ILanguageImpl language = context.languageIdentifierService().identify(resource);
    if (language == null) {
        return null;
    }
    final String text = context.sourceTextService().text(resource);
    final ISpoofaxInputUnit inputUnit = context.unitService().inputUnit(resource, text, language, null);
    final ISpoofaxParseUnit result = context.syntaxService().parse(inputUnit);
    if (!result.valid()) {
        return null;
    }
    return input.persistResult ? OutputPersisted.of(result.ast()) : OutputTransient.of(result.ast());
}
Also used : ISpoofaxParseUnit(org.metaborg.spoofax.core.unit.ISpoofaxParseUnit) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) ISpoofaxInputUnit(org.metaborg.spoofax.core.unit.ISpoofaxInputUnit) FileObject(org.apache.commons.vfs2.FileObject)

Example 40 with ILanguageImpl

use of org.metaborg.core.language.ILanguageImpl in project spoofax by metaborg.

the class Builder method build.

@Override
public IBuildOutput<P, A, AU, T> build(BuildInput input, IProgress progress, ICancel cancel) throws InterruptedException {
    cancel.throwIfCancelled();
    final Multimap<ILanguageImpl, IdentifiedResourceChange> changes = ArrayListMultimap.create();
    identifyResources(input.sourceChanges, input, changes, cancel);
    if (changes.size() == 0) {
        // When there are no source changes, keep the old state and skip building.
        final IBuildOutputInternal<P, A, AU, T> buildOutput = buildOutputProvider.get();
        buildOutput.setState(input.state);
        return buildOutput;
    }
    cancel.throwIfCancelled();
    logger.info("Building " + input.project.location());
    final BuildState newState = new BuildState();
    final IBuildOutputInternal<P, A, AU, T> buildOutput = buildOutputProvider.get();
    buildOutput.setState(newState);
    final Iterable<ILanguageImpl> buildOrder = input.buildOrder.buildOrder();
    progress.setWorkRemaining(Iterables.size(buildOrder));
    for (ILanguageImpl language : buildOrder) {
        cancel.throwIfCancelled();
        final LanguageBuildState languageState = input.state.get(resourceService, languageIdentifier, language);
        final Collection<IdentifiedResourceChange> sourceChanges = changes.get(language);
        if (sourceChanges.size() == 0) {
            // When there are no source changes for this language, keep the old state and don't build.
            newState.add(language, languageState);
            continue;
        }
        final Iterable<FileObject> includePaths = input.includePaths.get(language);
        final Iterable<IdentifiedResource> includeFiles = languagePathService.toFiles(includePaths, language);
        final LanguageBuildDiff diff = languageState.diff(changes.get(language), includeFiles);
        final boolean pardoned = input.pardonedLanguages.contains(language);
        final Collection<FileObject> newResources = updateLanguageResources(input, language, diff, buildOutput, pardoned, progress.subProgress(1), cancel);
        final Iterable<ResourceChange> newResourceChanges = ResourceUtils.toChanges(newResources, ResourceChangeKind.Create);
        identifyResources(newResourceChanges, input, changes, cancel);
        newState.add(language, diff.newState);
    }
    final IMessagePrinter printer = input.messagePrinter;
    if (printer != null) {
        printer.printSummary();
    }
    return buildOutput;
}
Also used : IdentifiedResourceChange(org.metaborg.core.resource.IdentifiedResourceChange) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) IMessagePrinter(org.metaborg.core.messages.IMessagePrinter) FileObject(org.apache.commons.vfs2.FileObject) IdentifiedResource(org.metaborg.core.language.IdentifiedResource) ResourceChange(org.metaborg.core.resource.ResourceChange) IdentifiedResourceChange(org.metaborg.core.resource.IdentifiedResourceChange)

Aggregations

ILanguageImpl (org.metaborg.core.language.ILanguageImpl)53 FileObject (org.apache.commons.vfs2.FileObject)32 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)24 MetaborgException (org.metaborg.core.MetaborgException)17 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)15 ILanguage (org.metaborg.core.language.ILanguage)14 HybridInterpreter (org.strategoxt.HybridInterpreter)13 LanguageIdentifier (org.metaborg.core.language.LanguageIdentifier)11 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)11 Test (org.junit.Test)10 IContext (org.metaborg.core.context.IContext)10 LanguageVersion (org.metaborg.core.language.LanguageVersion)10 MetaborgRuntimeException (org.metaborg.core.MetaborgRuntimeException)9 MetaborgTest (org.metaborg.core.test.MetaborgTest)9 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)9 IProject (org.metaborg.core.project.IProject)8 Nullable (javax.annotation.Nullable)6 ISpoofaxParseUnit (org.metaborg.spoofax.core.unit.ISpoofaxParseUnit)6 IdentifiedResource (org.metaborg.core.language.IdentifiedResource)5 ISourceRegion (org.metaborg.core.source.ISourceRegion)5