1 19 20 package org.netbeans.lib.lexer.test; 21 22 import java.lang.reflect.Method ; 23 import java.util.ArrayList ; 24 import java.util.List ; 25 26 27 32 public class CharRangesDump { 33 34 public static void main(String ... args) { 35 String methodName = (args[0] != null) ? args[0] : "isWhitespace"; 36 new CharRangesDump(new CharacterMethodAcceptor(methodName)).dump(); 37 } 38 39 private UnicodeCharAcceptor acceptor; 40 41 private List <Integer > charRanges = new ArrayList <Integer >(); 42 43 public CharRangesDump(UnicodeCharAcceptor acceptor) { 44 if (acceptor == null) { 45 throw new IllegalArgumentException ("acceptor must be non-null"); 46 } 47 this.acceptor = acceptor; 48 initRanges(); 49 } 50 51 private void initRanges() { 52 int seqStart = -1; 53 for (int i = 0; i <= Character.MAX_CODE_POINT + 1; i++) { 54 if ((i <= Character.MAX_CODE_POINT) && acceptor.isAccepted(i)) { 55 if (seqStart == -1) { 56 seqStart = i; 57 } } else { if (seqStart != -1) { charRanges.add(seqStart); 61 charRanges.add(i - 1); 62 seqStart = -1; 63 } 64 } 65 } 66 } 67 68 public List <Integer > charRanges() { 69 return charRanges; 70 } 71 72 public void dump() { 73 StringBuilder sb = new StringBuilder (acceptor.toString()); 74 sb.append('\n'); 75 for (int i = 0; i < charRanges.size(); i++) { 76 Integer start = charRanges.get(i++); 77 Integer end = charRanges.get(i); 78 if (!start.equals(end)) { sb.append("Sequence: " + charToString(start) + " - " 80 + charToString(end)); 81 int seqLen = end - start; 82 if (seqLen >= 2 && seqLen <= 9) { 83 sb.append(" incl. "); 84 for (int c = start + 1; c < end; c++) { 85 if (c != start + 1) { 86 sb.append(", "); 87 } 88 sb.append('\''); 89 sb.append(LexerTestUtilities.debugText(String.valueOf((char)c))); 90 sb.append('\''); 91 } 92 } 93 sb.append('\n'); 94 } else { sb.append("Char: " + charToString(start) + '\n'); 96 } 97 } 98 99 System.err.println(sb.toString()); 100 } 101 102 public void dumpAsserts() { 103 StringBuilder sb = new StringBuilder (); 104 for (int i = 0; i < charRanges.size(); i++) { 105 Integer start = charRanges.get(i++); 106 Integer end = charRanges.get(i); 107 if (start > 0x7F) { break; 109 } 110 appendAssert(sb, i - 1, start); 111 appendAssert(sb, i, end); 112 } 113 System.err.println(sb.toString()); 114 } 115 116 private static void appendAssert(StringBuilder sb, int index, Integer value) { 117 sb.append("TestCase.assertEquals(charRanges.get(" + index + ").intValue(), 0x" 118 + Integer.toString(value.intValue(), 16) + ");\n"); 119 } 120 121 private String charToString(int i) { 122 return "'" + LexerTestUtilities.debugText(String.valueOf((char)i)) 123 + "', " + i + "(0x" + Integer.toString(i, 16) + ")"; 124 } 125 126 public interface UnicodeCharAcceptor { 127 128 boolean isAccepted(int c); 129 130 } 131 132 public static final class CharacterMethodAcceptor implements UnicodeCharAcceptor { 133 134 private final Method characterClassMethod; 135 136 public CharacterMethodAcceptor(String methodName) { 137 try { 138 characterClassMethod = Character .class.getDeclaredMethod( 139 methodName, new Class [] { int.class }); 140 } catch (Exception e) { 141 throw new IllegalArgumentException (e); 142 } 143 } 144 145 public boolean isAccepted(int c) { 146 try { 147 Object result = characterClassMethod.invoke(null, new Integer (c)); 148 assert (result instanceof Boolean ); 149 return (Boolean.TRUE.equals(result)); 150 } catch (Exception e) { 151 throw new IllegalStateException (e); 152 } 153 } 154 155 public String toString() { 156 return characterClassMethod.toString(); 157 } 158 159 } 160 161 } 162 | Popular Tags |