1 21 22 package net.percederberg.grammatica.parser.re; 23 24 31 public class RegExpException extends Exception { 32 33 38 public static final int UNEXPECTED_CHARACTER = 1; 39 40 44 public static final int UNTERMINATED_PATTERN = 2; 45 46 51 public static final int UNSUPPORTED_SPECIAL_CHARACTER = 3; 52 53 58 public static final int UNSUPPORTED_ESCAPE_CHARACTER = 4; 59 60 65 public static final int INVALID_REPEAT_COUNT = 5; 66 67 70 private int type; 71 72 75 private int position; 76 77 80 private String pattern; 81 82 89 public RegExpException(int type, int pos, String pattern) { 90 this.type = type; 91 this.position = pos; 92 this.pattern = pattern; 93 } 94 95 100 public String getMessage() { 101 StringBuffer buffer = new StringBuffer (); 102 103 switch (type) { 105 case UNEXPECTED_CHARACTER: 106 buffer.append("unexpected character"); 107 break; 108 case UNTERMINATED_PATTERN: 109 buffer.append("unterminated pattern"); 110 break; 111 case UNSUPPORTED_SPECIAL_CHARACTER: 112 buffer.append("unsupported character"); 113 break; 114 case UNSUPPORTED_ESCAPE_CHARACTER: 115 buffer.append("unsupported escape character"); 116 break; 117 case INVALID_REPEAT_COUNT: 118 buffer.append("invalid repeat count"); 119 break; 120 default: 121 buffer.append("internal error"); 122 break; 123 } 124 125 buffer.append(": "); 127 if (position < pattern.length()) { 128 buffer.append('\''); 129 buffer.append(pattern.substring(position)); 130 buffer.append('\''); 131 } else { 132 buffer.append("<end of pattern>"); 133 } 134 135 buffer.append(" at position "); 137 buffer.append(position); 138 139 return buffer.toString(); 140 } 141 } 142 | Popular Tags |