01: package persistence.antlr;
02:
03: /* ANTLR Translator Generator
04: * Project led by Terence Parr at http://www.jGuru.com
05: * Software rights: http://www.antlr.org/license.html
06: *
07: */
08:
09: //
10: // ANTLR C# Code Generator by Kunle Odutola : kunle UNDERSCORE odutola AT hotmail DOT com
11: //
12: class CSharpCharFormatter implements CharFormatter {
13:
14: /** Given a character value, return a string representing the character
15: * that can be embedded inside a string literal or character literal
16: * This works for Java/C/C++ code-generation and languages with compatible
17: * special-character-escapment.
18: * Code-generators for languages should override this method.
19: * @param c The character of interest.
20: * @param forCharLiteral true to escape for char literal, false for string literal
21: */
22: public String escapeChar(int c, boolean forCharLiteral) {
23: switch (c) {
24: // case GrammarAnalyzer.EPSILON_TYPE : return "<end-of-token>";
25: case '\n':
26: return "\\n";
27: case '\t':
28: return "\\t";
29: case '\r':
30: return "\\r";
31: case '\\':
32: return "\\\\";
33: case '\'':
34: return forCharLiteral ? "\\'" : "'";
35: case '"':
36: return forCharLiteral ? "\"" : "\\\"";
37: default:
38: if (c < ' ' || c > 126) {
39: if ((0x0000 <= c) && (c <= 0x000F)) {
40: return "\\u000" + Integer.toString(c, 16);
41: } else if ((0x0010 <= c) && (c <= 0x00FF)) {
42: return "\\u00" + Integer.toString(c, 16);
43: } else if ((0x0100 <= c) && (c <= 0x0FFF)) {
44: return "\\u0" + Integer.toString(c, 16);
45: } else {
46: return "\\u" + Integer.toString(c, 16);
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: * @param s The String to be changed into a literal
57: */
58: public String escapeString(String s) {
59: String retval = new String();
60: for (int i = 0; i < s.length(); i++) {
61: retval += escapeChar(s.charAt(i), false);
62: }
63: return retval;
64: }
65:
66: /** Given a character value, return a string representing the character
67: * literal that can be recognized by the target language compiler.
68: * This works for languages that use single-quotes for character literals.
69: * Code-generators for languages should override this method.
70: * @param c The character of interest.
71: */
72: public String literalChar(int c) {
73: return "'" + escapeChar(c, true) + "'";
74: }
75:
76: /** Converts a String into a string literal
77: * This works for languages that use double-quotes for string literals.
78: * Code-generators for languages should override this method.
79: * @param s The String to be changed into a literal
80: */
81: public String literalString(String s) {
82: //return "\"" + escapeString(s) + "\"";
83: return "@\"\"\"" + escapeString(s) + "\"\"\"";
84: }
85: }
|