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