1 package antlr;
2
3 * <b>SOFTWARE RIGHTS</b>
5 * <p>
6 * ANTLR 2.5.0 MageLang Institute, 1998
7 * <p>
8 * We reserve no legal rights to the ANTLR--it is fully in the
9 * public domain. An individual or company may do whatever
10 * they wish with source code distributed with ANTLR or the
11 * code generated by ANTLR, including the incorporation of
12 * ANTLR, or its output, into commerical software.
13 * <p>
14 * We encourage users to develop software with ANTLR. However,
15 * we do ask that credit is given to us for developing
16 * ANTLR. By "credit", we mean that if you use ANTLR or
17 * incorporate any source code into one of your programs
18 * (commercial product, research project, or otherwise) that
19 * you acknowledge this fact somewhere in the documentation,
20 * research report, etc... If you like ANTLR and have
21 * developed a nice tool with the output, please mention that
22 * you developed it using ANTLR. In addition, we ask that the
23 * headers remain intact in our source code. As long as these
24 * guidelines are kept, we expect to continue enhancing this
25 * system and expect to make other tools available as they are
26 * completed.
27 * <p>
28 * The ANTLR gang:
29 * @version ANTLR 2.5.0 MageLang Institute, 1998
30 * @author Terence Parr, <a href=http://www.MageLang.com>MageLang Institute</a>
31 * @author <br>John Lilley, <a href=http://www.Empathy.com>Empathy Software</a>
32 */
33 import antlr.collections.impl.BitSet;
34 import antlr.collections.AST;
35
36 public class MismatchedTokenException extends ParserException {
37 String[] tokenNames;
39 public Token token;
41 public AST node;
43
44 String tokenText=null;
46 public static final int TOKEN = 1;
48 public static final int NOT_TOKEN = 2;
49 public static final int RANGE = 3;
50 public static final int NOT_RANGE = 4;
51 public static final int SET = 5;
52 public static final int NOT_SET = 6;
53 protected int mismatchType;
55
56 protected int expecting;
58
59 protected int upper;
61
62 protected BitSet set;
64
65
66 public MismatchedTokenException() {
67 super("Mismatched Token: expecting any AST node");
68 }
69 public MismatchedTokenException(String[] tokenNames_, AST node, int lower, int upper_, boolean matchNot) {
71 super("Mismatched Token");
72 tokenNames = tokenNames_;
73 this.node = node;
74 if ( node==null ) {
75 tokenText = "<empty tree>";
76 }
77 else {
78 tokenText = node.toString();
79 }
80 expecting = lower;
81 upper = upper_;
82 mismatchType = matchNot ? NOT_RANGE : RANGE;
83 }
84 public MismatchedTokenException(String[] tokenNames_, AST node, int expecting_, boolean matchNot) {
86 super("Mismatched Token");
87 tokenNames = tokenNames_;
88 this.node = node;
89 if ( node==null ) {
90 tokenText = "<empty tree>";
91 }
92 else {
93 tokenText = node.toString();
94 }
95 expecting = expecting_;
96 mismatchType = matchNot ? NOT_TOKEN : TOKEN;
97 }
98 public MismatchedTokenException(String[] tokenNames_, AST node, BitSet set_, boolean matchNot) {
100 super("Mismatched Token");
101 tokenNames = tokenNames_;
102 this.node = node;
103 if ( node==null ) {
104 tokenText = "<empty tree>";
105 }
106 else {
107 tokenText = node.toString();
108 }
109 set = set_;
110 mismatchType = matchNot ? NOT_SET : SET;
111 }
112 public MismatchedTokenException(String[] tokenNames_, Token token_, int lower, int upper_, boolean matchNot) {
114 super("Mismatched Token");
115 tokenNames = tokenNames_;
116 token = token_;
117 tokenText = token.getText();
118 expecting = lower;
119 upper = upper_;
120 mismatchType = matchNot ? NOT_RANGE : RANGE;
121 }
122 public MismatchedTokenException(String[] tokenNames_, Token token_, int expecting_, boolean matchNot) {
124 super("Mismatched Token");
125 tokenNames = tokenNames_;
126 token = token_;
127 tokenText = token.getText();
128 expecting = expecting_;
129 mismatchType = matchNot ? NOT_TOKEN : TOKEN;
130 }
131 public MismatchedTokenException(String[] tokenNames_, Token token_, BitSet set_, boolean matchNot) {
133 super("Mismatched Token");
134 tokenNames = tokenNames_;
135 token = token_;
136 tokenText = token.getText();
137 set = set_;
138 mismatchType = matchNot ? NOT_SET : SET;
139 }
140 private String tokenName(int tokenType)
141 {
142 if (tokenType == Token.INVALID_TYPE) {
143 return "<Set of tokens>";
144 }
145 else if (tokenType < 0 || tokenType >= tokenNames.length) {
146 return "<" + String.valueOf(tokenType) + ">";
147 }
148 else {
149 return tokenNames[tokenType];
150 }
151 }
152 public String toString() {
153 String s = (token == null) ? "" : "line(" + token.getLine() + "), ";
154 switch (mismatchType) {
155 case TOKEN :
156 s += "expecting " + tokenName(expecting) + ", found '" + tokenText + "'";
157 break;
158 case NOT_TOKEN :
159 s += "expecting anything but " + tokenName(expecting) + "; got it anyway";
160 break;
161 case RANGE :
162 s += "expecting token in range: " + tokenName(expecting) + ".." + tokenName(upper) + ", found '" + tokenText + "'";
163 break;
164 case NOT_RANGE :
165 s += "expecting token NOT in range: " + tokenName(expecting) + ".." + tokenName(upper) + ", found '" + tokenText + "'";
166 break;
167 case SET :
168 case NOT_SET :
169 s += "expecting " + (mismatchType == NOT_SET ? "NOT " : "") + "one of (";
170 int[] elems = set.toArray();
171 for (int i = 0; i < elems.length; i++) {
172 s += " ";
173 s += tokenName(elems[i]);
174 }
175 s += "), found '" + tokenText + "'";
176 break;
177 default :
178 s = super.toString();
179 break;
180 }
181 return s;
182 }
183 }
184