1 2 package com.genimen.djeneric.repository.oql.core; 3 4 import java.io.PrintStream ; 5 import java.io.PrintWriter ; 6 import java.io.StringWriter ; 7 8 17 public class ParseException extends Exception 18 { 19 private static final long serialVersionUID = 3690196534013015865L; 20 21 private static final String CHAINED_MSG = "Chained to: "; 22 23 protected int _line = 0; 24 protected int _column = 0; 25 26 private String _stackTrace = null; 27 private String _additionalInfo = null; 28 transient private boolean _ignoreMyOwnStack = false; 29 30 42 public ParseException(Token currentTokenVal, int[][] expectedTokenSequencesVal, String [] tokenImageVal) 43 { 44 super(""); 45 specialConstructor = true; 46 currentToken = currentTokenVal; 47 expectedTokenSequences = expectedTokenSequencesVal; 48 tokenImage = tokenImageVal; 49 50 _line = currentToken.next.beginLine; 51 _column = currentToken.next.beginColumn; 52 53 } 54 55 public ParseException(String message, int atLine, int atColumn) 56 { 57 super(message); 58 specialConstructor = false; 59 _line = atLine; 60 _column = atColumn; 61 } 62 63 public ParseException(Exception chainThis, int atLine, int atColumn) 64 { 65 super(chainThis.getClass().getName() + ": " + chainThis.getMessage()); 66 storeStack(chainThis); 67 specialConstructor = false; 68 _line = atLine; 69 _column = atColumn; 70 } 71 72 public ParseException(Exception chainThis, int atLine, int atColumn, String additionalInfo) 73 { 74 super(chainThis.getClass().getName() + ": " + chainThis.getMessage()); 75 storeStack(chainThis); 76 specialConstructor = false; 77 _line = atLine; 78 _column = atColumn; 79 _additionalInfo = additionalInfo; 80 } 81 82 91 92 public ParseException() 93 { 94 super(); 95 specialConstructor = false; 96 } 97 98 public ParseException(String message) 99 { 100 super(message); 101 specialConstructor = false; 102 } 103 104 public void printStackTrace() 105 { 106 if (_stackTrace != null) 107 { 108 System.err.println(_stackTrace); 109 if (!_ignoreMyOwnStack) System.err.print(CHAINED_MSG); 110 } 111 if (_additionalInfo != null) 112 { 113 System.err.println(_additionalInfo); 114 } 115 if (!_ignoreMyOwnStack) super.printStackTrace(); 116 } 117 118 public void printStackTrace(PrintStream printStream) 119 { 120 if (_stackTrace != null) 121 { 122 printStream.println(_stackTrace); 123 if (!_ignoreMyOwnStack) printStream.print(CHAINED_MSG); 124 } 125 if (_additionalInfo != null) 126 { 127 printStream.println(_additionalInfo); 128 } 129 if (!_ignoreMyOwnStack) super.printStackTrace(printStream); 130 } 131 132 public void printStackTrace(PrintWriter writer) 133 { 134 if (_stackTrace != null) 135 { 136 writer.println(_stackTrace); 137 if (!_ignoreMyOwnStack) writer.print(CHAINED_MSG); 138 } 139 if (_additionalInfo != null) 140 { 141 writer.println(_additionalInfo); 142 } 143 if (!_ignoreMyOwnStack) super.printStackTrace(writer); 144 } 145 146 private void storeStack(Exception theEx) 147 { 148 _stackTrace = stack2String(theEx); 149 _ignoreMyOwnStack = true; } 151 152 private String stack2String(Exception x) 153 { 154 if (x == null) return ""; 155 156 StringWriter stringWriter = new StringWriter (); 157 java.io.PrintWriter stackTrace = new java.io.PrintWriter (stringWriter); 158 x.printStackTrace(stackTrace); 159 String result = stringWriter.toString().trim(); 160 if (result.length() == 0) result = null; 161 162 return result; 163 } 164 165 public int getLine() 166 { 167 return _line; 168 } 169 170 public int getColumn() 171 { 172 return _column; 173 } 174 175 public void setLine(int l) 176 { 177 _line = l; 178 } 179 180 public void setColumn(int c) 181 { 182 _column = c; 183 } 184 185 190 protected boolean specialConstructor; 191 192 197 public Token currentToken; 198 199 204 public int[][] expectedTokenSequences; 205 206 211 public String [] tokenImage; 212 213 223 public String getMessage() 224 { 225 if (!specialConstructor) 226 { 227 return super.getMessage(); 228 } 229 String expected = ""; 230 int maxSize = 0; 231 for (int i = 0; i < expectedTokenSequences.length; i++) 232 { 233 if (maxSize < expectedTokenSequences[i].length) 234 { 235 maxSize = expectedTokenSequences[i].length; 236 } 237 for (int j = 0; j < expectedTokenSequences[i].length; j++) 238 { 239 expected += tokenImage[expectedTokenSequences[i][j]] + " "; 240 } 241 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) 242 { 243 expected += "..."; 244 } 245 expected += eol + " "; 246 } 247 String retval = "Encountered \""; 248 Token tok = currentToken.next; 249 for (int i = 0; i < maxSize; i++) 250 { 251 if (i != 0) retval += " "; 252 if (tok.kind == 0) 253 { 254 retval += tokenImage[0]; 255 break; 256 } 257 retval += add_escapes(tok.image); 258 tok = tok.next; 259 } 260 retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn; 261 retval += "." + eol; 262 if (expectedTokenSequences.length == 1) 263 { 264 retval += "Was expecting:" + eol + " "; 265 } 266 else 267 { 268 retval += "Was expecting one of:" + eol + " "; 269 } 270 retval += expected; 271 return retval; 272 } 273 274 277 protected String eol = System.getProperty("line.separator", "\n"); 278 279 284 protected String add_escapes(String str) 285 { 286 StringBuffer retval = new StringBuffer (); 287 char ch; 288 for (int i = 0; i < str.length(); i++) 289 { 290 switch (str.charAt(i)) 291 { 292 case 0 : 293 continue; 294 case '\b' : 295 retval.append("\\b"); 296 continue; 297 case '\t' : 298 retval.append("\\t"); 299 continue; 300 case '\n' : 301 retval.append("\\n"); 302 continue; 303 case '\f' : 304 retval.append("\\f"); 305 continue; 306 case '\r' : 307 retval.append("\\r"); 308 continue; 309 case '\"' : 310 retval.append("\\\""); 311 continue; 312 case '\'' : 313 retval.append("\\\'"); 314 continue; 315 case '\\' : 316 retval.append("\\\\"); 317 continue; 318 default : 319 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) 320 { 321 String s = "0000" + Integer.toString(ch, 16); 322 retval.append("\\u" + s.substring(s.length() - 4, s.length())); 323 } 324 else 325 { 326 retval.append(ch); 327 } 328 continue; 329 } 330 } 331 return retval.toString(); 332 } 333 334 public String getAdditionalInfo() 335 { 336 return _additionalInfo; 337 } 338 339 public void setAdditionalInfo(String additionalInfo) 340 { 341 _additionalInfo = additionalInfo; 342 } 343 344 } | Popular Tags |