001: package persistence.antlr;
002:
003: /* ANTLR Translator Generator
004: * Project led by Terence Parr at http://www.jGuru.com
005: * Software rights: http://www.antlr.org/license.html
006: *
007: */
008:
009: import persistence.antlr.collections.impl.BitSet;
010: import persistence.antlr.collections.AST;
011:
012: public class MismatchedTokenException extends RecognitionException {
013: // Token names array for formatting
014: String[] tokenNames;
015: // The token that was encountered
016: public Token token;
017: // The offending AST node if tree walking
018: public AST node;
019:
020: String tokenText = null; // taken from node or token object
021:
022: // Types of tokens
023: public static final int TOKEN = 1;
024: public static final int NOT_TOKEN = 2;
025: public static final int RANGE = 3;
026: public static final int NOT_RANGE = 4;
027: public static final int SET = 5;
028: public static final int NOT_SET = 6;
029: // One of the above
030: public int mismatchType;
031:
032: // For TOKEN/NOT_TOKEN and RANGE/NOT_RANGE
033: public int expecting;
034:
035: // For RANGE/NOT_RANGE (expecting is lower bound of range)
036: public int upper;
037:
038: // For SET/NOT_SET
039: public BitSet set;
040:
041: /** Looking for AST wildcard, didn't find it */
042: public MismatchedTokenException() {
043: super ("Mismatched Token: expecting any AST node", "<AST>", -1,
044: -1);
045: }
046:
047: // Expected range / not range
048: public MismatchedTokenException(String[] tokenNames_, AST node_,
049: int lower, int upper_, boolean matchNot) {
050: super ("Mismatched Token", "<AST>", node_ == null ? -1 : node_
051: .getLine(), node_ == null ? -1 : node_.getColumn());
052: tokenNames = tokenNames_;
053: node = node_;
054: if (node_ == null) {
055: tokenText = "<empty tree>";
056: } else {
057: tokenText = node_.toString();
058: }
059: mismatchType = matchNot ? NOT_RANGE : RANGE;
060: expecting = lower;
061: upper = upper_;
062: }
063:
064: // Expected token / not token
065: public MismatchedTokenException(String[] tokenNames_, AST node_,
066: int expecting_, boolean matchNot) {
067: super ("Mismatched Token", "<AST>", node_ == null ? -1 : node_
068: .getLine(), node_ == null ? -1 : node_.getColumn());
069: tokenNames = tokenNames_;
070: node = node_;
071: if (node_ == null) {
072: tokenText = "<empty tree>";
073: } else {
074: tokenText = node_.toString();
075: }
076: mismatchType = matchNot ? NOT_TOKEN : TOKEN;
077: expecting = expecting_;
078: }
079:
080: // Expected BitSet / not BitSet
081: public MismatchedTokenException(String[] tokenNames_, AST node_,
082: BitSet set_, boolean matchNot) {
083: super ("Mismatched Token", "<AST>", node_ == null ? -1 : node_
084: .getLine(), node_ == null ? -1 : node_.getColumn());
085: tokenNames = tokenNames_;
086: node = node_;
087: if (node_ == null) {
088: tokenText = "<empty tree>";
089: } else {
090: tokenText = node_.toString();
091: }
092: mismatchType = matchNot ? NOT_SET : SET;
093: set = set_;
094: }
095:
096: // Expected range / not range
097: public MismatchedTokenException(String[] tokenNames_, Token token_,
098: int lower, int upper_, boolean matchNot, String fileName_) {
099: super ("Mismatched Token", fileName_, token_.getLine(), token_
100: .getColumn());
101: tokenNames = tokenNames_;
102: token = token_;
103: tokenText = token_.getText();
104: mismatchType = matchNot ? NOT_RANGE : RANGE;
105: expecting = lower;
106: upper = upper_;
107: }
108:
109: // Expected token / not token
110: public MismatchedTokenException(String[] tokenNames_, Token token_,
111: int expecting_, boolean matchNot, String fileName_) {
112: super ("Mismatched Token", fileName_, token_.getLine(), token_
113: .getColumn());
114: tokenNames = tokenNames_;
115: token = token_;
116: tokenText = token_.getText();
117: mismatchType = matchNot ? NOT_TOKEN : TOKEN;
118: expecting = expecting_;
119: }
120:
121: // Expected BitSet / not BitSet
122: public MismatchedTokenException(String[] tokenNames_, Token token_,
123: BitSet set_, boolean matchNot, String fileName_) {
124: super ("Mismatched Token", fileName_, token_.getLine(), token_
125: .getColumn());
126: tokenNames = tokenNames_;
127: token = token_;
128: tokenText = token_.getText();
129: mismatchType = matchNot ? NOT_SET : SET;
130: set = set_;
131: }
132:
133: /**
134: * Returns a clean error message (no line number/column information)
135: */
136: public String getMessage() {
137: StringBuffer sb = new StringBuffer();
138:
139: switch (mismatchType) {
140: case TOKEN:
141: sb.append("expecting " + tokenName(expecting) + ", found '"
142: + tokenText + "'");
143: break;
144: case NOT_TOKEN:
145: sb.append("expecting anything but " + tokenName(expecting)
146: + "; got it anyway");
147: break;
148: case RANGE:
149: sb.append("expecting token in range: "
150: + tokenName(expecting) + ".." + tokenName(upper)
151: + ", found '" + tokenText + "'");
152: break;
153: case NOT_RANGE:
154: sb.append("expecting token NOT in range: "
155: + tokenName(expecting) + ".." + tokenName(upper)
156: + ", found '" + tokenText + "'");
157: break;
158: case SET:
159: case NOT_SET:
160: sb.append("expecting "
161: + (mismatchType == NOT_SET ? "NOT " : "")
162: + "one of (");
163: int[] elems = set.toArray();
164: for (int i = 0; i < elems.length; i++) {
165: sb.append(" ");
166: sb.append(tokenName(elems[i]));
167: }
168: sb.append("), found '" + tokenText + "'");
169: break;
170: default:
171: sb.append(super .getMessage());
172: break;
173: }
174:
175: return sb.toString();
176: }
177:
178: private String tokenName(int tokenType) {
179: if (tokenType == Token.INVALID_TYPE) {
180: return "<Set of tokens>";
181: } else if (tokenType < 0 || tokenType >= tokenNames.length) {
182: return "<" + String.valueOf(tokenType) + ">";
183: } else {
184: return tokenNames[tokenType];
185: }
186: }
187: }
|