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