KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > antlr > NoViableAltForCharException


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

9
10 public class NoViableAltForCharException extends RecognitionException {
11     public char foundChar;
12
13     public NoViableAltForCharException(char c, CharScanner scanner) {
14         super("NoViableAlt", scanner.getFilename(),
15               scanner.getLine(), scanner.getColumn());
16         foundChar = c;
17     }
18
19     public NoViableAltForCharException(char c, String JavaDoc fileName, int line, int column) {
20         super("NoViableAlt", fileName, line, column);
21         foundChar = c;
22     }
23
24     /**
25      * Returns a clean error message (no line number/column information)
26      */

27     public String JavaDoc getMessage() {
28         String JavaDoc mesg = "unexpected char: ";
29
30         // I'm trying to mirror a change in the C++ stuff.
31
// But java seems to lack something isprint-ish..
32
// so we do it manually. This is probably to restrictive.
33

34         if ((foundChar >= ' ') && (foundChar <= '~')) {
35             mesg += '\'';
36             mesg += foundChar;
37             mesg += '\'';
38         }
39         else {
40             mesg += "0x";
41
42             int t = (int)foundChar >> 4;
43
44             if (t < 10)
45                 mesg += (char)(t | 0x30);
46             else
47                 mesg += (char)(t + 0x37);
48
49             t = (int)foundChar & 0xF;
50
51             if (t < 10)
52                 mesg += (char)(t | 0x30);
53             else
54                 mesg += (char)(t + 0x37);
55         }
56         return mesg;
57     }
58 }
59
Popular Tags