1 7 package com.ibm.icu.text; 8 import java.util.HashMap ; 9 import java.util.List ; 10 import java.util.ArrayList ; 11 import java.util.Map ; 12 import java.io.OutputStream ; 13 import java.io.DataOutputStream ; 14 import java.io.IOException ; 15 import com.ibm.icu.impl.Assert; 16 17 class RBBIRuleBuilder { 18 22 String fDebugEnv; String fRules; RBBIRuleScanner fScanner; 26 27 RBBINode[] fTreeRoots = new RBBINode[4]; 33 static final int fForwardTree = 0; static final int fReverseTree = 1; static final int fSafeFwdTree = 2; static final int fSafeRevTree = 3; int fDefaultTree = fForwardTree; 40 boolean fChainRules; 43 boolean fLBCMNoChain; 46 boolean fLookAheadHardBreak; 50 RBBISetBuilder fSetBuilder; List fUSetNodes; RBBITableBuilder fForwardTables; RBBITableBuilder fReverseTables; 54 RBBITableBuilder fSafeFwdTables; 55 RBBITableBuilder fSafeRevTables; 56 57 Map fStatusSets = new HashMap (); 64 List fRuleStatusVals; static final int U_BRK_ERROR_START = 0x10200; 79 80 81 static final int U_BRK_INTERNAL_ERROR = 0x10201; 82 83 84 static final int U_BRK_HEX_DIGITS_EXPECTED = 0x10202; 85 86 87 static final int U_BRK_SEMICOLON_EXPECTED = 0x10203; 88 89 90 static final int U_BRK_RULE_SYNTAX = 0x10204; 91 92 93 static final int U_BRK_UNCLOSED_SET = 0x10205; 94 95 96 static final int U_BRK_ASSIGN_ERROR = 0x10206; 97 98 99 static final int U_BRK_VARIABLE_REDFINITION = 0x10207; 100 101 102 static final int U_BRK_MISMATCHED_PAREN = 0x10208; 103 104 105 static final int U_BRK_NEW_LINE_IN_QUOTED_STRING = 0x10209; 106 107 108 static final int U_BRK_UNDEFINED_VARIABLE = 0x1020a; 109 110 111 static final int U_BRK_INIT_ERROR = 0x1020b; 112 113 114 static final int U_BRK_RULE_EMPTY_SET = 0x1020c; 115 116 117 static final int U_BRK_UNRECOGNIZED_OPTION = 0x1020d; 118 119 120 static final int U_BRK_MALFORMED_RULE_TAG = 0x1020e; 121 122 static final int U_BRK_MALFORMED_SET = 0x1020f; 123 124 static final int U_BRK_ERROR_LIMIT = 0x10210; 125 126 127 128 RBBIRuleBuilder(String rules) 134 { 135 fDebugEnv = System.getProperty("U_RBBIDEBUG"); 136 fRules = rules; 137 fUSetNodes = new ArrayList (); 138 fRuleStatusVals = new ArrayList (); 139 fScanner = new RBBIRuleScanner(this); 140 fSetBuilder = new RBBISetBuilder(this); 141 } 142 143 static final int align8(int i) 152 { 153 return (i + 7) & 0xfffffff8; 154 } 155 156 void flattenData(OutputStream os) throws IOException { 157 DataOutputStream dos = new DataOutputStream (os); 158 int i; 159 160 String strippedRules = RBBIRuleScanner.stripRules(fRules); 162 163 int headerSize = 24 * 4; int forwardTableSize = align8(fForwardTables.getTableSize()); 170 int reverseTableSize = align8(fReverseTables.getTableSize()); 171 int safeFwdTableSize = align8(fSafeFwdTables.getTableSize()); 172 int safeRevTableSize = align8(fSafeRevTables.getTableSize()); 173 int trieSize = align8(fSetBuilder.getTrieSize()); 174 int statusTableSize = align8(fRuleStatusVals.size() * 4); 175 int rulesSize = align8((strippedRules.length()) * 2); 176 int totalSize = headerSize + forwardTableSize + reverseTableSize 177 + safeFwdTableSize + safeRevTableSize 178 + statusTableSize + trieSize + rulesSize; 179 int outputPos = 0; 181 byte[] ICUDataHeader = new byte[0x80]; 188 dos.write(ICUDataHeader); 189 190 int[] header = new int[RBBIDataWrapper.DH_SIZE]; header[RBBIDataWrapper.DH_MAGIC] = 0xb1a0; 195 header[RBBIDataWrapper.DH_FORMATVERSION] = 0x03010000; header[RBBIDataWrapper.DH_LENGTH] = totalSize; header[RBBIDataWrapper.DH_CATCOUNT] = fSetBuilder.getNumCharCategories(); header[RBBIDataWrapper.DH_FTABLE] = headerSize; header[RBBIDataWrapper.DH_FTABLELEN] = forwardTableSize; header[RBBIDataWrapper.DH_RTABLE] = header[RBBIDataWrapper.DH_FTABLE] + forwardTableSize; header[RBBIDataWrapper.DH_RTABLELEN] = reverseTableSize; header[RBBIDataWrapper.DH_SFTABLE] = header[RBBIDataWrapper.DH_RTABLE] 203 + reverseTableSize; header[RBBIDataWrapper.DH_SFTABLELEN] = safeFwdTableSize; header[RBBIDataWrapper.DH_SRTABLE] = header[RBBIDataWrapper.DH_SFTABLE] 206 + safeFwdTableSize; header[RBBIDataWrapper.DH_SRTABLELEN] = safeRevTableSize; header[RBBIDataWrapper.DH_TRIE] = header[RBBIDataWrapper.DH_SRTABLE] 209 + safeRevTableSize; header[RBBIDataWrapper.DH_TRIELEN] = fSetBuilder.getTrieSize(); header[RBBIDataWrapper.DH_STATUSTABLE] = header[RBBIDataWrapper.DH_TRIE] 212 + header[RBBIDataWrapper.DH_TRIELEN]; 213 header[RBBIDataWrapper.DH_STATUSTABLELEN] = statusTableSize; header[RBBIDataWrapper.DH_RULESOURCE] = header[RBBIDataWrapper.DH_STATUSTABLE] 215 + statusTableSize; 216 header[RBBIDataWrapper.DH_RULESOURCELEN] = strippedRules.length() * 2; 217 for (i = 0; i < header.length; i++) { 218 dos.writeInt(header[i]); 219 outputPos += 4; 220 } 221 222 short[] tableData; 224 tableData = fForwardTables.exportTable(); 225 Assert.assrt(outputPos == header[4]); 226 for (i = 0; i < tableData.length; i++) { 227 dos.writeShort(tableData[i]); 228 outputPos += 2; 229 } 230 231 tableData = fReverseTables.exportTable(); 232 Assert.assrt(outputPos == header[6]); 233 for (i = 0; i < tableData.length; i++) { 234 dos.writeShort(tableData[i]); 235 outputPos += 2; 236 } 237 238 Assert.assrt(outputPos == header[8]); 239 tableData = fSafeFwdTables.exportTable(); 240 for (i = 0; i < tableData.length; i++) { 241 dos.writeShort(tableData[i]); 242 outputPos += 2; 243 } 244 245 Assert.assrt(outputPos == header[10]); 246 tableData = fSafeRevTables.exportTable(); 247 for (i = 0; i < tableData.length; i++) { 248 dos.writeShort(tableData[i]); 249 outputPos += 2; 250 } 251 252 Assert.assrt(outputPos == header[12]); 254 fSetBuilder.serializeTrie(os); 255 outputPos += header[13]; 256 while (outputPos % 8 != 0) { dos.write(0); 258 outputPos += 1; 259 } 260 261 Assert.assrt(outputPos == header[16]); 263 for (i = 0; i < fRuleStatusVals.size(); i++) { 264 Integer val = (Integer ) fRuleStatusVals.get(i); 265 dos.writeInt(val.intValue()); 266 outputPos += 4; 267 } 268 269 while (outputPos % 8 != 0) { dos.write(0); 271 outputPos += 1; 272 } 273 274 Assert.assrt(outputPos == header[14]); 277 dos.writeChars(strippedRules); 278 outputPos += strippedRules.length() * 2; 279 while (outputPos % 8 != 0) { dos.write(0); 281 outputPos += 1; 282 } 283 } 284 285 static void compileRules(String rules, OutputStream os) throws IOException 292 { 293 RBBIRuleBuilder builder = new RBBIRuleBuilder(rules); 298 builder.fScanner.parse(); 299 300 builder.fSetBuilder.build(); 307 308 builder.fForwardTables = new RBBITableBuilder(builder, fForwardTree); 312 builder.fReverseTables = new RBBITableBuilder(builder, fReverseTree); 313 builder.fSafeFwdTables = new RBBITableBuilder(builder, fSafeFwdTree); 314 builder.fSafeRevTables = new RBBITableBuilder(builder, fSafeRevTree); 315 builder.fForwardTables.build(); 316 builder.fReverseTables.build(); 317 builder.fSafeFwdTables.build(); 318 builder.fSafeRevTables.build(); 319 if (builder.fDebugEnv != null 320 && builder.fDebugEnv.indexOf("states") >= 0) { 321 builder.fForwardTables.printRuleStatusTable(); 322 } 323 324 builder.flattenData(os); 329 } 330 } 331 | Popular Tags |