2 **********************************************************************
3 * Copyright (C) 1999-2008, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 * Date Name Description
7 * 11/17/99 aliu Creation.
8 **********************************************************************
11 #include "unicode/utypes.h"
13 #if !UCONFIG_NO_TRANSLITERATION
15 #include "unicode/uobject.h"
16 #include "unicode/parseerr.h"
17 #include "unicode/parsepos.h"
18 #include "unicode/putil.h"
19 #include "unicode/uchar.h"
20 #include "unicode/ustring.h"
21 #include "unicode/uniset.h"
32 #include "unicode/symtable.h"
42 #define VARIABLE_DEF_OP ((UChar)0x003D) /*=*/
43 #define FORWARD_RULE_OP ((UChar)0x003E) /*>*/
44 #define REVERSE_RULE_OP ((UChar)0x003C) /*<*/
45 #define FWDREV_RULE_OP ((UChar)0x007E) /*~*/ // internal rep of <> op
47 // Other special characters
48 #define QUOTE ((UChar)0x0027) /*'*/
49 #define ESCAPE ((UChar)0x005C) /*\*/
50 #define END_OF_RULE ((UChar)0x003B) /*;*/
51 #define RULE_COMMENT_CHAR ((UChar)0x0023) /*#*/
53 #define SEGMENT_OPEN ((UChar)0x0028) /*(*/
54 #define SEGMENT_CLOSE ((UChar)0x0029) /*)*/
55 #define CONTEXT_ANTE ((UChar)0x007B) /*{*/
56 #define CONTEXT_POST ((UChar)0x007D) /*}*/
57 #define CURSOR_POS ((UChar)0x007C) /*|*/
58 #define CURSOR_OFFSET ((UChar)0x0040) /*@*/
59 #define ANCHOR_START ((UChar)0x005E) /*^*/
60 #define KLEENE_STAR ((UChar)0x002A) /***/
61 #define ONE_OR_MORE ((UChar)0x002B) /*+*/
62 #define ZERO_OR_ONE ((UChar)0x003F) /*?*/
64 #define DOT ((UChar)46) /*.*/
66 static const UChar DOT_SET
[] = { // "[^[:Zp:][:Zl:]\r\n$]";
67 91, 94, 91, 58, 90, 112, 58, 93, 91, 58, 90,
68 108, 58, 93, 92, 114, 92, 110, 36, 93, 0
71 // A function is denoted &Source-Target/Variant(text)
72 #define FUNCTION ((UChar)38) /*&*/
74 // Aliases for some of the syntax characters. These are provided so
75 // transliteration rules can be expressed in XML without clashing with
76 // XML syntax characters '<', '>', and '&'.
77 #define ALT_REVERSE_RULE_OP ((UChar)0x2190) // Left Arrow
78 #define ALT_FORWARD_RULE_OP ((UChar)0x2192) // Right Arrow
79 #define ALT_FWDREV_RULE_OP ((UChar)0x2194) // Left Right Arrow
80 #define ALT_FUNCTION ((UChar)0x2206) // Increment (~Greek Capital Delta)
82 // Special characters disallowed at the top level
83 static const UChar ILLEGAL_TOP
[] = {41,0}; // ")"
85 // Special characters disallowed within a segment
86 static const UChar ILLEGAL_SEG
[] = {123,125,124,64,0}; // "{}|@"
88 // Special characters disallowed within a function argument
89 static const UChar ILLEGAL_FUNC
[] = {94,40,46,42,43,63,123,125,124,64,0}; // "^(.*+?{}|@"
91 // By definition, the ANCHOR_END special character is a
92 // trailing SymbolTable.SYMBOL_REF character.
93 // private static final char ANCHOR_END = '$';
95 static const UChar gOPERATORS
[] = { // "=><"
96 VARIABLE_DEF_OP
, FORWARD_RULE_OP
, REVERSE_RULE_OP
,
97 ALT_FORWARD_RULE_OP
, ALT_REVERSE_RULE_OP
, ALT_FWDREV_RULE_OP
,
101 static const UChar HALF_ENDERS
[] = { // "=><;"
102 VARIABLE_DEF_OP
, FORWARD_RULE_OP
, REVERSE_RULE_OP
,
103 ALT_FORWARD_RULE_OP
, ALT_REVERSE_RULE_OP
, ALT_FWDREV_RULE_OP
,
108 // These are also used in Transliterator::toRules()
109 static const int32_t ID_TOKEN_LEN
= 2;
110 static const UChar ID_TOKEN
[] = { 0x3A, 0x3A }; // ':', ':'
113 commented out until we do real ::BEGIN/::END functionality
114 static const int32_t BEGIN_TOKEN_LEN = 5;
115 static const UChar BEGIN_TOKEN[] = { 0x42, 0x45, 0x47, 0x49, 0x4e }; // 'BEGIN'
117 static const int32_t END_TOKEN_LEN = 3;
118 static const UChar END_TOKEN[] = { 0x45, 0x4e, 0x44 }; // 'END'
123 //----------------------------------------------------------------------
125 //----------------------------------------------------------------------
128 * This class implements the SymbolTable interface. It is used
129 * during parsing to give UnicodeSet access to variables that
130 * have been defined so far. Note that it uses variablesVector,
131 * _not_ data.setVariables.
133 class ParseData
: public UMemory
, public SymbolTable
{
135 const TransliterationRuleData
* data
; // alias
137 const UVector
* variablesVector
; // alias
139 const Hashtable
* variableNames
; // alias
141 ParseData(const TransliterationRuleData
* data
= 0,
142 const UVector
* variablesVector
= 0,
143 const Hashtable
* variableNames
= 0);
145 virtual const UnicodeString
* lookup(const UnicodeString
& s
) const;
147 virtual const UnicodeFunctor
* lookupMatcher(UChar32 ch
) const;
149 virtual UnicodeString
parseReference(const UnicodeString
& text
,
150 ParsePosition
& pos
, int32_t limit
) const;
152 * Return true if the given character is a matcher standin or a plain
153 * character (non standin).
155 UBool
isMatcher(UChar32 ch
);
158 * Return true if the given character is a replacer standin or a plain
159 * character (non standin).
161 UBool
isReplacer(UChar32 ch
);
164 ParseData(const ParseData
&other
); // forbid copying of this class
165 ParseData
&operator=(const ParseData
&other
); // forbid copying of this class
168 ParseData::ParseData(const TransliterationRuleData
* d
,
170 const Hashtable
* vNames
) :
171 data(d
), variablesVector(sets
), variableNames(vNames
) {}
174 * Implement SymbolTable API.
176 const UnicodeString
* ParseData::lookup(const UnicodeString
& name
) const {
177 return (const UnicodeString
*) variableNames
->get(name
);
181 * Implement SymbolTable API.
183 const UnicodeFunctor
* ParseData::lookupMatcher(UChar32 ch
) const {
184 // Note that we cannot use data.lookupSet() because the
185 // set array has not been constructed yet.
186 const UnicodeFunctor
* set
= NULL
;
187 int32_t i
= ch
- data
->variablesBase
;
188 if (i
>= 0 && i
< variablesVector
->size()) {
189 int32_t i
= ch
- data
->variablesBase
;
190 set
= (i
< variablesVector
->size()) ?
191 (UnicodeFunctor
*) variablesVector
->elementAt(i
) : 0;
197 * Implement SymbolTable API. Parse out a symbol reference
200 UnicodeString
ParseData::parseReference(const UnicodeString
& text
,
201 ParsePosition
& pos
, int32_t limit
) const {
202 int32_t start
= pos
.getIndex();
204 UnicodeString result
;
206 UChar c
= text
.charAt(i
);
207 if ((i
==start
&& !u_isIDStart(c
)) || !u_isIDPart(c
)) {
212 if (i
== start
) { // No valid name chars
213 return result
; // Indicate failure with empty string
216 text
.extractBetween(start
, i
, result
);
220 UBool
ParseData::isMatcher(UChar32 ch
) {
221 // Note that we cannot use data.lookup() because the
222 // set array has not been constructed yet.
223 int32_t i
= ch
- data
->variablesBase
;
224 if (i
>= 0 && i
< variablesVector
->size()) {
225 UnicodeFunctor
*f
= (UnicodeFunctor
*) variablesVector
->elementAt(i
);
226 return f
!= NULL
&& f
->toMatcher() != NULL
;
232 * Return true if the given character is a replacer standin or a plain
233 * character (non standin).
235 UBool
ParseData::isReplacer(UChar32 ch
) {
236 // Note that we cannot use data.lookup() because the
237 // set array has not been constructed yet.
238 int i
= ch
- data
->variablesBase
;
239 if (i
>= 0 && i
< variablesVector
->size()) {
240 UnicodeFunctor
*f
= (UnicodeFunctor
*) variablesVector
->elementAt(i
);
241 return f
!= NULL
&& f
->toReplacer() != NULL
;
246 //----------------------------------------------------------------------
248 //----------------------------------------------------------------------
251 * A class representing one side of a rule. This class knows how to
252 * parse half of a rule. It is tightly coupled to the method
253 * RuleBasedTransliterator.Parser.parseRule().
255 class RuleHalf
: public UMemory
{
261 int32_t cursor
; // position of cursor in text
262 int32_t ante
; // position of ante context marker '{' in text
263 int32_t post
; // position of post context marker '}' in text
265 // Record the offset to the cursor either to the left or to the
266 // right of the key. This is indicated by characters on the output
267 // side that allow the cursor to be positioned arbitrarily within
268 // the matching text. For example, abc{def} > | @@@ xyz; changes
269 // def to xyz and moves the cursor to before abc. Offset characters
270 // must be at the start or end, and they cannot move the cursor past
271 // the ante- or postcontext text. Placeholders are only valid in
272 // output text. The length of the ante and post context is
273 // determined at runtime, because of supplementals and quantifiers.
274 int32_t cursorOffset
; // only nonzero on output side
276 // Position of first CURSOR_OFFSET on _right_. This will be -1
277 // for |@, -2 for |@@, etc., and 1 for @|, 2 for @@|, etc.
278 int32_t cursorOffsetPos
;
284 * The segment number from 1..n of the next '(' we see
285 * during parsing; 1-based.
287 int32_t nextSegmentNumber
;
289 TransliteratorParser
& parser
;
291 //--------------------------------------------------
294 RuleHalf(TransliteratorParser
& parser
);
297 int32_t parse(const UnicodeString
& rule
, int32_t pos
, int32_t limit
, UErrorCode
& status
);
299 int32_t parseSection(const UnicodeString
& rule
, int32_t pos
, int32_t limit
,
301 const UnicodeString
& illegal
,
308 void removeContext();
311 * Return true if this half looks like valid output, that is, does not
312 * contain quantifiers or other special input-only elements.
314 UBool
isValidOutput(TransliteratorParser
& parser
);
317 * Return true if this half looks like valid input, that is, does not
318 * contain functions or other special output-only elements.
320 UBool
isValidInput(TransliteratorParser
& parser
);
322 int syntaxError(UErrorCode code
,
323 const UnicodeString
& rule
,
325 UErrorCode
& status
) {
326 return parser
.syntaxError(code
, rule
, start
, status
);
330 // Disallowed methods; no impl.
331 RuleHalf(const RuleHalf
&);
332 RuleHalf
& operator=(const RuleHalf
&);
335 RuleHalf::RuleHalf(TransliteratorParser
& p
) :
343 anchorStart
= anchorEnd
= FALSE
;
344 nextSegmentNumber
= 1;
347 RuleHalf::~RuleHalf() {
351 * Parse one side of a rule, stopping at either the limit,
352 * the END_OF_RULE character, or an operator.
353 * @return the index after the terminating character, or
354 * if limit was reached, limit
356 int32_t RuleHalf::parse(const UnicodeString
& rule
, int32_t pos
, int32_t limit
, UErrorCode
& status
) {
359 pos
= parseSection(rule
, pos
, limit
, text
, ILLEGAL_TOP
, FALSE
, status
);
361 if (cursorOffset
> 0 && cursor
!= cursorOffsetPos
) {
362 return syntaxError(U_MISPLACED_CURSOR_OFFSET
, rule
, start
, status
);
369 * Parse a section of one side of a rule, stopping at either
370 * the limit, the END_OF_RULE character, an operator, or a
371 * segment close character. This method parses both a
372 * top-level rule half and a segment within such a rule half.
373 * It calls itself recursively to parse segments and nested
375 * @param buf buffer into which to accumulate the rule pattern
376 * characters, either literal characters from the rule or
377 * standins for UnicodeMatcher objects including segments.
378 * @param illegal the set of special characters that is illegal during
380 * @param isSegment if true, then we've already seen a '(' and
381 * pos on entry points right after it. Accumulate everything
382 * up to the closing ')', put it in a segment matcher object,
383 * generate a standin for it, and add the standin to buf. As
384 * a side effect, update the segments vector with a reference
385 * to the segment matcher. This works recursively for nested
386 * segments. If isSegment is false, just accumulate
387 * characters into buf.
388 * @return the index after the terminating character, or
389 * if limit was reached, limit
391 int32_t RuleHalf::parseSection(const UnicodeString
& rule
, int32_t pos
, int32_t limit
,
393 const UnicodeString
& illegal
,
394 UBool isSegment
, UErrorCode
& status
) {
397 UnicodeString scratch
;
399 int32_t quoteStart
= -1; // Most recent 'single quoted string'
400 int32_t quoteLimit
= -1;
401 int32_t varStart
= -1; // Most recent $variableReference
402 int32_t varLimit
= -1;
403 int32_t bufStart
= buf
.length();
405 while (pos
< limit
&& !done
) {
406 // Since all syntax characters are in the BMP, fetching
407 // 16-bit code units suffices here.
408 UChar c
= rule
.charAt(pos
++);
409 if (uprv_isRuleWhiteSpace(c
)) {
410 // Ignore whitespace. Note that this is not Unicode
411 // spaces, but Java spaces -- a subset, representing
412 // whitespace likely to be seen in code.
415 if (u_strchr(HALF_ENDERS
, c
) != NULL
) {
418 return syntaxError(U_UNCLOSED_SEGMENT
, rule
, start
, status
);
423 // Text after a presumed end anchor is a syntax err
424 return syntaxError(U_MALFORMED_VARIABLE_REFERENCE
, rule
, start
, status
);
426 if (UnicodeSet::resemblesPattern(rule
, pos
-1)) {
427 pp
.setIndex(pos
-1); // Backup to opening '['
428 buf
.append(parser
.parseSet(rule
, pp
, status
));
429 if (U_FAILURE(status
)) {
430 return syntaxError(U_MALFORMED_SET
, rule
, start
, status
);
438 return syntaxError(U_TRAILING_BACKSLASH
, rule
, start
, status
);
440 UChar32 escaped
= rule
.unescapeAt(pos
); // pos is already past '\\'
441 if (escaped
== (UChar32
) -1) {
442 return syntaxError(U_MALFORMED_UNICODE_ESCAPE
, rule
, start
, status
);
444 if (!parser
.checkVariableRange(escaped
)) {
445 return syntaxError(U_VARIABLE_RANGE_OVERLAP
, rule
, start
, status
);
450 // Handle quoted matter
452 int32_t iq
= rule
.indexOf(QUOTE
, pos
);
454 buf
.append(c
); // Parse [''] outside quotes as [']
457 /* This loop picks up a run of quoted text of the
458 * form 'aaaa' each time through. If this run
459 * hasn't really ended ('aaaa''bbbb') then it keeps
460 * looping, each time adding on a new run. When it
461 * reaches the final quote it breaks.
463 quoteStart
= buf
.length();
466 return syntaxError(U_UNTERMINATED_QUOTE
, rule
, start
, status
);
469 rule
.extractBetween(pos
, iq
, scratch
);
472 if (pos
< limit
&& rule
.charAt(pos
) == QUOTE
) {
473 // Parse [''] inside quotes as [']
474 iq
= rule
.indexOf(QUOTE
, pos
+1);
480 quoteLimit
= buf
.length();
482 for (iq
=quoteStart
; iq
<quoteLimit
; ++iq
) {
483 if (!parser
.checkVariableRange(buf
.charAt(iq
))) {
484 return syntaxError(U_VARIABLE_RANGE_OVERLAP
, rule
, start
, status
);
491 if (!parser
.checkVariableRange(c
)) {
492 return syntaxError(U_VARIABLE_RANGE_OVERLAP
, rule
, start
, status
);
495 if (illegal
.indexOf(c
) >= 0) {
496 syntaxError(U_ILLEGAL_CHARACTER
, rule
, start
, status
);
501 //------------------------------------------------------
502 // Elements allowed within and out of segments
503 //------------------------------------------------------
505 if (buf
.length() == 0 && !anchorStart
) {
508 return syntaxError(U_MISPLACED_ANCHOR_START
,
509 rule
, start
, status
);
514 // bufSegStart is the offset in buf to the first
515 // character of the segment we are parsing.
516 int32_t bufSegStart
= buf
.length();
518 // Record segment number now, since nextSegmentNumber
519 // will be incremented during the call to parseSection
520 // if there are nested segments.
521 int32_t segmentNumber
= nextSegmentNumber
++; // 1-based
524 pos
= parseSection(rule
, pos
, limit
, buf
, ILLEGAL_SEG
, TRUE
, status
);
526 // After parsing a segment, the relevant characters are
527 // in buf, starting at offset bufSegStart. Extract them
528 // into a string matcher, and replace them with a
529 // standin for that matcher.
531 new StringMatcher(buf
, bufSegStart
, buf
.length(),
532 segmentNumber
, *parser
.curData
);
534 return syntaxError(U_MEMORY_ALLOCATION_ERROR
, rule
, start
, status
);
537 // Record and associate object and segment number
538 parser
.setSegmentObject(segmentNumber
, m
, status
);
539 buf
.truncate(bufSegStart
);
540 buf
.append(parser
.getSegmentStandin(segmentNumber
, status
));
547 TransliteratorIDParser::SingleID
* single
=
548 TransliteratorIDParser::parseFilterID(rule
, iref
);
549 // The next character MUST be a segment open
550 if (single
== NULL
||
551 !ICU_Utility::parseChar(rule
, iref
, SEGMENT_OPEN
)) {
552 return syntaxError(U_INVALID_FUNCTION
, rule
, start
, status
);
555 Transliterator
*t
= single
->createInstance();
558 return syntaxError(U_INVALID_FUNCTION
, rule
, start
, status
);
561 // bufSegStart is the offset in buf to the first
562 // character of the segment we are parsing.
563 int32_t bufSegStart
= buf
.length();
566 pos
= parseSection(rule
, iref
, limit
, buf
, ILLEGAL_FUNC
, TRUE
, status
);
568 // After parsing a segment, the relevant characters are
569 // in buf, starting at offset bufSegStart.
570 UnicodeString output
;
571 buf
.extractBetween(bufSegStart
, buf
.length(), output
);
572 FunctionReplacer
*r
=
573 new FunctionReplacer(t
, new StringReplacer(output
, parser
.curData
));
575 return syntaxError(U_MEMORY_ALLOCATION_ERROR
, rule
, start
, status
);
578 // Replace the buffer contents with a stand-in
579 buf
.truncate(bufSegStart
);
580 buf
.append(parser
.generateStandInFor(r
, status
));
583 case SymbolTable::SYMBOL_REF
:
584 // Handle variable references and segment references "$1" .. "$9"
586 // A variable reference must be followed immediately
587 // by a Unicode identifier start and zero or more
588 // Unicode identifier part characters, or by a digit
589 // 1..9 if it is a segment reference.
591 // A variable ref character at the end acts as
592 // an anchor to the context limit, as in perl.
596 // Parse "$1" "$2" .. "$9" .. (no upper limit)
597 c
= rule
.charAt(pos
);
598 int32_t r
= u_digit(c
, 10);
599 if (r
>= 1 && r
<= 9) {
600 r
= ICU_Utility::parseNumber(rule
, pos
, 10);
602 return syntaxError(U_UNDEFINED_SEGMENT_REFERENCE
,
603 rule
, start
, status
);
605 buf
.append(parser
.getSegmentStandin(r
, status
));
608 UnicodeString name
= parser
.parseData
->
609 parseReference(rule
, pp
, limit
);
610 if (name
.length() == 0) {
611 // This means the '$' was not followed by a
612 // valid name. Try to interpret it as an
613 // end anchor then. If this also doesn't work
614 // (if we see a following character) then signal
620 // If this is a variable definition statement,
621 // then the LHS variable will be undefined. In
622 // that case appendVariableDef() will append the
623 // special placeholder char variableLimit-1.
624 varStart
= buf
.length();
625 parser
.appendVariableDef(name
, buf
, status
);
626 varLimit
= buf
.length();
631 buf
.append(parser
.getDotStandIn(status
));
636 // Quantifiers. We handle single characters, quoted strings,
637 // variable references, and segments.
639 // 'foo'+ matches foofoofoo
640 // $v+ matches xyxyxy if $v == xy
641 // (seg)+ matches segsegseg
643 if (isSegment
&& buf
.length() == bufStart
) {
644 // The */+ immediately follows '('
645 return syntaxError(U_MISPLACED_QUANTIFIER
, rule
, start
, status
);
648 int32_t qstart
, qlimit
;
649 // The */+ follows an isolated character or quote
650 // or variable reference
651 if (buf
.length() == quoteLimit
) {
652 // The */+ follows a 'quoted string'
655 } else if (buf
.length() == varLimit
) {
656 // The */+ follows a $variableReference
660 // The */+ follows a single character, possibly
662 qstart
= buf
.length() - 1;
667 new StringMatcher(buf
, qstart
, qlimit
, 0, *parser
.curData
);
669 return syntaxError(U_MEMORY_ALLOCATION_ERROR
, rule
, start
, status
);
672 int32_t max
= Quantifier::MAX
;
682 // do nothing -- min, max already set
684 m
= new Quantifier(m
, min
, max
);
686 return syntaxError(U_MEMORY_ALLOCATION_ERROR
, rule
, start
, status
);
688 buf
.truncate(qstart
);
689 buf
.append(parser
.generateStandInFor(m
, status
));
693 //------------------------------------------------------
694 // Elements allowed ONLY WITHIN segments
695 //------------------------------------------------------
697 // assert(isSegment);
698 // We're done parsing a segment.
702 //------------------------------------------------------
703 // Elements allowed ONLY OUTSIDE segments
704 //------------------------------------------------------
707 return syntaxError(U_MULTIPLE_ANTE_CONTEXTS
, rule
, start
, status
);
713 return syntaxError(U_MULTIPLE_POST_CONTEXTS
, rule
, start
, status
);
719 return syntaxError(U_MULTIPLE_CURSORS
, rule
, start
, status
);
721 cursor
= buf
.length();
724 if (cursorOffset
< 0) {
725 if (buf
.length() > 0) {
726 return syntaxError(U_MISPLACED_CURSOR_OFFSET
, rule
, start
, status
);
729 } else if (cursorOffset
> 0) {
730 if (buf
.length() != cursorOffsetPos
|| cursor
>= 0) {
731 return syntaxError(U_MISPLACED_CURSOR_OFFSET
, rule
, start
, status
);
735 if (cursor
== 0 && buf
.length() == 0) {
737 } else if (cursor
< 0) {
738 cursorOffsetPos
= buf
.length();
741 return syntaxError(U_MISPLACED_CURSOR_OFFSET
, rule
, start
, status
);
747 //------------------------------------------------------
748 // Non-special characters
749 //------------------------------------------------------
751 // Disallow unquoted characters other than [0-9A-Za-z]
752 // in the printable ASCII range. These characters are
753 // reserved for possible future use.
754 if (c
>= 0x0021 && c
<= 0x007E &&
755 !((c
>= 0x0030/*'0'*/ && c
<= 0x0039/*'9'*/) ||
756 (c
>= 0x0041/*'A'*/ && c
<= 0x005A/*'Z'*/) ||
757 (c
>= 0x0061/*'a'*/ && c
<= 0x007A/*'z'*/))) {
758 return syntaxError(U_UNQUOTED_SPECIAL
, rule
, start
, status
);
771 void RuleHalf::removeContext() {
772 //text = text.substring(ante < 0 ? 0 : ante,
773 // post < 0 ? text.length() : post);
778 text
.removeBetween(0, ante
);
781 anchorStart
= anchorEnd
= FALSE
;
785 * Return true if this half looks like valid output, that is, does not
786 * contain quantifiers or other special input-only elements.
788 UBool
RuleHalf::isValidOutput(TransliteratorParser
& transParser
) {
789 for (int32_t i
=0; i
<text
.length(); ) {
790 UChar32 c
= text
.char32At(i
);
791 i
+= UTF_CHAR_LENGTH(c
);
792 if (!transParser
.parseData
->isReplacer(c
)) {
800 * Return true if this half looks like valid input, that is, does not
801 * contain functions or other special output-only elements.
803 UBool
RuleHalf::isValidInput(TransliteratorParser
& transParser
) {
804 for (int32_t i
=0; i
<text
.length(); ) {
805 UChar32 c
= text
.char32At(i
);
806 i
+= UTF_CHAR_LENGTH(c
);
807 if (!transParser
.parseData
->isMatcher(c
)) {
814 //----------------------------------------------------------------------
816 //----------------------------------------------------------------------
821 TransliteratorParser::TransliteratorParser(UErrorCode
&statusReturn
) :
822 dataVector(statusReturn
),
823 idBlockVector(statusReturn
),
824 variablesVector(statusReturn
),
825 segmentObjects(statusReturn
)
827 idBlockVector
.setDeleter(uhash_deleteUnicodeString
);
829 compoundFilter
= NULL
;
831 variableNames
.setValueDeleter(uhash_deleteUnicodeString
);
837 TransliteratorParser::~TransliteratorParser() {
838 while (!dataVector
.isEmpty())
839 delete (TransliterationRuleData
*)(dataVector
.orphanElementAt(0));
840 delete compoundFilter
;
842 while (!variablesVector
.isEmpty())
843 delete (UnicodeFunctor
*)variablesVector
.orphanElementAt(0);
847 TransliteratorParser::parse(const UnicodeString
& rules
,
848 UTransDirection transDirection
,
852 parseRules(rules
, transDirection
, ec
);
858 * Return the compound filter parsed by parse(). Caller owns result.
860 UnicodeSet
* TransliteratorParser::orphanCompoundFilter() {
861 UnicodeSet
* f
= compoundFilter
;
862 compoundFilter
= NULL
;
866 //----------------------------------------------------------------------
867 // Private implementation
868 //----------------------------------------------------------------------
871 * Parse the given string as a sequence of rules, separated by newline
872 * characters ('\n'), and cause this object to implement those rules. Any
873 * previous rules are discarded. Typically this method is called exactly
874 * once, during construction.
875 * @exception IllegalArgumentException if there is a syntax error in the
878 void TransliteratorParser::parseRules(const UnicodeString
& rule
,
879 UTransDirection theDirection
,
882 // Clear error struct
883 uprv_memset(&parseError
, 0, sizeof(parseError
));
884 parseError
.line
= parseError
.offset
= -1;
886 UBool parsingIDs
= TRUE
;
887 int32_t ruleCount
= 0;
889 while (!dataVector
.isEmpty()) {
890 delete (TransliterationRuleData
*)(dataVector
.orphanElementAt(0));
892 if (U_FAILURE(status
)) {
896 idBlockVector
.removeAllElements();
898 direction
= theDirection
;
901 delete compoundFilter
;
902 compoundFilter
= NULL
;
904 while (!variablesVector
.isEmpty()) {
905 delete (UnicodeFunctor
*)variablesVector
.orphanElementAt(0);
907 variableNames
.removeAll();
908 parseData
= new ParseData(0, &variablesVector
, &variableNames
);
909 if (parseData
== NULL
) {
910 status
= U_MEMORY_ALLOCATION_ERROR
;
914 dotStandIn
= (UChar
) -1;
916 UnicodeString
*tempstr
= NULL
; // used for memory allocation error checking
917 UnicodeString str
; // scratch
918 UnicodeString idBlockResult
;
920 int32_t limit
= rule
.length();
922 // The compound filter offset is an index into idBlockResult.
923 // If it is 0, then the compound filter occurred at the start,
924 // and it is the offset to the _start_ of the compound filter
925 // pattern. Otherwise it is the offset to the _limit_ of the
926 // compound filter pattern within idBlockResult.
927 compoundFilter
= NULL
;
928 int32_t compoundFilterOffset
= -1;
930 while (pos
< limit
&& U_SUCCESS(status
)) {
931 UChar c
= rule
.charAt(pos
++);
932 if (uprv_isRuleWhiteSpace(c
)) {
933 // Ignore leading whitespace.
936 // Skip lines starting with the comment character
937 if (c
== RULE_COMMENT_CHAR
) {
938 pos
= rule
.indexOf((UChar
)0x000A /*\n*/, pos
) + 1;
940 break; // No "\n" found; rest of rule is a commnet
942 continue; // Either fall out or restart with next line
946 if (c
== END_OF_RULE
)
949 // keep track of how many rules we've seen
952 // We've found the start of a rule or ID. c is its first
953 // character, and pos points past c.
955 // Look for an ID token. Must have at least ID_TOKEN_LEN + 1
957 if ((pos
+ ID_TOKEN_LEN
+ 1) <= limit
&&
958 rule
.compare(pos
, ID_TOKEN_LEN
, ID_TOKEN
) == 0) {
960 c
= rule
.charAt(pos
);
961 while (uprv_isRuleWhiteSpace(c
) && pos
< limit
) {
963 c
= rule
.charAt(pos
);
969 if (curData
!= NULL
) {
970 if (direction
== UTRANS_FORWARD
)
971 dataVector
.addElement(curData
, status
);
973 dataVector
.insertElementAt(curData
, 0, status
);
979 TransliteratorIDParser::SingleID
* id
=
980 TransliteratorIDParser::parseSingleID(rule
, p
, direction
, status
);
981 if (p
!= pos
&& ICU_Utility::parseChar(rule
, p
, END_OF_RULE
)) {
982 // Successful ::ID parse.
984 if (direction
== UTRANS_FORWARD
) {
985 idBlockResult
.append(id
->canonID
).append(END_OF_RULE
);
987 idBlockResult
.insert(0, END_OF_RULE
);
988 idBlockResult
.insert(0, id
->canonID
);
992 // Couldn't parse an ID. Try to parse a global filter
993 int32_t withParens
= -1;
994 UnicodeSet
* f
= TransliteratorIDParser::parseGlobalFilter(rule
, p
, direction
, withParens
, NULL
);
996 if (ICU_Utility::parseChar(rule
, p
, END_OF_RULE
)
997 && (direction
== UTRANS_FORWARD
) == (withParens
== 0))
999 if (compoundFilter
!= NULL
) {
1000 // Multiple compound filters
1001 syntaxError(U_MULTIPLE_COMPOUND_FILTERS
, rule
, pos
, status
);
1005 compoundFilterOffset
= ruleCount
;
1012 // Can be parsed as neither an ID nor a global filter
1013 syntaxError(U_INVALID_ID
, rule
, pos
, status
);
1020 tempstr
= new UnicodeString(idBlockResult
);
1021 // NULL pointer check
1022 if (tempstr
== NULL
) {
1023 status
= U_MEMORY_ALLOCATION_ERROR
;
1026 if (direction
== UTRANS_FORWARD
)
1027 idBlockVector
.addElement(tempstr
, status
);
1029 idBlockVector
.insertElementAt(tempstr
, 0, status
);
1030 idBlockResult
.remove();
1032 curData
= new TransliterationRuleData(status
);
1033 // NULL pointer check
1034 if (curData
== NULL
) {
1035 status
= U_MEMORY_ALLOCATION_ERROR
;
1038 parseData
->data
= curData
;
1040 // By default, rules use part of the private use area
1041 // E000..F8FF for variables and other stand-ins. Currently
1042 // the range F000..F8FF is typically sufficient. The 'use
1043 // variable range' pragma allows rule sets to modify this.
1044 setVariableRange(0xF000, 0xF8FF, status
);
1047 if (resemblesPragma(rule
, pos
, limit
)) {
1048 int32_t ppp
= parsePragma(rule
, pos
, limit
, status
);
1050 syntaxError(U_MALFORMED_PRAGMA
, rule
, pos
, status
);
1055 pos
= parseRule(rule
, pos
, limit
, status
);
1060 if (parsingIDs
&& idBlockResult
.length() > 0) {
1061 tempstr
= new UnicodeString(idBlockResult
);
1062 // NULL pointer check
1063 if (tempstr
== NULL
) {
1064 status
= U_MEMORY_ALLOCATION_ERROR
;
1067 if (direction
== UTRANS_FORWARD
)
1068 idBlockVector
.addElement(tempstr
, status
);
1070 idBlockVector
.insertElementAt(tempstr
, 0, status
);
1072 else if (!parsingIDs
&& curData
!= NULL
) {
1073 if (direction
== UTRANS_FORWARD
)
1074 dataVector
.addElement(curData
, status
);
1076 dataVector
.insertElementAt(curData
, 0, status
);
1079 if (U_SUCCESS(status
)) {
1080 // Convert the set vector to an array
1081 int32_t i
, dataVectorSize
= dataVector
.size();
1082 for (i
= 0; i
< dataVectorSize
; i
++) {
1083 TransliterationRuleData
* data
= (TransliterationRuleData
*)dataVector
.elementAt(i
);
1084 data
->variablesLength
= variablesVector
.size();
1085 if (data
->variablesLength
== 0) {
1086 data
->variables
= 0;
1088 data
->variables
= (UnicodeFunctor
**)uprv_malloc(data
->variablesLength
* sizeof(UnicodeFunctor
*));
1089 // NULL pointer check
1090 if (data
->variables
== NULL
) {
1091 status
= U_MEMORY_ALLOCATION_ERROR
;
1094 data
->variablesAreOwned
= (i
== 0);
1097 for (int32_t j
= 0; j
< data
->variablesLength
; j
++) {
1098 data
->variables
[j
] =
1099 ((UnicodeSet
*)variablesVector
.elementAt(j
));
1102 data
->variableNames
.removeAll();
1104 const UHashElement
* he
= variableNames
.nextElement(pos
);
1105 while (he
!= NULL
) {
1106 UnicodeString
* tempus
= (UnicodeString
*)(((UnicodeString
*)(he
->value
.pointer
))->clone());
1107 if (tempus
== NULL
) {
1108 status
= U_MEMORY_ALLOCATION_ERROR
;
1111 data
->variableNames
.put(*((UnicodeString
*)(he
->key
.pointer
)),
1113 he
= variableNames
.nextElement(pos
);
1116 variablesVector
.removeAllElements(); // keeps them from getting deleted when we succeed
1119 if (compoundFilter
!= NULL
) {
1120 if ((direction
== UTRANS_FORWARD
&& compoundFilterOffset
!= 1) ||
1121 (direction
== UTRANS_REVERSE
&& compoundFilterOffset
!= ruleCount
)) {
1122 status
= U_MISPLACED_COMPOUND_FILTER
;
1126 for (i
= 0; i
< dataVectorSize
; i
++) {
1127 TransliterationRuleData
* data
= (TransliterationRuleData
*)dataVector
.elementAt(i
);
1128 data
->ruleSet
.freeze(parseError
, status
);
1130 if (idBlockVector
.size() == 1 && ((UnicodeString
*)idBlockVector
.elementAt(0))->isEmpty()) {
1131 idBlockVector
.removeElementAt(0);
1137 * Set the variable range to [start, end] (inclusive).
1139 void TransliteratorParser::setVariableRange(int32_t start
, int32_t end
, UErrorCode
& status
) {
1140 if (start
> end
|| start
< 0 || end
> 0xFFFF) {
1141 status
= U_MALFORMED_PRAGMA
;
1145 curData
->variablesBase
= (UChar
) start
;
1146 if (dataVector
.size() == 0) {
1147 variableNext
= (UChar
) start
;
1148 variableLimit
= (UChar
) (end
+ 1);
1153 * Assert that the given character is NOT within the variable range.
1154 * If it is, return FALSE. This is neccesary to ensure that the
1155 * variable range does not overlap characters used in a rule.
1157 UBool
TransliteratorParser::checkVariableRange(UChar32 ch
) const {
1158 return !(ch
>= curData
->variablesBase
&& ch
< variableLimit
);
1162 * Set the maximum backup to 'backup', in response to a pragma
1165 void TransliteratorParser::pragmaMaximumBackup(int32_t /*backup*/) {
1170 * Begin normalizing all rules using the given mode, in response
1171 * to a pragma statement.
1173 void TransliteratorParser::pragmaNormalizeRules(UNormalizationMode
/*mode*/) {
1177 static const UChar PRAGMA_USE
[] = {0x75,0x73,0x65,0x20,0}; // "use "
1179 static const UChar PRAGMA_VARIABLE_RANGE
[] = {0x7E,0x76,0x61,0x72,0x69,0x61,0x62,0x6C,0x65,0x20,0x72,0x61,0x6E,0x67,0x65,0x20,0x23,0x20,0x23,0x7E,0x3B,0}; // "~variable range # #~;"
1181 static const UChar PRAGMA_MAXIMUM_BACKUP
[] = {0x7E,0x6D,0x61,0x78,0x69,0x6D,0x75,0x6D,0x20,0x62,0x61,0x63,0x6B,0x75,0x70,0x20,0x23,0x7E,0x3B,0}; // "~maximum backup #~;"
1183 static const UChar PRAGMA_NFD_RULES
[] = {0x7E,0x6E,0x66,0x64,0x20,0x72,0x75,0x6C,0x65,0x73,0x7E,0x3B,0}; // "~nfd rules~;"
1185 static const UChar PRAGMA_NFC_RULES
[] = {0x7E,0x6E,0x66,0x63,0x20,0x72,0x75,0x6C,0x65,0x73,0x7E,0x3B,0}; // "~nfc rules~;"
1188 * Return true if the given rule looks like a pragma.
1189 * @param pos offset to the first non-whitespace character
1191 * @param limit pointer past the last character of the rule.
1193 UBool
TransliteratorParser::resemblesPragma(const UnicodeString
& rule
, int32_t pos
, int32_t limit
) {
1194 // Must start with /use\s/i
1195 return ICU_Utility::parsePattern(rule
, pos
, limit
, PRAGMA_USE
, NULL
) >= 0;
1199 * Parse a pragma. This method assumes resemblesPragma() has
1200 * already returned true.
1201 * @param pos offset to the first non-whitespace character
1203 * @param limit pointer past the last character of the rule.
1204 * @return the position index after the final ';' of the pragma,
1207 int32_t TransliteratorParser::parsePragma(const UnicodeString
& rule
, int32_t pos
, int32_t limit
, UErrorCode
& status
) {
1210 // resemblesPragma() has already returned true, so we
1211 // know that pos points to /use\s/i; we can skip 4 characters
1215 // Here are the pragmas we recognize:
1216 // use variable range 0xE000 0xEFFF;
1217 // use maximum backup 16;
1220 int p
= ICU_Utility::parsePattern(rule
, pos
, limit
, PRAGMA_VARIABLE_RANGE
, array
);
1222 setVariableRange(array
[0], array
[1], status
);
1226 p
= ICU_Utility::parsePattern(rule
, pos
, limit
, PRAGMA_MAXIMUM_BACKUP
, array
);
1228 pragmaMaximumBackup(array
[0]);
1232 p
= ICU_Utility::parsePattern(rule
, pos
, limit
, PRAGMA_NFD_RULES
, NULL
);
1234 pragmaNormalizeRules(UNORM_NFD
);
1238 p
= ICU_Utility::parsePattern(rule
, pos
, limit
, PRAGMA_NFC_RULES
, NULL
);
1240 pragmaNormalizeRules(UNORM_NFC
);
1244 // Syntax error: unable to parse pragma
1249 * MAIN PARSER. Parse the next rule in the given rule string, starting
1250 * at pos. Return the index after the last character parsed. Do not
1251 * parse characters at or after limit.
1253 * Important: The character at pos must be a non-whitespace character
1254 * that is not the comment character.
1256 * This method handles quoting, escaping, and whitespace removal. It
1257 * parses the end-of-rule character. It recognizes context and cursor
1258 * indicators. Once it does a lexical breakdown of the rule at pos, it
1259 * creates a rule object and adds it to our rule list.
1261 int32_t TransliteratorParser::parseRule(const UnicodeString
& rule
, int32_t pos
, int32_t limit
, UErrorCode
& status
) {
1262 // Locate the left side, operator, and right side
1263 int32_t start
= pos
;
1267 // Set up segments data
1268 segmentStandins
.truncate(0);
1269 segmentObjects
.removeAllElements();
1271 // Use pointers to automatics to make swapping possible.
1272 RuleHalf
_left(*this), _right(*this);
1273 RuleHalf
* left
= &_left
;
1274 RuleHalf
* right
= &_right
;
1276 undefinedVariableName
.remove();
1277 pos
= left
->parse(rule
, pos
, limit
, status
);
1278 if (U_FAILURE(status
)) {
1282 if (pos
== limit
|| u_strchr(gOPERATORS
, (op
= rule
.charAt(--pos
))) == NULL
) {
1283 return syntaxError(U_MISSING_OPERATOR
, rule
, start
, status
);
1287 // Found an operator char. Check for forward-reverse operator.
1288 if (op
== REVERSE_RULE_OP
&&
1289 (pos
< limit
&& rule
.charAt(pos
) == FORWARD_RULE_OP
)) {
1291 op
= FWDREV_RULE_OP
;
1294 // Translate alternate op characters.
1296 case ALT_FORWARD_RULE_OP
:
1297 op
= FORWARD_RULE_OP
;
1299 case ALT_REVERSE_RULE_OP
:
1300 op
= REVERSE_RULE_OP
;
1302 case ALT_FWDREV_RULE_OP
:
1303 op
= FWDREV_RULE_OP
;
1307 pos
= right
->parse(rule
, pos
, limit
, status
);
1308 if (U_FAILURE(status
)) {
1313 if (rule
.charAt(--pos
) == END_OF_RULE
) {
1316 // RuleHalf parser must have terminated at an operator
1317 return syntaxError(U_UNQUOTED_SPECIAL
, rule
, start
, status
);
1321 if (op
== VARIABLE_DEF_OP
) {
1322 // LHS is the name. RHS is a single character, either a literal
1323 // or a set (already parsed). If RHS is longer than one
1324 // character, it is either a multi-character string, or multiple
1325 // sets, or a mixture of chars and sets -- syntax error.
1327 // We expect to see a single undefined variable (the one being
1329 if (undefinedVariableName
.length() == 0) {
1330 // "Missing '$' or duplicate definition"
1331 return syntaxError(U_BAD_VARIABLE_DEFINITION
, rule
, start
, status
);
1333 if (left
->text
.length() != 1 || left
->text
.charAt(0) != variableLimit
) {
1335 return syntaxError(U_MALFORMED_VARIABLE_DEFINITION
, rule
, start
, status
);
1337 if (left
->anchorStart
|| left
->anchorEnd
||
1338 right
->anchorStart
|| right
->anchorEnd
) {
1339 return syntaxError(U_MALFORMED_VARIABLE_DEFINITION
, rule
, start
, status
);
1341 // We allow anything on the right, including an empty string.
1342 UnicodeString
* value
= new UnicodeString(right
->text
);
1343 // NULL pointer check
1344 if (value
== NULL
) {
1345 return syntaxError(U_MEMORY_ALLOCATION_ERROR
, rule
, start
, status
);
1347 variableNames
.put(undefinedVariableName
, value
, status
);
1352 // If this is not a variable definition rule, we shouldn't have
1353 // any undefined variable names.
1354 if (undefinedVariableName
.length() != 0) {
1355 return syntaxError(// "Undefined variable $" + undefinedVariableName,
1356 U_UNDEFINED_VARIABLE
,
1357 rule
, start
, status
);
1361 if (segmentStandins
.length() > segmentObjects
.size()) {
1362 syntaxError(U_UNDEFINED_SEGMENT_REFERENCE
, rule
, start
, status
);
1364 for (i
=0; i
<segmentStandins
.length(); ++i
) {
1365 if (segmentStandins
.charAt(i
) == 0) {
1366 syntaxError(U_INTERNAL_TRANSLITERATOR_ERROR
, rule
, start
, status
); // will never happen
1369 for (i
=0; i
<segmentObjects
.size(); ++i
) {
1370 if (segmentObjects
.elementAt(i
) == NULL
) {
1371 syntaxError(U_INTERNAL_TRANSLITERATOR_ERROR
, rule
, start
, status
); // will never happen
1375 // If the direction we want doesn't match the rule
1376 // direction, do nothing.
1377 if (op
!= FWDREV_RULE_OP
&&
1378 ((direction
== UTRANS_FORWARD
) != (op
== FORWARD_RULE_OP
))) {
1382 // Transform the rule into a forward rule by swapping the
1383 // sides if necessary.
1384 if (direction
== UTRANS_REVERSE
) {
1389 // Remove non-applicable elements in forward-reverse
1390 // rules. Bidirectional rules ignore elements that do not
1392 if (op
== FWDREV_RULE_OP
) {
1393 right
->removeContext();
1395 left
->cursorOffset
= 0;
1398 // Normalize context
1399 if (left
->ante
< 0) {
1402 if (left
->post
< 0) {
1403 left
->post
= left
->text
.length();
1406 // Context is only allowed on the input side. Cursors are only
1407 // allowed on the output side. Segment delimiters can only appear
1408 // on the left, and references on the right. Cursor offset
1409 // cannot appear without an explicit cursor. Cursor offset
1410 // cannot place the cursor outside the limits of the context.
1411 // Anchors are only allowed on the input side.
1412 if (right
->ante
>= 0 || right
->post
>= 0 || left
->cursor
>= 0 ||
1413 (right
->cursorOffset
!= 0 && right
->cursor
< 0) ||
1414 // - The following two checks were used to ensure that the
1415 // - the cursor offset stayed within the ante- or postcontext.
1416 // - However, with the addition of quantifiers, we have to
1417 // - allow arbitrary cursor offsets and do runtime checking.
1418 //(right->cursorOffset > (left->text.length() - left->post)) ||
1419 //(-right->cursorOffset > left->ante) ||
1420 right
->anchorStart
|| right
->anchorEnd
||
1421 !left
->isValidInput(*this) || !right
->isValidOutput(*this) ||
1422 left
->ante
> left
->post
) {
1424 return syntaxError(U_MALFORMED_RULE
, rule
, start
, status
);
1427 // Flatten segment objects vector to an array
1428 UnicodeFunctor
** segmentsArray
= NULL
;
1429 if (segmentObjects
.size() > 0) {
1430 segmentsArray
= (UnicodeFunctor
**)uprv_malloc(segmentObjects
.size() * sizeof(UnicodeFunctor
*));
1431 // Null pointer check
1432 if (segmentsArray
== NULL
) {
1433 return syntaxError(U_MEMORY_ALLOCATION_ERROR
, rule
, start
, status
);
1435 segmentObjects
.toArray((void**) segmentsArray
);
1437 TransliterationRule
* temptr
= new TransliterationRule(
1438 left
->text
, left
->ante
, left
->post
,
1439 right
->text
, right
->cursor
, right
->cursorOffset
,
1441 segmentObjects
.size(),
1442 left
->anchorStart
, left
->anchorEnd
,
1445 //Null pointer check
1446 if (temptr
== NULL
) {
1447 uprv_free(segmentsArray
);
1448 return syntaxError(U_MEMORY_ALLOCATION_ERROR
, rule
, start
, status
);
1451 curData
->ruleSet
.addRule(temptr
, status
);
1457 * Called by main parser upon syntax error. Search the rule string
1458 * for the probable end of the rule. Of course, if the error is that
1459 * the end of rule marker is missing, then the rule end will not be found.
1460 * In any case the rule start will be correctly reported.
1461 * @param msg error description
1462 * @param rule pattern string
1463 * @param start position of first character of current rule
1465 int32_t TransliteratorParser::syntaxError(UErrorCode parseErrorCode
,
1466 const UnicodeString
& rule
,
1470 parseError
.offset
= pos
;
1471 parseError
.line
= 0 ; /* we are not using line numbers */
1474 const int32_t LEN
= U_PARSE_CONTEXT_LEN
- 1;
1475 int32_t start
= uprv_max(pos
- LEN
, 0);
1478 rule
.extract(start
,stop
-start
,parseError
.preContext
);
1479 //null terminate the buffer
1480 parseError
.preContext
[stop
-start
] = 0;
1484 stop
= uprv_min(pos
+ LEN
, rule
.length());
1486 rule
.extract(start
,stop
-start
,parseError
.postContext
);
1487 //null terminate the buffer
1488 parseError
.postContext
[stop
-start
]= 0;
1490 status
= (UErrorCode
)parseErrorCode
;
1496 * Parse a UnicodeSet out, store it, and return the stand-in character
1497 * used to represent it.
1499 UChar
TransliteratorParser::parseSet(const UnicodeString
& rule
,
1501 UErrorCode
& status
) {
1502 UnicodeSet
* set
= new UnicodeSet(rule
, pos
, USET_IGNORE_SPACE
, parseData
, status
);
1503 // Null pointer check
1505 status
= U_MEMORY_ALLOCATION_ERROR
;
1506 return (UChar
)0x0000; // Return empty character with error.
1509 return generateStandInFor(set
, status
);
1513 * Generate and return a stand-in for a new UnicodeFunctor. Store
1514 * the matcher (adopt it).
1516 UChar
TransliteratorParser::generateStandInFor(UnicodeFunctor
* adopted
, UErrorCode
& status
) {
1517 // assert(obj != null);
1519 // Look up previous stand-in, if any. This is a short list
1520 // (typical n is 0, 1, or 2); linear search is optimal.
1521 for (int32_t i
=0; i
<variablesVector
.size(); ++i
) {
1522 if (variablesVector
.elementAt(i
) == adopted
) { // [sic] pointer comparison
1523 return (UChar
) (curData
->variablesBase
+ i
);
1527 if (variableNext
>= variableLimit
) {
1529 status
= U_VARIABLE_RANGE_EXHAUSTED
;
1532 variablesVector
.addElement(adopted
, status
);
1533 return variableNext
++;
1537 * Return the standin for segment seg (1-based).
1539 UChar
TransliteratorParser::getSegmentStandin(int32_t seg
, UErrorCode
& status
) {
1540 // Special character used to indicate an empty spot
1541 UChar empty
= curData
->variablesBase
- 1;
1542 while (segmentStandins
.length() < seg
) {
1543 segmentStandins
.append(empty
);
1545 UChar c
= segmentStandins
.charAt(seg
-1);
1547 if (variableNext
>= variableLimit
) {
1548 status
= U_VARIABLE_RANGE_EXHAUSTED
;
1552 // Set a placeholder in the master variables vector that will be
1553 // filled in later by setSegmentObject(). We know that we will get
1554 // called first because setSegmentObject() will call us.
1555 variablesVector
.addElement((void*) NULL
, status
);
1556 segmentStandins
.setCharAt(seg
-1, c
);
1562 * Set the object for segment seg (1-based).
1564 void TransliteratorParser::setSegmentObject(int32_t seg
, StringMatcher
* adopted
, UErrorCode
& status
) {
1565 // Since we call parseSection() recursively, nested
1566 // segments will result in segment i+1 getting parsed
1567 // and stored before segment i; be careful with the
1568 // vector handling here.
1569 if (segmentObjects
.size() < seg
) {
1570 segmentObjects
.setSize(seg
, status
);
1572 int32_t index
= getSegmentStandin(seg
, status
) - curData
->variablesBase
;
1573 if (segmentObjects
.elementAt(seg
-1) != NULL
||
1574 variablesVector
.elementAt(index
) != NULL
) {
1575 // should never happen
1576 status
= U_INTERNAL_TRANSLITERATOR_ERROR
;
1579 segmentObjects
.setElementAt(adopted
, seg
-1);
1580 variablesVector
.setElementAt(adopted
, index
);
1584 * Return the stand-in for the dot set. It is allocated the first
1585 * time and reused thereafter.
1587 UChar
TransliteratorParser::getDotStandIn(UErrorCode
& status
) {
1588 if (dotStandIn
== (UChar
) -1) {
1589 UnicodeSet
* tempus
= new UnicodeSet(DOT_SET
, status
);
1590 // Null pointer check.
1591 if (tempus
== NULL
) {
1592 status
= U_MEMORY_ALLOCATION_ERROR
;
1593 return (UChar
)0x0000;
1595 dotStandIn
= generateStandInFor(tempus
, status
);
1601 * Append the value of the given variable name to the given
1604 void TransliteratorParser::appendVariableDef(const UnicodeString
& name
,
1606 UErrorCode
& status
) {
1607 const UnicodeString
* s
= (const UnicodeString
*) variableNames
.get(name
);
1609 // We allow one undefined variable so that variable definition
1610 // statements work. For the first undefined variable we return
1611 // the special placeholder variableLimit-1, and save the variable
1613 if (undefinedVariableName
.length() == 0) {
1614 undefinedVariableName
= name
;
1615 if (variableNext
>= variableLimit
) {
1616 // throw new RuntimeException("Private use variables exhausted");
1617 status
= U_ILLEGAL_ARGUMENT_ERROR
;
1620 buf
.append((UChar
) --variableLimit
);
1622 //throw new IllegalArgumentException("Undefined variable $"
1624 status
= U_ILLEGAL_ARGUMENT_ERROR
;
1633 * Glue method to get around access restrictions in C++.
1635 /*Transliterator* TransliteratorParser::createBasicInstance(const UnicodeString& id, const UnicodeString* canonID) {
1636 return Transliterator::createBasicInstance(id, canonID);
1642 utrans_stripRules(const UChar
*source
, int32_t sourceLen
, UChar
*target
, UErrorCode
*status
) {
1645 //const UChar *sourceStart = source;
1646 const UChar
*targetStart
= target
;
1647 const UChar
*sourceLimit
= source
+sourceLen
;
1648 UChar
*targetLimit
= target
+sourceLen
;
1650 UBool quoted
= FALSE
;
1653 uprv_memset(target
, 0, sourceLen
*U_SIZEOF_UCHAR
);
1655 /* read the rules into the buffer */
1656 while (source
< sourceLimit
)
1659 U16_NEXT_UNSAFE(source
, index
, c
);
1662 quoted
= (UBool
)!quoted
;
1665 if (c
== RULE_COMMENT_CHAR
) {
1666 /* skip comments and all preceding spaces */
1667 while (targetStart
< target
&& *(target
- 1) == 0x0020) {
1673 while (c
!= CR
&& c
!= LF
);
1675 else if (c
== ESCAPE
) {
1676 UChar32 c2
= *source
;
1677 if (c2
== CR
|| c2
== LF
) {
1678 /* A backslash at the end of a line. */
1679 /* Since we're stripping lines, ignore the backslash. */
1683 if (c2
== 0x0075 && source
+5 < sourceLimit
) { /* \u seen. \U isn't unescaped. */
1684 int32_t escapeOffset
= 0;
1685 UnicodeString
escapedStr(source
, 5);
1686 c2
= escapedStr
.unescapeAt(escapeOffset
);
1688 if (c2
== (UChar32
)0xFFFFFFFF || escapeOffset
== 0)
1690 *status
= U_PARSE_ERROR
;
1693 if (!uprv_isRuleWhiteSpace(c2
) && !u_iscntrl(c2
) && !u_ispunct(c2
)) {
1694 /* It was escaped for a reason. Write what it was suppose to be. */
1699 else if (c2
== QUOTE
) {
1700 /* \' seen. Make sure we don't do anything when we see it again. */
1701 quoted
= (UBool
)!quoted
;
1705 if (c
== CR
|| c
== LF
)
1707 /* ignore spaces carriage returns, and all leading spaces on the next line.
1708 * and line feed unless in the form \uXXXX
1711 while (source
< sourceLimit
) {
1713 if (c
!= CR
&& c
!= LF
&& c
!= 0x0020) {
1721 /* Append UChar * after dissembling if c > 0xffff*/
1723 U16_APPEND_UNSAFE(target
, index
, c
);
1726 if (target
< targetLimit
) {
1729 return (int32_t)(target
-targetStart
);
1732 #endif /* #if !UCONFIG_NO_TRANSLITERATION */