1 21 22 package net.percederberg.grammatica; 23 24 import java.io.OutputStream ; 25 import java.io.PrintWriter ; 26 import java.io.Writer ; 27 28 import net.percederberg.grammatica.parser.Analyzer; 29 import net.percederberg.grammatica.parser.Node; 30 31 39 public class TreePrinter extends Analyzer { 40 41 44 private int indentation = 0; 45 46 49 private PrintWriter output; 50 51 56 public TreePrinter(OutputStream output) { 57 this(new PrintWriter (output)); 58 } 59 60 65 public TreePrinter(Writer output) { 66 if (output instanceof PrintWriter ) { 67 this.output = (PrintWriter ) output; 68 } else { 69 this.output = new PrintWriter (output); 70 } 71 } 72 73 80 protected void enter(Node node) { 81 for (int i = 0; i < indentation; i++) { 82 output.print(" "); 83 } 84 output.println(node.toString()); 85 output.flush(); 86 indentation++; 87 } 88 89 100 protected Node exit(Node node) { 101 indentation--; 102 return null; 103 } 104 } 105 | Popular Tags |