KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > persistence > antlr > JavaCharFormatter


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 class JavaCharFormatter implements CharFormatter {
10
11
12     /** Given a character value, return a string representing the character
13      * that can be embedded inside a string literal or character literal
14      * This works for Java/C/C++ code-generation and languages with compatible
15      * special-character-escapment.
16      * Code-generators for languages should override this method.
17      * @param c The character of interest.
18      * @param forCharLiteral true to escape for char literal, false for string literal
19      */

20     public String JavaDoc escapeChar(int c, boolean forCharLiteral) {
21         switch (c) {
22             // case GrammarAnalyzer.EPSILON_TYPE : return "<end-of-token>";
23
case '\n':
24                 return "\\n";
25             case '\t':
26                 return "\\t";
27             case '\r':
28                 return "\\r";
29             case '\\':
30                 return "\\\\";
31             case '\'':
32                 return forCharLiteral ? "\\'" : "'";
33             case '"':
34                 return forCharLiteral ? "\"" : "\\\"";
35             default :
36                 if (c < ' ' || c > 126) {
37                     if ((0x0000 <= c) && (c <= 0x000F)) {
38                         return "\\u000" + Integer.toString(c, 16);
39                     }
40                     else if ((0x0010 <= c) && (c <= 0x00FF)) {
41                         return "\\u00" + Integer.toString(c, 16);
42                     }
43                     else if ((0x0100 <= c) && (c <= 0x0FFF)) {
44                         return "\\u0" + Integer.toString(c, 16);
45                     }
46                     else {
47                         return "\\u" + Integer.toString(c, 16);
48                     }
49                 }
50                 else {
51                     return String.valueOf((char)c);
52                 }
53         }
54     }
55
56     /** Converts a String into a representation that can be use as a literal
57      * when surrounded by double-quotes.
58      * @param s The String to be changed into a literal
59      */

60     public String JavaDoc escapeString(String JavaDoc s) {
61         String JavaDoc retval = new String JavaDoc();
62         for (int i = 0; i < s.length(); i++) {
63             retval += escapeChar(s.charAt(i), false);
64         }
65         return retval;
66     }
67
68     /** Given a character value, return a string representing the character
69      * literal that can be recognized by the target language compiler.
70      * This works for languages that use single-quotes for character literals.
71      * Code-generators for languages should override this method.
72      * @param c The character of interest.
73      */

74     public String JavaDoc literalChar(int c) {
75         return "'" + escapeChar(c, true) + "'";
76     }
77
78     /** Converts a String into a string literal
79      * This works for languages that use double-quotes for string literals.
80      * Code-generators for languages should override this method.
81      * @param s The String to be changed into a literal
82      */

83     public String JavaDoc literalString(String JavaDoc s) {
84         return "\"" + escapeString(s) + "\"";
85     }
86 }
87
Popular Tags