1 2 package com.genimen.djeneric.tools.scriptengine.core; 3 4 import java.io.PrintStream ; 5 import java.io.PrintWriter ; 6 import java.io.StringWriter ; 7 8 import com.genimen.djeneric.language.Messages; 9 10 15 public class ParseException extends Exception 16 { 17 private static final long serialVersionUID = 1L; 18 19 private static final String CHAINED_MSG = "Chained to: "; 20 21 protected int _line = 0; 22 protected int _column = 0; 23 24 private String _stackTrace = null; 25 transient private boolean _ignoreMyOwnStack = false; 26 27 34 public ParseException(Token currentTokenVal, int[][] expectedTokenSequencesVal, String [] tokenImageVal) 35 { 36 super(""); 37 specialConstructor = true; 38 currentToken = currentTokenVal; 39 expectedTokenSequences = expectedTokenSequencesVal; 40 tokenImage = tokenImageVal; 41 42 _line = currentToken.next.beginLine; 43 _column = currentToken.next.beginColumn; 44 45 } 46 47 public ParseException(String message, SimpleNode cause) 48 { 49 super(message); 50 specialConstructor = false; 51 _line = cause.getLine(); 52 _column = cause.getColumn(); 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(Throwable 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(Throwable chainThis, SimpleNode cause) 73 { 74 super(chainThis.getClass().getName() + ": " + chainThis.getMessage()); 75 storeStack(chainThis); 76 specialConstructor = false; 77 _line = cause.getLine(); 78 _column = cause.getColumn(); 79 } 80 81 public ParseException(Throwable chainThis) 82 { 83 super(chainThis.getClass().getName() + ": " + chainThis.getMessage()); 84 storeStack(chainThis); 85 specialConstructor = false; 86 87 if (chainThis instanceof ParseException) 88 { 89 ParseException cause = (ParseException) chainThis; 90 _line = cause._line; 91 _column = cause._line; 92 } 93 } 94 95 101 102 public ParseException() 103 { 104 super(); 105 specialConstructor = false; 106 } 107 108 public ParseException(String message) 109 { 110 super(message); 111 specialConstructor = false; 112 } 113 114 public void printStackTrace() 115 { 116 if (_stackTrace != null) 117 { 118 System.err.println(_stackTrace); 119 if (!_ignoreMyOwnStack) System.err.print(CHAINED_MSG); 120 } 121 if (!_ignoreMyOwnStack) super.printStackTrace(); 122 } 123 124 public void printStackTrace(PrintStream printStream) 125 { 126 if (_stackTrace != null) 127 { 128 printStream.println(_stackTrace); 129 if (!_ignoreMyOwnStack) printStream.print(CHAINED_MSG); 130 } 131 if (!_ignoreMyOwnStack) super.printStackTrace(printStream); 132 } 133 134 public void printStackTrace(PrintWriter writer) 135 { 136 if (_stackTrace != null) 137 { 138 writer.println(_stackTrace); 139 if (!_ignoreMyOwnStack) writer.print(CHAINED_MSG); 140 } 141 if (!_ignoreMyOwnStack) super.printStackTrace(writer); 142 } 143 144 private void storeStack(Throwable theEx) 145 { 146 _stackTrace = stack2String(theEx); 147 _ignoreMyOwnStack = true; } 149 150 private String stack2String(Throwable x) 151 { 152 if (x == null) return ""; 153 154 StringWriter stringWriter = new StringWriter (); 155 java.io.PrintWriter stackTrace = new java.io.PrintWriter (stringWriter); 156 x.printStackTrace(stackTrace); 157 String result = stringWriter.toString().trim(); 158 if (result.length() == 0) result = null; 159 160 return result; 161 } 162 163 public int getLine() 164 { 165 return _line; 166 } 167 168 public int getColumn() 169 { 170 return _column; 171 } 172 173 public void setLine(int l) 174 { 175 _line = l; 176 } 177 178 public void setColumn(int c) 179 { 180 _column = c; 181 } 182 183 187 protected boolean specialConstructor; 188 189 193 public Token currentToken; 194 195 199 public int[][] expectedTokenSequences; 200 201 205 public String [] tokenImage; 206 207 214 public String getMessage() 215 { 216 if (!specialConstructor) 217 { 218 return super.getMessage(); 219 } 220 String expected = ""; 221 int maxSize = 0; 222 for (int i = 0; i < expectedTokenSequences.length; i++) 223 { 224 if (maxSize < expectedTokenSequences[i].length) 225 { 226 maxSize = expectedTokenSequences[i].length; 227 } 228 for (int j = 0; j < expectedTokenSequences[i].length; j++) 229 { 230 expected += tokenImage[expectedTokenSequences[i][j]] + " "; 231 } 232 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) 233 { 234 expected += "..."; 235 } 236 expected += eol + " "; 237 } 238 String retval = Messages.getString("ParseException.Encountered") + " \""; 239 Token tok = currentToken.next; 240 for (int i = 0; i < maxSize; i++) 241 { 242 if (i != 0) retval += " "; 243 if (tok.kind == 0) 244 { 245 retval += tokenImage[0]; 246 break; 247 } 248 retval += add_escapes(tok.image); 249 tok = tok.next; 250 } 251 retval += "\" " 252 + Messages.getString("ParseException.position", String.valueOf(currentToken.next.beginLine), String 253 .valueOf(currentToken.next.beginColumn)); 254 retval += "." + eol; 255 if (expectedTokenSequences.length == 1) 256 { 257 retval += Messages.getString("ParseException.WasExpecting") + eol + " "; 258 } 259 else 260 { 261 retval += Messages.getString("ParseException.WasExpectingOne") + eol + " "; 262 } 263 retval += expected; 264 return retval; 265 } 266 267 270 protected String eol = System.getProperty("line.separator", "\n"); 271 272 276 protected String add_escapes(String str) 277 { 278 StringBuffer retval = new StringBuffer (); 279 char ch; 280 for (int i = 0; i < str.length(); i++) 281 { 282 switch (str.charAt(i)) 283 { 284 case 0 : 285 continue; 286 case '\b' : 287 retval.append("\\b"); 288 continue; 289 case '\t' : 290 retval.append("\\t"); 291 continue; 292 case '\n' : 293 retval.append("\\n"); 294 continue; 295 case '\f' : 296 retval.append("\\f"); 297 continue; 298 case '\r' : 299 retval.append("\\r"); 300 continue; 301 case '\"' : 302 retval.append("\\\""); 303 continue; 304 case '\'' : 305 retval.append("\\\'"); 306 continue; 307 case '\\' : 308 retval.append("\\\\"); 309 continue; 310 default : 311 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) 312 { 313 String s = "0000" + Integer.toString(ch, 16); 314 retval.append("\\u" + s.substring(s.length() - 4, s.length())); 315 } 316 else 317 { 318 retval.append(ch); 319 } 320 continue; 321 } 322 } 323 return retval.toString(); 324 } 325 326 } | Popular Tags |