1 package antlr; 2 3 9 10 import antlr.collections.impl.BitSet; 11 12 public class MismatchedCharException extends RecognitionException { 13 public static final int CHAR = 1; 15 public static final int NOT_CHAR = 2; 16 public static final int RANGE = 3; 17 public static final int NOT_RANGE = 4; 18 public static final int SET = 5; 19 public static final int NOT_SET = 6; 20 21 public int mismatchType; 23 24 public char foundChar; 26 27 public int expecting; 29 30 public int upper; 32 33 public BitSet set; 35 36 public CharScanner scanner; 38 39 42 public MismatchedCharException() { 43 super("Mismatched char"); 44 } 45 46 public MismatchedCharException(char c, char lower, char upper_, boolean matchNot, CharScanner scanner_) { 48 super("Mismatched char", scanner_.getFilename(), scanner_.getLine(), scanner_.getColumn()); 49 mismatchType = matchNot ? NOT_RANGE : RANGE; 50 foundChar = c; 51 expecting = lower; 52 upper = upper_; 53 scanner = scanner_; 54 } 55 56 public MismatchedCharException(char c, char expecting_, boolean matchNot, CharScanner scanner_) { 58 super("Mismatched char", scanner_.getFilename(), scanner_.getLine(), scanner_.getColumn()); 59 mismatchType = matchNot ? NOT_CHAR : CHAR; 60 foundChar = c; 61 expecting = expecting_; 62 scanner = scanner_; 63 } 64 65 public MismatchedCharException(char c, BitSet set_, boolean matchNot, CharScanner scanner_) { 67 super("Mismatched char", scanner_.getFilename(), scanner_.getLine(), scanner_.getColumn()); 68 mismatchType = matchNot ? NOT_SET : SET; 69 foundChar = c; 70 set = set_; 71 scanner = scanner_; 72 } 73 74 77 public String getMessage() { 78 StringBuffer sb = new StringBuffer (); 79 80 switch (mismatchType) { 81 case CHAR: 82 sb.append("expecting '" + (char)expecting + "', found '" + (char)foundChar + "'"); 83 break; 84 case NOT_CHAR: 85 sb.append("expecting anything but '" + (char)expecting + "'; got it anyway"); 86 break; 87 case RANGE: 88 sb.append("expecting token in range: '" + (char)expecting + "'..'" + (char)upper + "', found '" + (char)foundChar + "'"); 89 break; 90 case NOT_RANGE: 91 sb.append("expecting token NOT in range: " + (char)expecting + "'..'" + (char)upper + "', found '" + (char)foundChar + "'"); 92 break; 93 case SET: 94 case NOT_SET: 95 sb.append("expecting " + (mismatchType == NOT_SET ? "NOT " : "") + "one of ("); 96 int[] elems = set.toArray(); 97 for (int i = 0; i < elems.length; i++) { 98 sb.append(" '"); 99 sb.append((char)elems[i]); 100 sb.append("'"); 101 } 102 sb.append("), found '" + (char)foundChar + "'"); 103 break; 104 default : 105 sb.append(super.getMessage()); 106 break; 107 } 108 109 return sb.toString(); 110 } 111 } 112 | Popular Tags |