5 // Copyright (C) 2002-2006, International Business Machines Corporation and others.
6 // All Rights Reserved.
8 // This file contains the Rule Based Break Iterator Rule Builder functions for
9 // scanning the rules and assembling a parse tree. This is the first phase
10 // of compiling the rules.
12 // The overall of the rules is managed by class RBBIRuleBuilder, which will
13 // create and use an instance of this class as part of the process.
16 #include "unicode/utypes.h"
18 #if !UCONFIG_NO_BREAK_ITERATION
20 #include "unicode/unistr.h"
21 #include "unicode/uniset.h"
22 #include "unicode/uchar.h"
23 #include "unicode/uchriter.h"
24 #include "unicode/parsepos.h"
25 #include "unicode/parseerr.h"
30 #include "rbbirpt.h" // Contains state table for the rbbi rules parser.
31 // generated by a Perl script.
40 //----------------------------------------------------------------------------------------
42 // Unicode Set init strings for each of the character classes needed for parsing a rule file.
43 // (Initialized with hex values for portability to EBCDIC based machines.
44 // Really ugly, but there's no good way to avoid it.)
46 // The sets are referred to by name in the rbbirpt.txt, which is the
47 // source form of the state transition table for the RBBI rule parser.
49 //----------------------------------------------------------------------------------------
50 static const UChar gRuleSet_rule_char_pattern
[] = {
51 // [ ^ [ \ p { Z } \ u 0 0 2 0
52 0x5b, 0x5e, 0x5b, 0x5c, 0x70, 0x7b, 0x5a, 0x7d, 0x5c, 0x75, 0x30, 0x30, 0x32, 0x30,
53 // - \ u 0 0 7 f ] - [ \ p
54 0x2d, 0x5c, 0x75, 0x30, 0x30, 0x37, 0x66, 0x5d, 0x2d, 0x5b, 0x5c, 0x70,
55 // { L } ] - [ \ p { N } ] ]
56 0x7b, 0x4c, 0x7d, 0x5d, 0x2d, 0x5b, 0x5c, 0x70, 0x7b, 0x4e, 0x7d, 0x5d, 0x5d, 0};
58 static const UChar gRuleSet_name_char_pattern
[] = {
59 // [ _ \ p { L } \ p { N } ]
60 0x5b, 0x5f, 0x5c, 0x70, 0x7b, 0x4c, 0x7d, 0x5c, 0x70, 0x7b, 0x4e, 0x7d, 0x5d, 0};
62 static const UChar gRuleSet_digit_char_pattern
[] = {
64 0x5b, 0x30, 0x2d, 0x39, 0x5d, 0};
66 static const UChar gRuleSet_name_start_char_pattern
[] = {
68 0x5b, 0x5f, 0x5c, 0x70, 0x7b, 0x4c, 0x7d, 0x5d, 0 };
70 static const UChar kAny
[] = {0x61, 0x6e, 0x79, 0x00}; // "any"
74 static void U_CALLCONV
RBBISetTable_deleter(void *p
) {
75 RBBISetTableEl
*px
= (RBBISetTableEl
*)p
;
77 // Note: px->val is owned by the linked list "fSetsListHead" in scanner.
78 // Don't delete the value nodes here.
85 //----------------------------------------------------------------------------------------
89 //----------------------------------------------------------------------------------------
90 RBBIRuleScanner::RBBIRuleScanner(RBBIRuleBuilder
*rb
)
94 fStack
[fStackPtr
] = 0;
99 fRuleSets
[kRuleSet_rule_char
-128] = NULL
;
100 fRuleSets
[kRuleSet_white_space
-128] = NULL
;
101 fRuleSets
[kRuleSet_name_char
-128] = NULL
;
102 fRuleSets
[kRuleSet_name_start_char
-128] = NULL
;
103 fRuleSets
[kRuleSet_digit_char
-128] = NULL
;
110 fReverseRule
= FALSE
;
111 fLookAheadRule
= FALSE
;
117 // Do not check status until after all critical fields are sufficiently initialized
118 // that the destructor can run cleanly.
119 if (U_FAILURE(*rb
->fStatus
)) {
124 // Set up the constant Unicode Sets.
125 // Note: These could be made static, lazily initialized, and shared among
126 // all instances of RBBIRuleScanners. BUT this is quite a bit simpler,
127 // and the time to build these few sets should be small compared to a
128 // full break iterator build.
129 fRuleSets
[kRuleSet_rule_char
-128] = new UnicodeSet(gRuleSet_rule_char_pattern
, *rb
->fStatus
);
130 fRuleSets
[kRuleSet_white_space
-128] = (UnicodeSet
*) uprv_openRuleWhiteSpaceSet(rb
->fStatus
);
131 fRuleSets
[kRuleSet_name_char
-128] = new UnicodeSet(gRuleSet_name_char_pattern
, *rb
->fStatus
);
132 fRuleSets
[kRuleSet_name_start_char
-128] = new UnicodeSet(gRuleSet_name_start_char_pattern
, *rb
->fStatus
);
133 fRuleSets
[kRuleSet_digit_char
-128] = new UnicodeSet(gRuleSet_digit_char_pattern
, *rb
->fStatus
);
134 if (*rb
->fStatus
== U_ILLEGAL_ARGUMENT_ERROR
) {
135 // This case happens if ICU's data is missing. UnicodeSet tries to look up property
136 // names from the init string, can't find them, and claims an illegal arguement.
137 // Change the error so that the actual problem will be clearer to users.
138 *rb
->fStatus
= U_BRK_INIT_ERROR
;
140 if (U_FAILURE(*rb
->fStatus
)) {
144 fSymbolTable
= new RBBISymbolTable(this, rb
->fRules
, *rb
->fStatus
);
145 fSetTable
= uhash_open(uhash_hashUnicodeString
, uhash_compareUnicodeString
, NULL
, rb
->fStatus
);
146 uhash_setValueDeleter(fSetTable
, RBBISetTable_deleter
);
151 //----------------------------------------------------------------------------------------
155 //----------------------------------------------------------------------------------------
156 RBBIRuleScanner::~RBBIRuleScanner() {
157 delete fRuleSets
[kRuleSet_rule_char
-128];
158 delete fRuleSets
[kRuleSet_white_space
-128];
159 delete fRuleSets
[kRuleSet_name_char
-128];
160 delete fRuleSets
[kRuleSet_name_start_char
-128];
161 delete fRuleSets
[kRuleSet_digit_char
-128];
164 if (fSetTable
!= NULL
) {
165 uhash_close(fSetTable
);
172 // Normally has one entry, which is the entire parse tree for the rules.
173 // If errors occured, there may be additional subtrees left on the stack.
174 while (fNodeStackPtr
> 0) {
175 delete fNodeStack
[fNodeStackPtr
];
181 //----------------------------------------------------------------------------------------
183 // doParseAction Do some action during rule parsing.
184 // Called by the parse state machine.
185 // Actions build the parse tree and Unicode Sets,
186 // and maintain the parse stack for nested expressions.
188 // TODO: unify EParseAction and RBBI_RuleParseAction enum types.
189 // They represent exactly the same thing. They're separate
190 // only to work around enum forward declaration restrictions
191 // in some compilers, while at the same time avoiding multiple
192 // definitions problems. I'm sure that there's a better way.
194 //----------------------------------------------------------------------------------------
195 UBool
RBBIRuleScanner::doParseActions(EParseAction action
)
199 UBool returnVal
= TRUE
;
201 switch ((RBBI_RuleParseAction
)action
) {
204 pushNewNode(RBBINode::opStart
);
209 case doExprOrOperator
:
211 fixOpStack(RBBINode::precOpCat
);
212 RBBINode
*operandNode
= fNodeStack
[fNodeStackPtr
--];
213 RBBINode
*orNode
= pushNewNode(RBBINode::opOr
);
214 orNode
->fLeftChild
= operandNode
;
215 operandNode
->fParent
= orNode
;
219 case doExprCatOperator
:
220 // concatenation operator.
221 // For the implicit concatenation of adjacent terms in an expression that are
222 // not separated by any other operator. Action is invoked between the
223 // actions for the two terms.
225 fixOpStack(RBBINode::precOpCat
);
226 RBBINode
*operandNode
= fNodeStack
[fNodeStackPtr
--];
227 RBBINode
*catNode
= pushNewNode(RBBINode::opCat
);
228 catNode
->fLeftChild
= operandNode
;
229 operandNode
->fParent
= catNode
;
235 // The openParen node is a dummy operation type with a low precedence,
236 // which has the affect of ensuring that any real binary op that
237 // follows within the parens binds more tightly to the operands than
238 // stuff outside of the parens.
239 pushNewNode(RBBINode::opLParen
);
243 fixOpStack(RBBINode::precLParen
);
250 // We've just scanned "$variable = "
251 // The top of the node stack has the $variable ref node.
253 // Save the start position of the RHS text in the StartExpression node
254 // that precedes the $variableReference node on the stack.
255 // This will eventually be used when saving the full $variable replacement
257 n
= fNodeStack
[fNodeStackPtr
-1];
258 n
->fFirstPos
= fNextIndex
; // move past the '='
260 // Push a new start-of-expression node; needed to keep parse of the
261 // RHS expression happy.
262 pushNewNode(RBBINode::opStart
);
270 // We have reached the end of an assignement statement.
271 // Current scan char is the ';' that terminates the assignment.
273 // Terminate expression, leaves expression parse tree rooted in TOS node.
274 fixOpStack(RBBINode::precStart
);
276 RBBINode
*startExprNode
= fNodeStack
[fNodeStackPtr
-2];
277 RBBINode
*varRefNode
= fNodeStack
[fNodeStackPtr
-1];
278 RBBINode
*RHSExprNode
= fNodeStack
[fNodeStackPtr
];
280 // Save original text of right side of assignment, excluding the terminating ';'
281 // in the root of the node for the right-hand-side expression.
282 RHSExprNode
->fFirstPos
= startExprNode
->fFirstPos
;
283 RHSExprNode
->fLastPos
= fScanIndex
;
284 fRB
->fRules
.extractBetween(RHSExprNode
->fFirstPos
, RHSExprNode
->fLastPos
, RHSExprNode
->fText
);
286 // Expression parse tree becomes l. child of the $variable reference node.
287 varRefNode
->fLeftChild
= RHSExprNode
;
288 RHSExprNode
->fParent
= varRefNode
;
290 // Make a symbol table entry for the $variableRef node.
291 fSymbolTable
->addEntry(varRefNode
->fText
, varRefNode
, *fRB
->fStatus
);
292 if (U_FAILURE(*fRB
->fStatus
)) {
293 // This is a round-about way to get the parse position set
294 // so that duplicate symbols error messages include a line number.
295 UErrorCode t
= *fRB
->fStatus
;
296 *fRB
->fStatus
= U_ZERO_ERROR
;
300 // Clean up the stack.
301 delete startExprNode
;
308 fixOpStack(RBBINode::precStart
); // Terminate expression, leaves expression
309 if (U_FAILURE(*fRB
->fStatus
)) { // parse tree rooted in TOS node.
313 if (fRB
->fDebugEnv
&& uprv_strstr(fRB
->fDebugEnv
, "rtree")) {printNodeStack("end of rule");}
315 U_ASSERT(fNodeStackPtr
== 1);
317 // If this rule includes a look-ahead '/', add a endMark node to the
319 if (fLookAheadRule
) {
320 RBBINode
*thisRule
= fNodeStack
[fNodeStackPtr
];
321 RBBINode
*endNode
= pushNewNode(RBBINode::endMark
);
322 RBBINode
*catNode
= pushNewNode(RBBINode::opCat
);
324 catNode
->fLeftChild
= thisRule
;
325 catNode
->fRightChild
= endNode
;
326 fNodeStack
[fNodeStackPtr
] = catNode
;
327 endNode
->fVal
= fRuleNum
;
328 endNode
->fLookAheadEnd
= TRUE
;
331 // All rule expressions are ORed together.
332 // The ';' that terminates an expression really just functions as a '|' with
333 // a low operator prededence.
335 // Each of the four sets of rules are collected separately.
336 // (forward, reverse, safe_forward, safe_reverse)
337 // OR this rule into the appropriate group of them.
339 RBBINode
**destRules
= (fReverseRule
? &fRB
->fReverseTree
: fRB
->fDefaultTree
);
341 if (*destRules
!= NULL
) {
342 // This is not the first rule encounted.
343 // OR previous stuff (from *destRules)
344 // with the current rule expression (on the Node Stack)
345 // with the resulting OR expression going to *destRules
347 RBBINode
*thisRule
= fNodeStack
[fNodeStackPtr
];
348 RBBINode
*prevRules
= *destRules
;
349 RBBINode
*orNode
= pushNewNode(RBBINode::opOr
);
350 orNode
->fLeftChild
= prevRules
;
351 prevRules
->fParent
= orNode
;
352 orNode
->fRightChild
= thisRule
;
353 thisRule
->fParent
= orNode
;
358 // This is the first rule encountered (for this direction).
359 // Just move its parse tree from the stack to *destRules.
360 *destRules
= fNodeStack
[fNodeStackPtr
];
362 fReverseRule
= FALSE
; // in preparation for the next rule.
363 fLookAheadRule
= FALSE
;
370 error(U_BRK_RULE_SYNTAX
);
375 case doVariableNameExpectedErr
:
376 error(U_BRK_RULE_SYNTAX
);
381 // Unary operands + ? *
382 // These all appear after the operand to which they apply.
383 // When we hit one, the operand (may be a whole sub expression)
384 // will be on the top of the stack.
385 // Unary Operator becomes TOS, with the old TOS as its one child.
388 RBBINode
*operandNode
= fNodeStack
[fNodeStackPtr
--];
389 RBBINode
*plusNode
= pushNewNode(RBBINode::opPlus
);
390 plusNode
->fLeftChild
= operandNode
;
391 operandNode
->fParent
= plusNode
;
395 case doUnaryOpQuestion
:
397 RBBINode
*operandNode
= fNodeStack
[fNodeStackPtr
--];
398 RBBINode
*qNode
= pushNewNode(RBBINode::opQuestion
);
399 qNode
->fLeftChild
= operandNode
;
400 operandNode
->fParent
= qNode
;
406 RBBINode
*operandNode
= fNodeStack
[fNodeStackPtr
--];
407 RBBINode
*starNode
= pushNewNode(RBBINode::opStar
);
408 starNode
->fLeftChild
= operandNode
;
409 operandNode
->fParent
= starNode
;
414 // A "Rule Character" is any single character that is a literal part
415 // of the regular expression. Like a, b and c in the expression "(abc*) | [:L:]"
416 // These are pretty uncommon in break rules; the terms are more commonly
417 // sets. To keep things uniform, treat these characters like as
418 // sets that just happen to contain only one character.
420 n
= pushNewNode(RBBINode::setRef
);
421 findSetFor(fC
.fChar
, n
);
422 n
->fFirstPos
= fScanIndex
;
423 n
->fLastPos
= fNextIndex
;
424 fRB
->fRules
.extractBetween(n
->fFirstPos
, n
->fLastPos
, n
->fText
);
429 // scanned a ".", meaning match any single character.
431 n
= pushNewNode(RBBINode::setRef
);
433 n
->fFirstPos
= fScanIndex
;
434 n
->fLastPos
= fNextIndex
;
435 fRB
->fRules
.extractBetween(n
->fFirstPos
, n
->fLastPos
, n
->fText
);
440 // Scanned a '/', which identifies a look-ahead break position in a rule.
441 n
= pushNewNode(RBBINode::lookAhead
);
443 n
->fFirstPos
= fScanIndex
;
444 n
->fLastPos
= fNextIndex
;
445 fRB
->fRules
.extractBetween(n
->fFirstPos
, n
->fLastPos
, n
->fText
);
446 fLookAheadRule
= TRUE
;
450 case doStartTagValue
:
451 // Scanned a '{', the opening delimiter for a tag value within a rule.
452 n
= pushNewNode(RBBINode::tag
);
454 n
->fFirstPos
= fScanIndex
;
455 n
->fLastPos
= fNextIndex
;
459 // Just scanned a decimal digit that's part of a tag value
461 n
= fNodeStack
[fNodeStackPtr
];
462 uint32_t v
= u_charDigitValue(fC
.fChar
);
464 n
->fVal
= n
->fVal
*10 + v
;
469 n
= fNodeStack
[fNodeStackPtr
];
470 n
->fLastPos
= fNextIndex
;
471 fRB
->fRules
.extractBetween(n
->fFirstPos
, n
->fLastPos
, n
->fText
);
474 case doTagExpectedError
:
475 error(U_BRK_MALFORMED_RULE_TAG
);
480 // Scanning a !!option. At the start of string.
481 fOptionStart
= fScanIndex
;
486 UnicodeString
opt(fRB
->fRules
, fOptionStart
, fScanIndex
-fOptionStart
);
487 if (opt
== UNICODE_STRING("chain", 5)) {
488 fRB
->fChainRules
= TRUE
;
489 } else if (opt
== UNICODE_STRING("LBCMNoChain", 11)) {
490 fRB
->fLBCMNoChain
= TRUE
;
491 } else if (opt
== UNICODE_STRING("forward", 7)) {
492 fRB
->fDefaultTree
= &fRB
->fForwardTree
;
493 } else if (opt
== UNICODE_STRING("reverse", 7)) {
494 fRB
->fDefaultTree
= &fRB
->fReverseTree
;
495 } else if (opt
== UNICODE_STRING("safe_forward", 12)) {
496 fRB
->fDefaultTree
= &fRB
->fSafeFwdTree
;
497 } else if (opt
== UNICODE_STRING("safe_reverse", 12)) {
498 fRB
->fDefaultTree
= &fRB
->fSafeRevTree
;
499 } else if (opt
== UNICODE_STRING("lookAheadHardBreak", 18)) {
500 fRB
->fLookAheadHardBreak
= TRUE
;
502 error(U_BRK_UNRECOGNIZED_OPTION
);
511 case doStartVariableName
:
512 n
= pushNewNode(RBBINode::varRef
);
513 if (U_FAILURE(*fRB
->fStatus
)) {
516 n
->fFirstPos
= fScanIndex
;
519 case doEndVariableName
:
520 n
= fNodeStack
[fNodeStackPtr
];
521 if (n
==NULL
|| n
->fType
!= RBBINode::varRef
) {
522 error(U_BRK_INTERNAL_ERROR
);
525 n
->fLastPos
= fScanIndex
;
526 fRB
->fRules
.extractBetween(n
->fFirstPos
+1, n
->fLastPos
, n
->fText
);
527 // Look the newly scanned name up in the symbol table
528 // If there's an entry, set the l. child of the var ref to the replacement expression.
529 // (We also pass through here when scanning assignments, but no harm is done, other
530 // than a slight wasted effort that seems hard to avoid. Lookup will be null)
531 n
->fLeftChild
= fSymbolTable
->lookupNode(n
->fText
);
535 n
= fNodeStack
[fNodeStackPtr
];
536 if (n
->fLeftChild
== NULL
) {
537 error(U_BRK_UNDEFINED_VARIABLE
);
545 case doRuleErrorAssignExpr
:
546 error(U_BRK_ASSIGN_ERROR
);
554 case doScanUnicodeSet
:
559 error(U_BRK_INTERNAL_ERROR
);
569 //----------------------------------------------------------------------------------------
571 // Error Report a rule parse error.
572 // Only report it if no previous error has been recorded.
574 //----------------------------------------------------------------------------------------
575 void RBBIRuleScanner::error(UErrorCode e
) {
576 if (U_SUCCESS(*fRB
->fStatus
)) {
578 fRB
->fParseError
->line
= fLineNum
;
579 fRB
->fParseError
->offset
= fCharNum
;
580 fRB
->fParseError
->preContext
[0] = 0;
581 fRB
->fParseError
->preContext
[0] = 0;
588 //----------------------------------------------------------------------------------------
590 // fixOpStack The parse stack holds partially assembled chunks of the parse tree.
591 // An entry on the stack may be as small as a single setRef node,
592 // or as large as the parse tree
593 // for an entire expression (this will be the one item left on the stack
594 // when the parsing of an RBBI rule completes.
596 // This function is called when a binary operator is encountered.
597 // It looks back up the stack for operators that are not yet associated
598 // with a right operand, and if the precedence of the stacked operator >=
599 // the precedence of the current operator, binds the operand left,
600 // to the previously encountered operator.
602 //----------------------------------------------------------------------------------------
603 void RBBIRuleScanner::fixOpStack(RBBINode::OpPrecedence p
) {
605 // printNodeStack("entering fixOpStack()");
607 n
= fNodeStack
[fNodeStackPtr
-1]; // an operator node
608 if (n
->fPrecedence
== 0) {
609 RBBIDebugPuts("RBBIRuleScanner::fixOpStack, bad operator node");
610 error(U_BRK_INTERNAL_ERROR
);
614 if (n
->fPrecedence
< p
|| n
->fPrecedence
<= RBBINode::precLParen
) {
615 // The most recent operand goes with the current operator,
616 // not with the previously stacked one.
619 // Stack operator is a binary op ( '|' or concatenation)
620 // TOS operand becomes right child of this operator.
621 // Resulting subexpression becomes the TOS operand.
622 n
->fRightChild
= fNodeStack
[fNodeStackPtr
];
623 fNodeStack
[fNodeStackPtr
]->fParent
= n
;
625 // printNodeStack("looping in fixOpStack() ");
628 if (p
<= RBBINode::precLParen
) {
629 // Scan is at a right paren or end of expression.
630 // The scanned item must match the stack, or else there was an error.
631 // Discard the left paren (or start expr) node from the stack,
632 // leaving the completed (sub)expression as TOS.
633 if (n
->fPrecedence
!= p
) {
634 // Right paren encountered matched start of expression node, or
635 // end of expression matched with a left paren node.
636 error(U_BRK_MISMATCHED_PAREN
);
638 fNodeStack
[fNodeStackPtr
-1] = fNodeStack
[fNodeStackPtr
];
640 // Delete the now-discarded LParen or Start node.
643 // printNodeStack("leaving fixOpStack()");
649 //----------------------------------------------------------------------------------------
651 // findSetFor given a UnicodeString,
652 // - find the corresponding Unicode Set (uset node)
653 // (create one if necessary)
654 // - Set fLeftChild of the caller's node (should be a setRef node)
656 // Maintain a hash table of uset nodes, so the same one is always used
657 // for the same string.
658 // If a "to adopt" set is provided and we haven't seen this key before,
659 // add the provided set to the hash table.
660 // If the string is one (32 bit) char in length, the set contains
661 // just one element which is the char in question.
662 // If the string is "any", return a set containing all chars.
664 //----------------------------------------------------------------------------------------
665 void RBBIRuleScanner::findSetFor(const UnicodeString
&s
, RBBINode
*node
, UnicodeSet
*setToAdopt
) {
669 // First check whether we've already cached a set for this string.
670 // If so, just use the cached set in the new node.
671 // delete any set provided by the caller, since we own it.
672 el
= (RBBISetTableEl
*)uhash_get(fSetTable
, &s
);
675 node
->fLeftChild
= el
->val
;
676 U_ASSERT(node
->fLeftChild
->fType
== RBBINode::uset
);
680 // Haven't seen this set before.
681 // If the caller didn't provide us with a prebuilt set,
682 // create a new UnicodeSet now.
683 if (setToAdopt
== NULL
) {
684 if (s
.compare(kAny
, -1) == 0) {
685 setToAdopt
= new UnicodeSet(0x000000, 0x10ffff);
689 setToAdopt
= new UnicodeSet(c
, c
);
694 // Make a new uset node to refer to this UnicodeSet
695 // This new uset node becomes the child of the caller's setReference node.
697 RBBINode
*usetNode
= new RBBINode(RBBINode::uset
);
698 usetNode
->fInputSet
= setToAdopt
;
699 usetNode
->fParent
= node
;
700 node
->fLeftChild
= usetNode
;
705 // Add the new uset node to the list of all uset nodes.
707 fRB
->fUSetNodes
->addElement(usetNode
, *fRB
->fStatus
);
711 // Add the new set to the set hash table.
713 el
= (RBBISetTableEl
*)uprv_malloc(sizeof(RBBISetTableEl
));
714 UnicodeString
*tkey
= new UnicodeString(s
);
715 if (tkey
== NULL
|| el
== NULL
|| setToAdopt
== NULL
) {
716 error(U_MEMORY_ALLOCATION_ERROR
);
721 uhash_put(fSetTable
, el
->key
, el
, fRB
->fStatus
);
729 // Assorted Unicode character constants.
730 // Numeric because there is no portable way to enter them as literals.
733 static const UChar chCR
= 0x0d; // New lines, for terminating comments.
734 static const UChar chLF
= 0x0a;
735 static const UChar chNEL
= 0x85; // NEL newline variant
736 static const UChar chLS
= 0x2028; // Unicode Line Separator
737 static const UChar chApos
= 0x27; // single quote, for quoted chars.
738 static const UChar chPound
= 0x23; // '#', introduces a comment.
739 static const UChar chBackSlash
= 0x5c; // '\' introduces a char escape
740 static const UChar chLParen
= 0x28;
741 static const UChar chRParen
= 0x29;
744 //----------------------------------------------------------------------------------------
746 // stripRules Return a rules string without unnecessary
749 //----------------------------------------------------------------------------------------
750 UnicodeString
RBBIRuleScanner::stripRules(const UnicodeString
&rules
) {
751 UnicodeString strippedRules
;
752 int rulesLength
= rules
.length();
753 for (int idx
= 0; idx
< rulesLength
; ) {
754 UChar ch
= rules
[idx
++];
756 while (idx
< rulesLength
757 && ch
!= chCR
&& ch
!= chLF
&& ch
!= chNEL
)
762 if (!u_isISOControl(ch
)) {
763 strippedRules
.append(ch
);
766 // strippedRules = strippedRules.unescape();
767 return strippedRules
;
771 //----------------------------------------------------------------------------------------
773 // nextCharLL Low Level Next Char from rule input source.
774 // Get a char from the input character iterator,
775 // keep track of input position for error reporting.
777 //----------------------------------------------------------------------------------------
778 UChar32
RBBIRuleScanner::nextCharLL() {
781 if (fNextIndex
>= fRB
->fRules
.length()) {
784 ch
= fRB
->fRules
.char32At(fNextIndex
);
785 fNextIndex
= fRB
->fRules
.moveIndex32(fNextIndex
, 1);
790 ch
== chLF
&& fLastChar
!= chCR
) {
791 // Character is starting a new line. Bump up the line number, and
792 // reset the column to 0.
796 error(U_BRK_NEW_LINE_IN_QUOTED_STRING
);
801 // Character is not starting a new line. Except in the case of a
802 // LF following a CR, increment the column position.
812 //---------------------------------------------------------------------------------
814 // nextChar for rules scanning. At this level, we handle stripping
815 // out comments and processing backslash character escapes.
816 // The rest of the rules grammar is handled at the next level up.
818 //---------------------------------------------------------------------------------
819 void RBBIRuleScanner::nextChar(RBBIRuleChar
&c
) {
821 // Unicode Character constants needed for the processing done by nextChar(),
822 // in hex because literals wont work on EBCDIC machines.
824 fScanIndex
= fNextIndex
;
825 c
.fChar
= nextCharLL();
829 // check for '' sequence.
830 // These are recognized in all contexts, whether in quoted text or not.
832 if (c
.fChar
== chApos
) {
833 if (fRB
->fRules
.char32At(fNextIndex
) == chApos
) {
834 c
.fChar
= nextCharLL(); // get nextChar officially so character counts
835 c
.fEscaped
= TRUE
; // stay correct.
839 // Single quote, by itself.
840 // Toggle quoting mode.
841 // Return either '(' or ')', because quotes cause a grouping of the quoted text.
842 fQuoteMode
= !fQuoteMode
;
843 if (fQuoteMode
== TRUE
) {
848 c
.fEscaped
= FALSE
; // The paren that we return is not escaped.
858 // We are not in a 'quoted region' of the source.
860 if (c
.fChar
== chPound
) {
861 // Start of a comment. Consume the rest of it.
862 // The new-line char that terminates the comment is always returned.
863 // It will be treated as white-space, and serves to break up anything
864 // that might otherwise incorrectly clump together with a comment in
865 // the middle (a variable name, for example.)
867 c
.fChar
= nextCharLL();
868 if (c
.fChar
== (UChar32
)-1 || // EOF
872 c
.fChar
== chLS
) {break;}
875 if (c
.fChar
== (UChar32
)-1) {
880 // check for backslash escaped characters.
881 // Use UnicodeString::unescapeAt() to handle them.
883 if (c
.fChar
== chBackSlash
) {
885 int32_t startX
= fNextIndex
;
886 c
.fChar
= fRB
->fRules
.unescapeAt(fNextIndex
);
887 if (fNextIndex
== startX
) {
888 error(U_BRK_HEX_DIGITS_EXPECTED
);
890 fCharNum
+= fNextIndex
-startX
;
893 // putc(c.fChar, stdout);
896 //---------------------------------------------------------------------------------
898 // Parse RBBI rules. The state machine for rules parsing is here.
899 // The state tables are hand-written in the file rbbirpt.txt,
900 // and converted to the form used here by a perl
903 //---------------------------------------------------------------------------------
904 void RBBIRuleScanner::parse() {
906 const RBBIRuleTableEl
*tableEl
;
908 if (U_FAILURE(*fRB
->fStatus
)) {
915 // Main loop for the rule parsing state machine.
916 // Runs once per state transition.
917 // Each time through optionally performs, depending on the state table,
918 // - an advance to the the next input char
919 // - an action to be performed.
920 // - pushing or popping a state to/from the local state return stack.
923 // Bail out if anything has gone wrong.
924 // RBBI rule file parsing stops on the first error encountered.
925 if (U_FAILURE(*fRB
->fStatus
)) {
929 // Quit if state == 0. This is the normal way to exit the state machine.
935 // Find the state table element that matches the input char from the rule, or the
936 // class of the input character. Start with the first table row for this
937 // state, then linearly scan forward until we find a row that matches the
938 // character. The last row for each state always matches all characters, so
939 // the search will stop there, if not before.
941 tableEl
= &gRuleParseStateTable
[state
];
943 if (fRB
->fDebugEnv
&& uprv_strstr(fRB
->fDebugEnv
, "scan")) {
944 RBBIDebugPrintf("char, line, col = (\'%c\', %d, %d) state=%s ",
945 fC
.fChar
, fLineNum
, fCharNum
, RBBIRuleStateNames
[state
]);
951 if (fRB
->fDebugEnv
&& uprv_strstr(fRB
->fDebugEnv
, "scan")) { RBBIDebugPrintf(".");}
953 if (tableEl
->fCharClass
< 127 && fC
.fEscaped
== FALSE
&& tableEl
->fCharClass
== fC
.fChar
) {
954 // Table row specified an individual character, not a set, and
955 // the input character is not escaped, and
956 // the input character matched it.
959 if (tableEl
->fCharClass
== 255) {
960 // Table row specified default, match anything character class.
963 if (tableEl
->fCharClass
== 254 && fC
.fEscaped
) {
964 // Table row specified "escaped" and the char was escaped.
967 if (tableEl
->fCharClass
== 253 && fC
.fEscaped
&&
968 (fC
.fChar
== 0x50 || fC
.fChar
== 0x70 )) {
969 // Table row specified "escaped P" and the char is either 'p' or 'P'.
972 if (tableEl
->fCharClass
== 252 && fC
.fChar
== (UChar32
)-1) {
973 // Table row specified eof and we hit eof on the input.
977 if (tableEl
->fCharClass
>= 128 && tableEl
->fCharClass
< 240 && // Table specs a char class &&
978 fC
.fEscaped
== FALSE
&& // char is not escaped &&
979 fC
.fChar
!= (UChar32
)-1) { // char is not EOF
980 UnicodeSet
*uniset
= fRuleSets
[tableEl
->fCharClass
-128];
981 if (uniset
->contains(fC
.fChar
)) {
982 // Table row specified a character class, or set of characters,
983 // and the current char matches it.
988 // No match on this row, advance to the next row for this state,
991 if (fRB
->fDebugEnv
&& uprv_strstr(fRB
->fDebugEnv
, "scan")) { RBBIDebugPuts("");}
994 // We've found the row of the state table that matches the current input
995 // character from the rules string.
996 // Perform any action specified by this row in the state table.
997 if (doParseActions((EParseAction
)tableEl
->fAction
) == FALSE
) {
998 // Break out of the state machine loop if the
999 // the action signalled some kind of error, or
1000 // the action was to exit, occurs on normal end-of-rules-input.
1004 if (tableEl
->fPushState
!= 0) {
1006 if (fStackPtr
>= kStackSize
) {
1007 error(U_BRK_INTERNAL_ERROR
);
1008 RBBIDebugPuts("RBBIRuleScanner::parse() - state stack overflow.");
1011 fStack
[fStackPtr
] = tableEl
->fPushState
;
1014 if (tableEl
->fNextChar
) {
1018 // Get the next state from the table entry, or from the
1019 // state stack if the next state was specified as "pop".
1020 if (tableEl
->fNextState
!= 255) {
1021 state
= tableEl
->fNextState
;
1023 state
= fStack
[fStackPtr
];
1025 if (fStackPtr
< 0) {
1026 error(U_BRK_INTERNAL_ERROR
);
1027 RBBIDebugPuts("RBBIRuleScanner::parse() - state stack underflow.");
1035 // If there were NO user specified reverse rules, set up the equivalent of ".*;"
1037 if (fRB
->fReverseTree
== NULL
) {
1038 fRB
->fReverseTree
= pushNewNode(RBBINode::opStar
);
1039 RBBINode
*operand
= pushNewNode(RBBINode::setRef
);
1040 findSetFor(kAny
, operand
);
1041 fRB
->fReverseTree
->fLeftChild
= operand
;
1042 operand
->fParent
= fRB
->fReverseTree
;
1048 // Parsing of the input RBBI rules is complete.
1049 // We now have a parse tree for the rule expressions
1050 // and a list of all UnicodeSets that are referenced.
1053 if (fRB
->fDebugEnv
&& uprv_strstr(fRB
->fDebugEnv
, "symbols")) {fSymbolTable
->rbbiSymtablePrint();}
1054 if (fRB
->fDebugEnv
&& uprv_strstr(fRB
->fDebugEnv
, "ptree"))
1056 RBBIDebugPrintf("Completed Forward Rules Parse Tree...\n");
1057 fRB
->fForwardTree
->printTree(TRUE
);
1058 RBBIDebugPrintf("\nCompleted Reverse Rules Parse Tree...\n");
1059 fRB
->fReverseTree
->printTree(TRUE
);
1060 RBBIDebugPrintf("\nCompleted Safe Point Forward Rules Parse Tree...\n");
1061 fRB
->fSafeFwdTree
->printTree(TRUE
);
1062 RBBIDebugPrintf("\nCompleted Safe Point Reverse Rules Parse Tree...\n");
1063 fRB
->fSafeRevTree
->printTree(TRUE
);
1069 //---------------------------------------------------------------------------------
1071 // printNodeStack for debugging...
1073 //---------------------------------------------------------------------------------
1075 void RBBIRuleScanner::printNodeStack(const char *title
) {
1077 RBBIDebugPrintf("%s. Dumping node stack...\n", title
);
1078 for (i
=fNodeStackPtr
; i
>0; i
--) {fNodeStack
[i
]->printTree(TRUE
);}
1085 //---------------------------------------------------------------------------------
1087 // pushNewNode create a new RBBINode of the specified type and push it
1088 // onto the stack of nodes.
1090 //---------------------------------------------------------------------------------
1091 RBBINode
*RBBIRuleScanner::pushNewNode(RBBINode::NodeType t
) {
1093 if (fNodeStackPtr
>= kStackSize
) {
1094 error(U_BRK_INTERNAL_ERROR
);
1095 RBBIDebugPuts("RBBIRuleScanner::pushNewNode - stack overflow.");
1096 *fRB
->fStatus
= U_BRK_INTERNAL_ERROR
;
1099 fNodeStack
[fNodeStackPtr
] = new RBBINode(t
);
1100 if (fNodeStack
[fNodeStackPtr
] == NULL
) {
1101 *fRB
->fStatus
= U_MEMORY_ALLOCATION_ERROR
;
1103 return fNodeStack
[fNodeStackPtr
];
1108 //---------------------------------------------------------------------------------
1110 // scanSet Construct a UnicodeSet from the text at the current scan
1111 // position. Advance the scan position to the first character
1114 // A new RBBI setref node referring to the set is pushed onto the node
1117 // The scan position is normally under the control of the state machine
1118 // that controls rule parsing. UnicodeSets, however, are parsed by
1119 // the UnicodeSet constructor, not by the RBBI rule parser.
1121 //---------------------------------------------------------------------------------
1122 void RBBIRuleScanner::scanSet() {
1128 if (U_FAILURE(*fRB
->fStatus
)) {
1132 pos
.setIndex(fScanIndex
);
1133 startPos
= fScanIndex
;
1134 UErrorCode localStatus
= U_ZERO_ERROR
;
1135 uset
= new UnicodeSet(fRB
->fRules
, pos
, USET_IGNORE_SPACE
,
1138 if (U_FAILURE(localStatus
)) {
1139 // TODO: Get more accurate position of the error from UnicodeSet's return info.
1140 // UnicodeSet appears to not be reporting correctly at this time.
1142 RBBIDebugPrintf("UnicodeSet parse postion.ErrorIndex = %d\n", pos
.getIndex());
1149 // Verify that the set contains at least one code point.
1151 if (uset
->isEmpty()) {
1152 // This set is empty.
1153 // Make it an error, because it almost certainly is not what the user wanted.
1154 // Also, avoids having to think about corner cases in the tree manipulation code
1155 // that occurs later on.
1156 error(U_BRK_RULE_EMPTY_SET
);
1162 // Advance the RBBI parse postion over the UnicodeSet pattern.
1163 // Don't just set fScanIndex because the line/char positions maintained
1164 // for error reporting would be thrown off.
1167 if (fNextIndex
>= i
) {
1173 if (U_SUCCESS(*fRB
->fStatus
)) {
1176 n
= pushNewNode(RBBINode::setRef
);
1177 n
->fFirstPos
= startPos
;
1178 n
->fLastPos
= fNextIndex
;
1179 fRB
->fRules
.extractBetween(n
->fFirstPos
, n
->fLastPos
, n
->fText
);
1180 // findSetFor() serves several purposes here:
1181 // - Adopts storage for the UnicodeSet, will be responsible for deleting.
1182 // - Mantains collection of all sets in use, needed later for establishing
1183 // character categories for run time engine.
1184 // - Eliminates mulitiple instances of the same set.
1185 // - Creates a new uset node if necessary (if this isn't a duplicate.)
1186 findSetFor(n
->fText
, n
, uset
);
1193 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */