1 2 3 25 26 package net.sourceforge.cobertura.javancss; 27 28 37 class ParseException extends Exception 38 { 39 40 private static final long serialVersionUID = 0L; 41 42 54 public ParseException(Token currentTokenVal, int[][] expectedTokenSequencesVal, 55 String [] tokenImageVal) 56 { 57 super(""); 58 specialConstructor = true; 59 currentToken = currentTokenVal; 60 expectedTokenSequences = expectedTokenSequencesVal; 61 tokenImage = tokenImageVal; 62 } 63 64 73 74 public ParseException() 75 { 76 super(); 77 specialConstructor = false; 78 } 79 80 public ParseException(String message) 81 { 82 super(message); 83 specialConstructor = false; 84 } 85 86 91 protected boolean specialConstructor; 92 93 98 public Token currentToken; 99 100 105 public int[][] expectedTokenSequences; 106 107 112 public String [] tokenImage; 113 114 124 public String getMessage() 125 { 126 if (!specialConstructor) 127 { 128 return super.getMessage(); 129 } 130 String expected = ""; 131 int maxSize = 0; 132 for (int i = 0; i < expectedTokenSequences.length; i++) 133 { 134 if (maxSize < expectedTokenSequences[i].length) 135 { 136 maxSize = expectedTokenSequences[i].length; 137 } 138 for (int j = 0; j < expectedTokenSequences[i].length; j++) 139 { 140 expected += tokenImage[expectedTokenSequences[i][j]] + " "; 141 } 142 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) 143 { 144 expected += "..."; 145 } 146 expected += eol + " "; 147 } 148 String retval = "Encountered \""; 149 Token tok = currentToken.next; 150 for (int i = 0; i < maxSize; i++) 151 { 152 if (i != 0) 153 retval += " "; 154 if (tok.kind == 0) 155 { 156 retval += tokenImage[0]; 157 break; 158 } 159 retval += add_escapes(tok.image); 160 tok = tok.next; 161 } 162 retval += "\" at line " + currentToken.next.beginLine + ", column " 163 + currentToken.next.beginColumn + "." + eol; 164 if (expectedTokenSequences.length == 1) 165 { 166 retval += "Was expecting:" + eol + " "; 167 } 168 else 169 { 170 retval += "Was expecting one of:" + eol + " "; 171 } 172 retval += expected; 173 return retval; 174 } 175 176 179 protected String eol = System.getProperty("line.separator", "\n"); 180 181 186 protected String add_escapes(String str) 187 { 188 StringBuffer retval = new StringBuffer (); 189 char ch; 190 for (int i = 0; i < str.length(); i++) 191 { 192 switch (str.charAt(i)) 193 { 194 case 0: 195 continue; 196 case '\b': 197 retval.append("\\b"); 198 continue; 199 case '\t': 200 retval.append("\\t"); 201 continue; 202 case '\n': 203 retval.append("\\n"); 204 continue; 205 case '\f': 206 retval.append("\\f"); 207 continue; 208 case '\r': 209 retval.append("\\r"); 210 continue; 211 case '\"': 212 retval.append("\\\""); 213 continue; 214 case '\'': 215 retval.append("\\\'"); 216 continue; 217 case '\\': 218 retval.append("\\\\"); 219 continue; 220 default: 221 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) 222 { 223 String s = "0000" + Integer.toString(ch, 16); 224 retval.append("\\u" + s.substring(s.length() - 4, s.length())); 225 } 226 else 227 { 228 retval.append(ch); 229 } 230 continue; 231 } 232 } 233 return retval.toString(); 234 } 235 236 } 237 | Popular Tags |