KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > antlr > JavaCharFormatter


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/JavaCharFormatter.java#5 $
8  */

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

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

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

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

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