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