DumpTree.jrag 2.02 KB
Newer Older
Niklas Fors's avatar
Niklas Fors committed
1
2
3
4
5
6
7
8
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException;

// We have been careful to not import many types here because the imports are added for
// _ALL_ AST nodes and can cause collisions in the abstract grammar namespace.

aspect DumpTree {
Johannes Mey's avatar
Johannes Mey committed
9
  private static final String ASTNode.DUMP_TREE_INDENT = "  ";
Niklas Fors's avatar
Niklas Fors committed
10

Johannes Mey's avatar
Johannes Mey committed
11
12
13
14
15
  public String ASTNode.dumpTree() {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    dumpTree(new PrintStream(bytes));
    return bytes.toString();
  }
Niklas Fors's avatar
Niklas Fors committed
16

Johannes Mey's avatar
Johannes Mey committed
17
18
19
20
  public void ASTNode.dumpTree(PrintStream out) {
    dumpTree(out, "");
    out.flush();
  }
Niklas Fors's avatar
Niklas Fors committed
21

Johannes Mey's avatar
Johannes Mey committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  public void ASTNode.dumpTree(PrintStream out, String indent) {
    out.print(indent + getClass().getSimpleName());
    out.print(getTokens());
    String extra = extraDumpInfo();
    if (!extra.isEmpty()) {
      out.print(" " + extra);
    }
    out.println();
    String childIndent = indent + DUMP_TREE_INDENT;
    for (ASTNode child : astChildren()) {
      if (child == null) {
        out.println(childIndent + "null");
      } else {
        child.dumpTree(out, childIndent);
      }
    }
  }
Niklas Fors's avatar
Niklas Fors committed
39

Johannes Mey's avatar
Johannes Mey committed
40
  public String ASTNode.extraDumpInfo() { return ""; }
Niklas Fors's avatar
Niklas Fors committed
41

Johannes Mey's avatar
Johannes Mey committed
42
43
44
45
46
47
48
  public String ASTNode.getTokens() {
    java.util.TreeSet<java.lang.reflect.Method> methods = new java.util.TreeSet<>(
        new java.util.Comparator<java.lang.reflect.Method>() {
          public int compare(java.lang.reflect.Method m1, java.lang.reflect.Method m2) {
            return m1.getName().compareTo(m2.getName());
          }
        });
Niklas Fors's avatar
Niklas Fors committed
49

Johannes Mey's avatar
Johannes Mey committed
50
    methods.addAll(java.util.Arrays.asList(getClass().getMethods()));
Niklas Fors's avatar
Niklas Fors committed
51

Johannes Mey's avatar
Johannes Mey committed
52
53
54
55
56
57
58
59
60
61
62
63
64
    String result = "";
    for (java.lang.reflect.Method method : methods) {
      ASTNodeAnnotation.Token token = method.getAnnotation(ASTNodeAnnotation.Token.class);
      if (token != null) {
        try {
          result += String.format(" %s=\"%s\"", token.name(), method.invoke(this));
        } catch (IllegalAccessException ignored) {
        } catch (InvocationTargetException ignored) {
        }
      }
    }
    return result;
  }
Niklas Fors's avatar
Niklas Fors committed
65
}