KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > visit > PrettyPrinter


1 package polyglot.visit;
2
3 import polyglot.ast.*;
4 import polyglot.frontend.*;
5 import polyglot.util.*;
6
7 import java.io.*;
8 import java.util.*;
9
10 /**
11  * A PrettyPrinter generates output code from the processed AST.
12  * Output is sent to a code writer passes into the printAst method.
13  *
14  * To use:
15  * new PrettyPrinter().printAst(node, new CodeWriter(out));
16  */

17 public class PrettyPrinter
18 {
19     protected boolean appendSemicolon = true;
20     protected boolean printType = true;
21
22     public PrettyPrinter() {
23     }
24
25     /** Flag indicating whether to print a ';' after certain statements.
26      * This is used when pretty-printing for loops. */

27     public boolean appendSemicolon() {
28         return appendSemicolon;
29     }
30
31     /** Set a flag indicating whether to print a ';' after certain statements.
32      * This is used when pretty-printing for loops. */

33     public boolean appendSemicolon(boolean a) {
34         boolean old = this.appendSemicolon;
35         this.appendSemicolon = a;
36     return old;
37     }
38
39     /** Flag indicating whether to print the type in a local declaration.
40      * This is used when pretty-printing for loops. */

41     public boolean printType() {
42         return printType;
43     }
44
45     /** Set a flag indicating whether to print type type in a local declaration.
46      * This is used when pretty-printing for loops. */

47     public boolean printType(boolean a) {
48         boolean old = this.printType;
49         this.printType = a;
50     return old;
51     }
52
53     /** Print an AST node using the given code writer. The
54      * <code>CodeWriter.flush()</code> method must be called after this method
55      * to ensure code is output. Use <code>printAst</code> rather than this
56      * method to print the entire AST; this method should only be called by
57      * nodes to print their children.
58      */

59     public void print(Node parent, Node child, CodeWriter w) {
60         if (child != null) {
61             child.del().prettyPrint(w, this);
62         }
63     }
64
65     /** Print an AST node using the given code writer. The code writer
66      * is flushed by this method. */

67     public void printAst(Node ast, CodeWriter w) {
68         print(null, ast, w);
69
70         try {
71             w.flush();
72         }
73         catch (IOException e) {
74         }
75     }
76 }
77
Popular Tags