1 2 60 package org.objectstyle.cayenne.exp.parser; 61 62 71 public class ParseException extends Exception { 72 73 85 public ParseException(Token currentTokenVal, 86 int[][] expectedTokenSequencesVal, 87 String [] tokenImageVal 88 ) 89 { 90 super(""); 91 specialConstructor = true; 92 currentToken = currentTokenVal; 93 expectedTokenSequences = expectedTokenSequencesVal; 94 tokenImage = tokenImageVal; 95 } 96 97 106 107 public ParseException() { 108 super(); 109 specialConstructor = false; 110 } 111 112 public ParseException(String message) { 113 super(message); 114 specialConstructor = false; 115 } 116 117 122 protected boolean specialConstructor; 123 124 129 public Token currentToken; 130 131 136 public int[][] expectedTokenSequences; 137 138 143 public String [] tokenImage; 144 145 155 public String getMessage() { 156 if (!specialConstructor) { 157 return super.getMessage(); 158 } 159 String expected = ""; 160 int maxSize = 0; 161 for (int i = 0; i < expectedTokenSequences.length; i++) { 162 if (maxSize < expectedTokenSequences[i].length) { 163 maxSize = expectedTokenSequences[i].length; 164 } 165 for (int j = 0; j < expectedTokenSequences[i].length; j++) { 166 expected += tokenImage[expectedTokenSequences[i][j]] + " "; 167 } 168 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) { 169 expected += "..."; 170 } 171 expected += eol + " "; 172 } 173 String retval = "Encountered \""; 174 Token tok = currentToken.next; 175 for (int i = 0; i < maxSize; i++) { 176 if (i != 0) retval += " "; 177 if (tok.kind == 0) { 178 retval += tokenImage[0]; 179 break; 180 } 181 retval += add_escapes(tok.image); 182 tok = tok.next; 183 } 184 retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn; 185 retval += "." + eol; 186 if (expectedTokenSequences.length == 1) { 187 retval += "Was expecting:" + eol + " "; 188 } else { 189 retval += "Was expecting one of:" + eol + " "; 190 } 191 retval += expected; 192 return retval; 193 } 194 195 198 protected String eol = System.getProperty("line.separator", "\n"); 199 200 205 protected String add_escapes(String str) { 206 StringBuffer retval = new StringBuffer (); 207 char ch; 208 for (int i = 0; i < str.length(); i++) { 209 switch (str.charAt(i)) 210 { 211 case 0 : 212 continue; 213 case '\b': 214 retval.append("\\b"); 215 continue; 216 case '\t': 217 retval.append("\\t"); 218 continue; 219 case '\n': 220 retval.append("\\n"); 221 continue; 222 case '\f': 223 retval.append("\\f"); 224 continue; 225 case '\r': 226 retval.append("\\r"); 227 continue; 228 case '\"': 229 retval.append("\\\""); 230 continue; 231 case '\'': 232 retval.append("\\\'"); 233 continue; 234 case '\\': 235 retval.append("\\\\"); 236 continue; 237 default: 238 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { 239 String s = "0000" + Integer.toString(ch, 16); 240 retval.append("\\u" + s.substring(s.length() - 4, s.length())); 241 } else { 242 retval.append(ch); 243 } 244 continue; 245 } 246 } 247 return retval.toString(); 248 } 249 250 } 251 | Popular Tags |