1 package antlr; 2 3 9 10 import antlr.collections.impl.BitSet; 11 import antlr.collections.AST; 12 13 public class MismatchedTokenException extends RecognitionException { 14 String [] tokenNames; 16 public Token token; 18 public AST node; 20 21 String tokenText = null; 23 public static final int TOKEN = 1; 25 public static final int NOT_TOKEN = 2; 26 public static final int RANGE = 3; 27 public static final int NOT_RANGE = 4; 28 public static final int SET = 5; 29 public static final int NOT_SET = 6; 30 public int mismatchType; 32 33 public int expecting; 35 36 public int upper; 38 39 public BitSet set; 41 42 43 public MismatchedTokenException() { 44 super("Mismatched Token: expecting any AST node", "<AST>", -1, -1); 45 } 46 47 public MismatchedTokenException(String [] tokenNames_, AST node_, int lower, int upper_, boolean matchNot) { 49 super("Mismatched Token", "<AST>", -1, -1); 50 tokenNames = tokenNames_; 51 node = node_; 52 if (node_ == null) { 53 tokenText = "<empty tree>"; 54 } 55 else { 56 tokenText = node_.toString(); 57 } 58 mismatchType = matchNot ? NOT_RANGE : RANGE; 59 expecting = lower; 60 upper = upper_; 61 } 62 63 public MismatchedTokenException(String [] tokenNames_, AST node_, int expecting_, boolean matchNot) { 65 super("Mismatched Token", "<AST>", -1, -1); 66 tokenNames = tokenNames_; 67 node = node_; 68 if (node_ == null) { 69 tokenText = "<empty tree>"; 70 } 71 else { 72 tokenText = node_.toString(); 73 } 74 mismatchType = matchNot ? NOT_TOKEN : TOKEN; 75 expecting = expecting_; 76 } 77 78 public MismatchedTokenException(String [] tokenNames_, AST node_, BitSet set_, boolean matchNot) { 80 super("Mismatched Token", "<AST>", -1, -1); 81 tokenNames = tokenNames_; 82 node = node_; 83 if (node_ == null) { 84 tokenText = "<empty tree>"; 85 } 86 else { 87 tokenText = node_.toString(); 88 } 89 mismatchType = matchNot ? NOT_SET : SET; 90 set = set_; 91 } 92 93 public MismatchedTokenException(String [] tokenNames_, Token token_, int lower, int upper_, boolean matchNot, String fileName_) { 95 super("Mismatched Token", fileName_, token_.getLine(), token_.getColumn()); 96 tokenNames = tokenNames_; 97 token = token_; 98 tokenText = token_.getText(); 99 mismatchType = matchNot ? NOT_RANGE : RANGE; 100 expecting = lower; 101 upper = upper_; 102 } 103 104 public MismatchedTokenException(String [] tokenNames_, Token token_, int expecting_, boolean matchNot, String fileName_) { 106 super("Mismatched Token", fileName_, token_.getLine(), token_.getColumn()); 107 tokenNames = tokenNames_; 108 token = token_; 109 tokenText = token_.getText(); 110 mismatchType = matchNot ? NOT_TOKEN : TOKEN; 111 expecting = expecting_; 112 } 113 114 public MismatchedTokenException(String [] tokenNames_, Token token_, BitSet set_, boolean matchNot, String fileName_) { 116 super("Mismatched Token", fileName_, token_.getLine(), token_.getColumn()); 117 tokenNames = tokenNames_; 118 token = token_; 119 tokenText = token_.getText(); 120 mismatchType = matchNot ? NOT_SET : SET; 121 set = set_; 122 } 123 124 127 public String getMessage() { 128 StringBuffer sb = new StringBuffer (); 129 130 switch (mismatchType) { 131 case TOKEN: 132 sb.append("expecting " + tokenName(expecting) + ", found '" + tokenText + "'"); 133 break; 134 case NOT_TOKEN: 135 sb.append("expecting anything but " + tokenName(expecting) + "; got it anyway"); 136 break; 137 case RANGE: 138 sb.append("expecting token in range: " + tokenName(expecting) + ".." + tokenName(upper) + ", found '" + tokenText + "'"); 139 break; 140 case NOT_RANGE: 141 sb.append("expecting token NOT in range: " + tokenName(expecting) + ".." + tokenName(upper) + ", found '" + tokenText + "'"); 142 break; 143 case SET: 144 case NOT_SET: 145 sb.append("expecting " + (mismatchType == NOT_SET ? "NOT " : "") + "one of ("); 146 int[] elems = set.toArray(); 147 for (int i = 0; i < elems.length; i++) { 148 sb.append(" "); 149 sb.append(tokenName(elems[i])); 150 } 151 sb.append("), found '" + tokenText + "'"); 152 break; 153 default : 154 sb.append(super.getMessage()); 155 break; 156 } 157 158 return sb.toString(); 159 } 160 161 private String tokenName(int tokenType) { 162 if (tokenType == Token.INVALID_TYPE) { 163 return "<Set of tokens>"; 164 } 165 else if (tokenType < 0 || tokenType >= tokenNames.length) { 166 return "<" + String.valueOf(tokenType) + ">"; 167 } 168 else { 169 return tokenNames[tokenType]; 170 } 171 } 172 } 173 | Popular Tags |