KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > persistence > antlr > MismatchedTokenException


1 package persistence.antlr;
2
3 /* ANTLR Translator Generator
4  * Project led by Terence Parr at http://www.jGuru.com
5  * Software rights: http://www.antlr.org/license.html
6  *
7  */

8
9 import persistence.antlr.collections.impl.BitSet;
10 import persistence.antlr.collections.AST;
11
12 public class MismatchedTokenException extends RecognitionException {
13     // Token names array for formatting
14
String JavaDoc[] tokenNames;
15     // The token that was encountered
16
public Token token;
17     // The offending AST node if tree walking
18
public AST node;
19
20     String JavaDoc tokenText = null; // taken from node or token object
21

22     // Types of tokens
23
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     // One of the above
30
public int mismatchType;
31
32     // For TOKEN/NOT_TOKEN and RANGE/NOT_RANGE
33
public int expecting;
34
35     // For RANGE/NOT_RANGE (expecting is lower bound of range)
36
public int upper;
37
38     // For SET/NOT_SET
39
public BitSet set;
40
41     /** Looking for AST wildcard, didn't find it */
42     public MismatchedTokenException() {
43         super("Mismatched Token: expecting any AST node", "<AST>", -1, -1);
44     }
45
46     // Expected range / not range
47
public MismatchedTokenException(String JavaDoc[] 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     // Expected token / not token
63
public MismatchedTokenException(String JavaDoc[] 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     // Expected BitSet / not BitSet
78
public MismatchedTokenException(String JavaDoc[] 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     // Expected range / not range
93
public MismatchedTokenException(String JavaDoc[] tokenNames_, Token token_, int lower, int upper_, boolean matchNot, String JavaDoc 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     // Expected token / not token
104
public MismatchedTokenException(String JavaDoc[] tokenNames_, Token token_, int expecting_, boolean matchNot, String JavaDoc 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     // Expected BitSet / not BitSet
114
public MismatchedTokenException(String JavaDoc[] tokenNames_, Token token_, BitSet set_, boolean matchNot, String JavaDoc 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     /**
124      * Returns a clean error message (no line number/column information)
125      */

126     public String JavaDoc getMessage() {
127         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc 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