Search in sources :

Example 6 with T

use of mb.nabl2.terms.matching.Transform.T 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)

Example 7 with T

use of mb.nabl2.terms.matching.Transform.T in project nabl by metaborg.

the class TermIndexTest method testSerializeSpecialized.

@Test
public void testSerializeSpecialized() throws Exception {
    ITerm t = ImmutableTermIndex.of("Hello, world!", 42);
    byte[] b1 = serialize(t);
    byte[] b2 = serialize(deserialize(b1));
    assertTrue(Arrays.equals(b1, b2));
}
Also used : ITerm(mb.nabl2.terms.ITerm) Test(org.junit.Test)

Example 8 with T

use of mb.nabl2.terms.matching.Transform.T in project nabl by metaborg.

the class HashcodeAndEqualsTest method testSerializeGeneric.

@Test
public void testSerializeGeneric() throws Exception {
    ITerm t = B.newAppl(SpecializedAppl.OP, B.newString("Hello, world!"), B.newInt(42));
    byte[] b1 = serialize(t);
    byte[] b2 = serialize(deserialize(b1));
    assertTrue(Arrays.equals(b1, b2));
}
Also used : ITerm(mb.nabl2.terms.ITerm) Test(org.junit.Test)

Example 9 with T

use of mb.nabl2.terms.matching.Transform.T in project nabl by metaborg.

the class ScopeGraphEdgePrimitive method call.

@Override
public Optional<ITerm> call(IScopeGraphUnit unit, ITerm term, List<ITerm> terms) throws InterpreterException {
    return unit.solution().flatMap(sol -> {
        final IRelation3<S, Label, ? extends ITerm> edges = getEdges(sol.scopeGraph());
        final IMatcher<S> sourceMatcher = getSourceMatcher();
        return M.<ITerm>cases(// @formatter:off
        M.term(sourceMatcher, (t, source) -> {
            List<ITerm> edgeTerms = Lists.newArrayList();
            for (Map.Entry<Label, ? extends ITerm> edge : edges.get(source)) {
                edgeTerms.add(B.newTuple(edge.getKey(), edge.getValue()));
            }
            return B.newList(edgeTerms);
        }), M.tuple2(sourceMatcher, Label.matcher(), (t, source, label) -> {
            List<ITerm> targetTerms = Lists.newArrayList();
            for (ITerm target : edges.get(source, label)) {
                targetTerms.add(target);
            }
            return B.newList(targetTerms);
        })).match(term, sol.unifier());
    });
}
Also used : Label(mb.nabl2.scopegraph.terms.Label) ITerm(mb.nabl2.terms.ITerm) Map(java.util.Map)

Example 10 with T

use of mb.nabl2.terms.matching.Transform.T in project nabl by metaborg.

the class VarMultimap method update.

public boolean update(final ITermVar var, IUnifier unifier) {
    final Collection<T> values = map.removeAll(var);
    final Set<ITermVar> reps = unifier.getVars(var);
    boolean change = false;
    for (ITermVar rep : reps) {
        change |= map.putAll(rep, values);
    }
    return change;
}
Also used : ITermVar(mb.nabl2.terms.ITermVar)

Aggregations

ITerm (mb.nabl2.terms.ITerm)7 ITermVar (mb.nabl2.terms.ITermVar)4 Test (org.junit.Test)3 List (java.util.List)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 SolveResult (mb.nabl2.solver.ISolver.SolveResult)2 HashMultimap (com.google.common.collect.HashMultimap)1 Lists (com.google.common.collect.Lists)1 Multimap (com.google.common.collect.Multimap)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Map (java.util.Map)1 Set (java.util.Set)1 ImmutableCEqual (mb.nabl2.constraints.equality.ImmutableCEqual)1 IMessageInfo (mb.nabl2.constraints.messages.IMessageInfo)1 ImmutableMessageInfo (mb.nabl2.constraints.messages.ImmutableMessageInfo)1 MessageContent (mb.nabl2.constraints.messages.MessageContent)1 CDistinct (mb.nabl2.constraints.sets.CDistinct)1 CEvalSet (mb.nabl2.constraints.sets.CEvalSet)1