001: /*
002: * TreePrinter.java
003: *
004: * This work is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published
006: * by the Free Software Foundation; either version 2 of the License,
007: * or (at your option) any later version.
008: *
009: * This work is distributed in the hope that it will be useful, but
010: * WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017: * USA
018: *
019: * As a special exception, the copyright holders of this library give
020: * you permission to link this library with independent modules to
021: * produce an executable, regardless of the license terms of these
022: * independent modules, and to copy and distribute the resulting
023: * executable under terms of your choice, provided that you also meet,
024: * for each linked independent module, the terms and conditions of the
025: * license of that module. An independent module is a module which is
026: * not derived from or based on this library. If you modify this
027: * library, you may extend this exception to your version of the
028: * library, but you are not obligated to do so. If you do not wish to
029: * do so, delete this exception statement from your version.
030: *
031: * Copyright (c) 2003 Per Cederberg. All rights reserved.
032: */
033:
034: package net.percederberg.grammatica;
035:
036: import java.io.OutputStream;
037: import java.io.PrintWriter;
038: import java.io.Writer;
039:
040: import net.percederberg.grammatica.parser.Analyzer;
041: import net.percederberg.grammatica.parser.Node;
042:
043: /**
044: * A parse tree printer. This class prints the parse tree while it is
045: * being parsed.
046: *
047: * @author Per Cederberg, <per at percederberg dot net>
048: * @version 1.4
049: * @since 1.4
050: */
051: public class TreePrinter extends Analyzer {
052:
053: /**
054: * The current indentation level.
055: */
056: private int indentation = 0;
057:
058: /**
059: * The output stream to use.
060: */
061: private PrintWriter output;
062:
063: /**
064: * Creates a new parse tree printer.
065: *
066: * @param output the output stream to use
067: */
068: public TreePrinter(OutputStream output) {
069: this (new PrintWriter(output));
070: }
071:
072: /**
073: * Creates a new parse tree printer.
074: *
075: * @param output the output stream to use
076: */
077: public TreePrinter(Writer output) {
078: if (output instanceof PrintWriter) {
079: this .output = (PrintWriter) output;
080: } else {
081: this .output = new PrintWriter(output);
082: }
083: }
084:
085: /**
086: * Called when entering a parse tree node. By default this method
087: * does nothing. A subclass can override this method to handle
088: * each node separately.
089: *
090: * @param node the node being entered
091: */
092: protected void enter(Node node) {
093: for (int i = 0; i < indentation; i++) {
094: output.print(" ");
095: }
096: output.println(node.toString());
097: output.flush();
098: indentation++;
099: }
100:
101: /**
102: * Called when exiting a parse tree node. By default this method
103: * returns the node. A subclass can override this method to handle
104: * each node separately. If no parse tree should be created, this
105: * method should return null.
106: *
107: * @param node the node being exited
108: *
109: * @return the node to add to the parse tree, or
110: * null to remove the node
111: */
112: protected Node exit(Node node) {
113: indentation--;
114: return null;
115: }
116: }
|