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