use of mb.nabl2.constraints.nameresolution.CResolve 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));
}
Aggregations