1 package persistence.antlr; 2 3 8 9 class JavaCharFormatter implements CharFormatter { 10 11 12 20 public String escapeChar(int c, boolean forCharLiteral) { 21 switch (c) { 22 case '\n': 24 return "\\n"; 25 case '\t': 26 return "\\t"; 27 case '\r': 28 return "\\r"; 29 case '\\': 30 return "\\\\"; 31 case '\'': 32 return forCharLiteral ? "\\'" : "'"; 33 case '"': 34 return forCharLiteral ? "\"" : "\\\""; 35 default : 36 if (c < ' ' || c > 126) { 37 if ((0x0000 <= c) && (c <= 0x000F)) { 38 return "\\u000" + Integer.toString(c, 16); 39 } 40 else if ((0x0010 <= c) && (c <= 0x00FF)) { 41 return "\\u00" + Integer.toString(c, 16); 42 } 43 else if ((0x0100 <= c) && (c <= 0x0FFF)) { 44 return "\\u0" + Integer.toString(c, 16); 45 } 46 else { 47 return "\\u" + Integer.toString(c, 16); 48 } 49 } 50 else { 51 return String.valueOf((char)c); 52 } 53 } 54 } 55 56 60 public String escapeString(String s) { 61 String retval = new String (); 62 for (int i = 0; i < s.length(); i++) { 63 retval += escapeChar(s.charAt(i), false); 64 } 65 return retval; 66 } 67 68 74 public String literalChar(int c) { 75 return "'" + escapeChar(c, true) + "'"; 76 } 77 78 83 public String literalString(String s) { 84 return "\"" + escapeString(s) + "\""; 85 } 86 } 87 | Popular Tags |