package se.chalmers.cs.gf.SyntaxTree; import se.chalmers.cs.gf.SyntaxTree.Absyn.*; public class PrettyPrinter { //For certain applications increasing the initial size of the buffer may improve performance. private static final int INITIAL_BUFFER_SIZE = 128; //You may wish to change the parentheses used in precedence. private static final String _L_PAREN = new String("("); private static final String _R_PAREN = new String(")"); //You may wish to change render private static void render(String s) { if (s.equals("{")) { buf_.append("\n"); indent(); buf_.append(s); _n_ = _n_ + 2; buf_.append("\n"); indent(); } else if (s.equals("(") || s.equals("[")) buf_.append(s); else if (s.equals(")") || s.equals("]")) { backup(); buf_.append(s); buf_.append(" "); } else if (s.equals("}")) { _n_ = _n_ - 2; backup(); backup(); buf_.append(s); buf_.append("\n"); indent(); } else if (s.equals(",")) { backup(); buf_.append(s); buf_.append(" "); } else if (s.equals(";")) { backup(); buf_.append(s); buf_.append("\n"); indent(); } else if (s.equals("")) return; else { buf_.append(s); buf_.append(" "); } } // print and show methods are defined for each category. public static String print(se.chalmers.cs.gf.SyntaxTree.Absyn.Tr foo) { pp(foo, 0); trim(); String temp = buf_.toString(); buf_.delete(0,buf_.length()); return temp; } public static String show(se.chalmers.cs.gf.SyntaxTree.Absyn.Tr foo) { sh(foo); String temp = buf_.toString(); buf_.delete(0,buf_.length()); return temp; } /*** You shouldn't need to change anything beyond this point. ***/ private static void pp(se.chalmers.cs.gf.SyntaxTree.Absyn.Tr foo, int _i_) { if (foo instanceof se.chalmers.cs.gf.SyntaxTree.Absyn.TNode) { se.chalmers.cs.gf.SyntaxTree.Absyn.TNode _tnode = (se.chalmers.cs.gf.SyntaxTree.Absyn.TNode) foo; if (_i_ > 0) render(_L_PAREN); pp(_tnode.ident_, 0); pp(_tnode.listtr_, 2); if (_i_ > 0) render(_R_PAREN); } if (foo instanceof se.chalmers.cs.gf.SyntaxTree.Absyn.TAtom) { se.chalmers.cs.gf.SyntaxTree.Absyn.TAtom _tatom = (se.chalmers.cs.gf.SyntaxTree.Absyn.TAtom) foo; if (_i_ > 2) render(_L_PAREN); pp(_tatom.ident_, 0); if (_i_ > 2) render(_R_PAREN); } if (foo instanceof se.chalmers.cs.gf.SyntaxTree.Absyn.TStr) { se.chalmers.cs.gf.SyntaxTree.Absyn.TStr _tstr = (se.chalmers.cs.gf.SyntaxTree.Absyn.TStr) foo; if (_i_ > 2) render(_L_PAREN); printQuoted(_tstr.string_); if (_i_ > 2) render(_R_PAREN); } if (foo instanceof se.chalmers.cs.gf.SyntaxTree.Absyn.TInt) { se.chalmers.cs.gf.SyntaxTree.Absyn.TInt _tint = (se.chalmers.cs.gf.SyntaxTree.Absyn.TInt) foo; if (_i_ > 2) render(_L_PAREN); pp(_tint.integer_, 0); if (_i_ > 2) render(_R_PAREN); } } private static void pp(se.chalmers.cs.gf.SyntaxTree.Absyn.ListTr foo, int _i_) { for (java.util.Iterator