/**
 * A simple mechanism for printing Pascal parse trees.  Intended mostly
 * as a debugging aid.
 *
 * @author Samuel A. Rebelsky
 * @version 1.0 of October 2002
 */
public class PascalTreePrinter
{
  private static final String indent = "  ";

  /**
   * Dump a Pascal Tree to the given output object.
   *
   * Pre: obj is either a Node or a Token.
   */
  public static void print(Object obj, SymbolPrinter helper, PrintWriter out)
  {
    print(obj, helper, out, "");
  } // print(Object,PrintWriter)

  private static void print(Object obj, 
                            SymbolPrinter helper, 
                            PrintWriter out, 
                            String offset) 
  {
    // Special case: The thing to be printed is null.  
    //   Don't print anything
    if (obj == null) {
    }
    // Standard case: A Node
    //   Print the symbol
    //   Print the children
    else if (obj instanceof Node) {
      Node n = (Node) obj;
      out.println(offset + helper.toString(n.getSymbol()));
      int numChildren = n.numChildren();
      for (int childNum = 0; childNum < numChildren; ++childNum) {
        print(obj.getChild(childNum), out, offset+indent);
      } // for each child
    } // if the object is a node.
    // Default case: Anything else
    //   Print it using the fascinating toString method provided
    //   by all objects.
    else {
      out.println(offset + obj.toString());
    } // if the object is not a node
  } // print(Object, PrintWriter, String)
} // class PascalTreePrinter

