1 package persistence.antlr; 2 3 8 9 import persistence.antlr.collections.impl.BitSet; 10 11 public class MismatchedCharException extends RecognitionException { 12 public static final int CHAR = 1; 14 public static final int NOT_CHAR = 2; 15 public static final int RANGE = 3; 16 public static final int NOT_RANGE = 4; 17 public static final int SET = 5; 18 public static final int NOT_SET = 6; 19 20 public int mismatchType; 22 23 public int foundChar; 25 26 public int expecting; 28 29 public int upper; 31 32 public BitSet set; 34 35 public CharScanner scanner; 37 38 41 public MismatchedCharException() { 42 super("Mismatched char"); 43 } 44 45 public MismatchedCharException(char c, char lower, char upper_, boolean matchNot, CharScanner scanner_) { 47 super("Mismatched char", scanner_.getFilename(), scanner_.getLine(), scanner_.getColumn()); 48 mismatchType = matchNot ? NOT_RANGE : RANGE; 49 foundChar = c; 50 expecting = lower; 51 upper = upper_; 52 scanner = scanner_; 53 } 54 55 public MismatchedCharException(char c, char expecting_, boolean matchNot, CharScanner scanner_) { 57 super("Mismatched char", scanner_.getFilename(), scanner_.getLine(), scanner_.getColumn()); 58 mismatchType = matchNot ? NOT_CHAR : CHAR; 59 foundChar = c; 60 expecting = expecting_; 61 scanner = scanner_; 62 } 63 64 public MismatchedCharException(char c, BitSet set_, boolean matchNot, CharScanner scanner_) { 66 super("Mismatched char", scanner_.getFilename(), scanner_.getLine(), scanner_.getColumn()); 67 mismatchType = matchNot ? NOT_SET : SET; 68 foundChar = c; 69 set = set_; 70 scanner = scanner_; 71 } 72 73 76 public String getMessage() { 77 StringBuffer sb = new StringBuffer (); 78 79 switch (mismatchType) { 80 case CHAR: 81 sb.append("expecting "); appendCharName(sb, expecting); 82 sb.append(", found "); appendCharName(sb, foundChar); 83 break; 84 case NOT_CHAR: 85 sb.append("expecting anything but '"); 86 appendCharName(sb, expecting); 87 sb.append("'; got it anyway"); 88 break; 89 case RANGE: 90 case NOT_RANGE: 91 sb.append("expecting token "); 92 if (mismatchType == NOT_RANGE) 93 sb.append("NOT "); 94 sb.append("in range: "); 95 appendCharName(sb, expecting); 96 sb.append(".."); 97 appendCharName(sb, upper); 98 sb.append(", found "); 99 appendCharName(sb, foundChar); 100 break; 101 case SET: 102 case NOT_SET: 103 sb.append("expecting " + (mismatchType == NOT_SET ? "NOT " : "") + "one of ("); 104 int[] elems = set.toArray(); 105 for (int i = 0; i < elems.length; i++) { 106 appendCharName(sb, elems[i]); 107 } 108 sb.append("), found "); 109 appendCharName(sb, foundChar); 110 break; 111 default : 112 sb.append(super.getMessage()); 113 break; 114 } 115 116 return sb.toString(); 117 } 118 119 122 private void appendCharName(StringBuffer sb, int c) { 123 switch (c) { 124 case 65535 : 125 sb.append("'<EOF>'"); 127 break; 128 case '\n' : 129 sb.append("'\\n'"); 130 break; 131 case '\r' : 132 sb.append("'\\r'"); 133 break; 134 case '\t' : 135 sb.append("'\\t'"); 136 break; 137 default : 138 sb.append('\''); 139 sb.append((char) c); 140 sb.append('\''); 141 break; 142 } 143 } 144 } 145 146 | Popular Tags |