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