1 2 package com.genimen.djeneric.tools.generator.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 = 1L; 20 21 private static final String CHAINED_MSG = "Chained to: "; 22 23 int _line = 0; 24 int _column = 0; 25 26 private String _stackTrace = null; 27 transient private boolean _ignoreMyOwnStack = false; 28 29 41 public ParseException(Token currentTokenVal, int[][] expectedTokenSequencesVal, String [] tokenImageVal) 42 { 43 super(""); 44 specialConstructor = true; 45 currentToken = currentTokenVal; 46 expectedTokenSequences = expectedTokenSequencesVal; 47 tokenImage = tokenImageVal; 48 49 _line = currentToken.next.beginLine; 50 _column = currentToken.next.beginColumn; 51 } 52 53 public ParseException() 54 { 55 super(); 56 specialConstructor = false; 57 } 58 59 68 69 public ParseException(String message, int atLine, int atColumn) 70 { 71 super(message); 72 specialConstructor = false; 73 _line = atLine; 74 _column = atColumn; 75 } 76 77 public ParseException(Exception chainThis, int atLine, int atColumn) 78 { 79 super(chainThis.getClass().getName() + ": " + chainThis.getMessage()); 80 storeStack(chainThis); 81 specialConstructor = false; 82 83 _line = atLine; 84 _column = atColumn; 85 } 86 87 public void printStackTrace() 88 { 89 if (_stackTrace != null) 90 { 91 System.err.println(_stackTrace); 92 if (!_ignoreMyOwnStack) System.err.print(CHAINED_MSG); 93 } 94 if (!_ignoreMyOwnStack) super.printStackTrace(); 95 } 96 97 public void printStackTrace(PrintStream printStream) 98 { 99 if (_stackTrace != null) 100 { 101 printStream.println(_stackTrace); 102 if (!_ignoreMyOwnStack) printStream.print(CHAINED_MSG); 103 } 104 if (!_ignoreMyOwnStack) super.printStackTrace(printStream); 105 } 106 107 public void printStackTrace(PrintWriter writer) 108 { 109 if (_stackTrace != null) 110 { 111 writer.println(_stackTrace); 112 if (!_ignoreMyOwnStack) writer.print(CHAINED_MSG); 113 } 114 if (!_ignoreMyOwnStack) super.printStackTrace(writer); 115 } 116 117 private void storeStack(Exception theEx) 118 { 119 _stackTrace = stack2String(theEx); 120 _ignoreMyOwnStack = true; } 122 123 private String stack2String(Exception x) 124 { 125 if (x == null) return ""; 126 127 StringWriter stringWriter = new StringWriter (); 128 java.io.PrintWriter stackTrace = new java.io.PrintWriter (stringWriter); 129 x.printStackTrace(stackTrace); 130 String result = stringWriter.toString().trim(); 131 if (result.length() == 0) result = null; 132 133 return result; 134 } 135 136 public int getLine() 137 { 138 return _line; 139 } 140 141 public int getColumn() 142 { 143 return _column; 144 } 145 146 public void setLine(int l) 147 { 148 _line = l; 149 } 150 151 public void setColumn(int c) 152 { 153 _column = c; 154 } 155 156 public ParseException(String message) 157 { 158 super(message); 159 specialConstructor = false; 160 } 161 162 167 protected boolean specialConstructor; 168 169 174 public Token currentToken; 175 176 181 public int[][] expectedTokenSequences; 182 183 188 public String [] tokenImage; 189 190 200 public String getMessage() 201 { 202 if (!specialConstructor) 203 { 204 return super.getMessage(); 205 } 206 String expected = ""; 207 int maxSize = 0; 208 for (int i = 0; i < expectedTokenSequences.length; i++) 209 { 210 if (maxSize < expectedTokenSequences[i].length) 211 { 212 maxSize = expectedTokenSequences[i].length; 213 } 214 for (int j = 0; j < expectedTokenSequences[i].length; j++) 215 { 216 expected += tokenImage[expectedTokenSequences[i][j]] + " "; 217 } 218 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) 219 { 220 expected += "..."; 221 } 222 expected += eol + " "; 223 } 224 String retval = "Encountered \""; 225 Token tok = currentToken.next; 226 for (int i = 0; i < maxSize; i++) 227 { 228 if (i != 0) retval += " "; 229 if (tok.kind == 0) 230 { 231 retval += tokenImage[0]; 232 break; 233 } 234 retval += add_escapes(tok.image); 235 tok = tok.next; 236 } 237 retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn; 238 retval += "." + eol; 239 if (expectedTokenSequences.length == 1) 240 { 241 retval += "Was expecting:" + eol + " "; 242 } 243 else 244 { 245 retval += "Was expecting one of:" + eol + " "; 246 } 247 retval += expected; 248 return retval; 249 } 250 251 254 protected String eol = System.getProperty("line.separator", "\n"); 255 256 261 protected String add_escapes(String str) 262 { 263 StringBuffer retval = new StringBuffer (); 264 char ch; 265 for (int i = 0; i < str.length(); i++) 266 { 267 switch (str.charAt(i)) 268 { 269 case 0 : 270 continue; 271 case '\b' : 272 retval.append("\\b"); 273 continue; 274 case '\t' : 275 retval.append("\\t"); 276 continue; 277 case '\n' : 278 retval.append("\\n"); 279 continue; 280 case '\f' : 281 retval.append("\\f"); 282 continue; 283 case '\r' : 284 retval.append("\\r"); 285 continue; 286 case '\"' : 287 retval.append("\\\""); 288 continue; 289 case '\'' : 290 retval.append("\\\'"); 291 continue; 292 case '\\' : 293 retval.append("\\\\"); 294 continue; 295 default : 296 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) 297 { 298 String s = "0000" + Integer.toString(ch, 16); 299 retval.append("\\u" + s.substring(s.length() - 4, s.length())); 300 } 301 else 302 { 303 retval.append(ch); 304 } 305 continue; 306 } 307 } 308 return retval.toString(); 309 } 310 311 } | Popular Tags |