1 package antlr;
2
3 * <b>SOFTWARE RIGHTS</b>
5 * <p>
6 * ANTLR 2.3.0 MageLang Institute, 1998
7 * <p>
8 * We reserve no legal rights to the ANTLR--it is fully in the
9 * public domain. An individual or company may do whatever
10 * they wish with source code distributed with ANTLR or the
11 * code generated by ANTLR, including the incorporation of
12 * ANTLR, or its output, into commerical software.
13 * <p>
14 * We encourage users to develop software with ANTLR. However,
15 * we do ask that credit is given to us for developing
16 * ANTLR. By "credit", we mean that if you use ANTLR or
17 * incorporate any source code into one of your programs
18 * (commercial product, research project, or otherwise) that
19 * you acknowledge this fact somewhere in the documentation,
20 * research report, etc... If you like ANTLR and have
21 * developed a nice tool with the output, please mention that
22 * you developed it using ANTLR. In addition, we ask that the
23 * headers remain intact in our source code. As long as these
24 * guidelines are kept, we expect to continue enhancing this
25 * system and expect to make other tools available as they are
26 * completed.
27 * <p>
28 * The ANTLR gang:
29 * @version ANTLR 2.3.0 MageLang Institute, 1998
30 * @author Terence Parr, <a href=http://www.MageLang.com>MageLang Institute</a>
31 * @author <br>John Lilley, <a href=http://www.Empathy.com>Empathy Software</a>
32 * @author <br><a href="mailto:pete@yamuna.demon.co.uk">Pete Wells</a>
33 */
34 class CppCharFormatter implements CharFormatter {
35
36
37 * that can be embedded inside a string literal or character literal
39 * This works for Java/C/C++ code-generation and languages with compatible
40 * special-character-escapment.
41 * Code-generators for languages should override this method.
42 * @param c The character of interest.
43 * @param forCharLiteral true to escape for char literal, false for string literal
44 */
45 public String escapeChar(int c, boolean forCharLiteral) {
46 switch (c) {
47 case '\n' : return "\\n";
48 case '\t' : return "\\t";
49 case '\r' : return "\\r";
50 case '\\' : return "\\\\";
51 case '\'' : return forCharLiteral ? "\\'" : "'";
52 case '"' : return forCharLiteral ? "\"" : "\\\"";
53 default :
54 if ( c<' '||c>126 ) {
55 if (c > 255) {
56 return "\\u" + Integer.toString(c,16);
57 }
58 else {
59 return "\\" + Integer.toString(c,8);
60 }
61 }
62 else {
63 return String.valueOf((char)c);
64 }
65 }
66 }
67 * when surrounded by double-quotes.
69 * @param s The String to be changed into a literal
70 */
71 public String escapeString(String s)
72 {
73 String retval = new String();
74 for (int i = 0; i < s.length(); i++)
75 {
76 retval += escapeChar(s.charAt(i), false);
77 }
78 return retval;
79 }
80 * literal that can be recognized by the target language compiler.
82 * This works for languages that use single-quotes for character literals.
83 * Code-generators for languages should override this method.
84 * @param c The character of interest.
85 */
86 public String literalChar(int c) {
87 return "static_cast<unsigned char>('" + escapeChar(c, true) + "')";
88 }
89 * 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 literalString(String s)
95 {
96 return "\"" + escapeString(s) + "\"";
97 }
98 }
99