| Home >> All >> org >> sablecc >> [ sablecc Javadoc ] |
Source code: org/sablecc/sablecc/PrettyPrinter.java
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 2 * This file is part of SableCC. * 3 * See the file "LICENSE" for copyright information and the * 4 * terms and conditions for copying, distribution and * 5 * modification of SableCC. * 6 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 7 8 package org.sablecc.sablecc; 9 10 import java.util.*; 11 import org.sablecc.sablecc.analysis.*; 12 import org.sablecc.sablecc.node.*; 13 14 public class PrettyPrinter extends DepthFirstAdapter 15 { 16 public static String production_INDENT = " " ; 17 public static String prod_transform_INDENT = " "; 18 public static String alternative_INDENT = " " ; 19 public static String alt_transform_INDENT = " " ; 20 21 public void caseAProductions(AProductions node) 22 { 23 System.err.println("Productions \n"); 24 AProd [] prods = 25 (AProd [])node.getProds().toArray(new AProd[0]); 26 for(int i = 0; i < prods.length; i++) 27 { 28 prods[i].apply(this); 29 } 30 } 31 32 public void caseAProd(AProd node) 33 { 34 System.err.print(production_INDENT + node.getId().getText()); 35 String hasProdTransform = "="; 36 if(node.getArrow() == null) 37 { 38 hasProdTransform = ""; 39 System.err.println(" = "); 40 } 41 System.err.println(); 42 43 AElem[] elems = (AElem [])node.getProdTransform().toArray(new AElem[0]); 44 45 //if(node.getArrow() != null) 46 if(elems.length > 0) 47 { 48 System.err.print(prod_transform_INDENT + "{-> "); 49 50 for(int i=0; i<elems.length; i++) 51 { 52 //System.err.print(elems[i] + " "); 53 elems[i].apply(this); 54 System.err.print(" "); 55 } 56 System.err.println(" } " + hasProdTransform); 57 } 58 59 Object[] alts = (Object[])node.getAlts().toArray(); 60 for(int i=0; i<alts.length-1; i++) 61 { 62 ((PAlt)alts[i]).apply(this); 63 System.err.println( " |"); 64 } 65 ((PAlt)alts[alts.length-1]).apply(this); 66 67 System.err.println("\n" + alternative_INDENT + ";\n"); 68 } 69 70 public void caseAParsedAlt(AParsedAlt node) 71 { 72 System.err.print("\n" + alternative_INDENT); 73 74 if(node.getAltName() != null) 75 { 76 System.err.print("{" + node.getAltName().getText()+"} "); 77 } 78 79 AElem[] listElems = (AElem[])node.getElems().toArray(new AElem[0]); 80 for(int i=0; i<listElems.length; i++) 81 { 82 //System.err.print(listElems[i]); 83 listElems[i].apply(this); 84 System.err.print(" "); 85 } 86 87 if(node.getAltTransform() != null) 88 { 89 node.getAltTransform().apply(this); 90 } 91 } 92 93 public void caseAAltTransform(AAltTransform node) 94 { 95 System.err.print("\n" + alt_transform_INDENT + "{-> "); 96 97 Object []terms = (Object[]) node.getTerms().toArray(); 98 for(int i=0; i<terms.length; i++) 99 { 100 ((PTerm)terms[i]).apply(this); 101 System.err.print(" "); 102 } 103 104 System.err.print(" } "); 105 } 106 107 public void caseAProdName(AProdName node) 108 { 109 System.err.print(node.getId().getText()); 110 if(node.getProdNameTail() != null) 111 { 112 System.err.print("." + node.getProdNameTail().getText()); 113 } 114 } 115 116 public void caseANewTerm(ANewTerm node) 117 { 118 System.err.print("New "); 119 node.getProdName().apply(this); 120 System.err.print(" (" ); 121 122 Object []params = node.getParams().toArray(); 123 if(params.length > 0) 124 { 125 for(int i=0; i<params.length-1; i++) 126 { 127 ((PTerm)params[i]).apply(this); 128 System.err.print(", "); 129 } 130 ((PTerm)params[params.length-1]).apply(this); 131 } 132 System.err.print(" )"); 133 } 134 135 public void caseAListTerm(AListTerm node) 136 { 137 System.err.print("[ "); 138 Object []list_terms = node.getListTerms().toArray(); 139 140 for(int i=0; i<list_terms.length; i++) 141 { 142 ((PListTerm)list_terms[i]).apply(this); 143 } 144 System.err.print(" ]"); 145 } 146 147 public void caseASimpleTerm(ASimpleTerm node) 148 { 149 if(node.getSpecifier() != null) 150 { 151 if(node.getSpecifier() instanceof ATokenSpecifier) 152 { 153 System.err.print("T."); 154 } 155 else 156 { 157 System.err.print("P."); 158 } 159 } 160 System.err.print(node.getId().getText() ); 161 if(node.getSimpleTermTail() != null) 162 { 163 System.err.print("." + node.getSimpleTermTail().getText()); 164 } 165 System.err.print(" "); 166 } 167 168 public void caseANullTerm(ANullTerm node) 169 { 170 System.err.print("Null "); 171 } 172 173 public void caseANewListTerm(ANewListTerm node) 174 { 175 System.err.print("New "); 176 node.getProdName().apply(this); 177 System.err.print(" (" ); 178 179 Object []params = node.getParams().toArray(); 180 if(params.length > 0) 181 { 182 for(int i=0; i<params.length-1; i++) 183 { 184 ((PTerm)params[i]).apply(this); 185 System.err.print(", "); 186 } 187 ((PTerm)params[params.length-1]).apply(this); 188 } 189 System.err.print(" )"); 190 } 191 192 public void caseASimpleListTerm(ASimpleListTerm node) 193 { 194 if(node.getSpecifier() != null) 195 { 196 if(node.getSpecifier() instanceof ATokenSpecifier) 197 { 198 System.err.print("T."); 199 } 200 else 201 { 202 System.err.print("P."); 203 } 204 } 205 System.err.print(node.getId().getText() ); 206 if(node.getSimpleTermTail() != null) 207 { 208 System.err.print("." + node.getSimpleTermTail().getText()); 209 } 210 System.err.print(" "); 211 } 212 213 public void caseAAst(AAst node) 214 { 215 System.err.print("Abstract Syntax Tree\n"); 216 217 AAstProd [] prods = 218 (AAstProd [])node.getProds().toArray(new AAstProd[0]); 219 for(int i = 0; i < prods.length; i++) 220 { 221 prods[i].apply(this); 222 } 223 } 224 225 public void caseAAstProd(AAstProd node) 226 { 227 System.err.println(production_INDENT + node.getId().getText() + " ="); 228 229 AAstAlt[] alts = (AAstAlt[])node.getAlts().toArray(new AAstAlt[0]); 230 for(int i=0; i<alts.length-1; i++) 231 { 232 alts[i].apply(this); 233 System.err.println( "| "); 234 } 235 alts[alts.length-1].apply(this); 236 237 System.err.println("\n" + alternative_INDENT + ";\n"); 238 } 239 240 public void caseAAstAlt(AAstAlt node) 241 { 242 System.err.print(alternative_INDENT); 243 244 if(node.getAltName() != null) 245 { 246 System.err.print("{" + node.getAltName().getText()+"} "); 247 } 248 249 AElem[] listElems = (AElem[])node.getElems().toArray(new AElem[0]); 250 for(int i=0; i<listElems.length; i++) 251 { 252 //System.err.print(listElems[i]); 253 listElems[i].apply(this); 254 System.err.print(" "); 255 } 256 } 257 258 public void caseAElem(AElem node) 259 { 260 if(node.getElemName() != null) 261 { 262 System.err.print("[" + node.getElemName().getText() + "]: "); 263 } 264 265 if(node.getSpecifier() != null) 266 { 267 if(node.getSpecifier() instanceof ATokenSpecifier) 268 { 269 System.err.print("T."); 270 } 271 else 272 { 273 System.err.print("P."); 274 } 275 } 276 277 System.err.print(node.getId().getText()); 278 if(node.getUnOp() != null) 279 { 280 node.getUnOp().apply(new DepthFirstAdapter() 281 { 282 public void caseAStarUnOp(AStarUnOp node) 283 { 284 System.err.print("*"); 285 } 286 287 public void caseAQMarkUnOp(AQMarkUnOp node) 288 { 289 System.err.print("?"); 290 } 291 292 public void caseAPlusUnOp(APlusUnOp node) 293 { 294 System.err.print("+"); 295 } 296 } 297 ); 298 } 299 } 300 }