Search in sources :

Example 1 with Paths

use of mb.nabl2.scopegraph.terms.path.Paths 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 2 with Paths

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

the class NameResolutionTerms method buildRef.

private ITerm buildRef(Occurrence ref) {
    final List<ITerm> paths = nameResolution.resolve(ref).orElseGet(() -> Set.Immutable.of()).stream().map(this::buildPath).collect(Collectors.toList());
    final ITerm result;
    if (paths.isEmpty()) {
        result = B.newAppl("NoResolution");
    } else {
        result = B.newAppl("Resolution", (ITerm) B.newList(paths));
    }
    return B.newTuple(ref, result);
}
Also used : ITerm(mb.nabl2.terms.ITerm)

Example 3 with Paths

use of mb.nabl2.scopegraph.terms.path.Paths 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)

Example 4 with Paths

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

the class SG_get_ref_resolution method call.

@Override
public Optional<? extends ITerm> call(IScopeGraphUnit unit, ITerm term, List<ITerm> terms) throws InterpreterException {
    return unit.solution().flatMap(s -> {
        return Occurrence.matcher().match(term, s.unifier()).<ITerm>flatMap(ref -> {
            return s.nameResolution().resolve(ref).map(paths -> {
                List<ITerm> pathTerms = Lists.newArrayListWithExpectedSize(paths.size());
                for (IResolutionPath<Scope, Label, Occurrence> path : paths) {
                    pathTerms.add(B.newTuple(path.getDeclaration(), Paths.toTerm(path)));
                }
                ITerm result = B.newList(pathTerms);
                return result;
            });
        });
    });
}
Also used : Scope(mb.nabl2.scopegraph.terms.Scope) Label(mb.nabl2.scopegraph.terms.Label) ITerm(mb.nabl2.terms.ITerm) Occurrence(mb.nabl2.scopegraph.terms.Occurrence)

Aggregations

ITerm (mb.nabl2.terms.ITerm)4 Label (mb.nabl2.scopegraph.terms.Label)3 Occurrence (mb.nabl2.scopegraph.terms.Occurrence)3 Scope (mb.nabl2.scopegraph.terms.Scope)3 IResolutionPath (mb.nabl2.scopegraph.path.IResolutionPath)2 Map (java.util.Map)1 Set (java.util.Set)1 IMessageInfo (mb.nabl2.constraints.messages.IMessageInfo)1 SolveResult (mb.nabl2.solver.ISolver.SolveResult)1 ImmutableSolveResult (mb.nabl2.solver.ImmutableSolveResult)1 TypeException (mb.nabl2.solver.TypeException)1