KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > antlr > MismatchedTokenException


1 package antlr;
2
3 /* ANTLR Translator Generator
4  * Project led by Terence Parr at http://www.jGuru.com
5  * Software rights: http://www.antlr.org/RIGHTS.html
6  *
7  * $Id: //depot/code/org.antlr/main/main/antlr/MismatchedTokenException.java#6 $
8  */

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

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

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