1 2 22 package org.apache.derby.impl.tools.ij; 23 24 33 public class ParseException extends Exception { 34 46 public ParseException(Token currentTokenVal, 47 int[][] expectedTokenSequencesVal, 48 String [] tokenImageVal 49 ) 50 { 51 super(""); 52 specialConstructor = true; 53 currentToken = currentTokenVal; 54 expectedTokenSequences = expectedTokenSequencesVal; 55 tokenImage = tokenImageVal; 56 } 57 58 67 68 public ParseException() { 69 super(); 70 specialConstructor = false; 71 } 72 73 public ParseException(String message) { 74 super(message); 75 specialConstructor = false; 76 } 77 78 83 protected boolean specialConstructor; 84 85 90 public Token currentToken; 91 92 97 public int[][] expectedTokenSequences; 98 99 104 public String [] tokenImage; 105 106 116 public String getMessage() { 117 if (!specialConstructor) { 118 return super.getMessage(); 119 } 120 String expected = ""; 121 int maxSize = 0; 122 for (int i = 0; i < expectedTokenSequences.length; i++) { 123 if (maxSize < expectedTokenSequences[i].length) { 124 maxSize = expectedTokenSequences[i].length; 125 } 126 for (int j = 0; j < expectedTokenSequences[i].length; j++) { 127 expected += tokenImage[expectedTokenSequences[i][j]] + " "; 128 } 129 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) { 130 expected += "..."; 131 } 132 expected += eol + " "; 133 } 134 String retval = "Encountered \""; 135 Token tok = currentToken.next; 136 for (int i = 0; i < maxSize; i++) { 137 if (i != 0) retval += " "; 138 if (tok.kind == 0) { 139 retval += tokenImage[0]; 140 break; 141 } 142 retval += add_escapes(tok.image); 143 tok = tok.next; 144 } 145 retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn; 146 157 return retval; 158 } 159 160 163 protected String eol = System.getProperty("line.separator", "\n"); 164 165 170 protected String add_escapes(String str) { 171 StringBuffer retval = new StringBuffer (); 172 char ch; 173 for (int i = 0; i < str.length(); i++) { 174 switch (str.charAt(i)) 175 { 176 case 0 : 177 continue; 178 case '\b': 179 retval.append("\\b"); 180 continue; 181 case '\t': 182 retval.append("\\t"); 183 continue; 184 case '\n': 185 retval.append("\\n"); 186 continue; 187 case '\f': 188 retval.append("\\f"); 189 continue; 190 case '\r': 191 retval.append("\\r"); 192 continue; 193 case '\"': 194 retval.append("\\\""); 195 continue; 196 case '\'': 197 retval.append("\\\'"); 198 continue; 199 case '\\': 200 retval.append("\\\\"); 201 continue; 202 default: 203 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { 204 String s = "0000" + Integer.toString(ch, 16); 205 retval.append("\\u" + s.substring(s.length() - 4, s.length())); 206 } else { 207 retval.append(ch); 208 } 209 continue; 210 } 211 } 212 return retval.toString(); 213 } 214 215 } 216 | Popular Tags |