4 // Copyright (C) 2002-2004, International Business Machines Corporation and others.
5 // All Rights Reserved.
7 // This file contains the RBBIRuleBuilder class implementation. This is the main class for
8 // building (compiling) break rules into the tables required by the runtime
12 #include "unicode/utypes.h"
14 #if !UCONFIG_NO_BREAK_ITERATION
16 #include "unicode/brkiter.h"
17 #include "unicode/rbbi.h"
18 #include "unicode/ubrk.h"
19 #include "unicode/unistr.h"
20 #include "unicode/uniset.h"
21 #include "unicode/uchar.h"
22 #include "unicode/uchriter.h"
23 #include "unicode/parsepos.h"
24 #include "unicode/parseerr.h"
40 //----------------------------------------------------------------------------------------
44 //----------------------------------------------------------------------------------------
45 RBBIRuleBuilder::RBBIRuleBuilder(const UnicodeString
&rules
,
46 UParseError
&parseErr
,
50 fStatus
= &status
; // status is checked below
51 fParseError
= &parseErr
;
54 fDebugEnv
= getenv("U_RBBIDEBUG");
62 fDefaultTree
= &fForwardTree
;
63 fForwardTables
= NULL
;
64 fReverseTables
= NULL
;
65 fSafeFwdTables
= NULL
;
66 fSafeRevTables
= NULL
;
67 fRuleStatusVals
= NULL
;
70 fLookAheadHardBreak
= FALSE
;
72 fRuleStatusVals
= NULL
;
76 if (U_FAILURE(status
)) {
80 fUSetNodes
= new UVector(status
); // bcos status gets overwritten here
81 fRuleStatusVals
= new UVector(status
);
82 fScanner
= new RBBIRuleScanner(this);
83 fSetBuilder
= new RBBISetBuilder(this);
84 if (U_FAILURE(status
)) {
87 if(fSetBuilder
== 0 || fScanner
== 0 || fUSetNodes
== 0 || fRuleStatusVals
== 0) {
88 status
= U_MEMORY_ALLOCATION_ERROR
;
94 //----------------------------------------------------------------------------------------
98 //----------------------------------------------------------------------------------------
99 RBBIRuleBuilder::~RBBIRuleBuilder() {
103 RBBINode
*n
= (RBBINode
*)fUSetNodes
->elementAt(i
);
112 delete fForwardTables
;
113 delete fReverseTables
;
114 delete fSafeFwdTables
;
115 delete fSafeRevTables
;
122 delete fRuleStatusVals
;
129 //----------------------------------------------------------------------------------------
131 // flattenData() - Collect up the compiled RBBI rule data and put it into
132 // the format for saving in ICU data files,
133 // which is also the format needed by the RBBI runtime engine.
135 //----------------------------------------------------------------------------------------
136 static int32_t align8(int32_t i
) {return (i
+7) & 0xfffffff8;}
138 RBBIDataHeader
*RBBIRuleBuilder::flattenData() {
141 if (U_FAILURE(*fStatus
)) {
145 // Remove comments and whitespace from the rules to make it smaller.
146 UnicodeString
strippedRules(RBBIRuleScanner::stripRules(fRules
));
148 // Calculate the size of each section in the data.
149 // Sizes here are padded up to a multiple of 8 for better memory alignment.
150 // Sections sizes actually stored in the header are for the actual data
151 // without the padding.
153 int32_t headerSize
= align8(sizeof(RBBIDataHeader
));
154 int32_t forwardTableSize
= align8(fForwardTables
->getTableSize());
155 int32_t reverseTableSize
= align8(fReverseTables
->getTableSize());
156 int32_t safeFwdTableSize
= align8(fSafeFwdTables
->getTableSize());
157 int32_t safeRevTableSize
= align8(fSafeRevTables
->getTableSize());
158 int32_t trieSize
= align8(fSetBuilder
->getTrieSize());
159 int32_t statusTableSize
= align8(fRuleStatusVals
->size() * sizeof(int32_t));
160 int32_t rulesSize
= align8((strippedRules
.length()+1) * sizeof(UChar
));
162 int32_t totalSize
= headerSize
+ forwardTableSize
+ reverseTableSize
163 + safeFwdTableSize
+ safeRevTableSize
164 + statusTableSize
+ trieSize
+ rulesSize
;
166 RBBIDataHeader
*data
= (RBBIDataHeader
*)uprv_malloc(totalSize
);
168 *fStatus
= U_MEMORY_ALLOCATION_ERROR
;
171 uprv_memset(data
, 0, totalSize
);
174 data
->fMagic
= 0xb1a0;
176 data
->fLength
= totalSize
;
177 data
->fCatCount
= fSetBuilder
->getNumCharCategories();
179 data
->fFTable
= headerSize
;
180 data
->fFTableLen
= forwardTableSize
;
181 data
->fRTable
= data
->fFTable
+ forwardTableSize
;
182 data
->fRTableLen
= reverseTableSize
;
183 data
->fSFTable
= data
->fRTable
+ reverseTableSize
;
184 data
->fSFTableLen
= safeFwdTableSize
;
185 data
->fSRTable
= data
->fSFTable
+ safeFwdTableSize
;
186 data
->fSRTableLen
= safeRevTableSize
;
188 data
->fTrie
= data
->fSRTable
+ safeRevTableSize
;
189 data
->fTrieLen
= fSetBuilder
->getTrieSize();
190 data
->fStatusTable
= data
->fTrie
+ trieSize
;
191 data
->fStatusTableLen
= statusTableSize
;
192 data
->fRuleSource
= data
->fStatusTable
+ statusTableSize
;
193 data
->fRuleSourceLen
= strippedRules
.length() * sizeof(UChar
);
195 uprv_memset(data
->fReserved
, 0, sizeof(data
->fReserved
));
197 fForwardTables
->exportTable((uint8_t *)data
+ data
->fFTable
);
198 fReverseTables
->exportTable((uint8_t *)data
+ data
->fRTable
);
199 fSafeFwdTables
->exportTable((uint8_t *)data
+ data
->fSFTable
);
200 fSafeRevTables
->exportTable((uint8_t *)data
+ data
->fSRTable
);
201 fSetBuilder
->serializeTrie ((uint8_t *)data
+ data
->fTrie
);
203 int32_t *ruleStatusTable
= (int32_t *)((uint8_t *)data
+ data
->fStatusTable
);
204 for (i
=0; i
<fRuleStatusVals
->size(); i
++) {
205 ruleStatusTable
[i
] = fRuleStatusVals
->elementAti(i
);
208 strippedRules
.extract((UChar
*)((uint8_t *)data
+data
->fRuleSource
), rulesSize
/2+1, *fStatus
);
218 //----------------------------------------------------------------------------------------
220 // createRuleBasedBreakIterator construct from source rules that are passed in
221 // in a UnicodeString
223 //----------------------------------------------------------------------------------------
225 RBBIRuleBuilder::createRuleBasedBreakIterator( const UnicodeString
&rules
,
226 UParseError
&parseError
,
229 // status checked below
232 // Read the input rules, generate a parse tree, symbol table,
233 // and list of all Unicode Sets referenced by the rules.
235 RBBIRuleBuilder
builder(rules
, parseError
, status
);
236 builder
.fScanner
->parse();
237 if (U_FAILURE(status
)) { // status checked here bcos build below doesn't
242 // UnicodeSet processing.
243 // Munge the Unicode Sets to create a set of character categories.
244 // Generate the mapping tables (TRIE) from input 32-bit characters to
245 // the character categories.
247 builder
.fSetBuilder
->build();
251 // Generate the DFA state transition table.
253 builder
.fForwardTables
= new RBBITableBuilder(&builder
, &builder
.fForwardTree
);
254 builder
.fReverseTables
= new RBBITableBuilder(&builder
, &builder
.fReverseTree
);
255 builder
.fSafeFwdTables
= new RBBITableBuilder(&builder
, &builder
.fSafeFwdTree
);
256 builder
.fSafeRevTables
= new RBBITableBuilder(&builder
, &builder
.fSafeRevTree
);
257 if (U_SUCCESS(status
)
258 && (builder
.fForwardTables
== NULL
|| builder
.fReverseTables
== NULL
||
259 builder
.fSafeFwdTables
== NULL
|| builder
.fSafeRevTables
== NULL
))
261 status
= U_MEMORY_ALLOCATION_ERROR
;
265 builder
.fForwardTables
->build();
266 builder
.fReverseTables
->build();
267 builder
.fSafeFwdTables
->build();
268 builder
.fSafeRevTables
->build();
269 if (U_FAILURE(status
)) {
274 if (builder
.fDebugEnv
&& uprv_strstr(builder
.fDebugEnv
, "states")) {
275 builder
.fForwardTables
->printRuleStatusTable();
280 // Package up the compiled data into a memory image
281 // in the run-time format.
283 RBBIDataHeader
*data
= builder
.flattenData(); // returns NULL if error
287 // Clean up the compiler related stuff
292 // Create a break iterator from the compiled rules.
293 // (Identical to creation from stored pre-compiled rules)
295 // status is checked after init in construction.
296 RuleBasedBreakIterator
*This
= new RuleBasedBreakIterator(data
, status
);
297 if (U_FAILURE(status
)) {
301 else if(This
== NULL
) { // test for NULL
302 status
= U_MEMORY_ALLOCATION_ERROR
;
309 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */