Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | |
michael@0 | 2 | // |
michael@0 | 3 | // file: rbbiscan.cpp |
michael@0 | 4 | // |
michael@0 | 5 | // Copyright (C) 2002-2012, International Business Machines Corporation and others. |
michael@0 | 6 | // All Rights Reserved. |
michael@0 | 7 | // |
michael@0 | 8 | // This file contains the Rule Based Break Iterator Rule Builder functions for |
michael@0 | 9 | // scanning the rules and assembling a parse tree. This is the first phase |
michael@0 | 10 | // of compiling the rules. |
michael@0 | 11 | // |
michael@0 | 12 | // The overall of the rules is managed by class RBBIRuleBuilder, which will |
michael@0 | 13 | // create and use an instance of this class as part of the process. |
michael@0 | 14 | // |
michael@0 | 15 | |
michael@0 | 16 | #include "unicode/utypes.h" |
michael@0 | 17 | |
michael@0 | 18 | #if !UCONFIG_NO_BREAK_ITERATION |
michael@0 | 19 | |
michael@0 | 20 | #include "unicode/unistr.h" |
michael@0 | 21 | #include "unicode/uniset.h" |
michael@0 | 22 | #include "unicode/uchar.h" |
michael@0 | 23 | #include "unicode/uchriter.h" |
michael@0 | 24 | #include "unicode/parsepos.h" |
michael@0 | 25 | #include "unicode/parseerr.h" |
michael@0 | 26 | #include "cmemory.h" |
michael@0 | 27 | #include "cstring.h" |
michael@0 | 28 | |
michael@0 | 29 | #include "rbbirpt.h" // Contains state table for the rbbi rules parser. |
michael@0 | 30 | // generated by a Perl script. |
michael@0 | 31 | #include "rbbirb.h" |
michael@0 | 32 | #include "rbbinode.h" |
michael@0 | 33 | #include "rbbiscan.h" |
michael@0 | 34 | #include "rbbitblb.h" |
michael@0 | 35 | |
michael@0 | 36 | #include "uassert.h" |
michael@0 | 37 | |
michael@0 | 38 | #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0])) |
michael@0 | 39 | |
michael@0 | 40 | //------------------------------------------------------------------------------ |
michael@0 | 41 | // |
michael@0 | 42 | // Unicode Set init strings for each of the character classes needed for parsing a rule file. |
michael@0 | 43 | // (Initialized with hex values for portability to EBCDIC based machines. |
michael@0 | 44 | // Really ugly, but there's no good way to avoid it.) |
michael@0 | 45 | // |
michael@0 | 46 | // The sets are referred to by name in the rbbirpt.txt, which is the |
michael@0 | 47 | // source form of the state transition table for the RBBI rule parser. |
michael@0 | 48 | // |
michael@0 | 49 | //------------------------------------------------------------------------------ |
michael@0 | 50 | static const UChar gRuleSet_rule_char_pattern[] = { |
michael@0 | 51 | // [ ^ [ \ p { Z } \ u 0 0 2 0 |
michael@0 | 52 | 0x5b, 0x5e, 0x5b, 0x5c, 0x70, 0x7b, 0x5a, 0x7d, 0x5c, 0x75, 0x30, 0x30, 0x32, 0x30, |
michael@0 | 53 | // - \ u 0 0 7 f ] - [ \ p |
michael@0 | 54 | 0x2d, 0x5c, 0x75, 0x30, 0x30, 0x37, 0x66, 0x5d, 0x2d, 0x5b, 0x5c, 0x70, |
michael@0 | 55 | // { L } ] - [ \ p { N } ] ] |
michael@0 | 56 | 0x7b, 0x4c, 0x7d, 0x5d, 0x2d, 0x5b, 0x5c, 0x70, 0x7b, 0x4e, 0x7d, 0x5d, 0x5d, 0}; |
michael@0 | 57 | |
michael@0 | 58 | static const UChar gRuleSet_name_char_pattern[] = { |
michael@0 | 59 | // [ _ \ p { L } \ p { N } ] |
michael@0 | 60 | 0x5b, 0x5f, 0x5c, 0x70, 0x7b, 0x4c, 0x7d, 0x5c, 0x70, 0x7b, 0x4e, 0x7d, 0x5d, 0}; |
michael@0 | 61 | |
michael@0 | 62 | static const UChar gRuleSet_digit_char_pattern[] = { |
michael@0 | 63 | // [ 0 - 9 ] |
michael@0 | 64 | 0x5b, 0x30, 0x2d, 0x39, 0x5d, 0}; |
michael@0 | 65 | |
michael@0 | 66 | static const UChar gRuleSet_name_start_char_pattern[] = { |
michael@0 | 67 | // [ _ \ p { L } ] |
michael@0 | 68 | 0x5b, 0x5f, 0x5c, 0x70, 0x7b, 0x4c, 0x7d, 0x5d, 0 }; |
michael@0 | 69 | |
michael@0 | 70 | static const UChar kAny[] = {0x61, 0x6e, 0x79, 0x00}; // "any" |
michael@0 | 71 | |
michael@0 | 72 | |
michael@0 | 73 | U_CDECL_BEGIN |
michael@0 | 74 | static void U_CALLCONV RBBISetTable_deleter(void *p) { |
michael@0 | 75 | icu::RBBISetTableEl *px = (icu::RBBISetTableEl *)p; |
michael@0 | 76 | delete px->key; |
michael@0 | 77 | // Note: px->val is owned by the linked list "fSetsListHead" in scanner. |
michael@0 | 78 | // Don't delete the value nodes here. |
michael@0 | 79 | uprv_free(px); |
michael@0 | 80 | } |
michael@0 | 81 | U_CDECL_END |
michael@0 | 82 | |
michael@0 | 83 | U_NAMESPACE_BEGIN |
michael@0 | 84 | |
michael@0 | 85 | //------------------------------------------------------------------------------ |
michael@0 | 86 | // |
michael@0 | 87 | // Constructor. |
michael@0 | 88 | // |
michael@0 | 89 | //------------------------------------------------------------------------------ |
michael@0 | 90 | RBBIRuleScanner::RBBIRuleScanner(RBBIRuleBuilder *rb) |
michael@0 | 91 | { |
michael@0 | 92 | fRB = rb; |
michael@0 | 93 | fStackPtr = 0; |
michael@0 | 94 | fStack[fStackPtr] = 0; |
michael@0 | 95 | fNodeStackPtr = 0; |
michael@0 | 96 | fRuleNum = 0; |
michael@0 | 97 | fNodeStack[0] = NULL; |
michael@0 | 98 | |
michael@0 | 99 | fSymbolTable = NULL; |
michael@0 | 100 | fSetTable = NULL; |
michael@0 | 101 | |
michael@0 | 102 | fScanIndex = 0; |
michael@0 | 103 | fNextIndex = 0; |
michael@0 | 104 | |
michael@0 | 105 | fReverseRule = FALSE; |
michael@0 | 106 | fLookAheadRule = FALSE; |
michael@0 | 107 | |
michael@0 | 108 | fLineNum = 1; |
michael@0 | 109 | fCharNum = 0; |
michael@0 | 110 | fQuoteMode = FALSE; |
michael@0 | 111 | |
michael@0 | 112 | // Do not check status until after all critical fields are sufficiently initialized |
michael@0 | 113 | // that the destructor can run cleanly. |
michael@0 | 114 | if (U_FAILURE(*rb->fStatus)) { |
michael@0 | 115 | return; |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | // |
michael@0 | 119 | // Set up the constant Unicode Sets. |
michael@0 | 120 | // Note: These could be made static, lazily initialized, and shared among |
michael@0 | 121 | // all instances of RBBIRuleScanners. BUT this is quite a bit simpler, |
michael@0 | 122 | // and the time to build these few sets should be small compared to a |
michael@0 | 123 | // full break iterator build. |
michael@0 | 124 | fRuleSets[kRuleSet_rule_char-128] |
michael@0 | 125 | = UnicodeSet(UnicodeString(gRuleSet_rule_char_pattern), *rb->fStatus); |
michael@0 | 126 | // fRuleSets[kRuleSet_white_space-128] = [:Pattern_White_Space:] |
michael@0 | 127 | fRuleSets[kRuleSet_white_space-128]. |
michael@0 | 128 | add(9, 0xd).add(0x20).add(0x85).add(0x200e, 0x200f).add(0x2028, 0x2029); |
michael@0 | 129 | fRuleSets[kRuleSet_name_char-128] |
michael@0 | 130 | = UnicodeSet(UnicodeString(gRuleSet_name_char_pattern), *rb->fStatus); |
michael@0 | 131 | fRuleSets[kRuleSet_name_start_char-128] |
michael@0 | 132 | = UnicodeSet(UnicodeString(gRuleSet_name_start_char_pattern), *rb->fStatus); |
michael@0 | 133 | fRuleSets[kRuleSet_digit_char-128] |
michael@0 | 134 | = UnicodeSet(UnicodeString(gRuleSet_digit_char_pattern), *rb->fStatus); |
michael@0 | 135 | if (*rb->fStatus == U_ILLEGAL_ARGUMENT_ERROR) { |
michael@0 | 136 | // This case happens if ICU's data is missing. UnicodeSet tries to look up property |
michael@0 | 137 | // names from the init string, can't find them, and claims an illegal argument. |
michael@0 | 138 | // Change the error so that the actual problem will be clearer to users. |
michael@0 | 139 | *rb->fStatus = U_BRK_INIT_ERROR; |
michael@0 | 140 | } |
michael@0 | 141 | if (U_FAILURE(*rb->fStatus)) { |
michael@0 | 142 | return; |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | fSymbolTable = new RBBISymbolTable(this, rb->fRules, *rb->fStatus); |
michael@0 | 146 | if (fSymbolTable == NULL) { |
michael@0 | 147 | *rb->fStatus = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 148 | return; |
michael@0 | 149 | } |
michael@0 | 150 | fSetTable = uhash_open(uhash_hashUnicodeString, uhash_compareUnicodeString, NULL, rb->fStatus); |
michael@0 | 151 | if (U_FAILURE(*rb->fStatus)) { |
michael@0 | 152 | return; |
michael@0 | 153 | } |
michael@0 | 154 | uhash_setValueDeleter(fSetTable, RBBISetTable_deleter); |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | |
michael@0 | 158 | |
michael@0 | 159 | //------------------------------------------------------------------------------ |
michael@0 | 160 | // |
michael@0 | 161 | // Destructor |
michael@0 | 162 | // |
michael@0 | 163 | //------------------------------------------------------------------------------ |
michael@0 | 164 | RBBIRuleScanner::~RBBIRuleScanner() { |
michael@0 | 165 | delete fSymbolTable; |
michael@0 | 166 | if (fSetTable != NULL) { |
michael@0 | 167 | uhash_close(fSetTable); |
michael@0 | 168 | fSetTable = NULL; |
michael@0 | 169 | |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | |
michael@0 | 173 | // Node Stack. |
michael@0 | 174 | // Normally has one entry, which is the entire parse tree for the rules. |
michael@0 | 175 | // If errors occured, there may be additional subtrees left on the stack. |
michael@0 | 176 | while (fNodeStackPtr > 0) { |
michael@0 | 177 | delete fNodeStack[fNodeStackPtr]; |
michael@0 | 178 | fNodeStackPtr--; |
michael@0 | 179 | } |
michael@0 | 180 | |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | //------------------------------------------------------------------------------ |
michael@0 | 184 | // |
michael@0 | 185 | // doParseAction Do some action during rule parsing. |
michael@0 | 186 | // Called by the parse state machine. |
michael@0 | 187 | // Actions build the parse tree and Unicode Sets, |
michael@0 | 188 | // and maintain the parse stack for nested expressions. |
michael@0 | 189 | // |
michael@0 | 190 | // TODO: unify EParseAction and RBBI_RuleParseAction enum types. |
michael@0 | 191 | // They represent exactly the same thing. They're separate |
michael@0 | 192 | // only to work around enum forward declaration restrictions |
michael@0 | 193 | // in some compilers, while at the same time avoiding multiple |
michael@0 | 194 | // definitions problems. I'm sure that there's a better way. |
michael@0 | 195 | // |
michael@0 | 196 | //------------------------------------------------------------------------------ |
michael@0 | 197 | UBool RBBIRuleScanner::doParseActions(int32_t action) |
michael@0 | 198 | { |
michael@0 | 199 | RBBINode *n = NULL; |
michael@0 | 200 | |
michael@0 | 201 | UBool returnVal = TRUE; |
michael@0 | 202 | |
michael@0 | 203 | switch (action) { |
michael@0 | 204 | |
michael@0 | 205 | case doExprStart: |
michael@0 | 206 | pushNewNode(RBBINode::opStart); |
michael@0 | 207 | fRuleNum++; |
michael@0 | 208 | break; |
michael@0 | 209 | |
michael@0 | 210 | |
michael@0 | 211 | case doExprOrOperator: |
michael@0 | 212 | { |
michael@0 | 213 | fixOpStack(RBBINode::precOpCat); |
michael@0 | 214 | RBBINode *operandNode = fNodeStack[fNodeStackPtr--]; |
michael@0 | 215 | RBBINode *orNode = pushNewNode(RBBINode::opOr); |
michael@0 | 216 | orNode->fLeftChild = operandNode; |
michael@0 | 217 | operandNode->fParent = orNode; |
michael@0 | 218 | } |
michael@0 | 219 | break; |
michael@0 | 220 | |
michael@0 | 221 | case doExprCatOperator: |
michael@0 | 222 | // concatenation operator. |
michael@0 | 223 | // For the implicit concatenation of adjacent terms in an expression that are |
michael@0 | 224 | // not separated by any other operator. Action is invoked between the |
michael@0 | 225 | // actions for the two terms. |
michael@0 | 226 | { |
michael@0 | 227 | fixOpStack(RBBINode::precOpCat); |
michael@0 | 228 | RBBINode *operandNode = fNodeStack[fNodeStackPtr--]; |
michael@0 | 229 | RBBINode *catNode = pushNewNode(RBBINode::opCat); |
michael@0 | 230 | catNode->fLeftChild = operandNode; |
michael@0 | 231 | operandNode->fParent = catNode; |
michael@0 | 232 | } |
michael@0 | 233 | break; |
michael@0 | 234 | |
michael@0 | 235 | case doLParen: |
michael@0 | 236 | // Open Paren. |
michael@0 | 237 | // The openParen node is a dummy operation type with a low precedence, |
michael@0 | 238 | // which has the affect of ensuring that any real binary op that |
michael@0 | 239 | // follows within the parens binds more tightly to the operands than |
michael@0 | 240 | // stuff outside of the parens. |
michael@0 | 241 | pushNewNode(RBBINode::opLParen); |
michael@0 | 242 | break; |
michael@0 | 243 | |
michael@0 | 244 | case doExprRParen: |
michael@0 | 245 | fixOpStack(RBBINode::precLParen); |
michael@0 | 246 | break; |
michael@0 | 247 | |
michael@0 | 248 | case doNOP: |
michael@0 | 249 | break; |
michael@0 | 250 | |
michael@0 | 251 | case doStartAssign: |
michael@0 | 252 | // We've just scanned "$variable = " |
michael@0 | 253 | // The top of the node stack has the $variable ref node. |
michael@0 | 254 | |
michael@0 | 255 | // Save the start position of the RHS text in the StartExpression node |
michael@0 | 256 | // that precedes the $variableReference node on the stack. |
michael@0 | 257 | // This will eventually be used when saving the full $variable replacement |
michael@0 | 258 | // text as a string. |
michael@0 | 259 | n = fNodeStack[fNodeStackPtr-1]; |
michael@0 | 260 | n->fFirstPos = fNextIndex; // move past the '=' |
michael@0 | 261 | |
michael@0 | 262 | // Push a new start-of-expression node; needed to keep parse of the |
michael@0 | 263 | // RHS expression happy. |
michael@0 | 264 | pushNewNode(RBBINode::opStart); |
michael@0 | 265 | break; |
michael@0 | 266 | |
michael@0 | 267 | |
michael@0 | 268 | |
michael@0 | 269 | |
michael@0 | 270 | case doEndAssign: |
michael@0 | 271 | { |
michael@0 | 272 | // We have reached the end of an assignement statement. |
michael@0 | 273 | // Current scan char is the ';' that terminates the assignment. |
michael@0 | 274 | |
michael@0 | 275 | // Terminate expression, leaves expression parse tree rooted in TOS node. |
michael@0 | 276 | fixOpStack(RBBINode::precStart); |
michael@0 | 277 | |
michael@0 | 278 | RBBINode *startExprNode = fNodeStack[fNodeStackPtr-2]; |
michael@0 | 279 | RBBINode *varRefNode = fNodeStack[fNodeStackPtr-1]; |
michael@0 | 280 | RBBINode *RHSExprNode = fNodeStack[fNodeStackPtr]; |
michael@0 | 281 | |
michael@0 | 282 | // Save original text of right side of assignment, excluding the terminating ';' |
michael@0 | 283 | // in the root of the node for the right-hand-side expression. |
michael@0 | 284 | RHSExprNode->fFirstPos = startExprNode->fFirstPos; |
michael@0 | 285 | RHSExprNode->fLastPos = fScanIndex; |
michael@0 | 286 | fRB->fRules.extractBetween(RHSExprNode->fFirstPos, RHSExprNode->fLastPos, RHSExprNode->fText); |
michael@0 | 287 | |
michael@0 | 288 | // Expression parse tree becomes l. child of the $variable reference node. |
michael@0 | 289 | varRefNode->fLeftChild = RHSExprNode; |
michael@0 | 290 | RHSExprNode->fParent = varRefNode; |
michael@0 | 291 | |
michael@0 | 292 | // Make a symbol table entry for the $variableRef node. |
michael@0 | 293 | fSymbolTable->addEntry(varRefNode->fText, varRefNode, *fRB->fStatus); |
michael@0 | 294 | if (U_FAILURE(*fRB->fStatus)) { |
michael@0 | 295 | // This is a round-about way to get the parse position set |
michael@0 | 296 | // so that duplicate symbols error messages include a line number. |
michael@0 | 297 | UErrorCode t = *fRB->fStatus; |
michael@0 | 298 | *fRB->fStatus = U_ZERO_ERROR; |
michael@0 | 299 | error(t); |
michael@0 | 300 | } |
michael@0 | 301 | |
michael@0 | 302 | // Clean up the stack. |
michael@0 | 303 | delete startExprNode; |
michael@0 | 304 | fNodeStackPtr-=3; |
michael@0 | 305 | break; |
michael@0 | 306 | } |
michael@0 | 307 | |
michael@0 | 308 | case doEndOfRule: |
michael@0 | 309 | { |
michael@0 | 310 | fixOpStack(RBBINode::precStart); // Terminate expression, leaves expression |
michael@0 | 311 | if (U_FAILURE(*fRB->fStatus)) { // parse tree rooted in TOS node. |
michael@0 | 312 | break; |
michael@0 | 313 | } |
michael@0 | 314 | #ifdef RBBI_DEBUG |
michael@0 | 315 | if (fRB->fDebugEnv && uprv_strstr(fRB->fDebugEnv, "rtree")) {printNodeStack("end of rule");} |
michael@0 | 316 | #endif |
michael@0 | 317 | U_ASSERT(fNodeStackPtr == 1); |
michael@0 | 318 | |
michael@0 | 319 | // If this rule includes a look-ahead '/', add a endMark node to the |
michael@0 | 320 | // expression tree. |
michael@0 | 321 | if (fLookAheadRule) { |
michael@0 | 322 | RBBINode *thisRule = fNodeStack[fNodeStackPtr]; |
michael@0 | 323 | RBBINode *endNode = pushNewNode(RBBINode::endMark); |
michael@0 | 324 | RBBINode *catNode = pushNewNode(RBBINode::opCat); |
michael@0 | 325 | fNodeStackPtr -= 2; |
michael@0 | 326 | catNode->fLeftChild = thisRule; |
michael@0 | 327 | catNode->fRightChild = endNode; |
michael@0 | 328 | fNodeStack[fNodeStackPtr] = catNode; |
michael@0 | 329 | endNode->fVal = fRuleNum; |
michael@0 | 330 | endNode->fLookAheadEnd = TRUE; |
michael@0 | 331 | } |
michael@0 | 332 | |
michael@0 | 333 | // All rule expressions are ORed together. |
michael@0 | 334 | // The ';' that terminates an expression really just functions as a '|' with |
michael@0 | 335 | // a low operator prededence. |
michael@0 | 336 | // |
michael@0 | 337 | // Each of the four sets of rules are collected separately. |
michael@0 | 338 | // (forward, reverse, safe_forward, safe_reverse) |
michael@0 | 339 | // OR this rule into the appropriate group of them. |
michael@0 | 340 | // |
michael@0 | 341 | RBBINode **destRules = (fReverseRule? &fRB->fReverseTree : fRB->fDefaultTree); |
michael@0 | 342 | |
michael@0 | 343 | if (*destRules != NULL) { |
michael@0 | 344 | // This is not the first rule encounted. |
michael@0 | 345 | // OR previous stuff (from *destRules) |
michael@0 | 346 | // with the current rule expression (on the Node Stack) |
michael@0 | 347 | // with the resulting OR expression going to *destRules |
michael@0 | 348 | // |
michael@0 | 349 | RBBINode *thisRule = fNodeStack[fNodeStackPtr]; |
michael@0 | 350 | RBBINode *prevRules = *destRules; |
michael@0 | 351 | RBBINode *orNode = pushNewNode(RBBINode::opOr); |
michael@0 | 352 | orNode->fLeftChild = prevRules; |
michael@0 | 353 | prevRules->fParent = orNode; |
michael@0 | 354 | orNode->fRightChild = thisRule; |
michael@0 | 355 | thisRule->fParent = orNode; |
michael@0 | 356 | *destRules = orNode; |
michael@0 | 357 | } |
michael@0 | 358 | else |
michael@0 | 359 | { |
michael@0 | 360 | // This is the first rule encountered (for this direction). |
michael@0 | 361 | // Just move its parse tree from the stack to *destRules. |
michael@0 | 362 | *destRules = fNodeStack[fNodeStackPtr]; |
michael@0 | 363 | } |
michael@0 | 364 | fReverseRule = FALSE; // in preparation for the next rule. |
michael@0 | 365 | fLookAheadRule = FALSE; |
michael@0 | 366 | fNodeStackPtr = 0; |
michael@0 | 367 | } |
michael@0 | 368 | break; |
michael@0 | 369 | |
michael@0 | 370 | |
michael@0 | 371 | case doRuleError: |
michael@0 | 372 | error(U_BRK_RULE_SYNTAX); |
michael@0 | 373 | returnVal = FALSE; |
michael@0 | 374 | break; |
michael@0 | 375 | |
michael@0 | 376 | |
michael@0 | 377 | case doVariableNameExpectedErr: |
michael@0 | 378 | error(U_BRK_RULE_SYNTAX); |
michael@0 | 379 | break; |
michael@0 | 380 | |
michael@0 | 381 | |
michael@0 | 382 | // |
michael@0 | 383 | // Unary operands + ? * |
michael@0 | 384 | // These all appear after the operand to which they apply. |
michael@0 | 385 | // When we hit one, the operand (may be a whole sub expression) |
michael@0 | 386 | // will be on the top of the stack. |
michael@0 | 387 | // Unary Operator becomes TOS, with the old TOS as its one child. |
michael@0 | 388 | case doUnaryOpPlus: |
michael@0 | 389 | { |
michael@0 | 390 | RBBINode *operandNode = fNodeStack[fNodeStackPtr--]; |
michael@0 | 391 | RBBINode *plusNode = pushNewNode(RBBINode::opPlus); |
michael@0 | 392 | plusNode->fLeftChild = operandNode; |
michael@0 | 393 | operandNode->fParent = plusNode; |
michael@0 | 394 | } |
michael@0 | 395 | break; |
michael@0 | 396 | |
michael@0 | 397 | case doUnaryOpQuestion: |
michael@0 | 398 | { |
michael@0 | 399 | RBBINode *operandNode = fNodeStack[fNodeStackPtr--]; |
michael@0 | 400 | RBBINode *qNode = pushNewNode(RBBINode::opQuestion); |
michael@0 | 401 | qNode->fLeftChild = operandNode; |
michael@0 | 402 | operandNode->fParent = qNode; |
michael@0 | 403 | } |
michael@0 | 404 | break; |
michael@0 | 405 | |
michael@0 | 406 | case doUnaryOpStar: |
michael@0 | 407 | { |
michael@0 | 408 | RBBINode *operandNode = fNodeStack[fNodeStackPtr--]; |
michael@0 | 409 | RBBINode *starNode = pushNewNode(RBBINode::opStar); |
michael@0 | 410 | starNode->fLeftChild = operandNode; |
michael@0 | 411 | operandNode->fParent = starNode; |
michael@0 | 412 | } |
michael@0 | 413 | break; |
michael@0 | 414 | |
michael@0 | 415 | case doRuleChar: |
michael@0 | 416 | // A "Rule Character" is any single character that is a literal part |
michael@0 | 417 | // of the regular expression. Like a, b and c in the expression "(abc*) | [:L:]" |
michael@0 | 418 | // These are pretty uncommon in break rules; the terms are more commonly |
michael@0 | 419 | // sets. To keep things uniform, treat these characters like as |
michael@0 | 420 | // sets that just happen to contain only one character. |
michael@0 | 421 | { |
michael@0 | 422 | n = pushNewNode(RBBINode::setRef); |
michael@0 | 423 | findSetFor(UnicodeString(fC.fChar), n); |
michael@0 | 424 | n->fFirstPos = fScanIndex; |
michael@0 | 425 | n->fLastPos = fNextIndex; |
michael@0 | 426 | fRB->fRules.extractBetween(n->fFirstPos, n->fLastPos, n->fText); |
michael@0 | 427 | break; |
michael@0 | 428 | } |
michael@0 | 429 | |
michael@0 | 430 | case doDotAny: |
michael@0 | 431 | // scanned a ".", meaning match any single character. |
michael@0 | 432 | { |
michael@0 | 433 | n = pushNewNode(RBBINode::setRef); |
michael@0 | 434 | findSetFor(UnicodeString(TRUE, kAny, 3), n); |
michael@0 | 435 | n->fFirstPos = fScanIndex; |
michael@0 | 436 | n->fLastPos = fNextIndex; |
michael@0 | 437 | fRB->fRules.extractBetween(n->fFirstPos, n->fLastPos, n->fText); |
michael@0 | 438 | break; |
michael@0 | 439 | } |
michael@0 | 440 | |
michael@0 | 441 | case doSlash: |
michael@0 | 442 | // Scanned a '/', which identifies a look-ahead break position in a rule. |
michael@0 | 443 | n = pushNewNode(RBBINode::lookAhead); |
michael@0 | 444 | n->fVal = fRuleNum; |
michael@0 | 445 | n->fFirstPos = fScanIndex; |
michael@0 | 446 | n->fLastPos = fNextIndex; |
michael@0 | 447 | fRB->fRules.extractBetween(n->fFirstPos, n->fLastPos, n->fText); |
michael@0 | 448 | fLookAheadRule = TRUE; |
michael@0 | 449 | break; |
michael@0 | 450 | |
michael@0 | 451 | |
michael@0 | 452 | case doStartTagValue: |
michael@0 | 453 | // Scanned a '{', the opening delimiter for a tag value within a rule. |
michael@0 | 454 | n = pushNewNode(RBBINode::tag); |
michael@0 | 455 | n->fVal = 0; |
michael@0 | 456 | n->fFirstPos = fScanIndex; |
michael@0 | 457 | n->fLastPos = fNextIndex; |
michael@0 | 458 | break; |
michael@0 | 459 | |
michael@0 | 460 | case doTagDigit: |
michael@0 | 461 | // Just scanned a decimal digit that's part of a tag value |
michael@0 | 462 | { |
michael@0 | 463 | n = fNodeStack[fNodeStackPtr]; |
michael@0 | 464 | uint32_t v = u_charDigitValue(fC.fChar); |
michael@0 | 465 | U_ASSERT(v < 10); |
michael@0 | 466 | n->fVal = n->fVal*10 + v; |
michael@0 | 467 | break; |
michael@0 | 468 | } |
michael@0 | 469 | |
michael@0 | 470 | case doTagValue: |
michael@0 | 471 | n = fNodeStack[fNodeStackPtr]; |
michael@0 | 472 | n->fLastPos = fNextIndex; |
michael@0 | 473 | fRB->fRules.extractBetween(n->fFirstPos, n->fLastPos, n->fText); |
michael@0 | 474 | break; |
michael@0 | 475 | |
michael@0 | 476 | case doTagExpectedError: |
michael@0 | 477 | error(U_BRK_MALFORMED_RULE_TAG); |
michael@0 | 478 | returnVal = FALSE; |
michael@0 | 479 | break; |
michael@0 | 480 | |
michael@0 | 481 | case doOptionStart: |
michael@0 | 482 | // Scanning a !!option. At the start of string. |
michael@0 | 483 | fOptionStart = fScanIndex; |
michael@0 | 484 | break; |
michael@0 | 485 | |
michael@0 | 486 | case doOptionEnd: |
michael@0 | 487 | { |
michael@0 | 488 | UnicodeString opt(fRB->fRules, fOptionStart, fScanIndex-fOptionStart); |
michael@0 | 489 | if (opt == UNICODE_STRING("chain", 5)) { |
michael@0 | 490 | fRB->fChainRules = TRUE; |
michael@0 | 491 | } else if (opt == UNICODE_STRING("LBCMNoChain", 11)) { |
michael@0 | 492 | fRB->fLBCMNoChain = TRUE; |
michael@0 | 493 | } else if (opt == UNICODE_STRING("forward", 7)) { |
michael@0 | 494 | fRB->fDefaultTree = &fRB->fForwardTree; |
michael@0 | 495 | } else if (opt == UNICODE_STRING("reverse", 7)) { |
michael@0 | 496 | fRB->fDefaultTree = &fRB->fReverseTree; |
michael@0 | 497 | } else if (opt == UNICODE_STRING("safe_forward", 12)) { |
michael@0 | 498 | fRB->fDefaultTree = &fRB->fSafeFwdTree; |
michael@0 | 499 | } else if (opt == UNICODE_STRING("safe_reverse", 12)) { |
michael@0 | 500 | fRB->fDefaultTree = &fRB->fSafeRevTree; |
michael@0 | 501 | } else if (opt == UNICODE_STRING("lookAheadHardBreak", 18)) { |
michael@0 | 502 | fRB->fLookAheadHardBreak = TRUE; |
michael@0 | 503 | } else { |
michael@0 | 504 | error(U_BRK_UNRECOGNIZED_OPTION); |
michael@0 | 505 | } |
michael@0 | 506 | } |
michael@0 | 507 | break; |
michael@0 | 508 | |
michael@0 | 509 | case doReverseDir: |
michael@0 | 510 | fReverseRule = TRUE; |
michael@0 | 511 | break; |
michael@0 | 512 | |
michael@0 | 513 | case doStartVariableName: |
michael@0 | 514 | n = pushNewNode(RBBINode::varRef); |
michael@0 | 515 | if (U_FAILURE(*fRB->fStatus)) { |
michael@0 | 516 | break; |
michael@0 | 517 | } |
michael@0 | 518 | n->fFirstPos = fScanIndex; |
michael@0 | 519 | break; |
michael@0 | 520 | |
michael@0 | 521 | case doEndVariableName: |
michael@0 | 522 | n = fNodeStack[fNodeStackPtr]; |
michael@0 | 523 | if (n==NULL || n->fType != RBBINode::varRef) { |
michael@0 | 524 | error(U_BRK_INTERNAL_ERROR); |
michael@0 | 525 | break; |
michael@0 | 526 | } |
michael@0 | 527 | n->fLastPos = fScanIndex; |
michael@0 | 528 | fRB->fRules.extractBetween(n->fFirstPos+1, n->fLastPos, n->fText); |
michael@0 | 529 | // Look the newly scanned name up in the symbol table |
michael@0 | 530 | // If there's an entry, set the l. child of the var ref to the replacement expression. |
michael@0 | 531 | // (We also pass through here when scanning assignments, but no harm is done, other |
michael@0 | 532 | // than a slight wasted effort that seems hard to avoid. Lookup will be null) |
michael@0 | 533 | n->fLeftChild = fSymbolTable->lookupNode(n->fText); |
michael@0 | 534 | break; |
michael@0 | 535 | |
michael@0 | 536 | case doCheckVarDef: |
michael@0 | 537 | n = fNodeStack[fNodeStackPtr]; |
michael@0 | 538 | if (n->fLeftChild == NULL) { |
michael@0 | 539 | error(U_BRK_UNDEFINED_VARIABLE); |
michael@0 | 540 | returnVal = FALSE; |
michael@0 | 541 | } |
michael@0 | 542 | break; |
michael@0 | 543 | |
michael@0 | 544 | case doExprFinished: |
michael@0 | 545 | break; |
michael@0 | 546 | |
michael@0 | 547 | case doRuleErrorAssignExpr: |
michael@0 | 548 | error(U_BRK_ASSIGN_ERROR); |
michael@0 | 549 | returnVal = FALSE; |
michael@0 | 550 | break; |
michael@0 | 551 | |
michael@0 | 552 | case doExit: |
michael@0 | 553 | returnVal = FALSE; |
michael@0 | 554 | break; |
michael@0 | 555 | |
michael@0 | 556 | case doScanUnicodeSet: |
michael@0 | 557 | scanSet(); |
michael@0 | 558 | break; |
michael@0 | 559 | |
michael@0 | 560 | default: |
michael@0 | 561 | error(U_BRK_INTERNAL_ERROR); |
michael@0 | 562 | returnVal = FALSE; |
michael@0 | 563 | break; |
michael@0 | 564 | } |
michael@0 | 565 | return returnVal; |
michael@0 | 566 | } |
michael@0 | 567 | |
michael@0 | 568 | |
michael@0 | 569 | |
michael@0 | 570 | |
michael@0 | 571 | //------------------------------------------------------------------------------ |
michael@0 | 572 | // |
michael@0 | 573 | // Error Report a rule parse error. |
michael@0 | 574 | // Only report it if no previous error has been recorded. |
michael@0 | 575 | // |
michael@0 | 576 | //------------------------------------------------------------------------------ |
michael@0 | 577 | void RBBIRuleScanner::error(UErrorCode e) { |
michael@0 | 578 | if (U_SUCCESS(*fRB->fStatus)) { |
michael@0 | 579 | *fRB->fStatus = e; |
michael@0 | 580 | if (fRB->fParseError) { |
michael@0 | 581 | fRB->fParseError->line = fLineNum; |
michael@0 | 582 | fRB->fParseError->offset = fCharNum; |
michael@0 | 583 | fRB->fParseError->preContext[0] = 0; |
michael@0 | 584 | fRB->fParseError->preContext[0] = 0; |
michael@0 | 585 | } |
michael@0 | 586 | } |
michael@0 | 587 | } |
michael@0 | 588 | |
michael@0 | 589 | |
michael@0 | 590 | |
michael@0 | 591 | |
michael@0 | 592 | //------------------------------------------------------------------------------ |
michael@0 | 593 | // |
michael@0 | 594 | // fixOpStack The parse stack holds partially assembled chunks of the parse tree. |
michael@0 | 595 | // An entry on the stack may be as small as a single setRef node, |
michael@0 | 596 | // or as large as the parse tree |
michael@0 | 597 | // for an entire expression (this will be the one item left on the stack |
michael@0 | 598 | // when the parsing of an RBBI rule completes. |
michael@0 | 599 | // |
michael@0 | 600 | // This function is called when a binary operator is encountered. |
michael@0 | 601 | // It looks back up the stack for operators that are not yet associated |
michael@0 | 602 | // with a right operand, and if the precedence of the stacked operator >= |
michael@0 | 603 | // the precedence of the current operator, binds the operand left, |
michael@0 | 604 | // to the previously encountered operator. |
michael@0 | 605 | // |
michael@0 | 606 | //------------------------------------------------------------------------------ |
michael@0 | 607 | void RBBIRuleScanner::fixOpStack(RBBINode::OpPrecedence p) { |
michael@0 | 608 | RBBINode *n; |
michael@0 | 609 | // printNodeStack("entering fixOpStack()"); |
michael@0 | 610 | for (;;) { |
michael@0 | 611 | n = fNodeStack[fNodeStackPtr-1]; // an operator node |
michael@0 | 612 | if (n->fPrecedence == 0) { |
michael@0 | 613 | RBBIDebugPuts("RBBIRuleScanner::fixOpStack, bad operator node"); |
michael@0 | 614 | error(U_BRK_INTERNAL_ERROR); |
michael@0 | 615 | return; |
michael@0 | 616 | } |
michael@0 | 617 | |
michael@0 | 618 | if (n->fPrecedence < p || n->fPrecedence <= RBBINode::precLParen) { |
michael@0 | 619 | // The most recent operand goes with the current operator, |
michael@0 | 620 | // not with the previously stacked one. |
michael@0 | 621 | break; |
michael@0 | 622 | } |
michael@0 | 623 | // Stack operator is a binary op ( '|' or concatenation) |
michael@0 | 624 | // TOS operand becomes right child of this operator. |
michael@0 | 625 | // Resulting subexpression becomes the TOS operand. |
michael@0 | 626 | n->fRightChild = fNodeStack[fNodeStackPtr]; |
michael@0 | 627 | fNodeStack[fNodeStackPtr]->fParent = n; |
michael@0 | 628 | fNodeStackPtr--; |
michael@0 | 629 | // printNodeStack("looping in fixOpStack() "); |
michael@0 | 630 | } |
michael@0 | 631 | |
michael@0 | 632 | if (p <= RBBINode::precLParen) { |
michael@0 | 633 | // Scan is at a right paren or end of expression. |
michael@0 | 634 | // The scanned item must match the stack, or else there was an error. |
michael@0 | 635 | // Discard the left paren (or start expr) node from the stack, |
michael@0 | 636 | // leaving the completed (sub)expression as TOS. |
michael@0 | 637 | if (n->fPrecedence != p) { |
michael@0 | 638 | // Right paren encountered matched start of expression node, or |
michael@0 | 639 | // end of expression matched with a left paren node. |
michael@0 | 640 | error(U_BRK_MISMATCHED_PAREN); |
michael@0 | 641 | } |
michael@0 | 642 | fNodeStack[fNodeStackPtr-1] = fNodeStack[fNodeStackPtr]; |
michael@0 | 643 | fNodeStackPtr--; |
michael@0 | 644 | // Delete the now-discarded LParen or Start node. |
michael@0 | 645 | delete n; |
michael@0 | 646 | } |
michael@0 | 647 | // printNodeStack("leaving fixOpStack()"); |
michael@0 | 648 | } |
michael@0 | 649 | |
michael@0 | 650 | |
michael@0 | 651 | |
michael@0 | 652 | |
michael@0 | 653 | //------------------------------------------------------------------------------ |
michael@0 | 654 | // |
michael@0 | 655 | // findSetFor given a UnicodeString, |
michael@0 | 656 | // - find the corresponding Unicode Set (uset node) |
michael@0 | 657 | // (create one if necessary) |
michael@0 | 658 | // - Set fLeftChild of the caller's node (should be a setRef node) |
michael@0 | 659 | // to the uset node |
michael@0 | 660 | // Maintain a hash table of uset nodes, so the same one is always used |
michael@0 | 661 | // for the same string. |
michael@0 | 662 | // If a "to adopt" set is provided and we haven't seen this key before, |
michael@0 | 663 | // add the provided set to the hash table. |
michael@0 | 664 | // If the string is one (32 bit) char in length, the set contains |
michael@0 | 665 | // just one element which is the char in question. |
michael@0 | 666 | // If the string is "any", return a set containing all chars. |
michael@0 | 667 | // |
michael@0 | 668 | //------------------------------------------------------------------------------ |
michael@0 | 669 | void RBBIRuleScanner::findSetFor(const UnicodeString &s, RBBINode *node, UnicodeSet *setToAdopt) { |
michael@0 | 670 | |
michael@0 | 671 | RBBISetTableEl *el; |
michael@0 | 672 | |
michael@0 | 673 | // First check whether we've already cached a set for this string. |
michael@0 | 674 | // If so, just use the cached set in the new node. |
michael@0 | 675 | // delete any set provided by the caller, since we own it. |
michael@0 | 676 | el = (RBBISetTableEl *)uhash_get(fSetTable, &s); |
michael@0 | 677 | if (el != NULL) { |
michael@0 | 678 | delete setToAdopt; |
michael@0 | 679 | node->fLeftChild = el->val; |
michael@0 | 680 | U_ASSERT(node->fLeftChild->fType == RBBINode::uset); |
michael@0 | 681 | return; |
michael@0 | 682 | } |
michael@0 | 683 | |
michael@0 | 684 | // Haven't seen this set before. |
michael@0 | 685 | // If the caller didn't provide us with a prebuilt set, |
michael@0 | 686 | // create a new UnicodeSet now. |
michael@0 | 687 | if (setToAdopt == NULL) { |
michael@0 | 688 | if (s.compare(kAny, -1) == 0) { |
michael@0 | 689 | setToAdopt = new UnicodeSet(0x000000, 0x10ffff); |
michael@0 | 690 | } else { |
michael@0 | 691 | UChar32 c; |
michael@0 | 692 | c = s.char32At(0); |
michael@0 | 693 | setToAdopt = new UnicodeSet(c, c); |
michael@0 | 694 | } |
michael@0 | 695 | } |
michael@0 | 696 | |
michael@0 | 697 | // |
michael@0 | 698 | // Make a new uset node to refer to this UnicodeSet |
michael@0 | 699 | // This new uset node becomes the child of the caller's setReference node. |
michael@0 | 700 | // |
michael@0 | 701 | RBBINode *usetNode = new RBBINode(RBBINode::uset); |
michael@0 | 702 | if (usetNode == NULL) { |
michael@0 | 703 | error(U_MEMORY_ALLOCATION_ERROR); |
michael@0 | 704 | return; |
michael@0 | 705 | } |
michael@0 | 706 | usetNode->fInputSet = setToAdopt; |
michael@0 | 707 | usetNode->fParent = node; |
michael@0 | 708 | node->fLeftChild = usetNode; |
michael@0 | 709 | usetNode->fText = s; |
michael@0 | 710 | |
michael@0 | 711 | |
michael@0 | 712 | // |
michael@0 | 713 | // Add the new uset node to the list of all uset nodes. |
michael@0 | 714 | // |
michael@0 | 715 | fRB->fUSetNodes->addElement(usetNode, *fRB->fStatus); |
michael@0 | 716 | |
michael@0 | 717 | |
michael@0 | 718 | // |
michael@0 | 719 | // Add the new set to the set hash table. |
michael@0 | 720 | // |
michael@0 | 721 | el = (RBBISetTableEl *)uprv_malloc(sizeof(RBBISetTableEl)); |
michael@0 | 722 | UnicodeString *tkey = new UnicodeString(s); |
michael@0 | 723 | if (tkey == NULL || el == NULL || setToAdopt == NULL) { |
michael@0 | 724 | // Delete to avoid memory leak |
michael@0 | 725 | delete tkey; |
michael@0 | 726 | tkey = NULL; |
michael@0 | 727 | uprv_free(el); |
michael@0 | 728 | el = NULL; |
michael@0 | 729 | delete setToAdopt; |
michael@0 | 730 | setToAdopt = NULL; |
michael@0 | 731 | |
michael@0 | 732 | error(U_MEMORY_ALLOCATION_ERROR); |
michael@0 | 733 | return; |
michael@0 | 734 | } |
michael@0 | 735 | el->key = tkey; |
michael@0 | 736 | el->val = usetNode; |
michael@0 | 737 | uhash_put(fSetTable, el->key, el, fRB->fStatus); |
michael@0 | 738 | |
michael@0 | 739 | return; |
michael@0 | 740 | } |
michael@0 | 741 | |
michael@0 | 742 | |
michael@0 | 743 | |
michael@0 | 744 | // |
michael@0 | 745 | // Assorted Unicode character constants. |
michael@0 | 746 | // Numeric because there is no portable way to enter them as literals. |
michael@0 | 747 | // (Think EBCDIC). |
michael@0 | 748 | // |
michael@0 | 749 | static const UChar chCR = 0x0d; // New lines, for terminating comments. |
michael@0 | 750 | static const UChar chLF = 0x0a; |
michael@0 | 751 | static const UChar chNEL = 0x85; // NEL newline variant |
michael@0 | 752 | static const UChar chLS = 0x2028; // Unicode Line Separator |
michael@0 | 753 | static const UChar chApos = 0x27; // single quote, for quoted chars. |
michael@0 | 754 | static const UChar chPound = 0x23; // '#', introduces a comment. |
michael@0 | 755 | static const UChar chBackSlash = 0x5c; // '\' introduces a char escape |
michael@0 | 756 | static const UChar chLParen = 0x28; |
michael@0 | 757 | static const UChar chRParen = 0x29; |
michael@0 | 758 | |
michael@0 | 759 | |
michael@0 | 760 | //------------------------------------------------------------------------------ |
michael@0 | 761 | // |
michael@0 | 762 | // stripRules Return a rules string without unnecessary |
michael@0 | 763 | // characters. |
michael@0 | 764 | // |
michael@0 | 765 | //------------------------------------------------------------------------------ |
michael@0 | 766 | UnicodeString RBBIRuleScanner::stripRules(const UnicodeString &rules) { |
michael@0 | 767 | UnicodeString strippedRules; |
michael@0 | 768 | int rulesLength = rules.length(); |
michael@0 | 769 | for (int idx = 0; idx < rulesLength; ) { |
michael@0 | 770 | UChar ch = rules[idx++]; |
michael@0 | 771 | if (ch == chPound) { |
michael@0 | 772 | while (idx < rulesLength |
michael@0 | 773 | && ch != chCR && ch != chLF && ch != chNEL) |
michael@0 | 774 | { |
michael@0 | 775 | ch = rules[idx++]; |
michael@0 | 776 | } |
michael@0 | 777 | } |
michael@0 | 778 | if (!u_isISOControl(ch)) { |
michael@0 | 779 | strippedRules.append(ch); |
michael@0 | 780 | } |
michael@0 | 781 | } |
michael@0 | 782 | // strippedRules = strippedRules.unescape(); |
michael@0 | 783 | return strippedRules; |
michael@0 | 784 | } |
michael@0 | 785 | |
michael@0 | 786 | |
michael@0 | 787 | //------------------------------------------------------------------------------ |
michael@0 | 788 | // |
michael@0 | 789 | // nextCharLL Low Level Next Char from rule input source. |
michael@0 | 790 | // Get a char from the input character iterator, |
michael@0 | 791 | // keep track of input position for error reporting. |
michael@0 | 792 | // |
michael@0 | 793 | //------------------------------------------------------------------------------ |
michael@0 | 794 | UChar32 RBBIRuleScanner::nextCharLL() { |
michael@0 | 795 | UChar32 ch; |
michael@0 | 796 | |
michael@0 | 797 | if (fNextIndex >= fRB->fRules.length()) { |
michael@0 | 798 | return (UChar32)-1; |
michael@0 | 799 | } |
michael@0 | 800 | ch = fRB->fRules.char32At(fNextIndex); |
michael@0 | 801 | fNextIndex = fRB->fRules.moveIndex32(fNextIndex, 1); |
michael@0 | 802 | |
michael@0 | 803 | if (ch == chCR || |
michael@0 | 804 | ch == chNEL || |
michael@0 | 805 | ch == chLS || |
michael@0 | 806 | (ch == chLF && fLastChar != chCR)) { |
michael@0 | 807 | // Character is starting a new line. Bump up the line number, and |
michael@0 | 808 | // reset the column to 0. |
michael@0 | 809 | fLineNum++; |
michael@0 | 810 | fCharNum=0; |
michael@0 | 811 | if (fQuoteMode) { |
michael@0 | 812 | error(U_BRK_NEW_LINE_IN_QUOTED_STRING); |
michael@0 | 813 | fQuoteMode = FALSE; |
michael@0 | 814 | } |
michael@0 | 815 | } |
michael@0 | 816 | else { |
michael@0 | 817 | // Character is not starting a new line. Except in the case of a |
michael@0 | 818 | // LF following a CR, increment the column position. |
michael@0 | 819 | if (ch != chLF) { |
michael@0 | 820 | fCharNum++; |
michael@0 | 821 | } |
michael@0 | 822 | } |
michael@0 | 823 | fLastChar = ch; |
michael@0 | 824 | return ch; |
michael@0 | 825 | } |
michael@0 | 826 | |
michael@0 | 827 | |
michael@0 | 828 | //------------------------------------------------------------------------------ |
michael@0 | 829 | // |
michael@0 | 830 | // nextChar for rules scanning. At this level, we handle stripping |
michael@0 | 831 | // out comments and processing backslash character escapes. |
michael@0 | 832 | // The rest of the rules grammar is handled at the next level up. |
michael@0 | 833 | // |
michael@0 | 834 | //------------------------------------------------------------------------------ |
michael@0 | 835 | void RBBIRuleScanner::nextChar(RBBIRuleChar &c) { |
michael@0 | 836 | |
michael@0 | 837 | // Unicode Character constants needed for the processing done by nextChar(), |
michael@0 | 838 | // in hex because literals wont work on EBCDIC machines. |
michael@0 | 839 | |
michael@0 | 840 | fScanIndex = fNextIndex; |
michael@0 | 841 | c.fChar = nextCharLL(); |
michael@0 | 842 | c.fEscaped = FALSE; |
michael@0 | 843 | |
michael@0 | 844 | // |
michael@0 | 845 | // check for '' sequence. |
michael@0 | 846 | // These are recognized in all contexts, whether in quoted text or not. |
michael@0 | 847 | // |
michael@0 | 848 | if (c.fChar == chApos) { |
michael@0 | 849 | if (fRB->fRules.char32At(fNextIndex) == chApos) { |
michael@0 | 850 | c.fChar = nextCharLL(); // get nextChar officially so character counts |
michael@0 | 851 | c.fEscaped = TRUE; // stay correct. |
michael@0 | 852 | } |
michael@0 | 853 | else |
michael@0 | 854 | { |
michael@0 | 855 | // Single quote, by itself. |
michael@0 | 856 | // Toggle quoting mode. |
michael@0 | 857 | // Return either '(' or ')', because quotes cause a grouping of the quoted text. |
michael@0 | 858 | fQuoteMode = !fQuoteMode; |
michael@0 | 859 | if (fQuoteMode == TRUE) { |
michael@0 | 860 | c.fChar = chLParen; |
michael@0 | 861 | } else { |
michael@0 | 862 | c.fChar = chRParen; |
michael@0 | 863 | } |
michael@0 | 864 | c.fEscaped = FALSE; // The paren that we return is not escaped. |
michael@0 | 865 | return; |
michael@0 | 866 | } |
michael@0 | 867 | } |
michael@0 | 868 | |
michael@0 | 869 | if (fQuoteMode) { |
michael@0 | 870 | c.fEscaped = TRUE; |
michael@0 | 871 | } |
michael@0 | 872 | else |
michael@0 | 873 | { |
michael@0 | 874 | // We are not in a 'quoted region' of the source. |
michael@0 | 875 | // |
michael@0 | 876 | if (c.fChar == chPound) { |
michael@0 | 877 | // Start of a comment. Consume the rest of it. |
michael@0 | 878 | // The new-line char that terminates the comment is always returned. |
michael@0 | 879 | // It will be treated as white-space, and serves to break up anything |
michael@0 | 880 | // that might otherwise incorrectly clump together with a comment in |
michael@0 | 881 | // the middle (a variable name, for example.) |
michael@0 | 882 | for (;;) { |
michael@0 | 883 | c.fChar = nextCharLL(); |
michael@0 | 884 | if (c.fChar == (UChar32)-1 || // EOF |
michael@0 | 885 | c.fChar == chCR || |
michael@0 | 886 | c.fChar == chLF || |
michael@0 | 887 | c.fChar == chNEL || |
michael@0 | 888 | c.fChar == chLS) {break;} |
michael@0 | 889 | } |
michael@0 | 890 | } |
michael@0 | 891 | if (c.fChar == (UChar32)-1) { |
michael@0 | 892 | return; |
michael@0 | 893 | } |
michael@0 | 894 | |
michael@0 | 895 | // |
michael@0 | 896 | // check for backslash escaped characters. |
michael@0 | 897 | // Use UnicodeString::unescapeAt() to handle them. |
michael@0 | 898 | // |
michael@0 | 899 | if (c.fChar == chBackSlash) { |
michael@0 | 900 | c.fEscaped = TRUE; |
michael@0 | 901 | int32_t startX = fNextIndex; |
michael@0 | 902 | c.fChar = fRB->fRules.unescapeAt(fNextIndex); |
michael@0 | 903 | if (fNextIndex == startX) { |
michael@0 | 904 | error(U_BRK_HEX_DIGITS_EXPECTED); |
michael@0 | 905 | } |
michael@0 | 906 | fCharNum += fNextIndex-startX; |
michael@0 | 907 | } |
michael@0 | 908 | } |
michael@0 | 909 | // putc(c.fChar, stdout); |
michael@0 | 910 | } |
michael@0 | 911 | |
michael@0 | 912 | //------------------------------------------------------------------------------ |
michael@0 | 913 | // |
michael@0 | 914 | // Parse RBBI rules. The state machine for rules parsing is here. |
michael@0 | 915 | // The state tables are hand-written in the file rbbirpt.txt, |
michael@0 | 916 | // and converted to the form used here by a perl |
michael@0 | 917 | // script rbbicst.pl |
michael@0 | 918 | // |
michael@0 | 919 | //------------------------------------------------------------------------------ |
michael@0 | 920 | void RBBIRuleScanner::parse() { |
michael@0 | 921 | uint16_t state; |
michael@0 | 922 | const RBBIRuleTableEl *tableEl; |
michael@0 | 923 | |
michael@0 | 924 | if (U_FAILURE(*fRB->fStatus)) { |
michael@0 | 925 | return; |
michael@0 | 926 | } |
michael@0 | 927 | |
michael@0 | 928 | state = 1; |
michael@0 | 929 | nextChar(fC); |
michael@0 | 930 | // |
michael@0 | 931 | // Main loop for the rule parsing state machine. |
michael@0 | 932 | // Runs once per state transition. |
michael@0 | 933 | // Each time through optionally performs, depending on the state table, |
michael@0 | 934 | // - an advance to the the next input char |
michael@0 | 935 | // - an action to be performed. |
michael@0 | 936 | // - pushing or popping a state to/from the local state return stack. |
michael@0 | 937 | // |
michael@0 | 938 | for (;;) { |
michael@0 | 939 | // Bail out if anything has gone wrong. |
michael@0 | 940 | // RBBI rule file parsing stops on the first error encountered. |
michael@0 | 941 | if (U_FAILURE(*fRB->fStatus)) { |
michael@0 | 942 | break; |
michael@0 | 943 | } |
michael@0 | 944 | |
michael@0 | 945 | // Quit if state == 0. This is the normal way to exit the state machine. |
michael@0 | 946 | // |
michael@0 | 947 | if (state == 0) { |
michael@0 | 948 | break; |
michael@0 | 949 | } |
michael@0 | 950 | |
michael@0 | 951 | // Find the state table element that matches the input char from the rule, or the |
michael@0 | 952 | // class of the input character. Start with the first table row for this |
michael@0 | 953 | // state, then linearly scan forward until we find a row that matches the |
michael@0 | 954 | // character. The last row for each state always matches all characters, so |
michael@0 | 955 | // the search will stop there, if not before. |
michael@0 | 956 | // |
michael@0 | 957 | tableEl = &gRuleParseStateTable[state]; |
michael@0 | 958 | #ifdef RBBI_DEBUG |
michael@0 | 959 | if (fRB->fDebugEnv && uprv_strstr(fRB->fDebugEnv, "scan")) { |
michael@0 | 960 | RBBIDebugPrintf("char, line, col = (\'%c\', %d, %d) state=%s ", |
michael@0 | 961 | fC.fChar, fLineNum, fCharNum, RBBIRuleStateNames[state]); |
michael@0 | 962 | } |
michael@0 | 963 | #endif |
michael@0 | 964 | |
michael@0 | 965 | for (;;) { |
michael@0 | 966 | #ifdef RBBI_DEBUG |
michael@0 | 967 | if (fRB->fDebugEnv && uprv_strstr(fRB->fDebugEnv, "scan")) { RBBIDebugPrintf(".");} |
michael@0 | 968 | #endif |
michael@0 | 969 | if (tableEl->fCharClass < 127 && fC.fEscaped == FALSE && tableEl->fCharClass == fC.fChar) { |
michael@0 | 970 | // Table row specified an individual character, not a set, and |
michael@0 | 971 | // the input character is not escaped, and |
michael@0 | 972 | // the input character matched it. |
michael@0 | 973 | break; |
michael@0 | 974 | } |
michael@0 | 975 | if (tableEl->fCharClass == 255) { |
michael@0 | 976 | // Table row specified default, match anything character class. |
michael@0 | 977 | break; |
michael@0 | 978 | } |
michael@0 | 979 | if (tableEl->fCharClass == 254 && fC.fEscaped) { |
michael@0 | 980 | // Table row specified "escaped" and the char was escaped. |
michael@0 | 981 | break; |
michael@0 | 982 | } |
michael@0 | 983 | if (tableEl->fCharClass == 253 && fC.fEscaped && |
michael@0 | 984 | (fC.fChar == 0x50 || fC.fChar == 0x70 )) { |
michael@0 | 985 | // Table row specified "escaped P" and the char is either 'p' or 'P'. |
michael@0 | 986 | break; |
michael@0 | 987 | } |
michael@0 | 988 | if (tableEl->fCharClass == 252 && fC.fChar == (UChar32)-1) { |
michael@0 | 989 | // Table row specified eof and we hit eof on the input. |
michael@0 | 990 | break; |
michael@0 | 991 | } |
michael@0 | 992 | |
michael@0 | 993 | if (tableEl->fCharClass >= 128 && tableEl->fCharClass < 240 && // Table specs a char class && |
michael@0 | 994 | fC.fEscaped == FALSE && // char is not escaped && |
michael@0 | 995 | fC.fChar != (UChar32)-1) { // char is not EOF |
michael@0 | 996 | U_ASSERT((tableEl->fCharClass-128) < LENGTHOF(fRuleSets)); |
michael@0 | 997 | if (fRuleSets[tableEl->fCharClass-128].contains(fC.fChar)) { |
michael@0 | 998 | // Table row specified a character class, or set of characters, |
michael@0 | 999 | // and the current char matches it. |
michael@0 | 1000 | break; |
michael@0 | 1001 | } |
michael@0 | 1002 | } |
michael@0 | 1003 | |
michael@0 | 1004 | // No match on this row, advance to the next row for this state, |
michael@0 | 1005 | tableEl++; |
michael@0 | 1006 | } |
michael@0 | 1007 | if (fRB->fDebugEnv && uprv_strstr(fRB->fDebugEnv, "scan")) { RBBIDebugPuts("");} |
michael@0 | 1008 | |
michael@0 | 1009 | // |
michael@0 | 1010 | // We've found the row of the state table that matches the current input |
michael@0 | 1011 | // character from the rules string. |
michael@0 | 1012 | // Perform any action specified by this row in the state table. |
michael@0 | 1013 | if (doParseActions((int32_t)tableEl->fAction) == FALSE) { |
michael@0 | 1014 | // Break out of the state machine loop if the |
michael@0 | 1015 | // the action signalled some kind of error, or |
michael@0 | 1016 | // the action was to exit, occurs on normal end-of-rules-input. |
michael@0 | 1017 | break; |
michael@0 | 1018 | } |
michael@0 | 1019 | |
michael@0 | 1020 | if (tableEl->fPushState != 0) { |
michael@0 | 1021 | fStackPtr++; |
michael@0 | 1022 | if (fStackPtr >= kStackSize) { |
michael@0 | 1023 | error(U_BRK_INTERNAL_ERROR); |
michael@0 | 1024 | RBBIDebugPuts("RBBIRuleScanner::parse() - state stack overflow."); |
michael@0 | 1025 | fStackPtr--; |
michael@0 | 1026 | } |
michael@0 | 1027 | fStack[fStackPtr] = tableEl->fPushState; |
michael@0 | 1028 | } |
michael@0 | 1029 | |
michael@0 | 1030 | if (tableEl->fNextChar) { |
michael@0 | 1031 | nextChar(fC); |
michael@0 | 1032 | } |
michael@0 | 1033 | |
michael@0 | 1034 | // Get the next state from the table entry, or from the |
michael@0 | 1035 | // state stack if the next state was specified as "pop". |
michael@0 | 1036 | if (tableEl->fNextState != 255) { |
michael@0 | 1037 | state = tableEl->fNextState; |
michael@0 | 1038 | } else { |
michael@0 | 1039 | state = fStack[fStackPtr]; |
michael@0 | 1040 | fStackPtr--; |
michael@0 | 1041 | if (fStackPtr < 0) { |
michael@0 | 1042 | error(U_BRK_INTERNAL_ERROR); |
michael@0 | 1043 | RBBIDebugPuts("RBBIRuleScanner::parse() - state stack underflow."); |
michael@0 | 1044 | fStackPtr++; |
michael@0 | 1045 | } |
michael@0 | 1046 | } |
michael@0 | 1047 | |
michael@0 | 1048 | } |
michael@0 | 1049 | |
michael@0 | 1050 | // |
michael@0 | 1051 | // If there were NO user specified reverse rules, set up the equivalent of ".*;" |
michael@0 | 1052 | // |
michael@0 | 1053 | if (fRB->fReverseTree == NULL) { |
michael@0 | 1054 | fRB->fReverseTree = pushNewNode(RBBINode::opStar); |
michael@0 | 1055 | RBBINode *operand = pushNewNode(RBBINode::setRef); |
michael@0 | 1056 | findSetFor(UnicodeString(TRUE, kAny, 3), operand); |
michael@0 | 1057 | fRB->fReverseTree->fLeftChild = operand; |
michael@0 | 1058 | operand->fParent = fRB->fReverseTree; |
michael@0 | 1059 | fNodeStackPtr -= 2; |
michael@0 | 1060 | } |
michael@0 | 1061 | |
michael@0 | 1062 | |
michael@0 | 1063 | // |
michael@0 | 1064 | // Parsing of the input RBBI rules is complete. |
michael@0 | 1065 | // We now have a parse tree for the rule expressions |
michael@0 | 1066 | // and a list of all UnicodeSets that are referenced. |
michael@0 | 1067 | // |
michael@0 | 1068 | #ifdef RBBI_DEBUG |
michael@0 | 1069 | if (fRB->fDebugEnv && uprv_strstr(fRB->fDebugEnv, "symbols")) {fSymbolTable->rbbiSymtablePrint();} |
michael@0 | 1070 | if (fRB->fDebugEnv && uprv_strstr(fRB->fDebugEnv, "ptree")) |
michael@0 | 1071 | { |
michael@0 | 1072 | RBBIDebugPrintf("Completed Forward Rules Parse Tree...\n"); |
michael@0 | 1073 | fRB->fForwardTree->printTree(TRUE); |
michael@0 | 1074 | RBBIDebugPrintf("\nCompleted Reverse Rules Parse Tree...\n"); |
michael@0 | 1075 | fRB->fReverseTree->printTree(TRUE); |
michael@0 | 1076 | RBBIDebugPrintf("\nCompleted Safe Point Forward Rules Parse Tree...\n"); |
michael@0 | 1077 | fRB->fSafeFwdTree->printTree(TRUE); |
michael@0 | 1078 | RBBIDebugPrintf("\nCompleted Safe Point Reverse Rules Parse Tree...\n"); |
michael@0 | 1079 | fRB->fSafeRevTree->printTree(TRUE); |
michael@0 | 1080 | } |
michael@0 | 1081 | #endif |
michael@0 | 1082 | } |
michael@0 | 1083 | |
michael@0 | 1084 | |
michael@0 | 1085 | //------------------------------------------------------------------------------ |
michael@0 | 1086 | // |
michael@0 | 1087 | // printNodeStack for debugging... |
michael@0 | 1088 | // |
michael@0 | 1089 | //------------------------------------------------------------------------------ |
michael@0 | 1090 | #ifdef RBBI_DEBUG |
michael@0 | 1091 | void RBBIRuleScanner::printNodeStack(const char *title) { |
michael@0 | 1092 | int i; |
michael@0 | 1093 | RBBIDebugPrintf("%s. Dumping node stack...\n", title); |
michael@0 | 1094 | for (i=fNodeStackPtr; i>0; i--) {fNodeStack[i]->printTree(TRUE);} |
michael@0 | 1095 | } |
michael@0 | 1096 | #endif |
michael@0 | 1097 | |
michael@0 | 1098 | |
michael@0 | 1099 | |
michael@0 | 1100 | |
michael@0 | 1101 | //------------------------------------------------------------------------------ |
michael@0 | 1102 | // |
michael@0 | 1103 | // pushNewNode create a new RBBINode of the specified type and push it |
michael@0 | 1104 | // onto the stack of nodes. |
michael@0 | 1105 | // |
michael@0 | 1106 | //------------------------------------------------------------------------------ |
michael@0 | 1107 | RBBINode *RBBIRuleScanner::pushNewNode(RBBINode::NodeType t) { |
michael@0 | 1108 | fNodeStackPtr++; |
michael@0 | 1109 | if (fNodeStackPtr >= kStackSize) { |
michael@0 | 1110 | error(U_BRK_INTERNAL_ERROR); |
michael@0 | 1111 | RBBIDebugPuts("RBBIRuleScanner::pushNewNode - stack overflow."); |
michael@0 | 1112 | *fRB->fStatus = U_BRK_INTERNAL_ERROR; |
michael@0 | 1113 | return NULL; |
michael@0 | 1114 | } |
michael@0 | 1115 | fNodeStack[fNodeStackPtr] = new RBBINode(t); |
michael@0 | 1116 | if (fNodeStack[fNodeStackPtr] == NULL) { |
michael@0 | 1117 | *fRB->fStatus = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 1118 | } |
michael@0 | 1119 | return fNodeStack[fNodeStackPtr]; |
michael@0 | 1120 | } |
michael@0 | 1121 | |
michael@0 | 1122 | |
michael@0 | 1123 | |
michael@0 | 1124 | //------------------------------------------------------------------------------ |
michael@0 | 1125 | // |
michael@0 | 1126 | // scanSet Construct a UnicodeSet from the text at the current scan |
michael@0 | 1127 | // position. Advance the scan position to the first character |
michael@0 | 1128 | // after the set. |
michael@0 | 1129 | // |
michael@0 | 1130 | // A new RBBI setref node referring to the set is pushed onto the node |
michael@0 | 1131 | // stack. |
michael@0 | 1132 | // |
michael@0 | 1133 | // The scan position is normally under the control of the state machine |
michael@0 | 1134 | // that controls rule parsing. UnicodeSets, however, are parsed by |
michael@0 | 1135 | // the UnicodeSet constructor, not by the RBBI rule parser. |
michael@0 | 1136 | // |
michael@0 | 1137 | //------------------------------------------------------------------------------ |
michael@0 | 1138 | void RBBIRuleScanner::scanSet() { |
michael@0 | 1139 | UnicodeSet *uset; |
michael@0 | 1140 | ParsePosition pos; |
michael@0 | 1141 | int startPos; |
michael@0 | 1142 | int i; |
michael@0 | 1143 | |
michael@0 | 1144 | if (U_FAILURE(*fRB->fStatus)) { |
michael@0 | 1145 | return; |
michael@0 | 1146 | } |
michael@0 | 1147 | |
michael@0 | 1148 | pos.setIndex(fScanIndex); |
michael@0 | 1149 | startPos = fScanIndex; |
michael@0 | 1150 | UErrorCode localStatus = U_ZERO_ERROR; |
michael@0 | 1151 | uset = new UnicodeSet(); |
michael@0 | 1152 | if (uset == NULL) { |
michael@0 | 1153 | localStatus = U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 1154 | } else { |
michael@0 | 1155 | uset->applyPatternIgnoreSpace(fRB->fRules, pos, fSymbolTable, localStatus); |
michael@0 | 1156 | } |
michael@0 | 1157 | if (U_FAILURE(localStatus)) { |
michael@0 | 1158 | // TODO: Get more accurate position of the error from UnicodeSet's return info. |
michael@0 | 1159 | // UnicodeSet appears to not be reporting correctly at this time. |
michael@0 | 1160 | #ifdef RBBI_DEBUG |
michael@0 | 1161 | RBBIDebugPrintf("UnicodeSet parse postion.ErrorIndex = %d\n", pos.getIndex()); |
michael@0 | 1162 | #endif |
michael@0 | 1163 | error(localStatus); |
michael@0 | 1164 | delete uset; |
michael@0 | 1165 | return; |
michael@0 | 1166 | } |
michael@0 | 1167 | |
michael@0 | 1168 | // Verify that the set contains at least one code point. |
michael@0 | 1169 | // |
michael@0 | 1170 | U_ASSERT(uset!=NULL); |
michael@0 | 1171 | if (uset->isEmpty()) { |
michael@0 | 1172 | // This set is empty. |
michael@0 | 1173 | // Make it an error, because it almost certainly is not what the user wanted. |
michael@0 | 1174 | // Also, avoids having to think about corner cases in the tree manipulation code |
michael@0 | 1175 | // that occurs later on. |
michael@0 | 1176 | error(U_BRK_RULE_EMPTY_SET); |
michael@0 | 1177 | delete uset; |
michael@0 | 1178 | return; |
michael@0 | 1179 | } |
michael@0 | 1180 | |
michael@0 | 1181 | |
michael@0 | 1182 | // Advance the RBBI parse postion over the UnicodeSet pattern. |
michael@0 | 1183 | // Don't just set fScanIndex because the line/char positions maintained |
michael@0 | 1184 | // for error reporting would be thrown off. |
michael@0 | 1185 | i = pos.getIndex(); |
michael@0 | 1186 | for (;;) { |
michael@0 | 1187 | if (fNextIndex >= i) { |
michael@0 | 1188 | break; |
michael@0 | 1189 | } |
michael@0 | 1190 | nextCharLL(); |
michael@0 | 1191 | } |
michael@0 | 1192 | |
michael@0 | 1193 | if (U_SUCCESS(*fRB->fStatus)) { |
michael@0 | 1194 | RBBINode *n; |
michael@0 | 1195 | |
michael@0 | 1196 | n = pushNewNode(RBBINode::setRef); |
michael@0 | 1197 | n->fFirstPos = startPos; |
michael@0 | 1198 | n->fLastPos = fNextIndex; |
michael@0 | 1199 | fRB->fRules.extractBetween(n->fFirstPos, n->fLastPos, n->fText); |
michael@0 | 1200 | // findSetFor() serves several purposes here: |
michael@0 | 1201 | // - Adopts storage for the UnicodeSet, will be responsible for deleting. |
michael@0 | 1202 | // - Mantains collection of all sets in use, needed later for establishing |
michael@0 | 1203 | // character categories for run time engine. |
michael@0 | 1204 | // - Eliminates mulitiple instances of the same set. |
michael@0 | 1205 | // - Creates a new uset node if necessary (if this isn't a duplicate.) |
michael@0 | 1206 | findSetFor(n->fText, n, uset); |
michael@0 | 1207 | } |
michael@0 | 1208 | |
michael@0 | 1209 | } |
michael@0 | 1210 | |
michael@0 | 1211 | U_NAMESPACE_END |
michael@0 | 1212 | |
michael@0 | 1213 | #endif /* #if !UCONFIG_NO_BREAK_ITERATION */ |