1 21 22 package net.percederberg.grammatica.parser; 23 24 import java.util.ArrayList ; 25 26 32 public class ParseException extends Exception { 33 34 39 public static final int INTERNAL_ERROR = 0; 40 41 45 public static final int IO_ERROR = 1; 46 47 51 public static final int UNEXPECTED_EOF_ERROR = 2; 52 53 58 public static final int UNEXPECTED_CHAR_ERROR = 3; 59 60 64 public static final int UNEXPECTED_TOKEN_ERROR = 4; 65 66 72 public static final int INVALID_TOKEN_ERROR = 5; 73 74 79 public static final int ANALYSIS_ERROR = 6; 80 81 84 private int type; 85 86 89 private String info; 90 91 95 private ArrayList details; 96 97 100 private int line; 101 102 105 private int column; 106 107 115 public ParseException(int type, 116 String info, 117 int line, 118 int column) { 119 120 this(type, info, null, line, column); 121 } 122 123 135 public ParseException(int type, 136 String info, 137 ArrayList details, 138 int line, 139 int column) { 140 141 super(); 142 this.type = type; 143 this.info = info; 144 this.details = details; 145 this.line = line; 146 this.column = column; 147 } 148 149 154 public int getErrorType() { 155 return type; 156 } 157 158 163 public String getInfo() { 164 return info; 165 } 166 167 172 public ArrayList getDetails() { 173 return new ArrayList (details); 174 } 175 176 182 public int getLine() { 183 return line; 184 } 185 186 192 public int getColumn() { 193 return column; 194 } 195 196 203 public String getMessage() { 204 StringBuffer buffer = new StringBuffer (); 205 206 buffer.append(getErrorMessage()); 208 209 if (line > 0 && column > 0) { 211 buffer.append(", on line "); 212 buffer.append(line); 213 buffer.append(" column: "); 214 buffer.append(column); 215 } 216 217 return buffer.toString(); 218 } 219 220 227 public String getErrorMessage() { 228 StringBuffer buffer = new StringBuffer (); 229 230 switch (type) { 231 case IO_ERROR: 232 buffer.append("I/O error: "); 233 buffer.append(info); 234 break; 235 case UNEXPECTED_EOF_ERROR: 236 buffer.append("unexpected end of file"); 237 break; 238 case UNEXPECTED_CHAR_ERROR: 239 buffer.append("unexpected character '"); 240 buffer.append(info); 241 buffer.append("'"); 242 break; 243 case UNEXPECTED_TOKEN_ERROR: 244 buffer.append("unexpected token "); 245 buffer.append(info); 246 if (details != null) { 247 buffer.append(", expected "); 248 if (details.size() > 1) { 249 buffer.append("one of "); 250 } 251 buffer.append(getMessageDetails()); 252 } 253 break; 254 case INVALID_TOKEN_ERROR: 255 buffer.append(info); 256 break; 257 case ANALYSIS_ERROR: 258 buffer.append(info); 259 break; 260 default: 261 buffer.append("internal error"); 262 if (info != null) { 263 buffer.append(": "); 264 buffer.append(info); 265 } 266 } 267 268 return buffer.toString(); 269 } 270 271 277 private String getMessageDetails() { 278 StringBuffer buffer = new StringBuffer (); 279 280 for (int i = 0; i < details.size(); i++) { 281 if (i > 0) { 282 buffer.append(", "); 283 if (i + 1 == details.size()) { 284 buffer.append("or "); 285 } 286 } 287 buffer.append(details.get(i)); 288 } 289 290 return buffer.toString(); 291 } 292 } 293 | Popular Tags |