1 8 9 package org.lsmp.djep.xjep; 10 import org.nfunk.jep.*; 12 13 import java.io.PrintStream ; 14 import java.util.Hashtable ; 15 29 public class PrintVisitor extends ErrorCatchingVisitor 30 { 31 32 public static final int FULL_BRACKET = 1; 33 private int maxLen = -1; 34 protected StringBuffer sb; 35 36 protected int mode=0; 38 private Hashtable specialRules = new Hashtable (); 39 40 41 42 public PrintVisitor() 43 { 44 55 } 56 57 58 59 60 public void print(Node node,PrintStream out) 61 { 62 sb = new StringBuffer (); 63 acceptCatchingErrors(node,null); 64 if(maxLen == -1) 65 out.print(sb); 66 else 67 { 68 while(true) { 69 if(sb.length() < maxLen) { 70 out.print(sb); 71 return; 72 } 73 int pos = maxLen-2; 74 for(int i=maxLen-2;i>=0;--i) { 75 char c = sb.charAt(i); 76 if(c == '+' || c == '-' || c == '*' || c == '/'){ 77 pos = i; break; 78 } 79 } 80 out.println(sb.substring(0,pos+1)); 82 sb.delete(0,pos+1); 83 } 84 } 85 } 86 87 88 public void print(Node node) { print(node,System.out); } 89 90 91 92 public void println(Node node,PrintStream out) 93 { 94 print(node,out); 95 out.println(""); 96 } 97 98 99 public void println(Node node) { println(node,System.out); } 100 101 102 103 public String toString(Node node) 104 { 105 sb = new StringBuffer (); 106 acceptCatchingErrors(node,null); 107 return sb.toString(); 108 } 109 110 131 public interface PrintRulesI 132 { 133 134 public void append(Node node,PrintVisitor pv) throws ParseException; 135 } 136 137 139 public void append(String s) { sb.append(s); } 140 141 143 public void addSpecialRule(Operator op,PrintRulesI rules) 144 { 145 specialRules.put(op,rules); 146 } 147 148 149 150 151 private void printNoBrackets(Node node) throws ParseException 152 { 153 node.jjtAccept(this,null); 154 } 155 156 157 private void printBrackets(Node node) throws ParseException 158 { 159 sb.append("("); 160 printNoBrackets(node); 161 sb.append(")"); 162 } 163 164 165 private Object visitUnary(ASTFunNode node, Object data) throws ParseException 166 { 167 Node rhs = node.jjtGetChild(0); 168 169 sb.append(node.getOperator().getSymbol()); 171 if(rhs instanceof ASTFunNode && ((ASTFunNode) rhs).isOperator()) 173 printBrackets(rhs); else 175 printNoBrackets(rhs); 176 177 return data; 178 } 179 180 public Object visit(ASTFunNode node, Object data) throws ParseException 181 { 182 if(!node.isOperator()) return visitFun(node); 183 if(node instanceof PrintRulesI) 184 { 185 ((PrintRulesI) node).append(node,this); 186 return null; 187 } 188 if(node.getOperator()==null) 189 { 190 throw new ParseException("Null operator in print for "+node); 191 } 192 if(specialRules.containsKey(node.getOperator())) 193 { 194 ((PrintRulesI) specialRules.get(node.getOperator())).append(node,this); 195 return null; 196 } 197 if(node.getPFMC() instanceof org.nfunk.jep.function.List) 198 { 199 append("["); 200 for(int i=0;i<node.jjtGetNumChildren();++i) 201 { 202 if(i>0) append(","); 203 node.jjtGetChild(i).jjtAccept(this, null); 204 } 205 append("]"); 206 return null; 207 } 208 209 if(((XOperator) node.getOperator()).isUnary()) 210 return visitUnary(node,data); 211 if(((XOperator) node.getOperator()).isBinary()) 212 { 213 Node lhs = node.jjtGetChild(0); 214 Node rhs = node.jjtGetChild(1); 215 XOperator top = (XOperator) node.getOperator(); 216 217 if((mode & FULL_BRACKET)!= 0) 218 { 219 printBrackets(lhs); 220 } 221 else if(lhs instanceof ASTFunNode && ((ASTFunNode) lhs).isOperator()) 222 { 223 XOperator lhsop = (XOperator) ((ASTFunNode) lhs).getOperator(); 224 if(top == lhsop) 225 { 226 if(top.getBinding() == XOperator.LEFT && top.isAssociative() ) 228 printNoBrackets(lhs); 229 else if(top.useBindingForPrint()) 230 printNoBrackets(lhs); 231 else 232 printBrackets(lhs); } 234 else if(top.getPrecedence() == lhsop.getPrecedence()) 235 { 236 if(lhsop.getBinding() == XOperator.LEFT && lhsop.isAssociative()) 237 printNoBrackets(lhs); 238 else if(lhsop.useBindingForPrint()) 239 printNoBrackets(lhs); 240 else printBrackets(lhs); 241 } 243 else if(top.getPrecedence() > lhsop.getPrecedence()) printNoBrackets(lhs); 245 else 246 printBrackets(lhs); 247 } 248 else 249 printNoBrackets(lhs); 250 251 sb.append(node.getOperator().getSymbol()); 253 255 if((mode & FULL_BRACKET)!= 0) 256 { 257 printBrackets(rhs); 258 } 259 else if(rhs instanceof ASTFunNode && ((ASTFunNode) rhs).isOperator()) 260 { 261 XOperator rhsop = (XOperator) ((ASTFunNode) rhs).getOperator(); 262 if(top == rhsop) 263 { 264 if(top.getBinding() == XOperator.RIGHT || top.isAssociative() ) printNoBrackets(rhs); 267 else 268 printBrackets(rhs); } 270 else if(top.getPrecedence() == rhsop.getPrecedence()) 271 { 272 if(top.getBinding() == XOperator.LEFT && top.isAssociative() ) printNoBrackets(rhs); else 275 printBrackets(rhs); } 277 else if(top.getPrecedence() > rhsop.getPrecedence()) printNoBrackets(rhs); 279 else 280 printBrackets(rhs); 281 } 282 else 283 printNoBrackets(rhs); 284 } 285 return null; 286 } 287 288 289 private Object visitFun(ASTFunNode node) throws ParseException 290 { 291 sb.append(node.getName()+"("); 292 for(int i=0;i<node.jjtGetNumChildren();++i) 293 { 294 if(i>0) sb.append(","); 295 node.jjtGetChild(i).jjtAccept(this, null); 296 } 297 sb.append(")"); 298 299 return null; 300 } 301 302 public Object visit(ASTVarNode node, Object data) throws ParseException { 303 sb.append(node.getName()); 304 return data; 305 } 306 307 public Object visit(ASTConstant node, Object data) { 308 sb.append(node.getValue()); 309 return data; 310 } 311 314 public int getMode() { 315 return mode; 316 } 317 318 327 public void setMode(int mode,boolean flag) { 328 if(flag) 329 this.mode |= mode; 330 else 331 this.mode ^= mode; 332 } 333 334 337 public int getMaxLen() { 338 return maxLen; 339 } 340 341 347 public void setMaxLen(int i) { 348 maxLen = i; 349 } 350 351 } 352 353 354 | Popular Tags |