1 21 22 package org.apache.derby.impl.sql.compile; 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 158 return retval; 159 } 160 161 164 protected String eol = System.getProperty("line.separator", "\n"); 165 166 171 protected String add_escapes(String str) { 172 StringBuffer retval = new StringBuffer (); 173 char ch; 174 for (int i = 0; i < str.length(); i++) { 175 switch (str.charAt(i)) 176 { 177 case 0 : 178 continue; 179 case '\b': 180 retval.append("\\b"); 181 continue; 182 case '\t': 183 retval.append("\\t"); 184 continue; 185 case '\n': 186 retval.append("\\n"); 187 continue; 188 case '\f': 189 retval.append("\\f"); 190 continue; 191 case '\r': 192 retval.append("\\r"); 193 continue; 194 case '\"': 195 retval.append("\\\""); 196 continue; 197 case '\'': 198 retval.append("\\\'"); 199 continue; 200 case '\\': 201 retval.append("\\\\"); 202 continue; 203 default: 204 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { 205 String s = "0000" + Integer.toString(ch, 16); 206 retval.append("\\u" + s.substring(s.length() - 4, s.length())); 207 } else { 208 retval.append(ch); 209 } 210 continue; 211 } 212 } 213 return retval.toString(); 214 } 215 216 } 217 | Popular Tags |