KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > antlr > CppCharFormatter


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

9
10 // C++ code generator by Pete Wells: pete@yamuna.demon.co.uk
11

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

23     public String JavaDoc escapeChar(int c, boolean forCharLiteral) {
24         switch (c) {
25         case '\n' : return "\\n";
26         case '\t' : return "\\t";
27         case '\r' : return "\\r";
28         case '\\' : return "\\\\";
29         case '\'' : return forCharLiteral ? "\\'" : "'";
30         case '"' : return forCharLiteral ? "\"" : "\\\"";
31         default :
32             if ( c<' '||c>126 ) {
33                 if (c > 255) {
34                     return "\\u" + Integer.toString(c,16);
35                 }
36                 else {
37                     return "\\" + Integer.toString(c,8);
38                 }
39             }
40             else {
41                 return String.valueOf((char)c);
42             }
43         }
44     }
45
46     /** Converts a String into a representation that can be use as a literal
47      * when surrounded by double-quotes.
48      * @param s The String to be changed into a literal
49      */

50     public String JavaDoc escapeString(String JavaDoc s)
51     {
52         String JavaDoc retval = new String JavaDoc();
53         for (int i = 0; i < s.length(); i++)
54         {
55             retval += escapeChar(s.charAt(i), false);
56         }
57         return retval;
58     }
59
60     /** Given a character value, return a string representing the character
61      * literal that can be recognized by the target language compiler.
62      * This works for languages that use single-quotes for character literals.
63      * Code-generators for languages should override this method.
64      * @param c The character of interest.
65      */

66     public String JavaDoc literalChar(int c) {
67         return "static_cast<unsigned char>('" + escapeChar(c, true) + "')";
68     }
69
70     /** Converts a String into a string literal
71      * This works for languages that use double-quotes for string literals.
72      * Code-generators for languages should override this method.
73      * @param s The String to be changed into a literal
74      */

75     public String JavaDoc literalString(String JavaDoc s)
76     {
77         return "\"" + escapeString(s) + "\"";
78     }
79 }
80
Popular Tags