KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > persistence > antlr > MismatchedCharException


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

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

76     public String JavaDoc getMessage() {
77         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
78
79         switch (mismatchType) {
80             case CHAR:
81                 sb.append("expecting "); appendCharName(sb, expecting);
82                 sb.append(", found "); appendCharName(sb, foundChar);
83                 break;
84             case NOT_CHAR:
85                 sb.append("expecting anything but '");
86                 appendCharName(sb, expecting);
87                 sb.append("'; got it anyway");
88                 break;
89             case RANGE:
90             case NOT_RANGE:
91                 sb.append("expecting token ");
92                 if (mismatchType == NOT_RANGE)
93                     sb.append("NOT ");
94                 sb.append("in range: ");
95                 appendCharName(sb, expecting);
96                 sb.append("..");
97                 appendCharName(sb, upper);
98                 sb.append(", found ");
99                 appendCharName(sb, foundChar);
100                 break;
101             case SET:
102             case NOT_SET:
103                 sb.append("expecting " + (mismatchType == NOT_SET ? "NOT " : "") + "one of (");
104                 int[] elems = set.toArray();
105                 for (int i = 0; i < elems.length; i++) {
106                     appendCharName(sb, elems[i]);
107                 }
108                 sb.append("), found ");
109                 appendCharName(sb, foundChar);
110                 break;
111             default :
112                 sb.append(super.getMessage());
113                 break;
114         }
115
116         return sb.toString();
117     }
118
119     /** Append a char to the msg buffer. If special,
120      * then show escaped version
121      */

122     private void appendCharName(StringBuffer JavaDoc sb, int c) {
123         switch (c) {
124         case 65535 :
125             // 65535 = (char) -1 = EOF
126
sb.append("'<EOF>'");
127             break;
128         case '\n' :
129             sb.append("'\\n'");
130             break;
131         case '\r' :
132             sb.append("'\\r'");
133             break;
134         case '\t' :
135             sb.append("'\\t'");
136             break;
137         default :
138             sb.append('\'');
139             sb.append((char) c);
140             sb.append('\'');
141             break;
142         }
143     }
144 }
145
146
Popular Tags