use of mb.nabl2.solver.TypeException 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));
}
use of mb.nabl2.solver.TypeException in project nabl by metaborg.
the class NameResolutionComponent method solve.
private Optional<SolveResult> solve(CAssoc a) {
final ITerm declTerm = unifier().findRecursive(a.getDeclaration());
if (!declTerm.isGround()) {
return Optional.empty();
}
final Occurrence decl = Occurrence.matcher().match(declTerm, unifier()).orElseThrow(() -> new TypeException("Expected an occurrence as first argument to " + a));
final Label label = a.getLabel();
final List<Scope> scopes = Lists.newArrayList(scopeGraph.getExportEdges().get(decl, label));
final SolveResult result;
switch(scopes.size()) {
case 0:
{
IMessageInfo message = a.getMessageInfo().withDefaultContent(MessageContent.builder().append(decl).append(" has no ").append(label).append(" associated scope.").build());
result = SolveResult.messages(message);
break;
}
case 1:
{
result = SolveResult.constraints(ImmutableCEqual.of(a.getScope(), scopes.get(0), a.getMessageInfo()));
break;
}
default:
{
IMessageInfo message = a.getMessageInfo().withDefaultContent(MessageContent.builder().append(decl).append(" has multiple ").append(label).append(" associated scope.").build());
result = SolveResult.messages(message);
break;
}
}
return Optional.of(result);
}
use of mb.nabl2.solver.TypeException in project nabl by metaborg.
the class PolymorphismComponent method solve.
private Optional<SolveResult> solve(CInstantiate inst) {
final ITerm declTerm = unifier().findRecursive(inst.getDeclaration());
if (!declTerm.isGround()) {
return Optional.empty();
}
final Occurrence decl = Occurrence.matcher().match(declTerm, unifier()).orElseThrow(() -> new TypeException("Expected an occurrence as first argument to " + inst));
if (!isInstSafe.test(decl)) {
return Optional.empty();
}
final Optional<ITerm> schemeTerm = getDeclProp.apply(decl, DeclProperties.TYPE_KEY);
if (!schemeTerm.isPresent()) {
return Optional.empty();
}
final Optional<Forall> forall = Forall.matcher().match(schemeTerm.get(), unifier());
final ITerm type;
// linked map to preserve key order
final Map<TypeVar, ITermVar> subst = Maps.newLinkedHashMap();
if (forall.isPresent()) {
final Forall scheme = forall.get();
scheme.getTypeVars().stream().forEach(v -> {
subst.put(v, B.newVar("", fresh(v.getName())));
});
type = subst(scheme.getType(), subst);
} else {
type = schemeTerm.get();
}
final IConstraint constraint = // @formatter:off
ImmutableCExists.of(subst.values(), ImmutableCConj.of(ImmutableCEqual.of(inst.getType(), type, inst.getMessageInfo()), ImmutableCEqual.of(inst.getInstVars(), B.newList(subst.keySet()), inst.getMessageInfo()), MessageInfo.empty()), inst.getMessageInfo());
// @formatter:on
SolveResult result = SolveResult.constraints(constraint);
return Optional.of(result);
}
use of mb.nabl2.solver.TypeException in project nabl by metaborg.
the class ScopeGraphComponent method solve.
private boolean solve(CGImportEdge c) {
ITerm scopeRep = unifier().findRecursive(c.getScope());
if (!scopeRep.isGround()) {
return false;
}
Scope scope = Scope.matcher().match(scopeRep, unifier()).orElseThrow(() -> new TypeException("Expected a scope but got " + scopeRep));
return findOccurrence(c.getReference()).map(ref -> {
scopeGraph.addImportEdge(scope, c.getLabel(), ref);
return true;
}).orElseGet(() -> {
scopeGraph.addIncompleteImportEdge(scope, c.getLabel(), c.getReference());
return true;
});
}
use of mb.nabl2.solver.TypeException in project nabl by metaborg.
the class ScopeGraphComponent method solve.
// ------------------------------------------------------------------------------------------------------//
private boolean solve(CGDecl c) {
final ITerm scopeTerm = unifier().findRecursive(c.getScope());
final ITerm declTerm = unifier().findRecursive(c.getDeclaration());
if (!(scopeTerm.isGround() && declTerm.isGround())) {
return false;
}
Scope scope = Scope.matcher().match(scopeTerm, unifier()).orElseThrow(() -> new TypeException("Expected a scope as first agument to " + c));
Occurrence decl = Occurrence.matcher().match(declTerm, unifier()).orElseThrow(() -> new TypeException("Expected an occurrence as second argument to " + c));
scopeGraph.addDecl(scope, decl);
return true;
}
Aggregations