1 21 22 package net.percederberg.grammatica.parser; 23 24 import java.util.ArrayList ; 25 26 34 public class ParserCreationException extends Exception { 35 36 41 public static final int INTERNAL_ERROR = 0; 42 43 48 public static final int INVALID_PARSER_ERROR = 1; 49 50 55 public static final int INVALID_TOKEN_ERROR = 2; 56 57 63 public static final int INVALID_PRODUCTION_ERROR = 3; 64 65 70 public static final int INFINITE_LOOP_ERROR = 4; 71 72 77 public static final int INHERENT_AMBIGUITY_ERROR = 5; 78 79 82 private int type; 83 84 88 private String name; 89 90 94 private String info; 95 96 100 private ArrayList details; 101 102 108 public ParserCreationException(int type, 109 String info) { 110 111 this(type, null, info); 112 } 113 114 121 public ParserCreationException(int type, 122 String name, 123 String info) { 124 125 this(type, name, info, null); 126 } 127 128 136 public ParserCreationException(int type, 137 String name, 138 String info, 139 ArrayList details) { 140 141 this.type = type; 142 this.name = name; 143 this.info = info; 144 this.details = details; 145 } 146 147 152 public int getErrorType() { 153 return type; 154 } 155 156 161 public String getName() { 162 return name; 163 } 164 165 170 public String getInfo() { 171 return info; 172 } 173 174 179 public String getDetails() { 180 StringBuffer buffer = new StringBuffer (); 181 182 if (details == null) { 183 return null; 184 } 185 for (int i = 0; i < details.size(); i++) { 186 if (i > 0) { 187 buffer.append(", "); 188 if (i + 1 == details.size()) { 189 buffer.append("and "); 190 } 191 } 192 buffer.append(details.get(i)); 193 } 194 195 return buffer.toString(); 196 } 197 198 204 public String getMessage() { 205 StringBuffer buffer = new StringBuffer (); 206 207 switch (type) { 208 case INVALID_PARSER_ERROR: 209 buffer.append("parser is invalid, as "); 210 buffer.append(info); 211 break; 212 case INVALID_TOKEN_ERROR: 213 buffer.append("token '"); 214 buffer.append(name); 215 buffer.append("' is invalid, as "); 216 buffer.append(info); 217 break; 218 case INVALID_PRODUCTION_ERROR: 219 buffer.append("production '"); 220 buffer.append(name); 221 buffer.append("' is invalid, as "); 222 buffer.append(info); 223 break; 224 case INFINITE_LOOP_ERROR: 225 buffer.append("infinite loop found in production pattern '"); 226 buffer.append(name); 227 buffer.append("'"); 228 break; 229 case INHERENT_AMBIGUITY_ERROR: 230 buffer.append("inherent ambiguity in production '"); 231 buffer.append(name); 232 buffer.append("'"); 233 if (info != null) { 234 buffer.append(" "); 235 buffer.append(info); 236 } 237 if (details != null) { 238 buffer.append(" starting with "); 239 if (details.size() > 1) { 240 buffer.append("tokens "); 241 } else { 242 buffer.append("token "); 243 } 244 buffer.append(getDetails()); 245 } 246 break; 247 default: 248 buffer.append("internal error"); 249 } 250 251 return buffer.toString(); 252 } 253 } 254 | Popular Tags |