1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
6 // Copyright (C) 2002-2011, International Business Machines Corporation and others.
7 // All Rights Reserved.
9 // This file contains the RBBIRuleBuilder class implementation. This is the main class for
10 // building (compiling) break rules into the tables required by the runtime
14 #include "unicode/utypes.h"
16 #if !UCONFIG_NO_BREAK_ITERATION
18 #include "unicode/brkiter.h"
19 #include "unicode/rbbi.h"
20 #include "unicode/ubrk.h"
21 #include "unicode/unistr.h"
22 #include "unicode/uniset.h"
23 #include "unicode/uchar.h"
24 #include "unicode/uchriter.h"
25 #include "unicode/parsepos.h"
26 #include "unicode/parseerr.h"
42 //----------------------------------------------------------------------------------------
46 //----------------------------------------------------------------------------------------
47 RBBIRuleBuilder::RBBIRuleBuilder(const UnicodeString
&rules
,
48 UParseError
*parseErr
,
52 fStatus
= &status
; // status is checked below
53 fParseError
= parseErr
;
56 fDebugEnv
= getenv("U_RBBIDEBUG");
64 fDefaultTree
= &fForwardTree
;
65 fForwardTables
= NULL
;
66 fReverseTables
= NULL
;
67 fSafeFwdTables
= NULL
;
68 fSafeRevTables
= NULL
;
69 fRuleStatusVals
= NULL
;
72 fLookAheadHardBreak
= FALSE
;
74 fRuleStatusVals
= NULL
;
78 uprv_memset(parseErr
, 0, sizeof(UParseError
));
81 if (U_FAILURE(status
)) {
85 fUSetNodes
= new UVector(status
); // bcos status gets overwritten here
86 fRuleStatusVals
= new UVector(status
);
87 fScanner
= new RBBIRuleScanner(this);
88 fSetBuilder
= new RBBISetBuilder(this);
89 if (U_FAILURE(status
)) {
92 if(fSetBuilder
== 0 || fScanner
== 0 || fUSetNodes
== 0 || fRuleStatusVals
== 0) {
93 status
= U_MEMORY_ALLOCATION_ERROR
;
99 //----------------------------------------------------------------------------------------
103 //----------------------------------------------------------------------------------------
104 RBBIRuleBuilder::~RBBIRuleBuilder() {
108 RBBINode
*n
= (RBBINode
*)fUSetNodes
->elementAt(i
);
117 delete fForwardTables
;
118 delete fReverseTables
;
119 delete fSafeFwdTables
;
120 delete fSafeRevTables
;
127 delete fRuleStatusVals
;
134 //----------------------------------------------------------------------------------------
136 // flattenData() - Collect up the compiled RBBI rule data and put it into
137 // the format for saving in ICU data files,
138 // which is also the format needed by the RBBI runtime engine.
140 //----------------------------------------------------------------------------------------
141 static int32_t align8(int32_t i
) {return (i
+7) & 0xfffffff8;}
143 RBBIDataHeader
*RBBIRuleBuilder::flattenData() {
146 if (U_FAILURE(*fStatus
)) {
150 // Remove comments and whitespace from the rules to make it smaller.
151 UnicodeString
strippedRules((const UnicodeString
&)RBBIRuleScanner::stripRules(fRules
));
153 // Calculate the size of each section in the data.
154 // Sizes here are padded up to a multiple of 8 for better memory alignment.
155 // Sections sizes actually stored in the header are for the actual data
156 // without the padding.
158 int32_t headerSize
= align8(sizeof(RBBIDataHeader
));
159 int32_t forwardTableSize
= align8(fForwardTables
->getTableSize());
160 int32_t reverseTableSize
= align8(fReverseTables
->getTableSize());
161 int32_t safeFwdTableSize
= align8(fSafeFwdTables
->getTableSize());
162 int32_t safeRevTableSize
= align8(fSafeRevTables
->getTableSize());
163 int32_t trieSize
= align8(fSetBuilder
->getTrieSize());
164 int32_t statusTableSize
= align8(fRuleStatusVals
->size() * sizeof(int32_t));
165 int32_t rulesSize
= align8((strippedRules
.length()+1) * sizeof(UChar
));
167 int32_t totalSize
= headerSize
+ forwardTableSize
+ reverseTableSize
168 + safeFwdTableSize
+ safeRevTableSize
169 + statusTableSize
+ trieSize
+ rulesSize
;
171 RBBIDataHeader
*data
= (RBBIDataHeader
*)uprv_malloc(totalSize
);
173 *fStatus
= U_MEMORY_ALLOCATION_ERROR
;
176 uprv_memset(data
, 0, totalSize
);
179 data
->fMagic
= 0xb1a0;
180 data
->fFormatVersion
[0] = 3;
181 data
->fFormatVersion
[1] = 1;
182 data
->fFormatVersion
[2] = 0;
183 data
->fFormatVersion
[3] = 0;
184 data
->fLength
= totalSize
;
185 data
->fCatCount
= fSetBuilder
->getNumCharCategories();
187 data
->fFTable
= headerSize
;
188 data
->fFTableLen
= forwardTableSize
;
189 data
->fRTable
= data
->fFTable
+ forwardTableSize
;
190 data
->fRTableLen
= reverseTableSize
;
191 data
->fSFTable
= data
->fRTable
+ reverseTableSize
;
192 data
->fSFTableLen
= safeFwdTableSize
;
193 data
->fSRTable
= data
->fSFTable
+ safeFwdTableSize
;
194 data
->fSRTableLen
= safeRevTableSize
;
196 data
->fTrie
= data
->fSRTable
+ safeRevTableSize
;
197 data
->fTrieLen
= fSetBuilder
->getTrieSize();
198 data
->fStatusTable
= data
->fTrie
+ trieSize
;
199 data
->fStatusTableLen
= statusTableSize
;
200 data
->fRuleSource
= data
->fStatusTable
+ statusTableSize
;
201 data
->fRuleSourceLen
= strippedRules
.length() * sizeof(UChar
);
203 uprv_memset(data
->fReserved
, 0, sizeof(data
->fReserved
));
205 fForwardTables
->exportTable((uint8_t *)data
+ data
->fFTable
);
206 fReverseTables
->exportTable((uint8_t *)data
+ data
->fRTable
);
207 fSafeFwdTables
->exportTable((uint8_t *)data
+ data
->fSFTable
);
208 fSafeRevTables
->exportTable((uint8_t *)data
+ data
->fSRTable
);
209 fSetBuilder
->serializeTrie ((uint8_t *)data
+ data
->fTrie
);
211 int32_t *ruleStatusTable
= (int32_t *)((uint8_t *)data
+ data
->fStatusTable
);
212 for (i
=0; i
<fRuleStatusVals
->size(); i
++) {
213 ruleStatusTable
[i
] = fRuleStatusVals
->elementAti(i
);
216 strippedRules
.extract((UChar
*)((uint8_t *)data
+data
->fRuleSource
), rulesSize
/2+1, *fStatus
);
226 //----------------------------------------------------------------------------------------
228 // createRuleBasedBreakIterator construct from source rules that are passed in
229 // in a UnicodeString
231 //----------------------------------------------------------------------------------------
233 RBBIRuleBuilder::createRuleBasedBreakIterator( const UnicodeString
&rules
,
234 UParseError
*parseError
,
237 // status checked below
240 // Read the input rules, generate a parse tree, symbol table,
241 // and list of all Unicode Sets referenced by the rules.
243 RBBIRuleBuilder
builder(rules
, parseError
, status
);
244 if (U_FAILURE(status
)) { // status checked here bcos build below doesn't
247 builder
.fScanner
->parse();
250 // UnicodeSet processing.
251 // Munge the Unicode Sets to create a set of character categories.
252 // Generate the mapping tables (TRIE) from input 32-bit characters to
253 // the character categories.
255 builder
.fSetBuilder
->build();
259 // Generate the DFA state transition table.
261 builder
.fForwardTables
= new RBBITableBuilder(&builder
, &builder
.fForwardTree
);
262 builder
.fReverseTables
= new RBBITableBuilder(&builder
, &builder
.fReverseTree
);
263 builder
.fSafeFwdTables
= new RBBITableBuilder(&builder
, &builder
.fSafeFwdTree
);
264 builder
.fSafeRevTables
= new RBBITableBuilder(&builder
, &builder
.fSafeRevTree
);
265 if (builder
.fForwardTables
== NULL
|| builder
.fReverseTables
== NULL
||
266 builder
.fSafeFwdTables
== NULL
|| builder
.fSafeRevTables
== NULL
)
268 status
= U_MEMORY_ALLOCATION_ERROR
;
269 delete builder
.fForwardTables
; builder
.fForwardTables
= NULL
;
270 delete builder
.fReverseTables
; builder
.fReverseTables
= NULL
;
271 delete builder
.fSafeFwdTables
; builder
.fSafeFwdTables
= NULL
;
272 delete builder
.fSafeRevTables
; builder
.fSafeRevTables
= NULL
;
276 builder
.fForwardTables
->build();
277 builder
.fReverseTables
->build();
278 builder
.fSafeFwdTables
->build();
279 builder
.fSafeRevTables
->build();
282 if (builder
.fDebugEnv
&& uprv_strstr(builder
.fDebugEnv
, "states")) {
283 builder
.fForwardTables
->printRuleStatusTable();
288 // Package up the compiled data into a memory image
289 // in the run-time format.
291 RBBIDataHeader
*data
= builder
.flattenData(); // returns NULL if error
292 if (U_FAILURE(*builder
.fStatus
)) {
298 // Clean up the compiler related stuff
303 // Create a break iterator from the compiled rules.
304 // (Identical to creation from stored pre-compiled rules)
306 // status is checked after init in construction.
307 RuleBasedBreakIterator
*This
= new RuleBasedBreakIterator(data
, status
);
308 if (U_FAILURE(status
)) {
312 else if(This
== NULL
) { // test for NULL
313 status
= U_MEMORY_ALLOCATION_ERROR
;
320 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */