Search in sources :

Example 11 with IClosableLock

use of org.metaborg.util.concurrent.IClosableLock in project spoofax by metaborg.

the class AbstractScopeGraphContext method readLock.

private IClosableLock readLock() {
    final Lock readLock = lock.readLock();
    final IClosableLock lock = new ClosableLock(readLock);
    return lock;
}
Also used : ClosableLock(org.metaborg.util.concurrent.ClosableLock) IClosableLock(org.metaborg.util.concurrent.IClosableLock) IClosableLock(org.metaborg.util.concurrent.IClosableLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock) ClosableLock(org.metaborg.util.concurrent.ClosableLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) IClosableLock(org.metaborg.util.concurrent.IClosableLock)

Example 12 with IClosableLock

use of org.metaborg.util.concurrent.IClosableLock in project spoofax by metaborg.

the class AbstractScopeGraphContext method reset.

@Override
public void reset() throws IOException {
    try (IClosableLock lock = writeLock()) {
        if (state != null) {
            state = null;
        }
        final FileObject contextFile = contextFile();
        contextFile.delete();
    }
}
Also used : IClosableLock(org.metaborg.util.concurrent.IClosableLock) FileObject(org.apache.commons.vfs2.FileObject)

Example 13 with IClosableLock

use of org.metaborg.util.concurrent.IClosableLock in project spoofax by metaborg.

the class Builder method analyze.

private Multimap<IContext, A> analyze(BuildInput input, ILanguageImpl langImpl, FileObject location, Multimap<IContext, P> sourceParseUnits, Iterable<P> includeParseUnits, boolean pardoned, Collection<AU> analyzeUpdates, Set<FileName> removedResources, Collection<IMessage> extraMessages, RefBool success, IProgress progress, ICancel cancel) throws InterruptedException {
    final int size = sourceParseUnits.size() + Iterables.size(includeParseUnits);
    final Multimap<IContext, A> allAnalyzeUnits = ArrayListMultimap.create();
    if (size == 0) {
        return allAnalyzeUnits;
    }
    final Set<Entry<IContext, Collection<P>>> toAnalyze = sourceParseUnits.asMap().entrySet();
    final int toAnalyzeSize = toAnalyze.size();
    progress.setWorkRemaining(toAnalyzeSize);
    progress.setDescription("Analyzing " + size + " file(s) of " + langImpl.belongsTo().name());
    logger.debug("Analyzing {} parse results in {} context(s)", size, toAnalyzeSize);
    for (Entry<IContext, Collection<P>> entry : toAnalyze) {
        cancel.throwIfCancelled();
        final IContext context = entry.getKey();
        final Iterable<P> parseResults = Iterables.concat(entry.getValue(), includeParseUnits);
        try {
            try (IClosableLock lock = context.write()) {
                analysisResultUpdater.invalidate(parseResults);
                final IAnalyzeResults<A, AU> results = analysisService.analyzeAll(parseResults, context, progress.subProgress(1), cancel);
                for (A result : results.results()) {
                    cancel.throwIfCancelled();
                    final boolean noErrors = printMessages(result.messages(), "Analysis", input, pardoned);
                    success.and(noErrors);
                    analysisResultUpdater.update(result, removedResources);
                    allAnalyzeUnits.put(context, result);
                }
                analyzeUpdates.addAll(results.updates());
            } finally {
                context.persist();
            }
        } catch (AnalysisException e) {
            final String message = "Analysis failed unexpectedly";
            final boolean noErrors = printMessage(message, e, input, pardoned);
            success.and(noErrors);
            analysisResultUpdater.error(parseResults, e);
            extraMessages.add(MessageFactory.newAnalysisErrorAtTop(location, message, e));
        } catch (IOException e) {
            final String message = "Persisting analysis data failed unexpectedly";
            final boolean noErrors = printMessage(message, e, input, pardoned);
            success.and(noErrors);
            extraMessages.add(MessageFactory.newAnalysisErrorAtTop(location, message, e));
        }
    }
    return allAnalyzeUnits;
}
Also used : IContext(org.metaborg.core.context.IContext) IOException(java.io.IOException) Entry(java.util.Map.Entry) AnalysisException(org.metaborg.core.analysis.AnalysisException) Collection(java.util.Collection) IClosableLock(org.metaborg.util.concurrent.IClosableLock)

Example 14 with IClosableLock

use of org.metaborg.util.concurrent.IClosableLock in project spoofax by metaborg.

the class HoverService method hover.

@Override
public Hover hover(int offset, 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<HoverFacet> facetContrib = facet(language);
    final HoverFacet facet = facetContrib.facet;
    final String strategy = facet.strategyName;
    try {
        final IProject project = context.project();
        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, 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 : 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 IClosableLock

use of org.metaborg.util.concurrent.IClosableLock 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)

Aggregations

IClosableLock (org.metaborg.util.concurrent.IClosableLock)15 FileObject (org.apache.commons.vfs2.FileObject)6 Lock (java.util.concurrent.locks.Lock)4 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)4 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)4 IContext (org.metaborg.core.context.IContext)4 ClosableLock (org.metaborg.util.concurrent.ClosableLock)4 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)3 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)3 Collection (java.util.Collection)2 FileName (org.apache.commons.vfs2.FileName)2 MetaborgException (org.metaborg.core.MetaborgException)2 AnalysisException (org.metaborg.core.analysis.AnalysisException)2 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)2 IProject (org.metaborg.core.project.IProject)2 ISourceRegion (org.metaborg.core.source.ISourceRegion)2 SourceRegion (org.metaborg.core.source.SourceRegion)2 TermWithRegion (org.metaborg.spoofax.core.tracing.TracingCommon.TermWithRegion)2 HybridInterpreter (org.strategoxt.HybridInterpreter)2 IOException (java.io.IOException)1