KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > antlr > MismatchedCharException


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/MismatchedCharException.java#4 $
8  */

9
10 import antlr.collections.impl.BitSet;
11
12 public class MismatchedCharException extends RecognitionException {
13     // Types of chars
14
public static final int CHAR = 1;
15     public static final int NOT_CHAR = 2;
16     public static final int RANGE = 3;
17     public static final int NOT_RANGE = 4;
18     public static final int SET = 5;
19     public static final int NOT_SET = 6;
20
21     // One of the above
22
public int mismatchType;
23
24     // what was found on the input stream
25
public char foundChar;
26
27     // For CHAR/NOT_CHAR and RANGE/NOT_RANGE
28
public int expecting;
29
30     // For RANGE/NOT_RANGE (expecting is lower bound of range)
31
public int upper;
32
33     // For SET/NOT_SET
34
public BitSet set;
35
36     // who knows...they may want to ask scanner questions
37
public CharScanner scanner;
38
39     /**
40      * MismatchedCharException constructor comment.
41      */

42     public MismatchedCharException() {
43         super("Mismatched char");
44     }
45
46     // Expected range / not range
47
public MismatchedCharException(char c, char lower, char upper_, boolean matchNot, CharScanner scanner_) {
48         super("Mismatched char", scanner_.getFilename(), scanner_.getLine(), scanner_.getColumn());
49         mismatchType = matchNot ? NOT_RANGE : RANGE;
50         foundChar = c;
51         expecting = lower;
52         upper = upper_;
53         scanner = scanner_;
54     }
55
56     // Expected token / not token
57
public MismatchedCharException(char c, char expecting_, boolean matchNot, CharScanner scanner_) {
58         super("Mismatched char", scanner_.getFilename(), scanner_.getLine(), scanner_.getColumn());
59         mismatchType = matchNot ? NOT_CHAR : CHAR;
60         foundChar = c;
61         expecting = expecting_;
62         scanner = scanner_;
63     }
64
65     // Expected BitSet / not BitSet
66
public MismatchedCharException(char c, BitSet set_, boolean matchNot, CharScanner scanner_) {
67         super("Mismatched char", scanner_.getFilename(), scanner_.getLine(), scanner_.getColumn());
68         mismatchType = matchNot ? NOT_SET : SET;
69         foundChar = c;
70         set = set_;
71         scanner = scanner_;
72     }
73
74     /**
75      * Returns a clean error message (no line number/column information)
76      */

77     public String JavaDoc getMessage() {
78         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
79
80         switch (mismatchType) {
81             case CHAR:
82                 sb.append("expecting '" + (char)expecting + "', found '" + (char)foundChar + "'");
83                 break;
84             case NOT_CHAR:
85                 sb.append("expecting anything but '" + (char)expecting + "'; got it anyway");
86                 break;
87             case RANGE:
88                 sb.append("expecting token in range: '" + (char)expecting + "'..'" + (char)upper + "', found '" + (char)foundChar + "'");
89                 break;
90             case NOT_RANGE:
91                 sb.append("expecting token NOT in range: " + (char)expecting + "'..'" + (char)upper + "', found '" + (char)foundChar + "'");
92                 break;
93             case SET:
94             case NOT_SET:
95                 sb.append("expecting " + (mismatchType == NOT_SET ? "NOT " : "") + "one of (");
96                 int[] elems = set.toArray();
97                 for (int i = 0; i < elems.length; i++) {
98                     sb.append(" '");
99                     sb.append((char)elems[i]);
100                     sb.append("'");
101                 }
102                 sb.append("), found '" + (char)foundChar + "'");
103                 break;
104             default :
105                 sb.append(super.getMessage());
106                 break;
107         }
108
109         return sb.toString();
110     }
111 }
112
Popular Tags