Search in sources :

Example 1 with Label

use of mb.nabl2.scopegraph.terms.Label in project nabl by metaborg.

the class InterpreterTerms method scopeEntries.

private static ITerm scopeEntries(IScopeGraph<Scope, Label, Occurrence> scopeGraph) {
    Map<ITerm, ITerm> entries = Maps.newHashMap();
    for (Scope scope : scopeGraph.getAllScopes()) {
        IListTerm decls = B.newList(scopeGraph.getDecls().inverse().get(scope));
        IListTerm refs = B.newList(scopeGraph.getRefs().inverse().get(scope));
        IListTerm edges = multimap(scopeGraph.getDirectEdges().get(scope));
        IListTerm imports = multimap(scopeGraph.getImportEdges().get(scope));
        ITerm entry = B.newAppl("SE", decls, refs, edges, imports);
        entries.put(scope, entry);
    }
    return map(entries.entrySet());
}
Also used : IListTerm(mb.nabl2.terms.IListTerm) Scope(mb.nabl2.scopegraph.terms.Scope) ITerm(mb.nabl2.terms.ITerm)

Example 2 with Label

use of mb.nabl2.scopegraph.terms.Label in project nabl by metaborg.

the class InterpreterTerms method refEntries.

private static ITerm refEntries(IScopeGraph<Scope, Label, Occurrence> scopeGraph) {
    Map<ITerm, ITerm> entries = Maps.newHashMap();
    for (Occurrence ref : scopeGraph.getAllRefs()) {
        ITerm scope = scopeGraph.getRefs().get(ref).map(s -> B.newList(s)).orElse(B.newNil());
        ITerm entry = B.newAppl("RE", scope);
        entries.put(ref, entry);
    }
    return map(entries.entrySet());
}
Also used : B(mb.nabl2.terms.build.TermBuild.B) Iterables(com.google.common.collect.Iterables) IUnifier(mb.nabl2.terms.unification.IUnifier) DeclProperties(mb.nabl2.constraints.namebinding.DeclProperties) ITerm(mb.nabl2.terms.ITerm) Set(java.util.Set) Scope(mb.nabl2.scopegraph.terms.Scope) IResolutionPath(mb.nabl2.scopegraph.path.IResolutionPath) Multimap(com.google.common.collect.Multimap) Occurrence(mb.nabl2.scopegraph.terms.Occurrence) IProperties(mb.nabl2.util.collections.IProperties) Maps(com.google.common.collect.Maps) IEsopNameResolution(mb.nabl2.scopegraph.esop.IEsopNameResolution) List(java.util.List) IScopeGraph(mb.nabl2.scopegraph.IScopeGraph) HashMultimap(com.google.common.collect.HashMultimap) Lists(com.google.common.collect.Lists) Paths(mb.nabl2.scopegraph.terms.path.Paths) Label(mb.nabl2.scopegraph.terms.Label) IListTerm(mb.nabl2.terms.IListTerm) Map(java.util.Map) LoggerUtils(org.metaborg.util.log.LoggerUtils) ISolution(mb.nabl2.solver.ISolution) ILogger(org.metaborg.util.log.ILogger) ITerm(mb.nabl2.terms.ITerm) Occurrence(mb.nabl2.scopegraph.terms.Occurrence)

Example 3 with Label

use of mb.nabl2.scopegraph.terms.Label in project nabl by metaborg.

the class InterpreterTerms method nameresolution.

private static ITerm nameresolution(IEsopNameResolution<Scope, Label, Occurrence> nameResolution) {
    nameResolution.resolveAll();
    final Map<ITerm, ITerm> entries = Maps.newHashMap();
    for (Map.Entry<Occurrence, Set<IResolutionPath<Scope, Label, Occurrence>>> entry : nameResolution.resolutionEntries()) {
        final Occurrence ref = entry.getKey();
        final Set<IResolutionPath<Scope, Label, Occurrence>> paths = entry.getValue();
        if (paths.size() == 1) {
            IResolutionPath<Scope, Label, Occurrence> path = Iterables.getOnlyElement(paths);
            ITerm value = B.newTuple(path.getDeclaration(), Paths.toTerm(path));
            entries.put(ref, value);
        } else {
            logger.warn("Can only convert a single path, but {} has {}.", ref, paths.size());
        }
    }
    return map(entries.entrySet());
}
Also used : IResolutionPath(mb.nabl2.scopegraph.path.IResolutionPath) Set(java.util.Set) Scope(mb.nabl2.scopegraph.terms.Scope) Label(mb.nabl2.scopegraph.terms.Label) ITerm(mb.nabl2.terms.ITerm) Map(java.util.Map) Occurrence(mb.nabl2.scopegraph.terms.Occurrence)

Example 4 with Label

use of mb.nabl2.scopegraph.terms.Label in project nabl by metaborg.

the class InterpreterTerms method declEntries.

private static ITerm declEntries(IScopeGraph<Scope, Label, Occurrence> scopeGraph) {
    Map<ITerm, ITerm> entries = Maps.newHashMap();
    for (Occurrence decl : scopeGraph.getAllDecls()) {
        ITerm scope = scopeGraph.getDecls().get(decl).map(s -> B.newList(s)).orElse(B.newNil());
        ITerm assocs = multimap(scopeGraph.getExportEdges().get(decl));
        ITerm entry = B.newAppl("DE", scope, assocs);
        entries.put(decl, entry);
    }
    return map(entries.entrySet());
}
Also used : B(mb.nabl2.terms.build.TermBuild.B) Iterables(com.google.common.collect.Iterables) IUnifier(mb.nabl2.terms.unification.IUnifier) DeclProperties(mb.nabl2.constraints.namebinding.DeclProperties) ITerm(mb.nabl2.terms.ITerm) Set(java.util.Set) Scope(mb.nabl2.scopegraph.terms.Scope) IResolutionPath(mb.nabl2.scopegraph.path.IResolutionPath) Multimap(com.google.common.collect.Multimap) Occurrence(mb.nabl2.scopegraph.terms.Occurrence) IProperties(mb.nabl2.util.collections.IProperties) Maps(com.google.common.collect.Maps) IEsopNameResolution(mb.nabl2.scopegraph.esop.IEsopNameResolution) List(java.util.List) IScopeGraph(mb.nabl2.scopegraph.IScopeGraph) HashMultimap(com.google.common.collect.HashMultimap) Lists(com.google.common.collect.Lists) Paths(mb.nabl2.scopegraph.terms.path.Paths) Label(mb.nabl2.scopegraph.terms.Label) IListTerm(mb.nabl2.terms.IListTerm) Map(java.util.Map) LoggerUtils(org.metaborg.util.log.LoggerUtils) ISolution(mb.nabl2.solver.ISolution) ILogger(org.metaborg.util.log.ILogger) ITerm(mb.nabl2.terms.ITerm) Occurrence(mb.nabl2.scopegraph.terms.Occurrence)

Example 5 with Label

use of mb.nabl2.scopegraph.terms.Label in project nabl by metaborg.

the class NameResolutionComponent method solve.

// ------------------------------------------------------------------------------------------------------//
private Optional<SolveResult> solve(CResolve r) {
    final ITerm refTerm = unifier().findRecursive(r.getReference());
    if (!refTerm.isGround()) {
        return Optional.empty();
    }
    final Occurrence ref = Occurrence.matcher().match(refTerm, unifier()).orElseThrow(() -> new TypeException("Expected an occurrence as first argument to " + r));
    final Optional<java.util.Set<IResolutionPath<Scope, Label, Occurrence>>> maybePathsAndDeps = nameResolution.resolve(ref);
    if (!maybePathsAndDeps.isPresent()) {
        return Optional.empty();
    }
    final java.util.Set<IResolutionPath<Scope, Label, Occurrence>> paths = maybePathsAndDeps.get();
    final List<Occurrence> declarations = Paths.resolutionPathsToDecls(paths);
    final Multimap<String, String> deps = HashMultimap.create();
    deps.putAll(ref.getIndex().getResource(), declarations.stream().map(d -> d.getIndex().getResource()).collect(Collectors.toSet()));
    final SolveResult result;
    switch(declarations.size()) {
        case 0:
            {
                IMessageInfo message = r.getMessageInfo().withDefaultContent(MessageContent.builder().append(ref).append(" does not resolve.").build());
                result = SolveResult.messages(message);
                break;
            }
        case 1:
            {
                final Occurrence decl = declarations.get(0);
                result = SolveResult.constraints(ImmutableCEqual.of(r.getDeclaration(), decl, r.getMessageInfo()));
                break;
            }
        default:
            {
                IMessageInfo message = r.getMessageInfo().withDefaultContent(MessageContent.builder().append("Resolution of ").append(ref).append(" is ambiguous.").build());
                result = SolveResult.messages(message);
                break;
            }
    }
    return Optional.of(ImmutableSolveResult.copyOf(result).withDependencies(deps));
}
Also used : IResolutionPath(mb.nabl2.scopegraph.path.IResolutionPath) Label(mb.nabl2.scopegraph.terms.Label) TypeException(mb.nabl2.solver.TypeException) IMessageInfo(mb.nabl2.constraints.messages.IMessageInfo) SolveResult(mb.nabl2.solver.ISolver.SolveResult) ImmutableSolveResult(mb.nabl2.solver.ImmutableSolveResult) Scope(mb.nabl2.scopegraph.terms.Scope) ITerm(mb.nabl2.terms.ITerm) Occurrence(mb.nabl2.scopegraph.terms.Occurrence)

Aggregations

ITerm (mb.nabl2.terms.ITerm)12 Label (mb.nabl2.scopegraph.terms.Label)11 Scope (mb.nabl2.scopegraph.terms.Scope)11 Occurrence (mb.nabl2.scopegraph.terms.Occurrence)10 Map (java.util.Map)7 ISolution (mb.nabl2.solver.ISolution)6 SolveResult (mb.nabl2.solver.ISolver.SolveResult)6 IUnifier (mb.nabl2.terms.unification.IUnifier)6 IEsopNameResolution (mb.nabl2.scopegraph.esop.IEsopNameResolution)5 Optional (java.util.Optional)4 NaBL2DebugConfig (mb.nabl2.config.NaBL2DebugConfig)4 IConstraint (mb.nabl2.constraints.IConstraint)4 IEsopScopeGraph (mb.nabl2.scopegraph.esop.IEsopScopeGraph)4 IResolutionPath (mb.nabl2.scopegraph.path.IResolutionPath)4 ISolver (mb.nabl2.solver.ISolver)4 ImmutableSolution (mb.nabl2.solver.ImmutableSolution)4 SolverConfig (mb.nabl2.solver.SolverConfig)4 SolverCore (mb.nabl2.solver.SolverCore)4 SolverException (mb.nabl2.solver.SolverException)4 BaseComponent (mb.nabl2.solver.components.BaseComponent)4