1 2 20 21 package org.apache.cayenne.exp.parser; 22 23 32 public class ParseException extends Exception { 33 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 retval += "." + eol; 147 if (expectedTokenSequences.length == 1) { 148 retval += "Was expecting:" + eol + " "; 149 } else { 150 retval += "Was expecting one of:" + eol + " "; 151 } 152 retval += expected; 153 return retval; 154 } 155 156 159 protected String eol = System.getProperty("line.separator", "\n"); 160 161 166 protected String add_escapes(String str) { 167 StringBuffer retval = new StringBuffer (); 168 char ch; 169 for (int i = 0; i < str.length(); i++) { 170 switch (str.charAt(i)) 171 { 172 case 0 : 173 continue; 174 case '\b': 175 retval.append("\\b"); 176 continue; 177 case '\t': 178 retval.append("\\t"); 179 continue; 180 case '\n': 181 retval.append("\\n"); 182 continue; 183 case '\f': 184 retval.append("\\f"); 185 continue; 186 case '\r': 187 retval.append("\\r"); 188 continue; 189 case '\"': 190 retval.append("\\\""); 191 continue; 192 case '\'': 193 retval.append("\\\'"); 194 continue; 195 case '\\': 196 retval.append("\\\\"); 197 continue; 198 default: 199 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { 200 String s = "0000" + Integer.toString(ch, 16); 201 retval.append("\\u" + s.substring(s.length() - 4, s.length())); 202 } else { 203 retval.append(ch); 204 } 205 continue; 206 } 207 } 208 return retval.toString(); 209 } 210 211 } 212 | Popular Tags |