KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > persistence > antlr > CSharpCharFormatter


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 //
10
// ANTLR C# Code Generator by Kunle Odutola : kunle UNDERSCORE odutola AT hotmail DOT com
11
//
12

13 class CSharpCharFormatter implements CharFormatter {
14
15
16     /** Given a character value, return a string representing the character
17      * that can be embedded inside a string literal or character literal
18      * This works for Java/C/C++ code-generation and languages with compatible
19      * special-character-escapment.
20      * Code-generators for languages should override this method.
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         switch (c)
26         {
27                 // case GrammarAnalyzer.EPSILON_TYPE : return "<end-of-token>";
28
case '\n' : return "\\n";
29             case '\t' : return "\\t";
30             case '\r' : return "\\r";
31             case '\\' : return "\\\\";
32             case '\'' : return forCharLiteral ? "\\'" : "'";
33             case '"' : return forCharLiteral ? "\"" : "\\\"";
34             default :
35                 if ( c<' '||c>126 )
36                 {
37                     if ( ( 0x0000 <= c ) && ( c <= 0x000F ) )
38                     {
39                         return "\\u000" + Integer.toString(c,16);
40                     }
41                     else if ( ( 0x0010 <= c ) && ( c <= 0x00FF ) )
42                     {
43                         return "\\u00" + Integer.toString(c,16);
44                     }
45                     else if ( ( 0x0100 <= c ) && ( c <= 0x0FFF ))
46                     {
47                         return "\\u0" + Integer.toString(c,16);
48                     }
49                     else
50                     {
51                         return "\\u" + Integer.toString(c,16);
52                     }
53                 }
54                 else
55                 {
56                     return String.valueOf((char)c);
57                 }
58         }
59     }
60     
61     
62     /** Converts a String into a representation that can be use as a literal
63      * when surrounded by double-quotes.
64      * @param s The String to be changed into a literal
65      */

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

83     public String JavaDoc literalChar(int c)
84     {
85         return "'" + escapeChar(c, true) + "'";
86     }
87     
88     
89     /** Converts a String into a string literal
90      * This works for languages that use double-quotes for string literals.
91      * Code-generators for languages should override this method.
92      * @param s The String to be changed into a literal
93      */

94     public String JavaDoc literalString(String JavaDoc s)
95     {
96         //return "\"" + escapeString(s) + "\"";
97
return "@\"\"\"" + escapeString(s) + "\"\"\"";
98     }
99 }
100
Popular Tags