Search in sources :

Example 6 with PredictionContext

use of org.antlr.v4.runtime.atn.PredictionContext in project antlr4 by antlr.

the class TestGraphNodes method test_x_$_fullctx.

@Test
public void test_x_$_fullctx() {
    PredictionContext r = PredictionContext.merge(x(), PredictionContext.EMPTY, fullCtx(), null);
    //		System.out.println(toDOTString(r, fullCtx()));
    String expecting = "digraph G {\n" + "rankdir=LR;\n" + "  s0[shape=record, label=\"<p0>|<p1>$\"];\n" + "  s1[label=\"$\"];\n" + "  s0:p0->s1[label=\"9\"];\n" + "}\n";
    assertEquals(expecting, toDOTString(r, fullCtx()));
}
Also used : SingletonPredictionContext(org.antlr.v4.runtime.atn.SingletonPredictionContext) PredictionContext(org.antlr.v4.runtime.atn.PredictionContext) ArrayPredictionContext(org.antlr.v4.runtime.atn.ArrayPredictionContext) Test(org.junit.Test)

Example 7 with PredictionContext

use of org.antlr.v4.runtime.atn.PredictionContext in project antlr4 by antlr.

the class TestGraphNodes method test_Aax_Aby.

@Test
public void test_Aax_Aby() {
    // ax + by but in arrays
    SingletonPredictionContext a = createSingleton(x(), 1);
    SingletonPredictionContext b = createSingleton(y(), 2);
    ArrayPredictionContext A1 = array(a);
    ArrayPredictionContext A2 = array(b);
    PredictionContext r = PredictionContext.merge(A1, A2, rootIsWildcard(), null);
    //		System.out.println(toDOTString(r, rootIsWildcard()));
    String expecting = "digraph G {\n" + "rankdir=LR;\n" + "  s0[shape=record, label=\"<p0>|<p1>\"];\n" + "  s2[label=\"2\"];\n" + "  s3[label=\"*\"];\n" + "  s1[label=\"1\"];\n" + "  s0:p0->s1[label=\"1\"];\n" + "  s0:p1->s2[label=\"2\"];\n" + "  s2->s3[label=\"10\"];\n" + "  s1->s3[label=\"9\"];\n" + "}\n";
    assertEquals(expecting, toDOTString(r, rootIsWildcard()));
}
Also used : ArrayPredictionContext(org.antlr.v4.runtime.atn.ArrayPredictionContext) SingletonPredictionContext(org.antlr.v4.runtime.atn.SingletonPredictionContext) SingletonPredictionContext(org.antlr.v4.runtime.atn.SingletonPredictionContext) PredictionContext(org.antlr.v4.runtime.atn.PredictionContext) ArrayPredictionContext(org.antlr.v4.runtime.atn.ArrayPredictionContext) Test(org.junit.Test)

Example 8 with PredictionContext

use of org.antlr.v4.runtime.atn.PredictionContext in project antlr4 by antlr.

the class TestGraphNodes method toDOTString.

private static String toDOTString(PredictionContext context, boolean rootIsWildcard) {
    StringBuilder nodes = new StringBuilder();
    StringBuilder edges = new StringBuilder();
    Map<PredictionContext, PredictionContext> visited = new IdentityHashMap<PredictionContext, PredictionContext>();
    Map<PredictionContext, Integer> contextIds = new IdentityHashMap<PredictionContext, Integer>();
    Deque<PredictionContext> workList = new ArrayDeque<PredictionContext>();
    visited.put(context, context);
    contextIds.put(context, contextIds.size());
    workList.add(context);
    while (!workList.isEmpty()) {
        PredictionContext current = workList.pop();
        nodes.append("  s").append(contextIds.get(current)).append('[');
        if (current.size() > 1) {
            nodes.append("shape=record, ");
        }
        nodes.append("label=\"");
        if (current.isEmpty()) {
            nodes.append(rootIsWildcard ? '*' : '$');
        } else if (current.size() > 1) {
            for (int i = 0; i < current.size(); i++) {
                if (i > 0) {
                    nodes.append('|');
                }
                nodes.append("<p").append(i).append('>');
                if (current.getReturnState(i) == PredictionContext.EMPTY_RETURN_STATE) {
                    nodes.append(rootIsWildcard ? '*' : '$');
                }
            }
        } else {
            nodes.append(contextIds.get(current));
        }
        nodes.append("\"];\n");
        if (current.isEmpty()) {
            continue;
        }
        for (int i = 0; i < current.size(); i++) {
            if (current.getReturnState(i) == PredictionContext.EMPTY_RETURN_STATE) {
                continue;
            }
            if (visited.put(current.getParent(i), current.getParent(i)) == null) {
                contextIds.put(current.getParent(i), contextIds.size());
                workList.push(current.getParent(i));
            }
            edges.append("  s").append(contextIds.get(current));
            if (current.size() > 1) {
                edges.append(":p").append(i);
            }
            edges.append("->");
            edges.append('s').append(contextIds.get(current.getParent(i)));
            edges.append("[label=\"").append(current.getReturnState(i)).append("\"]");
            edges.append(";\n");
        }
    }
    StringBuilder builder = new StringBuilder();
    builder.append("digraph G {\n");
    builder.append("rankdir=LR;\n");
    builder.append(nodes);
    builder.append(edges);
    builder.append("}\n");
    return builder.toString();
}
Also used : IdentityHashMap(java.util.IdentityHashMap) SingletonPredictionContext(org.antlr.v4.runtime.atn.SingletonPredictionContext) PredictionContext(org.antlr.v4.runtime.atn.PredictionContext) ArrayPredictionContext(org.antlr.v4.runtime.atn.ArrayPredictionContext) ArrayDeque(java.util.ArrayDeque)

Example 9 with PredictionContext

use of org.antlr.v4.runtime.atn.PredictionContext in project antlr4 by antlr.

the class TestGraphNodes method test_a$_bx_fullctx.

@Test
public void test_a$_bx_fullctx() {
    PredictionContext x2 = x();
    PredictionContext a = a();
    PredictionContext b = createSingleton(x2, 2);
    PredictionContext r = PredictionContext.merge(a, b, fullCtx(), null);
    //		System.out.println(toDOTString(r, fullCtx()));
    String expecting = "digraph G {\n" + "rankdir=LR;\n" + "  s0[shape=record, label=\"<p0>|<p1>\"];\n" + "  s2[label=\"2\"];\n" + "  s1[label=\"$\"];\n" + "  s0:p0->s1[label=\"1\"];\n" + "  s0:p1->s2[label=\"2\"];\n" + "  s2->s1[label=\"9\"];\n" + "}\n";
    assertEquals(expecting, toDOTString(r, fullCtx()));
}
Also used : SingletonPredictionContext(org.antlr.v4.runtime.atn.SingletonPredictionContext) PredictionContext(org.antlr.v4.runtime.atn.PredictionContext) ArrayPredictionContext(org.antlr.v4.runtime.atn.ArrayPredictionContext) Test(org.junit.Test)

Example 10 with PredictionContext

use of org.antlr.v4.runtime.atn.PredictionContext in project antlr4 by antlr.

the class TestGraphNodes method test_a$_bx.

@Test
public void test_a$_bx() {
    PredictionContext x2 = x();
    PredictionContext a = a();
    PredictionContext b = createSingleton(x2, 2);
    PredictionContext r = PredictionContext.merge(a, b, rootIsWildcard(), null);
    //		System.out.println(toDOTString(r, rootIsWildcard()));
    String expecting = "digraph G {\n" + "rankdir=LR;\n" + "  s0[shape=record, label=\"<p0>|<p1>\"];\n" + "  s2[label=\"2\"];\n" + "  s1[label=\"*\"];\n" + "  s0:p0->s1[label=\"1\"];\n" + "  s0:p1->s2[label=\"2\"];\n" + "  s2->s1[label=\"9\"];\n" + "}\n";
    assertEquals(expecting, toDOTString(r, rootIsWildcard()));
}
Also used : SingletonPredictionContext(org.antlr.v4.runtime.atn.SingletonPredictionContext) PredictionContext(org.antlr.v4.runtime.atn.PredictionContext) ArrayPredictionContext(org.antlr.v4.runtime.atn.ArrayPredictionContext) Test(org.junit.Test)

Aggregations

ArrayPredictionContext (org.antlr.v4.runtime.atn.ArrayPredictionContext)39 PredictionContext (org.antlr.v4.runtime.atn.PredictionContext)39 SingletonPredictionContext (org.antlr.v4.runtime.atn.SingletonPredictionContext)39 Test (org.junit.Test)38 ArrayDeque (java.util.ArrayDeque)1 BitSet (java.util.BitSet)1 IdentityHashMap (java.util.IdentityHashMap)1 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)1 Ignore (org.junit.Ignore)1