package mjc.analysis;

import org.apache.log4j.Logger;

import mjc.node.EOF;
import mjc.node.Node;
import mjc.node.Start;
import mjc.node.Token;

/**
 * Simple visitor to print AST to log.
 */
public class ASTPrinter extends DepthFirstAdapter {
    private static final Logger logger = Logger.getLogger(ASTPrinter.class);

    private int i = 1; // Indentation level.

    public void print(Start tree) {
        i = 1;
        tree.apply(this);
    }

    @Override
    public void defaultIn(final Node node) {
        logger.info(String.format("%" + i + "s%s", "", node.getClass().getSimpleName()));
        i += 2;
    }

    @Override
    public void defaultCase(Node node) {
        if (node instanceof EOF)
            return;

        // For tokens, we print both their name and value.
        Token token = (Token) node;
        logger.info(String.format("%" + i + "s%s(%s)", "",
                token.getClass().getSimpleName(), token.getText()));
    }

    @Override
    public void defaultOut(final Node node) {
        i -= 2;
    }
}

@@ -20,41 +20,8 @@
 s;%0A%0A
-import org.apache.log4j.Logger;%0A%0A
 impo
@@ -144,19 +144,31 @@
  AST to 
-log
+standard output
 .%0A */%0Apu
@@ -233,117 +233,113 @@
 ate 
-static final Logger logger = Logger.getLogger(ASTPrinter.class);%0A%0A    private int i = 1; // Indentation level
+int indent;            // Indentation level.%0A    private StringBuilder builder; // Builder for the output
 .%0A%0A 
@@ -377,23 +377,8 @@
 ) %7B%0A
-        i = 1;%0A
     
@@ -440,92 +440,302 @@
 oid 
-defaultIn(final Node node) %7B%0A        logger.info(String.format(%22%25%22 + i + %22s%25s%22, %22%22, 
+inStart(Start node) %7B%0A        indent = 0;%0A        builder = new StringBuilder();%0A    %7D%0A%0A    @Override%0A    public void outStart(Start node) %7B%0A        System.out.print(builder.toString());%0A    %7D%0A%0A    @Override%0A    public void defaultIn(final Node node) %7B%0A        indent();%0A        builder.append(
 node
@@ -761,17 +761,23 @@
 leName()
-)
+ + '%5Cn'
 );%0A     
@@ -780,16 +780,21 @@
        i
+ndent
  += 2;%0A 
@@ -840,249 +840,244 @@
 ault
-Case(Node node) %7B%0A        if (node instanceof EOF)%0A            return;%0A%0A        // For tokens, we print both their name and value.%0A        Token token = (Token) node;%0A        logger.info(String.format(%22%25%22 + i + %22s%25s(%25s)%22, %22%22,%0A               
+Out(final Node node) %7B%0A        indent -= 2;%0A    %7D%0A%0A    @Override%0A    public void defaultCase(Node node) %7B%0A        if (node instanceof EOF)%0A            return;%0A%0A        indent();%0A%0A        Token token = (Token) node;%0A        String name =
  tok
@@ -1109,9 +1109,31 @@
 me()
-,
+;%0A        String text =
  tok
@@ -1148,93 +1148,180 @@
 xt()
-))
 ;%0A    
-%7D%0A%0A
     
-@Override%0A    public void defaultOut(final Node node) %7B%0A        i -= 2
+builder.append(String.format(%22%25s(%25s)%5Cn%22, name, text));%0A    %7D%0A%0A    private void indent() %7B%0A        for (int i = 0; i %3C indent; ++i)%0A            builder.append(' ')
 ;%0A  

Use StringBuilder in ASTPrinter.