KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > persistence > antlr > CppCharFormatter


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 // C++ code generator by Pete Wells: pete@yamuna.demon.co.uk
10

11 class CppCharFormatter implements CharFormatter {
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      *
18      * Used internally in CppCharFormatter and in
19      * CppCodeGenerator.converJavaToCppString.
20      *
21      * @param c The character of interest.
22      * @param forCharLiteral true to escape for char literal, false for string literal
23      */

24     public String JavaDoc escapeChar(int c, boolean forCharLiteral) {
25         // System.out.println("CppCharFormatter.escapeChar("+c+")");
26
switch (c) {
27         case '\n' : return "\\n";
28         case '\t' : return "\\t";
29         case '\r' : return "\\r";
30         case '\\' : return "\\\\";
31         case '\'' : return forCharLiteral ? "\\'" : "'";
32         case '"' : return forCharLiteral ? "\"" : "\\\"";
33         default :
34             if ( c < ' ' || c > 126 )
35             {
36                 if (c > 255)
37                 {
38                     String JavaDoc s = Integer.toString(c,16);
39                     // put leading zeroes in front of the thing..
40
while( s.length() < 4 )
41                         s = '0' + s;
42                     return "\\u" + s;
43                 }
44                 else {
45                     return "\\" + Integer.toString(c,8);
46                 }
47             }
48             else {
49                 return String.valueOf((char)c);
50             }
51         }
52     }
53
54     /** Converts a String into a representation that can be use as a literal
55      * when surrounded by double-quotes.
56      *
57      * Used for escaping semantic predicate strings for exceptions.
58      *
59      * @param s The String to be changed into a literal
60      */

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

75     public String JavaDoc literalChar(int c) {
76         String JavaDoc ret = "0x"+Integer.toString(c,16);
77         if( c >= 0 && c <= 126 )
78             ret += " /* '"+escapeChar(c,true)+"' */ ";
79         return ret;
80     }
81
82     /** Converts a String into a string literal
83      * This works for languages that use double-quotes for string literals.
84      * Code-generators for languages should override this method.
85      *
86      * Used for the generation of the tables with token names
87      *
88      * @param s The String to be changed into a literal
89      */

90     public String JavaDoc literalString(String JavaDoc s)
91     {
92         return "\"" + escapeString(s) + "\"";
93     }
94
95 }
96
Popular Tags