2017-01-20 00:20:31 +00:00
// © 2016 and later: Unicode, Inc. and others.
2016-06-15 18:58:17 +00:00
// License & terms of use: http://www.unicode.org/copyright.html
1999-11-20 00:40:50 +00:00
/*
2004-10-29 23:42:49 +00:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2016-05-31 21:45:07 +00:00
* Copyright ( C ) 1999 - 2016 , International Business Machines
* Corporation and others . All Rights Reserved .
2004-10-29 23:42:49 +00:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Date Name Description
* 11 / 17 / 99 aliu Creation .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
2002-06-27 01:19:20 +00:00
# include "unicode/utypes.h"
2002-09-20 01:54:48 +00:00
# if !UCONFIG_NO_TRANSLITERATION
2002-06-27 01:19:20 +00:00
# include "unicode/uobject.h"
# include "unicode/parseerr.h"
# include "unicode/parsepos.h"
# include "unicode/putil.h"
# include "unicode/uchar.h"
# include "unicode/ustring.h"
# include "unicode/uniset.h"
2011-07-27 05:53:56 +00:00
# include "unicode/utf16.h"
2001-07-27 00:18:53 +00:00
# include "cstring.h"
2002-02-07 01:07:55 +00:00
# include "funcrepl.h"
2001-07-27 00:18:53 +00:00
# include "hash.h"
# include "quant.h"
2003-02-14 01:44:49 +00:00
# include "rbt.h"
2001-07-27 00:18:53 +00:00
# include "rbt_data.h"
1999-11-20 00:40:50 +00:00
# include "rbt_pars.h"
# include "rbt_rule.h"
2001-07-27 00:18:53 +00:00
# include "strmatch.h"
2002-02-07 01:07:55 +00:00
# include "strrepl.h"
2003-10-14 21:47:59 +00:00
# include "unicode/symtable.h"
2002-02-07 01:07:55 +00:00
# include "tridpars.h"
2001-10-10 19:29:45 +00:00
# include "uvector.h"
2005-06-09 17:30:48 +00:00
# include "hash.h"
2011-04-26 06:39:29 +00:00
# include "patternprops.h"
2001-11-27 21:54:58 +00:00
# include "util.h"
2002-07-16 17:50:42 +00:00
# include "cmemory.h"
2002-07-26 22:07:49 +00:00
# include "uprops.h"
2004-10-18 02:43:33 +00:00
# include "putilimp.h"
1999-11-20 00:40:50 +00:00
// Operators
2000-05-20 04:40:29 +00:00
# define VARIABLE_DEF_OP ((UChar)0x003D) /*=*/
# define FORWARD_RULE_OP ((UChar)0x003E) /*>*/
# define REVERSE_RULE_OP ((UChar)0x003C) /*<*/
# define FWDREV_RULE_OP ((UChar)0x007E) /*~*/ // internal rep of <> op
1999-11-20 00:40:50 +00:00
// Other special characters
2000-05-20 04:40:29 +00:00
# define QUOTE ((UChar)0x0027) /*'*/
# define ESCAPE ((UChar)0x005C) /*\*/
# define END_OF_RULE ((UChar)0x003B) /*;*/
# define RULE_COMMENT_CHAR ((UChar)0x0023) /*#*/
# define SEGMENT_OPEN ((UChar)0x0028) /*(*/
# define SEGMENT_CLOSE ((UChar)0x0029) /*)*/
# define CONTEXT_ANTE ((UChar)0x007B) /*{*/
# define CONTEXT_POST ((UChar)0x007D) /*}*/
# define CURSOR_POS ((UChar)0x007C) /*|*/
# define CURSOR_OFFSET ((UChar)0x0040) /*@*/
2000-08-30 20:43:25 +00:00
# define ANCHOR_START ((UChar)0x005E) /*^*/
2001-07-27 00:18:53 +00:00
# define KLEENE_STAR ((UChar)0x002A) /***/
# define ONE_OR_MORE ((UChar)0x002B) /*+*/
2001-07-30 23:23:51 +00:00
# define ZERO_OR_ONE ((UChar)0x003F) /*?*/
2000-08-30 20:43:25 +00:00
2001-10-17 19:21:12 +00:00
# define DOT ((UChar)46) /*.*/
static const UChar DOT_SET [ ] = { // "[^[:Zp:][:Zl:]\r\n$]";
91 , 94 , 91 , 58 , 90 , 112 , 58 , 93 , 91 , 58 , 90 ,
108 , 58 , 93 , 92 , 114 , 92 , 110 , 36 , 93 , 0
} ;
2002-02-07 01:07:55 +00:00
// A function is denoted &Source-Target/Variant(text)
# define FUNCTION ((UChar)38) /*&*/
2003-04-23 00:20:16 +00:00
// Aliases for some of the syntax characters. These are provided so
// transliteration rules can be expressed in XML without clashing with
// XML syntax characters '<', '>', and '&'.
# define ALT_REVERSE_RULE_OP ((UChar)0x2190) // Left Arrow
# define ALT_FORWARD_RULE_OP ((UChar)0x2192) // Right Arrow
# define ALT_FWDREV_RULE_OP ((UChar)0x2194) // Left Right Arrow
# define ALT_FUNCTION ((UChar)0x2206) // Increment (~Greek Capital Delta)
2002-02-07 01:07:55 +00:00
// Special characters disallowed at the top level
static const UChar ILLEGAL_TOP [ ] = { 41 , 0 } ; // ")"
// Special characters disallowed within a segment
static const UChar ILLEGAL_SEG [ ] = { 123 , 125 , 124 , 64 , 0 } ; // "{}|@"
// Special characters disallowed within a function argument
static const UChar ILLEGAL_FUNC [ ] = { 94 , 40 , 46 , 42 , 43 , 63 , 123 , 125 , 124 , 64 , 0 } ; // "^(.*+?{}|@"
2000-08-30 20:43:25 +00:00
// By definition, the ANCHOR_END special character is a
// trailing SymbolTable.SYMBOL_REF character.
// private static final char ANCHOR_END = '$';
2000-05-20 04:40:29 +00:00
2003-04-23 00:20:16 +00:00
static const UChar gOPERATORS [ ] = { // "=><"
VARIABLE_DEF_OP , FORWARD_RULE_OP , REVERSE_RULE_OP ,
ALT_FORWARD_RULE_OP , ALT_REVERSE_RULE_OP , ALT_FWDREV_RULE_OP ,
0
2001-08-06 23:57:45 +00:00
} ;
2001-07-13 21:09:41 +00:00
2003-04-23 00:20:16 +00:00
static const UChar HALF_ENDERS [ ] = { // "=><;"
VARIABLE_DEF_OP , FORWARD_RULE_OP , REVERSE_RULE_OP ,
ALT_FORWARD_RULE_OP , ALT_REVERSE_RULE_OP , ALT_FWDREV_RULE_OP ,
END_OF_RULE ,
0
2001-10-30 18:08:53 +00:00
} ;
2001-07-13 21:09:41 +00:00
// These are also used in Transliterator::toRules()
static const int32_t ID_TOKEN_LEN = 2 ;
static const UChar ID_TOKEN [ ] = { 0x3A , 0x3A } ; // ':', ':'
1999-11-20 00:40:50 +00:00
2005-06-09 17:30:48 +00:00
/*
commented out until we do real : : BEGIN / : : END functionality
static const int32_t BEGIN_TOKEN_LEN = 5 ;
static const UChar BEGIN_TOKEN [ ] = { 0x42 , 0x45 , 0x47 , 0x49 , 0x4e } ; // 'BEGIN'
static const int32_t END_TOKEN_LEN = 3 ;
static const UChar END_TOKEN [ ] = { 0x45 , 0x4e , 0x44 } ; // 'END'
*/
2001-10-08 23:26:58 +00:00
U_NAMESPACE_BEGIN
2000-02-08 02:49:15 +00:00
//----------------------------------------------------------------------
// BEGIN ParseData
//----------------------------------------------------------------------
/**
* This class implements the SymbolTable interface . It is used
* during parsing to give UnicodeSet access to variables that
2001-07-27 00:18:53 +00:00
* have been defined so far . Note that it uses variablesVector ,
2000-02-08 02:49:15 +00:00
* _not_ data . setVariables .
*/
2002-10-04 17:30:36 +00:00
class ParseData : public UMemory , public SymbolTable {
2000-02-08 02:49:15 +00:00
public :
const TransliterationRuleData * data ; // alias
2001-07-27 00:18:53 +00:00
const UVector * variablesVector ; // alias
2000-02-08 02:49:15 +00:00
2005-06-09 17:30:48 +00:00
const Hashtable * variableNames ; // alias
2000-02-08 02:49:15 +00:00
ParseData ( const TransliterationRuleData * data = 0 ,
2005-06-09 17:30:48 +00:00
const UVector * variablesVector = 0 ,
const Hashtable * variableNames = 0 ) ;
2000-02-08 02:49:15 +00:00
2011-07-26 05:32:25 +00:00
virtual ~ ParseData ( ) ;
2000-05-20 04:40:29 +00:00
virtual const UnicodeString * lookup ( const UnicodeString & s ) const ;
2002-02-07 01:07:55 +00:00
virtual const UnicodeFunctor * lookupMatcher ( UChar32 ch ) const ;
2000-05-20 04:40:29 +00:00
virtual UnicodeString parseReference ( const UnicodeString & text ,
ParsePosition & pos , int32_t limit ) const ;
2002-02-07 01:07:55 +00:00
/**
* Return true if the given character is a matcher standin or a plain
* character ( non standin ) .
*/
UBool isMatcher ( UChar32 ch ) ;
/**
* Return true if the given character is a replacer standin or a plain
* character ( non standin ) .
*/
UBool isReplacer ( UChar32 ch ) ;
2002-06-29 00:04:16 +00:00
private :
2002-10-04 17:30:36 +00:00
ParseData ( const ParseData & other ) ; // forbid copying of this class
ParseData & operator = ( const ParseData & other ) ; // forbid copying of this class
2000-02-08 02:49:15 +00:00
} ;
ParseData : : ParseData ( const TransliterationRuleData * d ,
2005-06-09 17:30:48 +00:00
const UVector * sets ,
const Hashtable * vNames ) :
data ( d ) , variablesVector ( sets ) , variableNames ( vNames ) { }
2000-02-08 02:49:15 +00:00
2011-07-26 05:32:25 +00:00
ParseData : : ~ ParseData ( ) { }
2000-02-08 02:49:15 +00:00
/**
2000-05-20 04:40:29 +00:00
* Implement SymbolTable API .
2000-02-08 02:49:15 +00:00
*/
2000-05-20 04:40:29 +00:00
const UnicodeString * ParseData : : lookup ( const UnicodeString & name ) const {
2005-06-09 17:30:48 +00:00
return ( const UnicodeString * ) variableNames - > get ( name ) ;
2000-05-20 04:40:29 +00:00
}
/**
* Implement SymbolTable API .
*/
2002-02-07 01:07:55 +00:00
const UnicodeFunctor * ParseData : : lookupMatcher ( UChar32 ch ) const {
2000-05-20 04:40:29 +00:00
// Note that we cannot use data.lookupSet() because the
// set array has not been constructed yet.
2002-02-07 01:07:55 +00:00
const UnicodeFunctor * set = NULL ;
2001-07-27 00:18:53 +00:00
int32_t i = ch - data - > variablesBase ;
if ( i > = 0 & & i < variablesVector - > size ( ) ) {
int32_t i = ch - data - > variablesBase ;
set = ( i < variablesVector - > size ( ) ) ?
2002-02-07 01:07:55 +00:00
( UnicodeFunctor * ) variablesVector - > elementAt ( i ) : 0 ;
2000-02-08 02:49:15 +00:00
}
2000-05-20 04:40:29 +00:00
return set ;
}
/**
* Implement SymbolTable API . Parse out a symbol reference
* name .
*/
UnicodeString ParseData : : parseReference ( const UnicodeString & text ,
ParsePosition & pos , int32_t limit ) const {
int32_t start = pos . getIndex ( ) ;
int32_t i = start ;
UnicodeString result ;
while ( i < limit ) {
UChar c = text . charAt ( i ) ;
2001-08-06 23:57:45 +00:00
if ( ( i = = start & & ! u_isIDStart ( c ) ) | | ! u_isIDPart ( c ) ) {
2000-05-20 04:40:29 +00:00
break ;
}
+ + i ;
}
if ( i = = start ) { // No valid name chars
return result ; // Indicate failure with empty string
}
pos . setIndex ( i ) ;
text . extractBetween ( start , i , result ) ;
return result ;
}
2002-02-07 01:07:55 +00:00
UBool ParseData : : isMatcher ( UChar32 ch ) {
// Note that we cannot use data.lookup() because the
// set array has not been constructed yet.
int32_t i = ch - data - > variablesBase ;
if ( i > = 0 & & i < variablesVector - > size ( ) ) {
2002-03-29 18:16:08 +00:00
UnicodeFunctor * f = ( UnicodeFunctor * ) variablesVector - > elementAt ( i ) ;
2002-04-02 02:55:31 +00:00
return f ! = NULL & & f - > toMatcher ( ) ! = NULL ;
2002-02-07 01:07:55 +00:00
}
return TRUE ;
}
/**
* Return true if the given character is a replacer standin or a plain
* character ( non standin ) .
*/
UBool ParseData : : isReplacer ( UChar32 ch ) {
// Note that we cannot use data.lookup() because the
// set array has not been constructed yet.
int i = ch - data - > variablesBase ;
if ( i > = 0 & & i < variablesVector - > size ( ) ) {
2002-03-29 18:16:08 +00:00
UnicodeFunctor * f = ( UnicodeFunctor * ) variablesVector - > elementAt ( i ) ;
2002-04-02 02:55:31 +00:00
return f ! = NULL & & f - > toReplacer ( ) ! = NULL ;
2002-02-07 01:07:55 +00:00
}
return TRUE ;
}
2000-05-20 04:40:29 +00:00
//----------------------------------------------------------------------
// BEGIN RuleHalf
//----------------------------------------------------------------------
/**
* A class representing one side of a rule . This class knows how to
* parse half of a rule . It is tightly coupled to the method
* RuleBasedTransliterator . Parser . parseRule ( ) .
*/
2002-10-04 18:06:33 +00:00
class RuleHalf : public UMemory {
2000-05-20 04:40:29 +00:00
public :
UnicodeString text ;
int32_t cursor ; // position of cursor in text
int32_t ante ; // position of ante context marker '{' in text
int32_t post ; // position of post context marker '}' in text
// Record the offset to the cursor either to the left or to the
// right of the key. This is indicated by characters on the output
// side that allow the cursor to be positioned arbitrarily within
// the matching text. For example, abc{def} > | @@@ xyz; changes
// def to xyz and moves the cursor to before abc. Offset characters
// must be at the start or end, and they cannot move the cursor past
// the ante- or postcontext text. Placeholders are only valid in
2001-11-27 21:34:15 +00:00
// output text. The length of the ante and post context is
// determined at runtime, because of supplementals and quantifiers.
2000-05-20 04:40:29 +00:00
int32_t cursorOffset ; // only nonzero on output side
2001-10-30 18:08:53 +00:00
// Position of first CURSOR_OFFSET on _right_. This will be -1
// for |@, -2 for |@@, etc., and 1 for @|, 2 for @@|, etc.
int32_t cursorOffsetPos ;
2000-08-30 20:43:25 +00:00
UBool anchorStart ;
UBool anchorEnd ;
2001-10-30 18:08:53 +00:00
/**
2002-02-07 01:07:55 +00:00
* The segment number from 1. . n of the next ' ( ' we see
* during parsing ; 1 - based .
2001-10-30 18:08:53 +00:00
*/
int32_t nextSegmentNumber ;
2000-08-30 20:43:25 +00:00
2001-07-13 21:09:41 +00:00
TransliteratorParser & parser ;
2000-05-20 04:40:29 +00:00
//--------------------------------------------------
// Methods
2001-07-13 21:09:41 +00:00
RuleHalf ( TransliteratorParser & parser ) ;
2000-05-20 04:40:29 +00:00
~ RuleHalf ( ) ;
2006-07-13 20:29:11 +00:00
int32_t parse ( const UnicodeString & rule , int32_t pos , int32_t limit , UErrorCode & status ) ;
2000-05-20 04:40:29 +00:00
2001-10-30 18:08:53 +00:00
int32_t parseSection ( const UnicodeString & rule , int32_t pos , int32_t limit ,
UnicodeString & buf ,
2002-02-07 01:07:55 +00:00
const UnicodeString & illegal ,
2006-07-13 20:29:11 +00:00
UBool isSegment ,
UErrorCode & status ) ;
2001-10-30 18:08:53 +00:00
2000-05-20 04:40:29 +00:00
/**
* Remove context .
*/
void removeContext ( ) ;
/**
2002-02-07 01:07:55 +00:00
* Return true if this half looks like valid output , that is , does not
* contain quantifiers or other special input - only elements .
*/
UBool isValidOutput ( TransliteratorParser & parser ) ;
/**
* Return true if this half looks like valid input , that is , does not
* contain functions or other special output - only elements .
2000-05-20 04:40:29 +00:00
*/
2002-02-07 01:07:55 +00:00
UBool isValidInput ( TransliteratorParser & parser ) ;
2000-05-20 04:40:29 +00:00
2001-08-17 02:20:35 +00:00
int syntaxError ( UErrorCode code ,
2000-05-20 04:40:29 +00:00
const UnicodeString & rule ,
2006-07-13 20:29:11 +00:00
int32_t start ,
UErrorCode & status ) {
return parser . syntaxError ( code , rule , start , status ) ;
2000-05-20 04:40:29 +00:00
}
2000-08-15 18:25:20 +00:00
private :
// Disallowed methods; no impl.
RuleHalf ( const RuleHalf & ) ;
RuleHalf & operator = ( const RuleHalf & ) ;
2000-05-20 04:40:29 +00:00
} ;
2001-10-30 18:08:53 +00:00
RuleHalf : : RuleHalf ( TransliteratorParser & p ) :
parser ( p )
{
2000-05-20 04:40:29 +00:00
cursor = - 1 ;
ante = - 1 ;
post = - 1 ;
cursorOffset = 0 ;
2001-10-30 18:08:53 +00:00
cursorOffsetPos = 0 ;
2000-08-30 20:43:25 +00:00
anchorStart = anchorEnd = FALSE ;
2002-02-07 01:07:55 +00:00
nextSegmentNumber = 1 ;
2000-05-20 04:40:29 +00:00
}
RuleHalf : : ~ RuleHalf ( ) {
}
/**
* Parse one side of a rule , stopping at either the limit ,
2001-10-30 18:08:53 +00:00
* the END_OF_RULE character , or an operator .
* @ return the index after the terminating character , or
* if limit was reached , limit
2000-05-20 04:40:29 +00:00
*/
2006-07-13 20:29:11 +00:00
int32_t RuleHalf : : parse ( const UnicodeString & rule , int32_t pos , int32_t limit , UErrorCode & status ) {
2000-05-20 04:40:29 +00:00
int32_t start = pos ;
2001-10-30 18:08:53 +00:00
text . truncate ( 0 ) ;
2011-07-07 18:46:19 +00:00
pos = parseSection ( rule , pos , limit , text , UnicodeString ( TRUE , ILLEGAL_TOP , - 1 ) , FALSE , status ) ;
2001-10-30 18:08:53 +00:00
if ( cursorOffset > 0 & & cursor ! = cursorOffsetPos ) {
2006-07-13 20:29:11 +00:00
return syntaxError ( U_MISPLACED_CURSOR_OFFSET , rule , start , status ) ;
2001-10-30 18:08:53 +00:00
}
return pos ;
}
/**
* Parse a section of one side of a rule , stopping at either
* the limit , the END_OF_RULE character , an operator , or a
* segment close character . This method parses both a
* top - level rule half and a segment within such a rule half .
* It calls itself recursively to parse segments and nested
* segments .
* @ param buf buffer into which to accumulate the rule pattern
* characters , either literal characters from the rule or
* standins for UnicodeMatcher objects including segments .
2002-02-07 01:07:55 +00:00
* @ param illegal the set of special characters that is illegal during
* this parse .
2001-10-30 18:08:53 +00:00
* @ param isSegment if true , then we ' ve already seen a ' ( ' and
* pos on entry points right after it . Accumulate everything
* up to the closing ' ) ' , put it in a segment matcher object ,
* generate a standin for it , and add the standin to buf . As
* a side effect , update the segments vector with a reference
* to the segment matcher . This works recursively for nested
* segments . If isSegment is false , just accumulate
* characters into buf .
* @ return the index after the terminating character , or
* if limit was reached , limit
*/
int32_t RuleHalf : : parseSection ( const UnicodeString & rule , int32_t pos , int32_t limit ,
UnicodeString & buf ,
2002-02-07 01:07:55 +00:00
const UnicodeString & illegal ,
2006-07-13 20:29:11 +00:00
UBool isSegment , UErrorCode & status ) {
2001-10-30 18:08:53 +00:00
int32_t start = pos ;
2000-05-20 04:40:29 +00:00
ParsePosition pp ;
UnicodeString scratch ;
2001-09-18 17:32:33 +00:00
UBool done = FALSE ;
2001-07-27 17:02:59 +00:00
int32_t quoteStart = - 1 ; // Most recent 'single quoted string'
int32_t quoteLimit = - 1 ;
int32_t varStart = - 1 ; // Most recent $variableReference
int32_t varLimit = - 1 ;
2002-02-07 01:07:55 +00:00
int32_t bufStart = buf . length ( ) ;
2001-10-30 18:08:53 +00:00
2000-05-20 04:40:29 +00:00
while ( pos < limit & & ! done ) {
2001-11-27 21:36:24 +00:00
// Since all syntax characters are in the BMP, fetching
// 16-bit code units suffices here.
2000-05-20 04:40:29 +00:00
UChar c = rule . charAt ( pos + + ) ;
2011-04-26 06:39:29 +00:00
if ( PatternProps : : isWhiteSpace ( c ) ) {
2000-05-20 04:40:29 +00:00
// Ignore whitespace. Note that this is not Unicode
// spaces, but Java spaces -- a subset, representing
// whitespace likely to be seen in code.
continue ;
}
2001-10-30 18:08:53 +00:00
if ( u_strchr ( HALF_ENDERS , c ) ! = NULL ) {
if ( isSegment ) {
// Unclosed segment
2006-07-13 20:29:11 +00:00
return syntaxError ( U_UNCLOSED_SEGMENT , rule , start , status ) ;
2001-10-30 18:08:53 +00:00
}
2000-08-30 20:43:25 +00:00
break ;
}
if ( anchorEnd ) {
// Text after a presumed end anchor is a syntax err
2006-07-13 20:29:11 +00:00
return syntaxError ( U_MALFORMED_VARIABLE_REFERENCE , rule , start , status ) ;
2000-08-30 20:43:25 +00:00
}
2001-10-17 19:21:12 +00:00
if ( UnicodeSet : : resemblesPattern ( rule , pos - 1 ) ) {
pp . setIndex ( pos - 1 ) ; // Backup to opening '['
2006-07-13 20:29:11 +00:00
buf . append ( parser . parseSet ( rule , pp , status ) ) ;
if ( U_FAILURE ( status ) ) {
return syntaxError ( U_MALFORMED_SET , rule , start , status ) ;
2001-10-17 19:21:12 +00:00
}
pos = pp . getIndex ( ) ;
continue ;
}
2000-05-20 04:40:29 +00:00
// Handle escapes
if ( c = = ESCAPE ) {
if ( pos = = limit ) {
2006-07-13 20:29:11 +00:00
return syntaxError ( U_TRAILING_BACKSLASH , rule , start , status ) ;
2000-05-20 04:40:29 +00:00
}
2000-07-16 13:42:38 +00:00
UChar32 escaped = rule . unescapeAt ( pos ) ; // pos is already past '\\'
if ( escaped = = ( UChar32 ) - 1 ) {
2006-07-13 20:29:11 +00:00
return syntaxError ( U_MALFORMED_UNICODE_ESCAPE , rule , start , status ) ;
2000-05-20 04:40:29 +00:00
}
2001-11-12 19:12:08 +00:00
if ( ! parser . checkVariableRange ( escaped ) ) {
2006-07-13 20:29:11 +00:00
return syntaxError ( U_VARIABLE_RANGE_OVERLAP , rule , start , status ) ;
2001-11-12 19:12:08 +00:00
}
2001-07-27 00:18:53 +00:00
buf . append ( escaped ) ;
2000-05-20 04:40:29 +00:00
continue ;
}
// Handle quoted matter
if ( c = = QUOTE ) {
int32_t iq = rule . indexOf ( QUOTE , pos ) ;
if ( iq = = pos ) {
buf . append ( c ) ; // Parse [''] outside quotes as [']
+ + pos ;
} else {
2002-02-07 01:07:55 +00:00
/* This loop picks up a run of quoted text of the
* form ' aaaa ' each time through . If this run
2000-05-20 04:40:29 +00:00
* hasn ' t really ended ( ' aaaa ' ' bbbb ' ) then it keeps
2002-02-07 01:07:55 +00:00
* looping , each time adding on a new run . When it
2000-05-20 04:40:29 +00:00
* reaches the final quote it breaks .
*/
2001-07-27 17:02:59 +00:00
quoteStart = buf . length ( ) ;
2000-05-20 04:40:29 +00:00
for ( ; ; ) {
if ( iq < 0 ) {
2006-07-13 20:29:11 +00:00
return syntaxError ( U_UNTERMINATED_QUOTE , rule , start , status ) ;
2000-05-20 04:40:29 +00:00
}
scratch . truncate ( 0 ) ;
rule . extractBetween ( pos , iq , scratch ) ;
buf . append ( scratch ) ;
pos = iq + 1 ;
if ( pos < limit & & rule . charAt ( pos ) = = QUOTE ) {
// Parse [''] inside quotes as [']
iq = rule . indexOf ( QUOTE , pos + 1 ) ;
// Continue looping
} else {
break ;
}
}
2001-07-27 17:02:59 +00:00
quoteLimit = buf . length ( ) ;
2001-11-12 19:12:08 +00:00
for ( iq = quoteStart ; iq < quoteLimit ; + + iq ) {
if ( ! parser . checkVariableRange ( buf . charAt ( iq ) ) ) {
2006-07-13 20:29:11 +00:00
return syntaxError ( U_VARIABLE_RANGE_OVERLAP , rule , start , status ) ;
2001-11-12 19:12:08 +00:00
}
}
2000-05-20 04:40:29 +00:00
}
continue ;
}
2001-11-12 19:12:08 +00:00
if ( ! parser . checkVariableRange ( c ) ) {
2006-07-13 20:29:11 +00:00
return syntaxError ( U_VARIABLE_RANGE_OVERLAP , rule , start , status ) ;
2001-11-12 19:12:08 +00:00
}
2002-02-07 01:07:55 +00:00
if ( illegal . indexOf ( c ) > = 0 ) {
2006-07-13 20:29:11 +00:00
syntaxError ( U_ILLEGAL_CHARACTER , rule , start , status ) ;
2002-02-07 01:07:55 +00:00
}
2000-05-20 04:40:29 +00:00
switch ( c ) {
2001-10-30 18:08:53 +00:00
//------------------------------------------------------
// Elements allowed within and out of segments
//------------------------------------------------------
2000-08-30 20:43:25 +00:00
case ANCHOR_START :
if ( buf . length ( ) = = 0 & & ! anchorStart ) {
anchorStart = TRUE ;
} else {
2001-08-16 00:55:16 +00:00
return syntaxError ( U_MISPLACED_ANCHOR_START ,
2006-07-13 20:29:11 +00:00
rule , start , status ) ;
2000-08-30 20:43:25 +00:00
}
break ;
2000-05-20 04:40:29 +00:00
case SEGMENT_OPEN :
2002-02-07 01:07:55 +00:00
{
// bufSegStart is the offset in buf to the first
// character of the segment we are parsing.
int32_t bufSegStart = buf . length ( ) ;
// Record segment number now, since nextSegmentNumber
// will be incremented during the call to parseSection
// if there are nested segments.
int32_t segmentNumber = nextSegmentNumber + + ; // 1-based
// Parse the segment
2011-07-07 18:46:19 +00:00
pos = parseSection ( rule , pos , limit , buf , UnicodeString ( TRUE , ILLEGAL_SEG , - 1 ) , TRUE , status ) ;
2002-02-07 01:07:55 +00:00
// After parsing a segment, the relevant characters are
// in buf, starting at offset bufSegStart. Extract them
// into a string matcher, and replace them with a
// standin for that matcher.
StringMatcher * m =
new StringMatcher ( buf , bufSegStart , buf . length ( ) ,
2005-06-09 17:30:48 +00:00
segmentNumber , * parser . curData ) ;
2008-01-14 22:29:44 +00:00
if ( m = = NULL ) {
2008-02-23 19:15:18 +00:00
return syntaxError ( U_MEMORY_ALLOCATION_ERROR , rule , start , status ) ;
2008-01-14 22:29:44 +00:00
}
2002-02-07 01:07:55 +00:00
// Record and associate object and segment number
2006-07-13 20:29:11 +00:00
parser . setSegmentObject ( segmentNumber , m , status ) ;
2002-02-07 01:07:55 +00:00
buf . truncate ( bufSegStart ) ;
2006-07-13 20:29:11 +00:00
buf . append ( parser . getSegmentStandin ( segmentNumber , status ) ) ;
2002-02-07 01:07:55 +00:00
}
break ;
case FUNCTION :
2003-04-23 00:20:16 +00:00
case ALT_FUNCTION :
2002-02-07 01:07:55 +00:00
{
int32_t iref = pos ;
2002-04-17 16:38:18 +00:00
TransliteratorIDParser : : SingleID * single =
TransliteratorIDParser : : parseFilterID ( rule , iref ) ;
2002-02-07 01:07:55 +00:00
// The next character MUST be a segment open
2002-04-17 16:38:18 +00:00
if ( single = = NULL | |
2002-02-07 01:07:55 +00:00
! ICU_Utility : : parseChar ( rule , iref , SEGMENT_OPEN ) ) {
2006-07-13 20:29:11 +00:00
return syntaxError ( U_INVALID_FUNCTION , rule , start , status ) ;
2002-02-07 01:07:55 +00:00
}
2002-04-17 16:38:18 +00:00
Transliterator * t = single - > createInstance ( ) ;
delete single ;
2002-02-07 01:07:55 +00:00
if ( t = = NULL ) {
2006-07-13 20:29:11 +00:00
return syntaxError ( U_INVALID_FUNCTION , rule , start , status ) ;
2002-02-07 01:07:55 +00:00
}
// bufSegStart is the offset in buf to the first
// character of the segment we are parsing.
int32_t bufSegStart = buf . length ( ) ;
// Parse the segment
2011-07-07 18:46:19 +00:00
pos = parseSection ( rule , iref , limit , buf , UnicodeString ( TRUE , ILLEGAL_FUNC , - 1 ) , TRUE , status ) ;
2002-02-07 01:07:55 +00:00
// After parsing a segment, the relevant characters are
// in buf, starting at offset bufSegStart.
UnicodeString output ;
buf . extractBetween ( bufSegStart , buf . length ( ) , output ) ;
FunctionReplacer * r =
2005-06-09 17:30:48 +00:00
new FunctionReplacer ( t , new StringReplacer ( output , parser . curData ) ) ;
2008-01-14 22:29:44 +00:00
if ( r = = NULL ) {
2008-02-23 19:15:18 +00:00
return syntaxError ( U_MEMORY_ALLOCATION_ERROR , rule , start , status ) ;
2008-01-14 22:29:44 +00:00
}
2002-02-07 01:07:55 +00:00
// Replace the buffer contents with a stand-in
buf . truncate ( bufSegStart ) ;
2006-07-13 20:29:11 +00:00
buf . append ( parser . generateStandInFor ( r , status ) ) ;
2002-02-07 01:07:55 +00:00
}
2000-05-20 04:40:29 +00:00
break ;
case SymbolTable : : SYMBOL_REF :
// Handle variable references and segment references "$1" .. "$9"
{
// A variable reference must be followed immediately
// by a Unicode identifier start and zero or more
// Unicode identifier part characters, or by a digit
// 1..9 if it is a segment reference.
if ( pos = = limit ) {
2000-08-30 20:43:25 +00:00
// A variable ref character at the end acts as
// an anchor to the context limit, as in perl.
anchorEnd = TRUE ;
break ;
2000-05-20 04:40:29 +00:00
}
2001-07-30 23:23:51 +00:00
// Parse "$1" "$2" .. "$9" .. (no upper limit)
2000-05-20 04:40:29 +00:00
c = rule . charAt ( pos ) ;
2002-02-07 01:07:55 +00:00
int32_t r = u_digit ( c , 10 ) ;
2000-05-20 04:40:29 +00:00
if ( r > = 1 & & r < = 9 ) {
2002-02-07 01:07:55 +00:00
r = ICU_Utility : : parseNumber ( rule , pos , 10 ) ;
if ( r < 0 ) {
return syntaxError ( U_UNDEFINED_SEGMENT_REFERENCE ,
2006-07-13 20:29:11 +00:00
rule , start , status ) ;
2000-05-20 04:40:29 +00:00
}
2006-07-13 20:29:11 +00:00
buf . append ( parser . getSegmentStandin ( r , status ) ) ;
2000-05-20 04:40:29 +00:00
} else {
pp . setIndex ( pos ) ;
UnicodeString name = parser . parseData - >
parseReference ( rule , pp , limit ) ;
if ( name . length ( ) = = 0 ) {
2000-08-30 20:43:25 +00:00
// This means the '$' was not followed by a
// valid name. Try to interpret it as an
// end anchor then. If this also doesn't work
// (if we see a following character) then signal
// an error.
anchorEnd = TRUE ;
break ;
2000-05-20 04:40:29 +00:00
}
pos = pp . getIndex ( ) ;
// If this is a variable definition statement,
// then the LHS variable will be undefined. In
// that case appendVariableDef() will append the
// special placeholder char variableLimit-1.
2001-07-27 17:02:59 +00:00
varStart = buf . length ( ) ;
2006-07-13 20:29:11 +00:00
parser . appendVariableDef ( name , buf , status ) ;
2001-07-27 17:02:59 +00:00
varLimit = buf . length ( ) ;
2000-05-20 04:40:29 +00:00
}
}
break ;
2001-10-30 18:08:53 +00:00
case DOT :
2006-07-13 20:29:11 +00:00
buf . append ( parser . getDotStandIn ( status ) ) ;
2001-10-30 18:08:53 +00:00
break ;
case KLEENE_STAR :
case ONE_OR_MORE :
case ZERO_OR_ONE :
// Quantifiers. We handle single characters, quoted strings,
// variable references, and segments.
// a+ matches aaa
// 'foo'+ matches foofoofoo
// $v+ matches xyxyxy if $v == xy
// (seg)+ matches segsegseg
{
2002-02-07 01:07:55 +00:00
if ( isSegment & & buf . length ( ) = = bufStart ) {
2001-10-30 18:08:53 +00:00
// The */+ immediately follows '('
2006-07-13 20:29:11 +00:00
return syntaxError ( U_MISPLACED_QUANTIFIER , rule , start , status ) ;
2001-10-30 18:08:53 +00:00
}
int32_t qstart , qlimit ;
// The */+ follows an isolated character or quote
// or variable reference
if ( buf . length ( ) = = quoteLimit ) {
// The */+ follows a 'quoted string'
qstart = quoteStart ;
qlimit = quoteLimit ;
} else if ( buf . length ( ) = = varLimit ) {
// The */+ follows a $variableReference
qstart = varStart ;
qlimit = varLimit ;
} else {
// The */+ follows a single character, possibly
// a segment standin
qstart = buf . length ( ) - 1 ;
qlimit = qstart + 1 ;
}
2002-02-07 01:07:55 +00:00
UnicodeFunctor * m =
2005-06-09 17:30:48 +00:00
new StringMatcher ( buf , qstart , qlimit , 0 , * parser . curData ) ;
2008-01-14 22:29:44 +00:00
if ( m = = NULL ) {
2008-02-23 19:15:18 +00:00
return syntaxError ( U_MEMORY_ALLOCATION_ERROR , rule , start , status ) ;
2008-01-14 22:29:44 +00:00
}
2001-10-30 18:08:53 +00:00
int32_t min = 0 ;
int32_t max = Quantifier : : MAX ;
switch ( c ) {
case ONE_OR_MORE :
min = 1 ;
break ;
case ZERO_OR_ONE :
min = 0 ;
max = 1 ;
break ;
// case KLEENE_STAR:
// do nothing -- min, max already set
}
m = new Quantifier ( m , min , max ) ;
2008-01-14 22:29:44 +00:00
if ( m = = NULL ) {
2008-02-23 19:15:18 +00:00
return syntaxError ( U_MEMORY_ALLOCATION_ERROR , rule , start , status ) ;
2008-01-14 22:29:44 +00:00
}
2001-10-30 18:08:53 +00:00
buf . truncate ( qstart ) ;
2006-07-13 20:29:11 +00:00
buf . append ( parser . generateStandInFor ( m , status ) ) ;
2001-10-30 18:08:53 +00:00
}
break ;
//------------------------------------------------------
// Elements allowed ONLY WITHIN segments
//------------------------------------------------------
case SEGMENT_CLOSE :
2002-02-07 01:07:55 +00:00
// assert(isSegment);
// We're done parsing a segment.
done = TRUE ;
break ;
2001-10-30 18:08:53 +00:00
//------------------------------------------------------
// Elements allowed ONLY OUTSIDE segments
//------------------------------------------------------
2000-05-20 04:40:29 +00:00
case CONTEXT_ANTE :
if ( ante > = 0 ) {
2006-07-13 20:29:11 +00:00
return syntaxError ( U_MULTIPLE_ANTE_CONTEXTS , rule , start , status ) ;
2000-05-20 04:40:29 +00:00
}
ante = buf . length ( ) ;
break ;
case CONTEXT_POST :
if ( post > = 0 ) {
2006-07-13 20:29:11 +00:00
return syntaxError ( U_MULTIPLE_POST_CONTEXTS , rule , start , status ) ;
2000-05-20 04:40:29 +00:00
}
post = buf . length ( ) ;
break ;
case CURSOR_POS :
if ( cursor > = 0 ) {
2006-07-13 20:29:11 +00:00
return syntaxError ( U_MULTIPLE_CURSORS , rule , start , status ) ;
2000-05-20 04:40:29 +00:00
}
cursor = buf . length ( ) ;
break ;
case CURSOR_OFFSET :
if ( cursorOffset < 0 ) {
if ( buf . length ( ) > 0 ) {
2006-07-13 20:29:11 +00:00
return syntaxError ( U_MISPLACED_CURSOR_OFFSET , rule , start , status ) ;
2000-05-20 04:40:29 +00:00
}
- - cursorOffset ;
} else if ( cursorOffset > 0 ) {
if ( buf . length ( ) ! = cursorOffsetPos | | cursor > = 0 ) {
2006-07-13 20:29:11 +00:00
return syntaxError ( U_MISPLACED_CURSOR_OFFSET , rule , start , status ) ;
2000-05-20 04:40:29 +00:00
}
+ + cursorOffset ;
} else {
if ( cursor = = 0 & & buf . length ( ) = = 0 ) {
cursorOffset = - 1 ;
} else if ( cursor < 0 ) {
cursorOffsetPos = buf . length ( ) ;
cursorOffset = 1 ;
} else {
2006-07-13 20:29:11 +00:00
return syntaxError ( U_MISPLACED_CURSOR_OFFSET , rule , start , status ) ;
2000-05-20 04:40:29 +00:00
}
}
break ;
2001-10-30 18:08:53 +00:00
//------------------------------------------------------
// Non-special characters
//------------------------------------------------------
2000-05-20 04:40:29 +00:00
default :
// Disallow unquoted characters other than [0-9A-Za-z]
// in the printable ASCII range. These characters are
// reserved for possible future use.
if ( c > = 0x0021 & & c < = 0x007E & &
! ( ( c > = 0x0030 /*'0'*/ & & c < = 0x0039 /*'9'*/ ) | |
( c > = 0x0041 /*'A'*/ & & c < = 0x005A /*'Z'*/ ) | |
( c > = 0x0061 /*'a'*/ & & c < = 0x007A /*'z'*/ ) ) ) {
2006-07-13 20:29:11 +00:00
return syntaxError ( U_UNQUOTED_SPECIAL , rule , start , status ) ;
2000-05-20 04:40:29 +00:00
}
buf . append ( c ) ;
break ;
}
}
return pos ;
}
/**
* Remove context .
*/
void RuleHalf : : removeContext ( ) {
//text = text.substring(ante < 0 ? 0 : ante,
// post < 0 ? text.length() : post);
if ( post > = 0 ) {
text . remove ( post ) ;
}
if ( ante > = 0 ) {
text . removeBetween ( 0 , ante ) ;
}
ante = post = - 1 ;
2000-08-30 20:43:25 +00:00
anchorStart = anchorEnd = FALSE ;
2000-05-20 04:40:29 +00:00
}
/**
2002-02-07 01:07:55 +00:00
* Return true if this half looks like valid output , that is , does not
* contain quantifiers or other special input - only elements .
2000-05-20 04:40:29 +00:00
*/
2002-03-01 03:41:41 +00:00
UBool RuleHalf : : isValidOutput ( TransliteratorParser & transParser ) {
2002-02-07 01:07:55 +00:00
for ( int32_t i = 0 ; i < text . length ( ) ; ) {
UChar32 c = text . char32At ( i ) ;
2011-07-27 05:53:56 +00:00
i + = U16_LENGTH ( c ) ;
2002-03-01 03:41:41 +00:00
if ( ! transParser . parseData - > isReplacer ( c ) ) {
2002-02-07 01:07:55 +00:00
return FALSE ;
}
2001-10-30 18:08:53 +00:00
}
2002-02-07 01:07:55 +00:00
return TRUE ;
}
/**
* Return true if this half looks like valid input , that is , does not
* contain functions or other special output - only elements .
*/
2002-03-01 03:41:41 +00:00
UBool RuleHalf : : isValidInput ( TransliteratorParser & transParser ) {
2002-02-07 01:07:55 +00:00
for ( int32_t i = 0 ; i < text . length ( ) ; ) {
UChar32 c = text . char32At ( i ) ;
2011-07-27 05:53:56 +00:00
i + = U16_LENGTH ( c ) ;
2002-03-01 03:41:41 +00:00
if ( ! transParser . parseData - > isMatcher ( c ) ) {
2002-02-07 01:07:55 +00:00
return FALSE ;
}
2001-11-20 18:03:30 +00:00
}
2002-02-07 01:07:55 +00:00
return TRUE ;
2000-02-08 02:49:15 +00:00
}
//----------------------------------------------------------------------
2001-10-10 19:29:45 +00:00
// PUBLIC API
2000-02-08 02:49:15 +00:00
//----------------------------------------------------------------------
1999-11-20 00:40:50 +00:00
2001-07-13 21:09:41 +00:00
/**
2001-10-10 19:29:45 +00:00
* Constructor .
2001-07-13 21:09:41 +00:00
*/
2006-04-14 21:09:42 +00:00
TransliteratorParser : : TransliteratorParser ( UErrorCode & statusReturn ) :
2006-07-13 20:29:11 +00:00
dataVector ( statusReturn ) ,
idBlockVector ( statusReturn ) ,
variablesVector ( statusReturn ) ,
segmentObjects ( statusReturn )
2006-04-14 21:09:42 +00:00
{
2011-06-03 05:23:57 +00:00
idBlockVector . setDeleter ( uprv_deleteUObject ) ;
2005-06-09 17:30:48 +00:00
curData = NULL ;
2001-10-10 19:29:45 +00:00
compoundFilter = NULL ;
parseData = NULL ;
2011-06-03 05:23:57 +00:00
variableNames . setValueDeleter ( uprv_deleteUObject ) ;
2001-07-13 21:09:41 +00:00
}
1999-11-20 00:40:50 +00:00
/**
2001-10-10 19:29:45 +00:00
* Destructor .
1999-11-20 00:40:50 +00:00
*/
2001-10-10 19:29:45 +00:00
TransliteratorParser : : ~ TransliteratorParser ( ) {
2006-04-14 21:09:42 +00:00
while ( ! dataVector . isEmpty ( ) )
delete ( TransliterationRuleData * ) ( dataVector . orphanElementAt ( 0 ) ) ;
2001-10-10 19:29:45 +00:00
delete compoundFilter ;
delete parseData ;
2006-04-14 21:09:42 +00:00
while ( ! variablesVector . isEmpty ( ) )
delete ( UnicodeFunctor * ) variablesVector . orphanElementAt ( 0 ) ;
2001-10-10 19:29:45 +00:00
}
2001-08-31 03:23:39 +00:00
2001-10-10 19:29:45 +00:00
void
TransliteratorParser : : parse ( const UnicodeString & rules ,
2001-10-19 23:57:38 +00:00
UTransDirection transDirection ,
2001-10-10 19:29:45 +00:00
UParseError & pe ,
UErrorCode & ec ) {
if ( U_SUCCESS ( ec ) ) {
2006-07-13 20:29:11 +00:00
parseRules ( rules , transDirection , ec ) ;
2001-10-10 19:29:45 +00:00
pe = parseError ;
2001-08-23 01:06:08 +00:00
}
2000-02-08 02:49:15 +00:00
}
/**
2001-10-10 19:29:45 +00:00
* Return the compound filter parsed by parse ( ) . Caller owns result .
*/
UnicodeSet * TransliteratorParser : : orphanCompoundFilter ( ) {
UnicodeSet * f = compoundFilter ;
compoundFilter = NULL ;
return f ;
}
//----------------------------------------------------------------------
// Private implementation
//----------------------------------------------------------------------
1999-11-20 00:40:50 +00:00
/**
* Parse the given string as a sequence of rules , separated by newline
* characters ( ' \n ' ) , and cause this object to implement those rules . Any
* previous rules are discarded . Typically this method is called exactly
* once , during construction .
* @ exception IllegalArgumentException if there is a syntax error in the
* rules
*/
2002-02-07 01:07:55 +00:00
void TransliteratorParser : : parseRules ( const UnicodeString & rule ,
2006-07-13 20:29:11 +00:00
UTransDirection theDirection ,
2006-09-28 17:39:21 +00:00
UErrorCode & status )
{
2001-07-17 18:13:51 +00:00
// Clear error struct
2006-09-28 17:39:21 +00:00
uprv_memset ( & parseError , 0 , sizeof ( parseError ) ) ;
2001-11-15 21:13:37 +00:00
parseError . line = parseError . offset = - 1 ;
2001-07-17 18:13:51 +00:00
2005-06-09 17:30:48 +00:00
UBool parsingIDs = TRUE ;
int32_t ruleCount = 0 ;
2006-04-14 21:09:42 +00:00
while ( ! dataVector . isEmpty ( ) ) {
delete ( TransliterationRuleData * ) ( dataVector . orphanElementAt ( 0 ) ) ;
2005-06-09 17:30:48 +00:00
}
1999-11-20 00:40:50 +00:00
if ( U_FAILURE ( status ) ) {
return ;
}
2000-02-08 02:49:15 +00:00
2006-04-14 21:09:42 +00:00
idBlockVector . removeAllElements ( ) ;
2005-06-09 17:30:48 +00:00
curData = NULL ;
2001-10-10 19:29:45 +00:00
direction = theDirection ;
ruleCount = 0 ;
delete compoundFilter ;
compoundFilter = NULL ;
2006-04-14 21:09:42 +00:00
while ( ! variablesVector . isEmpty ( ) ) {
delete ( UnicodeFunctor * ) variablesVector . orphanElementAt ( 0 ) ;
2000-03-18 01:42:45 +00:00
}
2006-04-14 21:09:42 +00:00
variableNames . removeAll ( ) ;
parseData = new ParseData ( 0 , & variablesVector , & variableNames ) ;
2001-10-10 19:29:45 +00:00
if ( parseData = = NULL ) {
status = U_MEMORY_ALLOCATION_ERROR ;
return ;
}
2001-10-23 23:26:47 +00:00
2001-10-17 19:21:12 +00:00
dotStandIn = ( UChar ) - 1 ;
1999-11-20 00:40:50 +00:00
2008-01-14 22:29:44 +00:00
UnicodeString * tempstr = NULL ; // used for memory allocation error checking
2001-07-13 21:09:41 +00:00
UnicodeString str ; // scratch
2005-06-09 17:30:48 +00:00
UnicodeString idBlockResult ;
2000-01-13 07:28:08 +00:00
int32_t pos = 0 ;
2002-02-07 01:07:55 +00:00
int32_t limit = rule . length ( ) ;
2001-10-10 19:29:45 +00:00
// The compound filter offset is an index into idBlockResult.
// If it is 0, then the compound filter occurred at the start,
// and it is the offset to the _start_ of the compound filter
// pattern. Otherwise it is the offset to the _limit_ of the
// compound filter pattern within idBlockResult.
compoundFilter = NULL ;
int32_t compoundFilterOffset = - 1 ;
2000-01-13 07:28:08 +00:00
while ( pos < limit & & U_SUCCESS ( status ) ) {
2002-02-07 01:07:55 +00:00
UChar c = rule . charAt ( pos + + ) ;
2011-04-26 06:39:29 +00:00
if ( PatternProps : : isWhiteSpace ( c ) ) {
2001-07-13 21:09:41 +00:00
// Ignore leading whitespace.
2000-01-13 07:28:08 +00:00
continue ;
1999-11-20 00:40:50 +00:00
}
2000-01-13 07:28:08 +00:00
// Skip lines starting with the comment character
if ( c = = RULE_COMMENT_CHAR ) {
2002-02-07 01:07:55 +00:00
pos = rule . indexOf ( ( UChar ) 0x000A /*\n*/ , pos ) + 1 ;
2000-01-13 07:28:08 +00:00
if ( pos = = 0 ) {
break ; // No "\n" found; rest of rule is a commnet
}
continue ; // Either fall out or restart with next line
1999-11-20 00:40:50 +00:00
}
2005-06-09 17:30:48 +00:00
// skip empty rules
if ( c = = END_OF_RULE )
continue ;
// keep track of how many rules we've seen
+ + ruleCount ;
2001-07-13 21:09:41 +00:00
// We've found the start of a rule or ID. c is its first
// character, and pos points past c.
- - pos ;
// Look for an ID token. Must have at least ID_TOKEN_LEN + 1
// chars left.
if ( ( pos + ID_TOKEN_LEN + 1 ) < = limit & &
2005-06-09 17:30:48 +00:00
rule . compare ( pos , ID_TOKEN_LEN , ID_TOKEN ) = = 0 ) {
2001-07-13 21:09:41 +00:00
pos + = ID_TOKEN_LEN ;
2002-02-07 01:07:55 +00:00
c = rule . charAt ( pos ) ;
2011-04-26 06:39:29 +00:00
while ( PatternProps : : isWhiteSpace ( c ) & & pos < limit ) {
2001-07-13 21:09:41 +00:00
+ + pos ;
2002-02-07 01:07:55 +00:00
c = rule . charAt ( pos ) ;
2001-07-13 21:09:41 +00:00
}
2002-02-07 01:07:55 +00:00
2001-07-13 21:09:41 +00:00
int32_t p = pos ;
2002-02-07 01:07:55 +00:00
2005-06-09 17:30:48 +00:00
if ( ! parsingIDs ) {
if ( curData ! = NULL ) {
if ( direction = = UTRANS_FORWARD )
2006-04-14 21:09:42 +00:00
dataVector . addElement ( curData , status ) ;
2005-06-09 17:30:48 +00:00
else
2006-04-14 21:09:42 +00:00
dataVector . insertElementAt ( curData , 0 , status ) ;
2005-06-09 17:30:48 +00:00
curData = NULL ;
}
parsingIDs = TRUE ;
}
2002-02-07 01:07:55 +00:00
TransliteratorIDParser : : SingleID * id =
2004-10-08 01:52:53 +00:00
TransliteratorIDParser : : parseSingleID ( rule , p , direction , status ) ;
2002-02-07 01:07:55 +00:00
if ( p ! = pos & & ICU_Utility : : parseChar ( rule , p , END_OF_RULE ) ) {
// Successful ::ID parse.
2005-06-09 17:30:48 +00:00
2002-02-07 01:07:55 +00:00
if ( direction = = UTRANS_FORWARD ) {
2005-06-09 17:30:48 +00:00
idBlockResult . append ( id - > canonID ) . append ( END_OF_RULE ) ;
2002-02-07 01:07:55 +00:00
} else {
2005-06-09 17:30:48 +00:00
idBlockResult . insert ( 0 , END_OF_RULE ) ;
idBlockResult . insert ( 0 , id - > canonID ) ;
2001-11-17 06:44:50 +00:00
}
2005-06-09 17:30:48 +00:00
2002-02-07 01:07:55 +00:00
} else {
// Couldn't parse an ID. Try to parse a global filter
int32_t withParens = - 1 ;
2005-06-09 17:30:48 +00:00
UnicodeSet * f = TransliteratorIDParser : : parseGlobalFilter ( rule , p , direction , withParens , NULL ) ;
2002-02-07 01:07:55 +00:00
if ( f ! = NULL ) {
2002-04-10 18:30:20 +00:00
if ( ICU_Utility : : parseChar ( rule , p , END_OF_RULE )
& & ( direction = = UTRANS_FORWARD ) = = ( withParens = = 0 ) )
{
if ( compoundFilter ! = NULL ) {
// Multiple compound filters
2006-07-13 20:29:11 +00:00
syntaxError ( U_MULTIPLE_COMPOUND_FILTERS , rule , pos , status ) ;
2002-04-10 18:30:20 +00:00
delete f ;
} else {
compoundFilter = f ;
2005-06-09 17:30:48 +00:00
compoundFilterOffset = ruleCount ;
2002-02-07 01:07:55 +00:00
}
} else {
delete f ;
2001-10-10 19:29:45 +00:00
}
2002-02-07 01:07:55 +00:00
} else {
// Invalid ::id
// Can be parsed as neither an ID nor a global filter
2006-07-13 20:29:11 +00:00
syntaxError ( U_INVALID_ID , rule , pos , status ) ;
2001-07-13 21:09:41 +00:00
}
}
2002-02-07 01:07:55 +00:00
delete id ;
pos = p ;
2001-07-13 21:09:41 +00:00
} else {
2005-06-09 17:30:48 +00:00
if ( parsingIDs ) {
2008-02-23 19:15:18 +00:00
tempstr = new UnicodeString ( idBlockResult ) ;
// NULL pointer check
if ( tempstr = = NULL ) {
status = U_MEMORY_ALLOCATION_ERROR ;
return ;
}
2005-06-09 17:30:48 +00:00
if ( direction = = UTRANS_FORWARD )
2008-01-14 22:29:44 +00:00
idBlockVector . addElement ( tempstr , status ) ;
2005-06-09 17:30:48 +00:00
else
2008-01-14 22:29:44 +00:00
idBlockVector . insertElementAt ( tempstr , 0 , status ) ;
2005-06-09 17:30:48 +00:00
idBlockResult . remove ( ) ;
parsingIDs = FALSE ;
curData = new TransliterationRuleData ( status ) ;
2008-01-14 22:29:44 +00:00
// NULL pointer check
if ( curData = = NULL ) {
2008-02-23 19:15:18 +00:00
status = U_MEMORY_ALLOCATION_ERROR ;
return ;
2008-01-14 22:29:44 +00:00
}
2005-06-09 17:30:48 +00:00
parseData - > data = curData ;
// By default, rules use part of the private use area
// E000..F8FF for variables and other stand-ins. Currently
// the range F000..F8FF is typically sufficient. The 'use
// variable range' pragma allows rule sets to modify this.
2006-07-13 20:29:11 +00:00
setVariableRange ( 0xF000 , 0xF8FF , status ) ;
2005-06-09 17:30:48 +00:00
}
if ( resemblesPragma ( rule , pos , limit ) ) {
2006-07-13 20:29:11 +00:00
int32_t ppp = parsePragma ( rule , pos , limit , status ) ;
2005-06-09 17:30:48 +00:00
if ( ppp < 0 ) {
2006-07-13 20:29:11 +00:00
syntaxError ( U_MALFORMED_PRAGMA , rule , pos , status ) ;
2001-07-13 21:09:41 +00:00
}
2005-06-09 17:30:48 +00:00
pos = ppp ;
// Parse a rule
} else {
2006-07-13 20:29:11 +00:00
pos = parseRule ( rule , pos , limit , status ) ;
2001-07-13 21:09:41 +00:00
}
}
2000-01-13 07:28:08 +00:00
}
2002-02-07 01:07:55 +00:00
2005-06-09 17:30:48 +00:00
if ( parsingIDs & & idBlockResult . length ( ) > 0 ) {
2008-02-23 19:15:18 +00:00
tempstr = new UnicodeString ( idBlockResult ) ;
// NULL pointer check
if ( tempstr = = NULL ) {
status = U_MEMORY_ALLOCATION_ERROR ;
return ;
}
2005-06-09 17:30:48 +00:00
if ( direction = = UTRANS_FORWARD )
2008-01-14 22:29:44 +00:00
idBlockVector . addElement ( tempstr , status ) ;
2005-06-09 17:30:48 +00:00
else
2008-01-14 22:29:44 +00:00
idBlockVector . insertElementAt ( tempstr , 0 , status ) ;
2002-07-16 17:50:42 +00:00
}
2005-06-09 17:30:48 +00:00
else if ( ! parsingIDs & & curData ! = NULL ) {
if ( direction = = UTRANS_FORWARD )
2006-04-14 21:09:42 +00:00
dataVector . addElement ( curData , status ) ;
2005-06-09 17:30:48 +00:00
else
2006-04-14 21:09:42 +00:00
dataVector . insertElementAt ( curData , 0 , status ) ;
2000-02-08 02:49:15 +00:00
}
2005-06-09 17:30:48 +00:00
2000-01-13 07:28:08 +00:00
if ( U_SUCCESS ( status ) ) {
2005-06-09 17:30:48 +00:00
// Convert the set vector to an array
2006-04-14 21:09:42 +00:00
int32_t i , dataVectorSize = dataVector . size ( ) ;
2005-06-09 20:41:23 +00:00
for ( i = 0 ; i < dataVectorSize ; i + + ) {
2006-04-14 21:09:42 +00:00
TransliterationRuleData * data = ( TransliterationRuleData * ) dataVector . elementAt ( i ) ;
data - > variablesLength = variablesVector . size ( ) ;
2005-06-09 17:30:48 +00:00
if ( data - > variablesLength = = 0 ) {
data - > variables = 0 ;
} else {
data - > variables = ( UnicodeFunctor * * ) uprv_malloc ( data - > variablesLength * sizeof ( UnicodeFunctor * ) ) ;
2008-01-14 22:29:44 +00:00
// NULL pointer check
if ( data - > variables = = NULL ) {
2008-02-23 19:15:18 +00:00
status = U_MEMORY_ALLOCATION_ERROR ;
return ;
2008-01-14 22:29:44 +00:00
}
2005-06-09 17:30:48 +00:00
data - > variablesAreOwned = ( i = = 0 ) ;
}
for ( int32_t j = 0 ; j < data - > variablesLength ; j + + ) {
data - > variables [ j ] =
2016-02-08 19:37:17 +00:00
static_cast < UnicodeFunctor * > ( variablesVector . elementAt ( j ) ) ;
2005-06-09 17:30:48 +00:00
}
2006-04-14 21:09:42 +00:00
data - > variableNames . removeAll ( ) ;
2014-12-09 23:54:56 +00:00
int32_t pos = UHASH_FIRST ;
2006-04-14 21:09:42 +00:00
const UHashElement * he = variableNames . nextElement ( pos ) ;
2005-06-09 17:30:48 +00:00
while ( he ! = NULL ) {
2008-02-23 19:15:18 +00:00
UnicodeString * tempus = ( UnicodeString * ) ( ( ( UnicodeString * ) ( he - > value . pointer ) ) - > clone ( ) ) ;
if ( tempus = = NULL ) {
status = U_MEMORY_ALLOCATION_ERROR ;
return ;
}
2006-04-14 21:09:42 +00:00
data - > variableNames . put ( * ( ( UnicodeString * ) ( he - > key . pointer ) ) ,
2008-01-14 22:29:44 +00:00
tempus , status ) ;
2006-04-14 21:09:42 +00:00
he = variableNames . nextElement ( pos ) ;
2005-06-09 17:30:48 +00:00
}
}
2006-04-14 21:09:42 +00:00
variablesVector . removeAllElements ( ) ; // keeps them from getting deleted when we succeed
2005-06-09 17:30:48 +00:00
// Index the rules
2001-10-10 19:29:45 +00:00
if ( compoundFilter ! = NULL ) {
2005-06-09 17:30:48 +00:00
if ( ( direction = = UTRANS_FORWARD & & compoundFilterOffset ! = 1 ) | |
( direction = = UTRANS_REVERSE & & compoundFilterOffset ! = ruleCount ) ) {
2001-10-10 19:29:45 +00:00
status = U_MISPLACED_COMPOUND_FILTER ;
}
}
2005-06-09 20:41:23 +00:00
for ( i = 0 ; i < dataVectorSize ; i + + ) {
2006-04-14 21:09:42 +00:00
TransliterationRuleData * data = ( TransliterationRuleData * ) dataVector . elementAt ( i ) ;
2005-06-09 17:30:48 +00:00
data - > ruleSet . freeze ( parseError , status ) ;
2001-07-13 21:09:41 +00:00
}
2006-04-14 21:09:42 +00:00
if ( idBlockVector . size ( ) = = 1 & & ( ( UnicodeString * ) idBlockVector . elementAt ( 0 ) ) - > isEmpty ( ) ) {
idBlockVector . removeElementAt ( 0 ) ;
2005-06-09 20:41:23 +00:00
}
1999-11-20 00:40:50 +00:00
}
}
2001-10-23 23:26:47 +00:00
/**
* Set the variable range to [ start , end ] ( inclusive ) .
*/
2006-07-13 20:29:11 +00:00
void TransliteratorParser : : setVariableRange ( int32_t start , int32_t end , UErrorCode & status ) {
2001-10-23 23:26:47 +00:00
if ( start > end | | start < 0 | | end > 0xFFFF ) {
status = U_MALFORMED_PRAGMA ;
return ;
}
2005-06-09 17:30:48 +00:00
curData - > variablesBase = ( UChar ) start ;
2006-04-14 21:09:42 +00:00
if ( dataVector . size ( ) = = 0 ) {
2005-06-09 17:30:48 +00:00
variableNext = ( UChar ) start ;
variableLimit = ( UChar ) ( end + 1 ) ;
}
2001-10-23 23:26:47 +00:00
}
2001-11-12 19:12:08 +00:00
/**
* Assert that the given character is NOT within the variable range .
* If it is , return FALSE . This is neccesary to ensure that the
* variable range does not overlap characters used in a rule .
*/
UBool TransliteratorParser : : checkVariableRange ( UChar32 ch ) const {
2005-06-09 17:30:48 +00:00
return ! ( ch > = curData - > variablesBase & & ch < variableLimit ) ;
2001-11-12 19:12:08 +00:00
}
2001-10-23 23:26:47 +00:00
/**
* Set the maximum backup to ' backup ' , in response to a pragma
* statement .
*/
2003-12-04 02:05:39 +00:00
void TransliteratorParser : : pragmaMaximumBackup ( int32_t /*backup*/ ) {
2001-10-23 23:26:47 +00:00
//TODO Finish
}
/**
* Begin normalizing all rules using the given mode , in response
* to a pragma statement .
*/
2003-12-04 02:05:39 +00:00
void TransliteratorParser : : pragmaNormalizeRules ( UNormalizationMode /*mode*/ ) {
2001-10-23 23:26:47 +00:00
//TODO Finish
}
2001-11-12 19:12:08 +00:00
static const UChar PRAGMA_USE [ ] = { 0x75 , 0x73 , 0x65 , 0x20 , 0 } ; // "use "
2001-10-23 23:26:47 +00:00
2001-11-12 19:12:08 +00:00
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 # #~;"
2001-10-23 23:26:47 +00:00
2001-11-12 19:12:08 +00:00
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 #~;"
2001-10-23 23:26:47 +00:00
2001-11-12 19:12:08 +00:00
static const UChar PRAGMA_NFD_RULES [ ] = { 0x7E , 0x6E , 0x66 , 0x64 , 0x20 , 0x72 , 0x75 , 0x6C , 0x65 , 0x73 , 0x7E , 0x3B , 0 } ; // "~nfd rules~;"
2001-10-23 23:26:47 +00:00
2001-11-12 19:12:08 +00:00
static const UChar PRAGMA_NFC_RULES [ ] = { 0x7E , 0x6E , 0x66 , 0x63 , 0x20 , 0x72 , 0x75 , 0x6C , 0x65 , 0x73 , 0x7E , 0x3B , 0 } ; // "~nfc rules~;"
2001-10-23 23:26:47 +00:00
/**
* Return true if the given rule looks like a pragma .
* @ param pos offset to the first non - whitespace character
* of the rule .
* @ param limit pointer past the last character of the rule .
*/
UBool TransliteratorParser : : resemblesPragma ( const UnicodeString & rule , int32_t pos , int32_t limit ) {
// Must start with /use\s/i
2011-07-07 18:46:19 +00:00
return ICU_Utility : : parsePattern ( rule , pos , limit , UnicodeString ( TRUE , PRAGMA_USE , 4 ) , NULL ) > = 0 ;
2001-10-23 23:26:47 +00:00
}
/**
* Parse a pragma . This method assumes resemblesPragma ( ) has
* already returned true .
* @ param pos offset to the first non - whitespace character
* of the rule .
* @ param limit pointer past the last character of the rule .
* @ return the position index after the final ' ; ' of the pragma ,
* or - 1 on failure .
*/
2006-07-13 20:29:11 +00:00
int32_t TransliteratorParser : : parsePragma ( const UnicodeString & rule , int32_t pos , int32_t limit , UErrorCode & status ) {
2001-10-23 23:26:47 +00:00
int32_t array [ 2 ] ;
// resemblesPragma() has already returned true, so we
// know that pos points to /use\s/i; we can skip 4 characters
// immediately
pos + = 4 ;
// Here are the pragmas we recognize:
// use variable range 0xE000 0xEFFF;
// use maximum backup 16;
// use nfd rules;
// use nfc rules;
2011-07-07 18:46:19 +00:00
int p = ICU_Utility : : parsePattern ( rule , pos , limit , UnicodeString ( TRUE , PRAGMA_VARIABLE_RANGE , - 1 ) , array ) ;
2001-10-23 23:26:47 +00:00
if ( p > = 0 ) {
2006-07-13 20:29:11 +00:00
setVariableRange ( array [ 0 ] , array [ 1 ] , status ) ;
2001-10-23 23:26:47 +00:00
return p ;
}
2011-07-07 18:46:19 +00:00
p = ICU_Utility : : parsePattern ( rule , pos , limit , UnicodeString ( TRUE , PRAGMA_MAXIMUM_BACKUP , - 1 ) , array ) ;
2001-10-23 23:26:47 +00:00
if ( p > = 0 ) {
pragmaMaximumBackup ( array [ 0 ] ) ;
return p ;
}
2011-07-07 18:46:19 +00:00
p = ICU_Utility : : parsePattern ( rule , pos , limit , UnicodeString ( TRUE , PRAGMA_NFD_RULES , - 1 ) , NULL ) ;
2001-10-23 23:26:47 +00:00
if ( p > = 0 ) {
pragmaNormalizeRules ( UNORM_NFD ) ;
return p ;
}
2011-07-07 18:46:19 +00:00
p = ICU_Utility : : parsePattern ( rule , pos , limit , UnicodeString ( TRUE , PRAGMA_NFC_RULES , - 1 ) , NULL ) ;
2001-10-23 23:26:47 +00:00
if ( p > = 0 ) {
pragmaNormalizeRules ( UNORM_NFC ) ;
return p ;
}
// Syntax error: unable to parse pragma
return - 1 ;
}
1999-11-20 00:40:50 +00:00
/**
2000-01-13 07:28:08 +00:00
* MAIN PARSER . Parse the next rule in the given rule string , starting
* at pos . Return the index after the last character parsed . Do not
* parse characters at or after limit .
*
* Important : The character at pos must be a non - whitespace character
* that is not the comment character .
*
* This method handles quoting , escaping , and whitespace removal . It
* parses the end - of - rule character . It recognizes context and cursor
* indicators . Once it does a lexical breakdown of the rule at pos , it
* creates a rule object and adds it to our rule list .
1999-11-20 00:40:50 +00:00
*/
2006-07-13 20:29:11 +00:00
int32_t TransliteratorParser : : parseRule ( const UnicodeString & rule , int32_t pos , int32_t limit , UErrorCode & status ) {
2000-01-13 07:28:08 +00:00
// Locate the left side, operator, and right side
int32_t start = pos ;
UChar op = 0 ;
2002-02-07 01:07:55 +00:00
int32_t i ;
// Set up segments data
segmentStandins . truncate ( 0 ) ;
2006-04-14 21:09:42 +00:00
segmentObjects . removeAllElements ( ) ;
2000-01-13 07:28:08 +00:00
2000-05-20 04:40:29 +00:00
// Use pointers to automatics to make swapping possible.
RuleHalf _left ( * this ) , _right ( * this ) ;
RuleHalf * left = & _left ;
RuleHalf * right = & _right ;
1999-11-20 00:40:50 +00:00
2000-05-20 04:40:29 +00:00
undefinedVariableName . remove ( ) ;
2006-07-13 20:29:11 +00:00
pos = left - > parse ( rule , pos , limit , status ) ;
2000-05-20 04:40:29 +00:00
if ( U_FAILURE ( status ) ) {
return start ;
}
2000-01-13 07:28:08 +00:00
2001-10-30 18:08:53 +00:00
if ( pos = = limit | | u_strchr ( gOPERATORS , ( op = rule . charAt ( - - pos ) ) ) = = NULL ) {
2006-07-13 20:29:11 +00:00
return syntaxError ( U_MISSING_OPERATOR , rule , start , status ) ;
2000-01-13 07:28:08 +00:00
}
2001-10-30 18:08:53 +00:00
+ + pos ;
2000-05-20 04:40:29 +00:00
// Found an operator char. Check for forward-reverse operator.
if ( op = = REVERSE_RULE_OP & &
( pos < limit & & rule . charAt ( pos ) = = FORWARD_RULE_OP ) ) {
+ + pos ;
op = FWDREV_RULE_OP ;
2000-01-13 07:28:08 +00:00
}
2003-04-23 00:20:16 +00:00
// Translate alternate op characters.
switch ( op ) {
case ALT_FORWARD_RULE_OP :
op = FORWARD_RULE_OP ;
break ;
case ALT_REVERSE_RULE_OP :
op = REVERSE_RULE_OP ;
break ;
case ALT_FWDREV_RULE_OP :
op = FWDREV_RULE_OP ;
break ;
}
2006-07-13 20:29:11 +00:00
pos = right - > parse ( rule , pos , limit , status ) ;
2000-05-20 04:40:29 +00:00
if ( U_FAILURE ( status ) ) {
return start ;
1999-11-20 00:40:50 +00:00
}
2000-05-20 04:40:29 +00:00
if ( pos < limit ) {
2001-10-30 18:08:53 +00:00
if ( rule . charAt ( - - pos ) = = END_OF_RULE ) {
2000-05-20 04:40:29 +00:00
+ + pos ;
} else {
// RuleHalf parser must have terminated at an operator
2006-07-13 20:29:11 +00:00
return syntaxError ( U_UNQUOTED_SPECIAL , rule , start , status ) ;
2000-05-20 04:40:29 +00:00
}
}
2000-01-13 07:28:08 +00:00
2000-05-20 04:40:29 +00:00
if ( op = = VARIABLE_DEF_OP ) {
2000-01-13 07:28:08 +00:00
// LHS is the name. RHS is a single character, either a literal
// or a set (already parsed). If RHS is longer than one
// character, it is either a multi-character string, or multiple
// sets, or a mixture of chars and sets -- syntax error.
2000-05-20 04:40:29 +00:00
// We expect to see a single undefined variable (the one being
// defined).
if ( undefinedVariableName . length ( ) = = 0 ) {
// "Missing '$' or duplicate definition"
2006-07-13 20:29:11 +00:00
return syntaxError ( U_BAD_VARIABLE_DEFINITION , rule , start , status ) ;
2000-01-13 07:28:08 +00:00
}
2000-05-20 04:40:29 +00:00
if ( left - > text . length ( ) ! = 1 | | left - > text . charAt ( 0 ) ! = variableLimit ) {
// "Malformed LHS"
2006-07-13 20:29:11 +00:00
return syntaxError ( U_MALFORMED_VARIABLE_DEFINITION , rule , start , status ) ;
2000-01-13 07:28:08 +00:00
}
2000-08-30 20:43:25 +00:00
if ( left - > anchorStart | | left - > anchorEnd | |
right - > anchorStart | | right - > anchorEnd ) {
2006-07-13 20:29:11 +00:00
return syntaxError ( U_MALFORMED_VARIABLE_DEFINITION , rule , start , status ) ;
2000-08-30 20:43:25 +00:00
}
2000-05-20 04:40:29 +00:00
// We allow anything on the right, including an empty string.
UnicodeString * value = new UnicodeString ( right - > text ) ;
2008-01-14 22:29:44 +00:00
// NULL pointer check
if ( value = = NULL ) {
2008-02-23 19:15:18 +00:00
return syntaxError ( U_MEMORY_ALLOCATION_ERROR , rule , start , status ) ;
2008-01-14 22:29:44 +00:00
}
2006-04-14 21:09:42 +00:00
variableNames . put ( undefinedVariableName , value , status ) ;
2000-05-20 04:40:29 +00:00
+ + variableLimit ;
return pos ;
}
// If this is not a variable definition rule, we shouldn't have
// any undefined variable names.
if ( undefinedVariableName . length ( ) ! = 0 ) {
2001-08-31 03:23:39 +00:00
return syntaxError ( // "Undefined variable $" + undefinedVariableName,
2001-08-16 00:55:16 +00:00
U_UNDEFINED_VARIABLE ,
2006-07-13 20:29:11 +00:00
rule , start , status ) ;
2000-05-20 04:40:29 +00:00
}
2002-02-07 01:07:55 +00:00
// Verify segments
2006-04-14 21:09:42 +00:00
if ( segmentStandins . length ( ) > segmentObjects . size ( ) ) {
2006-07-13 20:29:11 +00:00
syntaxError ( U_UNDEFINED_SEGMENT_REFERENCE , rule , start , status ) ;
2002-02-07 01:07:55 +00:00
}
for ( i = 0 ; i < segmentStandins . length ( ) ; + + i ) {
if ( segmentStandins . charAt ( i ) = = 0 ) {
2006-07-13 20:29:11 +00:00
syntaxError ( U_INTERNAL_TRANSLITERATOR_ERROR , rule , start , status ) ; // will never happen
2002-02-07 01:07:55 +00:00
}
}
2006-04-14 21:09:42 +00:00
for ( i = 0 ; i < segmentObjects . size ( ) ; + + i ) {
if ( segmentObjects . elementAt ( i ) = = NULL ) {
2006-07-13 20:29:11 +00:00
syntaxError ( U_INTERNAL_TRANSLITERATOR_ERROR , rule , start , status ) ; // will never happen
2002-02-07 01:07:55 +00:00
}
}
2000-05-20 04:40:29 +00:00
// If the direction we want doesn't match the rule
// direction, do nothing.
if ( op ! = FWDREV_RULE_OP & &
2000-06-27 19:00:38 +00:00
( ( direction = = UTRANS_FORWARD ) ! = ( op = = FORWARD_RULE_OP ) ) ) {
2000-05-20 04:40:29 +00:00
return pos ;
}
// Transform the rule into a forward rule by swapping the
// sides if necessary.
2000-06-27 19:00:38 +00:00
if ( direction = = UTRANS_REVERSE ) {
2000-05-20 04:40:29 +00:00
left = & _right ;
right = & _left ;
}
// Remove non-applicable elements in forward-reverse
// rules. Bidirectional rules ignore elements that do not
// apply.
if ( op = = FWDREV_RULE_OP ) {
right - > removeContext ( ) ;
2002-02-07 01:07:55 +00:00
left - > cursor = - 1 ;
2000-05-20 04:40:29 +00:00
left - > cursorOffset = 0 ;
}
// Normalize context
if ( left - > ante < 0 ) {
left - > ante = 0 ;
}
if ( left - > post < 0 ) {
left - > post = left - > text . length ( ) ;
}
// Context is only allowed on the input side. Cursors are only
// allowed on the output side. Segment delimiters can only appear
// on the left, and references on the right. Cursor offset
// cannot appear without an explicit cursor. Cursor offset
// cannot place the cursor outside the limits of the context.
2000-08-30 20:43:25 +00:00
// Anchors are only allowed on the input side.
2000-05-20 04:40:29 +00:00
if ( right - > ante > = 0 | | right - > post > = 0 | | left - > cursor > = 0 | |
( right - > cursorOffset ! = 0 & & right - > cursor < 0 ) | |
2001-08-04 00:15:12 +00:00
// - The following two checks were used to ensure that the
// - the cursor offset stayed within the ante- or postcontext.
// - However, with the addition of quantifiers, we have to
// - allow arbitrary cursor offsets and do runtime checking.
//(right->cursorOffset > (left->text.length() - left->post)) ||
//(-right->cursorOffset > left->ante) ||
2001-11-09 01:22:33 +00:00
right - > anchorStart | | right - > anchorEnd | |
2002-02-07 01:07:55 +00:00
! left - > isValidInput ( * this ) | | ! right - > isValidOutput ( * this ) | |
2001-11-14 19:26:56 +00:00
left - > ante > left - > post ) {
2000-08-30 20:43:25 +00:00
2006-07-13 20:29:11 +00:00
return syntaxError ( U_MALFORMED_RULE , rule , start , status ) ;
2000-05-20 04:40:29 +00:00
}
2002-02-07 01:07:55 +00:00
// Flatten segment objects vector to an array
UnicodeFunctor * * segmentsArray = NULL ;
2006-04-14 21:09:42 +00:00
if ( segmentObjects . size ( ) > 0 ) {
segmentsArray = ( UnicodeFunctor * * ) uprv_malloc ( segmentObjects . size ( ) * sizeof ( UnicodeFunctor * ) ) ;
2008-01-14 22:29:44 +00:00
// Null pointer check
if ( segmentsArray = = NULL ) {
2008-02-23 19:15:18 +00:00
return syntaxError ( U_MEMORY_ALLOCATION_ERROR , rule , start , status ) ;
2008-01-14 22:29:44 +00:00
}
2006-04-14 21:09:42 +00:00
segmentObjects . toArray ( ( void * * ) segmentsArray ) ;
1999-11-20 00:40:50 +00:00
}
2008-01-14 22:29:44 +00:00
TransliterationRule * temptr = new TransliterationRule (
left - > text , left - > ante , left - > post ,
right - > text , right - > cursor , right - > cursorOffset ,
segmentsArray ,
segmentObjects . size ( ) ,
left - > anchorStart , left - > anchorEnd ,
curData ,
status ) ;
//Null pointer check
if ( temptr = = NULL ) {
2008-02-23 19:15:18 +00:00
uprv_free ( segmentsArray ) ;
return syntaxError ( U_MEMORY_ALLOCATION_ERROR , rule , start , status ) ;
2008-01-14 22:29:44 +00:00
}
1999-11-20 00:40:50 +00:00
2008-01-14 22:29:44 +00:00
curData - > ruleSet . addRule ( temptr , status ) ;
2000-05-20 04:40:29 +00:00
2000-01-13 07:28:08 +00:00
return pos ;
1999-11-20 00:40:50 +00:00
}
/**
2000-01-13 07:28:08 +00:00
* Called by main parser upon syntax error . Search the rule string
* for the probable end of the rule . Of course , if the error is that
* the end of rule marker is missing , then the rule end will not be found .
* In any case the rule start will be correctly reported .
* @ param msg error description
* @ param rule pattern string
* @ param start position of first character of current rule
1999-11-20 00:40:50 +00:00
*/
2001-08-17 02:20:35 +00:00
int32_t TransliteratorParser : : syntaxError ( UErrorCode parseErrorCode ,
2006-07-13 20:29:11 +00:00
const UnicodeString & rule ,
int32_t pos ,
UErrorCode & status )
{
2001-10-10 19:29:45 +00:00
parseError . offset = pos ;
parseError . line = 0 ; /* we are not using line numbers */
// for pre-context
2001-11-09 00:12:59 +00:00
const int32_t LEN = U_PARSE_CONTEXT_LEN - 1 ;
int32_t start = uprv_max ( pos - LEN , 0 ) ;
2001-10-10 19:29:45 +00:00
int32_t stop = pos ;
2001-08-31 03:23:39 +00:00
2001-10-10 19:29:45 +00:00
rule . extract ( start , stop - start , parseError . preContext ) ;
//null terminate the buffer
parseError . preContext [ stop - start ] = 0 ;
2001-08-31 03:23:39 +00:00
2001-10-10 19:29:45 +00:00
//for post-context
2001-11-09 00:12:59 +00:00
start = pos ;
stop = uprv_min ( pos + LEN , rule . length ( ) ) ;
2001-08-31 03:23:39 +00:00
2001-10-10 19:29:45 +00:00
rule . extract ( start , stop - start , parseError . postContext ) ;
//null terminate the buffer
parseError . postContext [ stop - start ] = 0 ;
2001-08-16 00:55:16 +00:00
status = ( UErrorCode ) parseErrorCode ;
2001-08-31 03:23:39 +00:00
return pos ;
1999-11-20 00:40:50 +00:00
}
/**
2000-05-20 04:40:29 +00:00
* Parse a UnicodeSet out , store it , and return the stand - in character
* used to represent it .
1999-11-20 00:40:50 +00:00
*/
2001-07-13 21:09:41 +00:00
UChar TransliteratorParser : : parseSet ( const UnicodeString & rule ,
2006-07-13 20:29:11 +00:00
ParsePosition & pos ,
UErrorCode & status ) {
2003-10-14 21:47:59 +00:00
UnicodeSet * set = new UnicodeSet ( rule , pos , USET_IGNORE_SPACE , parseData , status ) ;
2008-01-14 22:29:44 +00:00
// Null pointer check
if ( set = = NULL ) {
2008-02-23 19:15:18 +00:00
status = U_MEMORY_ALLOCATION_ERROR ;
return ( UChar ) 0x0000 ; // Return empty character with error.
2008-01-14 22:29:44 +00:00
}
2001-07-27 00:18:53 +00:00
set - > compact ( ) ;
2006-07-13 20:29:11 +00:00
return generateStandInFor ( set , status ) ;
2001-07-27 00:18:53 +00:00
}
/**
2002-02-07 01:07:55 +00:00
* Generate and return a stand - in for a new UnicodeFunctor . Store
2001-07-27 00:18:53 +00:00
* the matcher ( adopt it ) .
*/
2006-07-13 20:29:11 +00:00
UChar TransliteratorParser : : generateStandInFor ( UnicodeFunctor * adopted , UErrorCode & status ) {
2002-02-07 01:07:55 +00:00
// assert(obj != null);
// Look up previous stand-in, if any. This is a short list
// (typical n is 0, 1, or 2); linear search is optimal.
2006-04-14 21:09:42 +00:00
for ( int32_t i = 0 ; i < variablesVector . size ( ) ; + + i ) {
if ( variablesVector . elementAt ( i ) = = adopted ) { // [sic] pointer comparison
2005-06-09 17:30:48 +00:00
return ( UChar ) ( curData - > variablesBase + i ) ;
2002-02-07 01:07:55 +00:00
}
}
2000-01-13 07:28:08 +00:00
if ( variableNext > = variableLimit ) {
2001-07-27 00:18:53 +00:00
delete adopted ;
2001-10-30 18:08:53 +00:00
status = U_VARIABLE_RANGE_EXHAUSTED ;
2000-01-13 07:28:08 +00:00
return 0 ;
1999-11-20 00:40:50 +00:00
}
2006-04-14 21:09:42 +00:00
variablesVector . addElement ( adopted , status ) ;
2000-02-08 02:49:15 +00:00
return variableNext + + ;
1999-11-20 00:40:50 +00:00
}
2002-02-07 01:07:55 +00:00
/**
* Return the standin for segment seg ( 1 - based ) .
*/
2006-07-13 20:29:11 +00:00
UChar TransliteratorParser : : getSegmentStandin ( int32_t seg , UErrorCode & status ) {
2002-02-07 01:07:55 +00:00
// Special character used to indicate an empty spot
2005-06-09 17:30:48 +00:00
UChar empty = curData - > variablesBase - 1 ;
2002-02-07 01:07:55 +00:00
while ( segmentStandins . length ( ) < seg ) {
segmentStandins . append ( empty ) ;
}
UChar c = segmentStandins . charAt ( seg - 1 ) ;
if ( c = = empty ) {
if ( variableNext > = variableLimit ) {
status = U_VARIABLE_RANGE_EXHAUSTED ;
return 0 ;
}
c = variableNext + + ;
// Set a placeholder in the master variables vector that will be
// filled in later by setSegmentObject(). We know that we will get
// called first because setSegmentObject() will call us.
2006-04-14 21:09:42 +00:00
variablesVector . addElement ( ( void * ) NULL , status ) ;
2002-02-07 01:07:55 +00:00
segmentStandins . setCharAt ( seg - 1 , c ) ;
}
return c ;
}
/**
* Set the object for segment seg ( 1 - based ) .
*/
2006-07-13 20:29:11 +00:00
void TransliteratorParser : : setSegmentObject ( int32_t seg , StringMatcher * adopted , UErrorCode & status ) {
2002-02-07 01:07:55 +00:00
// Since we call parseSection() recursively, nested
// segments will result in segment i+1 getting parsed
// and stored before segment i; be careful with the
// vector handling here.
2006-04-14 21:09:42 +00:00
if ( segmentObjects . size ( ) < seg ) {
2007-12-14 10:17:12 +00:00
segmentObjects . setSize ( seg , status ) ;
2002-02-07 01:07:55 +00:00
}
2006-07-13 20:29:11 +00:00
int32_t index = getSegmentStandin ( seg , status ) - curData - > variablesBase ;
2006-04-14 21:09:42 +00:00
if ( segmentObjects . elementAt ( seg - 1 ) ! = NULL | |
variablesVector . elementAt ( index ) ! = NULL ) {
2002-02-07 01:07:55 +00:00
// should never happen
status = U_INTERNAL_TRANSLITERATOR_ERROR ;
return ;
}
2006-04-14 21:09:42 +00:00
segmentObjects . setElementAt ( adopted , seg - 1 ) ;
variablesVector . setElementAt ( adopted , index ) ;
2002-02-07 01:07:55 +00:00
}
2001-10-17 19:21:12 +00:00
/**
* Return the stand - in for the dot set . It is allocated the first
* time and reused thereafter .
*/
2006-07-13 20:29:11 +00:00
UChar TransliteratorParser : : getDotStandIn ( UErrorCode & status ) {
2001-10-17 19:21:12 +00:00
if ( dotStandIn = = ( UChar ) - 1 ) {
2011-07-07 18:46:19 +00:00
UnicodeSet * tempus = new UnicodeSet ( UnicodeString ( TRUE , DOT_SET , - 1 ) , status ) ;
2008-02-23 19:15:18 +00:00
// Null pointer check.
if ( tempus = = NULL ) {
status = U_MEMORY_ALLOCATION_ERROR ;
return ( UChar ) 0x0000 ;
}
2008-01-14 22:29:44 +00:00
dotStandIn = generateStandInFor ( tempus , status ) ;
2001-10-17 19:21:12 +00:00
}
return dotStandIn ;
}
2000-05-20 04:40:29 +00:00
/**
* Append the value of the given variable name to the given
* UnicodeString .
*/
2001-07-13 21:09:41 +00:00
void TransliteratorParser : : appendVariableDef ( const UnicodeString & name ,
2006-07-13 20:29:11 +00:00
UnicodeString & buf ,
UErrorCode & status ) {
2006-04-14 21:09:42 +00:00
const UnicodeString * s = ( const UnicodeString * ) variableNames . get ( name ) ;
2000-05-20 04:40:29 +00:00
if ( s = = NULL ) {
// We allow one undefined variable so that variable definition
// statements work. For the first undefined variable we return
// the special placeholder variableLimit-1, and save the variable
// name.
if ( undefinedVariableName . length ( ) = = 0 ) {
undefinedVariableName = name ;
if ( variableNext > = variableLimit ) {
// throw new RuntimeException("Private use variables exhausted");
status = U_ILLEGAL_ARGUMENT_ERROR ;
return ;
}
buf . append ( ( UChar ) - - variableLimit ) ;
} else {
//throw new IllegalArgumentException("Undefined variable $"
// + name);
status = U_ILLEGAL_ARGUMENT_ERROR ;
return ;
}
} else {
buf . append ( * s ) ;
}
}
2002-02-07 01:07:55 +00:00
/**
* Glue method to get around access restrictions in C + + .
*/
2007-06-11 17:37:31 +00:00
/*Transliterator* TransliteratorParser::createBasicInstance(const UnicodeString& id, const UnicodeString* canonID) {
2002-02-07 01:07:55 +00:00
return Transliterator : : createBasicInstance ( id , canonID ) ;
2007-06-11 17:37:31 +00:00
} */
2001-07-30 23:23:51 +00:00
2001-10-08 23:26:58 +00:00
U_NAMESPACE_END
2002-09-20 01:54:48 +00:00
2005-03-08 07:04:29 +00:00
U_CAPI int32_t
utrans_stripRules ( const UChar * source , int32_t sourceLen , UChar * target , UErrorCode * status ) {
2006-09-04 16:36:21 +00:00
U_NAMESPACE_USE
2005-03-09 05:45:01 +00:00
//const UChar *sourceStart = source;
2005-03-08 07:04:29 +00:00
const UChar * targetStart = target ;
const UChar * sourceLimit = source + sourceLen ;
UChar * targetLimit = target + sourceLen ;
UChar32 c = 0 ;
UBool quoted = FALSE ;
int32_t index ;
uprv_memset ( target , 0 , sourceLen * U_SIZEOF_UCHAR ) ;
/* read the rules into the buffer */
while ( source < sourceLimit )
{
index = 0 ;
U16_NEXT_UNSAFE ( source , index , c ) ;
source + = index ;
if ( c = = QUOTE ) {
quoted = ( UBool ) ! quoted ;
}
else if ( ! quoted ) {
if ( c = = RULE_COMMENT_CHAR ) {
/* skip comments and all preceding spaces */
while ( targetStart < target & & * ( target - 1 ) = = 0x0020 ) {
target - - ;
}
do {
2015-09-04 23:45:19 +00:00
if ( source = = sourceLimit ) {
c = U_SENTINEL ;
break ;
}
2005-03-08 07:04:29 +00:00
c = * ( source + + ) ;
}
while ( c ! = CR & & c ! = LF ) ;
2015-09-04 23:45:19 +00:00
if ( c < 0 ) {
break ;
}
2005-03-08 07:04:29 +00:00
}
2015-09-04 23:45:19 +00:00
else if ( c = = ESCAPE & & source < sourceLimit ) {
2005-03-08 07:04:29 +00:00
UChar32 c2 = * source ;
if ( c2 = = CR | | c2 = = LF ) {
/* A backslash at the end of a line. */
/* Since we're stripping lines, ignore the backslash. */
source + + ;
continue ;
}
if ( c2 = = 0x0075 & & source + 5 < sourceLimit ) { /* \u seen. \U isn't unescaped. */
int32_t escapeOffset = 0 ;
UnicodeString escapedStr ( source , 5 ) ;
c2 = escapedStr . unescapeAt ( escapeOffset ) ;
if ( c2 = = ( UChar32 ) 0xFFFFFFFF | | escapeOffset = = 0 )
{
* status = U_PARSE_ERROR ;
return 0 ;
}
2011-04-26 06:39:29 +00:00
if ( ! PatternProps : : isWhiteSpace ( c2 ) & & ! u_iscntrl ( c2 ) & & ! u_ispunct ( c2 ) ) {
2005-03-08 07:04:29 +00:00
/* It was escaped for a reason. Write what it was suppose to be. */
source + = 5 ;
c = c2 ;
}
}
else if ( c2 = = QUOTE ) {
/* \' seen. Make sure we don't do anything when we see it again. */
quoted = ( UBool ) ! quoted ;
}
}
}
if ( c = = CR | | c = = LF )
{
/* ignore spaces carriage returns, and all leading spaces on the next line.
* and line feed unless in the form \ uXXXX
*/
quoted = FALSE ;
while ( source < sourceLimit ) {
c = * ( source ) ;
if ( c ! = CR & & c ! = LF & & c ! = 0x0020 ) {
break ;
}
source + + ;
}
continue ;
}
/* Append UChar * after dissembling if c > 0xffff*/
index = 0 ;
U16_APPEND_UNSAFE ( target , index , c ) ;
target + = index ;
}
if ( target < targetLimit ) {
* target = 0 ;
}
return ( int32_t ) ( target - targetStart ) ;
}
2002-09-20 01:54:48 +00:00
# endif /* #if !UCONFIG_NO_TRANSLITERATION */