001: package persistence.antlr;
002:
003: /* ANTLR Translator Generator
004: * Project led by Terence Parr at http://www.jGuru.com
005: * Software rights: http://www.antlr.org/license.html
006: *
007: */
008:
009: import persistence.antlr.collections.impl.BitSet;
010:
011: public class MismatchedCharException extends RecognitionException {
012: // Types of chars
013: public static final int CHAR = 1;
014: public static final int NOT_CHAR = 2;
015: public static final int RANGE = 3;
016: public static final int NOT_RANGE = 4;
017: public static final int SET = 5;
018: public static final int NOT_SET = 6;
019:
020: // One of the above
021: public int mismatchType;
022:
023: // what was found on the input stream
024: public int foundChar;
025:
026: // For CHAR/NOT_CHAR and RANGE/NOT_RANGE
027: public int expecting;
028:
029: // For RANGE/NOT_RANGE (expecting is lower bound of range)
030: public int upper;
031:
032: // For SET/NOT_SET
033: public BitSet set;
034:
035: // who knows...they may want to ask scanner questions
036: public CharScanner scanner;
037:
038: /**
039: * MismatchedCharException constructor comment.
040: */
041: public MismatchedCharException() {
042: super ("Mismatched char");
043: }
044:
045: // Expected range / not range
046: public MismatchedCharException(char c, char lower, char upper_,
047: boolean matchNot, CharScanner scanner_) {
048: super ("Mismatched char", scanner_.getFilename(), scanner_
049: .getLine(), scanner_.getColumn());
050: mismatchType = matchNot ? NOT_RANGE : RANGE;
051: foundChar = c;
052: expecting = lower;
053: upper = upper_;
054: scanner = scanner_;
055: }
056:
057: // Expected token / not token
058: public MismatchedCharException(char c, char expecting_,
059: boolean matchNot, CharScanner scanner_) {
060: super ("Mismatched char", scanner_.getFilename(), scanner_
061: .getLine(), scanner_.getColumn());
062: mismatchType = matchNot ? NOT_CHAR : CHAR;
063: foundChar = c;
064: expecting = expecting_;
065: scanner = scanner_;
066: }
067:
068: // Expected BitSet / not BitSet
069: public MismatchedCharException(char c, BitSet set_,
070: boolean matchNot, CharScanner scanner_) {
071: super ("Mismatched char", scanner_.getFilename(), scanner_
072: .getLine(), scanner_.getColumn());
073: mismatchType = matchNot ? NOT_SET : SET;
074: foundChar = c;
075: set = set_;
076: scanner = scanner_;
077: }
078:
079: /**
080: * Returns a clean error message (no line number/column information)
081: */
082: public String getMessage() {
083: StringBuffer sb = new StringBuffer();
084:
085: switch (mismatchType) {
086: case CHAR:
087: sb.append("expecting ");
088: appendCharName(sb, expecting);
089: sb.append(", found ");
090: appendCharName(sb, foundChar);
091: break;
092: case NOT_CHAR:
093: sb.append("expecting anything but '");
094: appendCharName(sb, expecting);
095: sb.append("'; got it anyway");
096: break;
097: case RANGE:
098: case NOT_RANGE:
099: sb.append("expecting token ");
100: if (mismatchType == NOT_RANGE)
101: sb.append("NOT ");
102: sb.append("in range: ");
103: appendCharName(sb, expecting);
104: sb.append("..");
105: appendCharName(sb, upper);
106: sb.append(", found ");
107: appendCharName(sb, foundChar);
108: break;
109: case SET:
110: case NOT_SET:
111: sb.append("expecting "
112: + (mismatchType == NOT_SET ? "NOT " : "")
113: + "one of (");
114: int[] elems = set.toArray();
115: for (int i = 0; i < elems.length; i++) {
116: appendCharName(sb, elems[i]);
117: }
118: sb.append("), found ");
119: appendCharName(sb, foundChar);
120: break;
121: default:
122: sb.append(super .getMessage());
123: break;
124: }
125:
126: return sb.toString();
127: }
128:
129: /** Append a char to the msg buffer. If special,
130: * then show escaped version
131: */
132: private void appendCharName(StringBuffer sb, int c) {
133: switch (c) {
134: case 65535:
135: // 65535 = (char) -1 = EOF
136: sb.append("'<EOF>'");
137: break;
138: case '\n':
139: sb.append("'\\n'");
140: break;
141: case '\r':
142: sb.append("'\\r'");
143: break;
144: case '\t':
145: sb.append("'\\t'");
146: break;
147: default:
148: sb.append('\'');
149: sb.append((char) c);
150: sb.append('\'');
151: break;
152: }
153: }
154: }
|