1 2 package org.nfunk.jep; 3 4 13 public class ParseException extends Exception { 14 15 27 public ParseException(Token currentTokenVal, 28 int[][] expectedTokenSequencesVal, 29 String [] tokenImageVal 30 ) 31 { 32 super(""); 33 specialConstructor = true; 34 currentToken = currentTokenVal; 35 expectedTokenSequences = expectedTokenSequencesVal; 36 tokenImage = tokenImageVal; 37 } 38 39 48 49 public ParseException() { 50 super(); 51 specialConstructor = false; 52 } 53 54 public ParseException(String message) { 55 super(message); 56 specialConstructor = false; 57 } 58 59 64 protected boolean specialConstructor; 65 66 71 public Token currentToken; 72 73 78 public int[][] expectedTokenSequences; 79 80 85 public String [] tokenImage; 86 87 97 public String getMessage() { 98 if (!specialConstructor) { 99 return super.getMessage(); 100 } 101 String expected = ""; 102 int maxSize = 0; 103 for (int i = 0; i < expectedTokenSequences.length; i++) { 104 if (maxSize < expectedTokenSequences[i].length) { 105 maxSize = expectedTokenSequences[i].length; 106 } 107 for (int j = 0; j < expectedTokenSequences[i].length; j++) { 108 expected += tokenImage[expectedTokenSequences[i][j]] + " "; 109 } 110 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) { 111 expected += "..."; 112 } 113 expected += eol + " "; 114 } 115 String retval = "Encountered \""; 116 Token tok = currentToken.next; 117 for (int i = 0; i < maxSize; i++) { 118 if (i != 0) retval += " "; 119 if (tok.kind == 0) { 120 retval += tokenImage[0]; 121 break; 122 } 123 retval += add_escapes(tok.image); 124 tok = tok.next; 125 } 126 retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn; 127 retval += "." + eol; 128 if (expectedTokenSequences.length == 1) { 129 retval += "Was expecting:" + eol + " "; 130 } else { 131 retval += "Was expecting one of:" + eol + " "; 132 } 133 retval += expected; 134 return retval; 135 } 136 137 143 public String getErrorInfo() 144 { 145 if (!specialConstructor) { 146 try { 147 return super.getMessage() + " at column " + currentToken.next.beginColumn + "."; 148 } 149 catch (Exception e) { 150 return super.getMessage(); 151 } 152 } 153 String expected = ""; 154 int maxSize = 0; 155 for (int i = 0; i < expectedTokenSequences.length; i++) { 156 if (maxSize < expectedTokenSequences[i].length) { 157 maxSize = expectedTokenSequences[i].length; 158 } 159 for (int j = 0; j < expectedTokenSequences[i].length; j++) { 160 expected += tokenImage[expectedTokenSequences[i][j]] + " "; 161 } 162 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) { 163 expected += "..."; 164 } 165 expected += eol + " "; 166 } 167 168 String retval = "Unexpected \""; 169 Token tok = currentToken.next; 170 for (int i = 0; i < maxSize; i++) { 171 if (i != 0) retval += " "; 172 if (tok.kind == 0) { 173 retval += tokenImage[0]; 174 break; 175 } 176 retval += add_escapes(tok.image); 177 tok = tok.next; 178 } 179 retval += "\" at column " + currentToken.next.beginColumn + "."; 180 return retval; 181 } 182 183 186 protected String eol = System.getProperty("line.separator", "\n"); 187 188 193 protected String add_escapes(String str) { 194 StringBuffer retval = new StringBuffer (); 195 char ch; 196 for (int i = 0; i < str.length(); i++) { 197 switch (str.charAt(i)) 198 { 199 case 0 : 200 continue; 201 case '\b': 202 retval.append("\\b"); 203 continue; 204 case '\t': 205 retval.append("\\t"); 206 continue; 207 case '\n': 208 retval.append("\\n"); 209 continue; 210 case '\f': 211 retval.append("\\f"); 212 continue; 213 case '\r': 214 retval.append("\\r"); 215 continue; 216 case '\"': 217 retval.append("\\\""); 218 continue; 219 case '\'': 220 retval.append("\\\'"); 221 continue; 222 case '\\': 223 retval.append("\\\\"); 224 continue; 225 default: 226 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { 227 String s = "0000" + Integer.toString(ch, 16); 228 retval.append("\\u" + s.substring(s.length() - 4, s.length())); 229 } else { 230 retval.append(ch); 231 } 232 continue; 233 } 234 } 235 return retval.toString(); 236 } 237 238 } 239 | Popular Tags |