1
2
3
4
5
6 package gov.nist.secauto.metaschema.core.metapath.antlr;
7
8 import org.antlr.v4.runtime.ParserRuleContext;
9 import org.antlr.v4.runtime.RuleContext;
10 import org.antlr.v4.runtime.tree.ParseTree;
11 import org.eclipse.jdt.annotation.NotOwning;
12
13 import java.io.PrintStream;
14
15 import edu.umd.cs.findbugs.annotations.NonNull;
16
17
18
19
20
21 public class ParseTreePrinter {
22 @SuppressWarnings("resource")
23 @NotOwning
24 @NonNull
25 private final PrintStream outputStream;
26 private boolean ignoringWrappers = true;
27
28
29
30
31
32
33
34 public ParseTreePrinter(@NotOwning @NonNull PrintStream outputStream) {
35 this.outputStream = outputStream;
36 }
37
38
39
40
41
42
43
44
45 public void setIgnoringWrappers(boolean ignoringWrappers) {
46 this.ignoringWrappers = ignoringWrappers;
47 }
48
49
50
51
52
53
54
55
56
57 @SuppressWarnings("PMD.UseVarargs")
58 public void print(ParseTree tree, String[] ruleNames) {
59 explore((RuleContext) tree.getPayload(), 0, ruleNames);
60 }
61
62 @SuppressWarnings("PMD.UseVarargs")
63 private void explore(RuleContext ctx, int indentation, String[] ruleNames) {
64 boolean toBeIgnored = ignoringWrappers && ctx.getChildCount() == 1 && ctx.getChild(0) instanceof ParserRuleContext;
65 String ruleName = ruleNames[ctx.getRuleIndex()];
66 for (int i = 0; i < indentation; i++) {
67 outputStream.print(" ");
68 }
69 outputStream.print(ruleName);
70 if (toBeIgnored) {
71 outputStream.print("(ignored)");
72 }
73 outputStream.print(": ");
74 outputStream.print(ctx.getText());
75 outputStream.println();
76
77 for (int i = 0; i < ctx.getChildCount(); i++) {
78 ParseTree element = ctx.getChild(i);
79 if (element instanceof RuleContext) {
80 explore((RuleContext) element, indentation + 1, ruleNames);
81 }
82 }
83 }
84 }