1 2 31 32 package bsh; 33 34 43 44 public class ParseException extends EvalError { 46 48 50 String sourceFile = "<unknown>"; 51 52 55 public void setErrorSourceFile( String file ) { 56 this.sourceFile = file; 57 } 58 59 public String getErrorSourceFile() { 60 return sourceFile; 61 } 62 63 65 77 public ParseException(Token currentTokenVal, 78 int[][] expectedTokenSequencesVal, 79 String [] tokenImageVal 80 ) 81 { 82 this(); 84 specialConstructor = true; 86 currentToken = currentTokenVal; 87 expectedTokenSequences = expectedTokenSequencesVal; 88 tokenImage = tokenImageVal; 89 } 90 91 100 101 public ParseException() { 102 this(""); 104 specialConstructor = false; 106 } 107 108 public ParseException(String message) { 109 super( message, null, null ); 112 specialConstructor = false; 114 } 115 116 121 protected boolean specialConstructor; 122 123 128 public Token currentToken; 129 130 135 public int[][] expectedTokenSequences; 136 137 142 public String [] tokenImage; 143 144 public String getMessage() { 146 return getMessage( false ); 147 } 148 150 160 public String getMessage( boolean debug ) { 162 if (!specialConstructor) { 164 return super.getMessage(); 165 } 166 String expected = ""; 167 int maxSize = 0; 168 for (int i = 0; i < expectedTokenSequences.length; i++) { 169 if (maxSize < expectedTokenSequences[i].length) { 170 maxSize = expectedTokenSequences[i].length; 171 } 172 for (int j = 0; j < expectedTokenSequences[i].length; j++) { 173 expected += tokenImage[expectedTokenSequences[i][j]] + " "; 174 } 175 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) { 176 expected += "..."; 177 } 178 expected += eol + " "; 179 } 180 String retval = "In file: "+ sourceFile +" Encountered \""; 182 Token tok = currentToken.next; 184 for (int i = 0; i < maxSize; i++) { 185 if (i != 0) retval += " "; 186 if (tok.kind == 0) { 187 retval += tokenImage[0]; 188 break; 189 } 190 retval += add_escapes(tok.image); 191 tok = tok.next; 192 } 193 retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn + "." + eol; 194 195 if ( debug ) 197 { 198 if (expectedTokenSequences.length == 1) { 199 retval += "Was expecting:" + eol + " "; 200 } else { 201 retval += "Was expecting one of:" + eol + " "; 202 } 203 204 retval += expected; 205 } 206 208 return retval; 209 } 210 211 214 protected String eol = System.getProperty("line.separator", "\n"); 215 216 221 protected String add_escapes(String str) { 222 StringBuffer retval = new StringBuffer (); 223 char ch; 224 for (int i = 0; i < str.length(); i++) { 225 switch (str.charAt(i)) 226 { 227 case 0 : 228 continue; 229 case '\b': 230 retval.append("\\b"); 231 continue; 232 case '\t': 233 retval.append("\\t"); 234 continue; 235 case '\n': 236 retval.append("\\n"); 237 continue; 238 case '\f': 239 retval.append("\\f"); 240 continue; 241 case '\r': 242 retval.append("\\r"); 243 continue; 244 case '\"': 245 retval.append("\\\""); 246 continue; 247 case '\'': 248 retval.append("\\\'"); 249 continue; 250 case '\\': 251 retval.append("\\\\"); 252 continue; 253 default: 254 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { 255 String s = "0000" + Integer.toString(ch, 16); 256 retval.append("\\u" + s.substring(s.length() - 4, s.length())); 257 } else { 258 retval.append(ch); 259 } 260 continue; 261 } 262 } 263 return retval.toString(); 264 } 265 266 268 public int getErrorLineNumber() 269 { 270 return currentToken.next.beginLine; 271 } 272 273 public String getErrorText() { 274 int maxSize = 0; 276 for (int i = 0; i < expectedTokenSequences.length; i++) { 277 if (maxSize < expectedTokenSequences[i].length) 278 maxSize = expectedTokenSequences[i].length; 279 } 280 281 String retval = ""; 282 Token tok = currentToken.next; 283 for (int i = 0; i < maxSize; i++) 284 { 285 if (i != 0) retval += " "; 286 if (tok.kind == 0) { 287 retval += tokenImage[0]; 288 break; 289 } 290 retval += add_escapes(tok.image); 291 tok = tok.next; 292 } 293 294 return retval; 295 } 296 297 public String toString() { 298 return getMessage(); 299 } 300 301 303 } 304 | Popular Tags |