1 41 42 43 package org.w3c.flute.parser; 44 45 import org.w3c.css.sac.CSSException; 46 47 56 public class ParseException extends CSSException { 57 58 70 public ParseException(Token currentTokenVal, 71 int[][] expectedTokenSequencesVal, 72 String [] tokenImageVal 73 ) 74 { 75 super(""); 76 specialConstructor = true; 77 currentToken = currentTokenVal; 78 expectedTokenSequences = expectedTokenSequencesVal; 79 tokenImage = tokenImageVal; 80 } 81 82 91 92 public ParseException() { 93 super(); 94 specialConstructor = false; 95 } 96 97 public ParseException(String message) { 98 super(message); 99 specialConstructor = false; 100 } 101 102 107 protected boolean specialConstructor; 108 109 114 public Token currentToken; 115 116 121 public int[][] expectedTokenSequences; 122 123 128 public String [] tokenImage; 129 130 140 public String getMessage() { 141 if (!specialConstructor) { 142 return super.getMessage(); 143 } 144 String expected = ""; 145 int maxSize = 0; 146 for (int i = 0; i < expectedTokenSequences.length; i++) { 147 if (maxSize < expectedTokenSequences[i].length) { 148 maxSize = expectedTokenSequences[i].length; 149 } 150 for (int j = 0; j < expectedTokenSequences[i].length; j++) { 151 expected += tokenImage[expectedTokenSequences[i][j]] + " "; 152 } 153 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) { 154 expected += "..."; 155 } 156 expected += eol + " "; 157 } 158 String retval = "Encountered \""; 159 Token tok = currentToken.next; 160 for (int i = 0; i < maxSize; i++) { 161 if (i != 0) retval += " "; 162 if (tok.kind == 0) { 163 retval += tokenImage[0]; 164 break; 165 } 166 retval += add_escapes(tok.image); 167 tok = tok.next; 168 } 169 retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn; 170 retval += "." + eol; 171 if (expectedTokenSequences.length == 1) { 172 retval += "Was expecting:" + eol + " "; 173 } else { 174 retval += "Was expecting one of:" + eol + " "; 175 } 176 retval += expected; 177 return retval; 178 } 179 180 183 protected String eol = System.getProperty("line.separator", "\n"); 184 185 190 protected String add_escapes(String str) { 191 StringBuffer retval = new StringBuffer (); 192 char ch; 193 for (int i = 0; i < str.length(); i++) { 194 switch (str.charAt(i)) 195 { 196 case 0 : 197 continue; 198 case '\b': 199 retval.append("\\b"); 200 continue; 201 case '\t': 202 retval.append("\\t"); 203 continue; 204 case '\n': 205 retval.append("\\n"); 206 continue; 207 case '\f': 208 retval.append("\\f"); 209 continue; 210 case '\r': 211 retval.append("\\r"); 212 continue; 213 case '\"': 214 retval.append("\\\""); 215 continue; 216 case '\'': 217 retval.append("\\\'"); 218 continue; 219 case '\\': 220 retval.append("\\\\"); 221 continue; 222 default: 223 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { 224 String s = "0000" + Integer.toString(ch, 16); 225 retval.append("\\u" + s.substring(s.length() - 4, s.length())); 226 } else { 227 retval.append(ch); 228 } 229 continue; 230 } 231 } 232 return retval.toString(); 233 } 234 235 } 236 | Popular Tags |