1 22 23 package org.enhydra.spi.conf.util; 24 25 34 public class ParseException extends Exception { 35 36 52 public ParseException(Token currentTokenVal, 53 int[][] expectedTokenSequencesVal, 54 String [] tokenImageVal 55 ) 56 { 57 super(""); 58 specialConstructor = true; 59 currentToken = currentTokenVal; 60 expectedTokenSequences = expectedTokenSequencesVal; 61 tokenImage = tokenImageVal; 62 } 63 64 73 74 public ParseException() { 75 super(); 76 specialConstructor = false; 77 } 78 79 public ParseException(String message) { 80 super(message); 81 specialConstructor = false; 82 } 83 84 89 protected boolean specialConstructor; 90 91 96 public Token currentToken; 97 98 103 public int[][] expectedTokenSequences; 104 105 110 public String [] tokenImage; 111 112 124 public String getMessage() { 125 if (!specialConstructor) { 126 return super.getMessage(); 127 } 128 String expected = ""; 129 int maxSize = 0; 130 for (int i = 0; i < expectedTokenSequences.length; i++) { 131 if (maxSize < expectedTokenSequences[i].length) { 132 maxSize = expectedTokenSequences[i].length; 133 } 134 for (int j = 0; j < expectedTokenSequences[i].length; j++) { 135 expected += tokenImage[expectedTokenSequences[i][j]] + " "; 136 } 137 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) { 138 expected += "..."; 139 } 140 expected += eol + " "; 141 } 142 String retval = "Encountered \""; 143 Token tok = currentToken.next; 144 for (int i = 0; i < maxSize; i++) { 145 if (i != 0) retval += " "; 146 if (tok.kind == 0) { 147 retval += tokenImage[0]; 148 break; 149 } 150 retval += add_escapes(tok.image); 151 tok = tok.next; 152 } 153 retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn; 154 retval += "." + eol; 155 if (expectedTokenSequences.length == 1) { 156 retval += "Was expecting:" + eol + " "; 157 } else { 158 retval += "Was expecting one of:" + eol + " "; 159 } 160 retval += expected; 161 return retval; 162 } 163 164 167 protected String eol = System.getProperty("line.separator", "\n"); 168 169 177 protected String add_escapes(String str) { 178 StringBuffer retval = new StringBuffer (); 179 char ch; 180 for (int i = 0; i < str.length(); i++) { 181 switch (str.charAt(i)) 182 { 183 case 0 : 184 continue; 185 case '\b': 186 retval.append("\\b"); 187 continue; 188 case '\t': 189 retval.append("\\t"); 190 continue; 191 case '\n': 192 retval.append("\\n"); 193 continue; 194 case '\f': 195 retval.append("\\f"); 196 continue; 197 case '\r': 198 retval.append("\\r"); 199 continue; 200 case '\"': 201 retval.append("\\\""); 202 continue; 203 case '\'': 204 retval.append("\\\'"); 205 continue; 206 case '\\': 207 retval.append("\\\\"); 208 continue; 209 default: 210 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { 211 String s = "0000" + Integer.toString(ch, 16); 212 retval.append("\\u" + s.substring(s.length() - 4, s.length())); 213 } else { 214 retval.append(ch); 215 } 216 continue; 217 } 218 } 219 return retval.toString(); 220 } 221 222 } 223 | Popular Tags |