1ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* 2ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ********************************************************************** 3b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho * Copyright (C) 1999-2011, International Business Machines 4ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Corporation and others. All Rights Reserved. 5ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ********************************************************************** 6ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Date Name Description 7ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 11/17/99 aliu Creation. 8ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ********************************************************************** 9ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 10ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 11ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/utypes.h" 12ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 13ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#if !UCONFIG_NO_TRANSLITERATION 14ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 15ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/uobject.h" 16ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/parseerr.h" 17ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/parsepos.h" 18ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/putil.h" 19ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/uchar.h" 20ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/ustring.h" 21ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/uniset.h" 22ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "cstring.h" 23ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "funcrepl.h" 24ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "hash.h" 25ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "quant.h" 26ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "rbt.h" 27ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "rbt_data.h" 28ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "rbt_pars.h" 29ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "rbt_rule.h" 30ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "strmatch.h" 31ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "strrepl.h" 32ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "unicode/symtable.h" 33ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "tridpars.h" 34ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "uvector.h" 35ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "hash.h" 36b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho#include "patternprops.h" 37ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "util.h" 38ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "cmemory.h" 39ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "uprops.h" 40ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "putilimp.h" 41ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 42ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// Operators 43ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define VARIABLE_DEF_OP ((UChar)0x003D) /*=*/ 44ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define FORWARD_RULE_OP ((UChar)0x003E) /*>*/ 45ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define REVERSE_RULE_OP ((UChar)0x003C) /*<*/ 46ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define FWDREV_RULE_OP ((UChar)0x007E) /*~*/ // internal rep of <> op 47ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 48ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// Other special characters 49ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define QUOTE ((UChar)0x0027) /*'*/ 50ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define ESCAPE ((UChar)0x005C) /*\*/ 51ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define END_OF_RULE ((UChar)0x003B) /*;*/ 52ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define RULE_COMMENT_CHAR ((UChar)0x0023) /*#*/ 53ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 54ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define SEGMENT_OPEN ((UChar)0x0028) /*(*/ 55ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define SEGMENT_CLOSE ((UChar)0x0029) /*)*/ 56ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define CONTEXT_ANTE ((UChar)0x007B) /*{*/ 57ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define CONTEXT_POST ((UChar)0x007D) /*}*/ 58ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define CURSOR_POS ((UChar)0x007C) /*|*/ 59ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define CURSOR_OFFSET ((UChar)0x0040) /*@*/ 60ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define ANCHOR_START ((UChar)0x005E) /*^*/ 61ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define KLEENE_STAR ((UChar)0x002A) /***/ 62ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define ONE_OR_MORE ((UChar)0x002B) /*+*/ 63ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define ZERO_OR_ONE ((UChar)0x003F) /*?*/ 64ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 65ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define DOT ((UChar)46) /*.*/ 66ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 67ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic const UChar DOT_SET[] = { // "[^[:Zp:][:Zl:]\r\n$]"; 68ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 91, 94, 91, 58, 90, 112, 58, 93, 91, 58, 90, 69ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 108, 58, 93, 92, 114, 92, 110, 36, 93, 0 70ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}; 71ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 72ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// A function is denoted &Source-Target/Variant(text) 73ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define FUNCTION ((UChar)38) /*&*/ 74ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 75ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// Aliases for some of the syntax characters. These are provided so 76ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// transliteration rules can be expressed in XML without clashing with 77ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// XML syntax characters '<', '>', and '&'. 78ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define ALT_REVERSE_RULE_OP ((UChar)0x2190) // Left Arrow 79ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define ALT_FORWARD_RULE_OP ((UChar)0x2192) // Right Arrow 80ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define ALT_FWDREV_RULE_OP ((UChar)0x2194) // Left Right Arrow 81ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define ALT_FUNCTION ((UChar)0x2206) // Increment (~Greek Capital Delta) 82ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 83ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// Special characters disallowed at the top level 84ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic const UChar ILLEGAL_TOP[] = {41,0}; // ")" 85ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 86ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// Special characters disallowed within a segment 87ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic const UChar ILLEGAL_SEG[] = {123,125,124,64,0}; // "{}|@" 88ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 89ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// Special characters disallowed within a function argument 90ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic const UChar ILLEGAL_FUNC[] = {94,40,46,42,43,63,123,125,124,64,0}; // "^(.*+?{}|@" 91ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 92ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// By definition, the ANCHOR_END special character is a 93ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// trailing SymbolTable.SYMBOL_REF character. 94ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// private static final char ANCHOR_END = '$'; 95ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 96ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic const UChar gOPERATORS[] = { // "=><" 97ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru VARIABLE_DEF_OP, FORWARD_RULE_OP, REVERSE_RULE_OP, 98ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ALT_FORWARD_RULE_OP, ALT_REVERSE_RULE_OP, ALT_FWDREV_RULE_OP, 99ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 0 100ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}; 101ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 102ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic const UChar HALF_ENDERS[] = { // "=><;" 103ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru VARIABLE_DEF_OP, FORWARD_RULE_OP, REVERSE_RULE_OP, 104ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ALT_FORWARD_RULE_OP, ALT_REVERSE_RULE_OP, ALT_FWDREV_RULE_OP, 105ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru END_OF_RULE, 106ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 0 107ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}; 108ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 109ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// These are also used in Transliterator::toRules() 110ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic const int32_t ID_TOKEN_LEN = 2; 111ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic const UChar ID_TOKEN[] = { 0x3A, 0x3A }; // ':', ':' 112ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 113ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/* 114ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querucommented out until we do real ::BEGIN/::END functionality 115ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic const int32_t BEGIN_TOKEN_LEN = 5; 116ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic const UChar BEGIN_TOKEN[] = { 0x42, 0x45, 0x47, 0x49, 0x4e }; // 'BEGIN' 117ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 118ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic const int32_t END_TOKEN_LEN = 3; 119ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic const UChar END_TOKEN[] = { 0x45, 0x4e, 0x44 }; // 'END' 120ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru*/ 121ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 122ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_NAMESPACE_BEGIN 123ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 124ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//---------------------------------------------------------------------- 125ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// BEGIN ParseData 126ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//---------------------------------------------------------------------- 127ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 128ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 129ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * This class implements the SymbolTable interface. It is used 130ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * during parsing to give UnicodeSet access to variables that 131ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * have been defined so far. Note that it uses variablesVector, 132ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * _not_ data.setVariables. 133ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 134ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruclass ParseData : public UMemory, public SymbolTable { 135ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querupublic: 136ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru const TransliterationRuleData* data; // alias 137ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 138ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru const UVector* variablesVector; // alias 139ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 140ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru const Hashtable* variableNames; // alias 141ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 142ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ParseData(const TransliterationRuleData* data = 0, 143ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru const UVector* variablesVector = 0, 144ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru const Hashtable* variableNames = 0); 145ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 146ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru virtual const UnicodeString* lookup(const UnicodeString& s) const; 147ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 148ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru virtual const UnicodeFunctor* lookupMatcher(UChar32 ch) const; 149ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 150ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru virtual UnicodeString parseReference(const UnicodeString& text, 151ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ParsePosition& pos, int32_t limit) const; 152ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /** 153ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Return true if the given character is a matcher standin or a plain 154ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * character (non standin). 155ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 156ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UBool isMatcher(UChar32 ch); 157ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 158ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /** 159ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Return true if the given character is a replacer standin or a plain 160ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * character (non standin). 161ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 162ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UBool isReplacer(UChar32 ch); 163ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 164ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruprivate: 165ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ParseData(const ParseData &other); // forbid copying of this class 166ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ParseData &operator=(const ParseData &other); // forbid copying of this class 167ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}; 168ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 169ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruParseData::ParseData(const TransliterationRuleData* d, 170ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru const UVector* sets, 171ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru const Hashtable* vNames) : 172ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru data(d), variablesVector(sets), variableNames(vNames) {} 173ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 174ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 175ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Implement SymbolTable API. 176ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 177ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruconst UnicodeString* ParseData::lookup(const UnicodeString& name) const { 178ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return (const UnicodeString*) variableNames->get(name); 179ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 180ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 181ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 182ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Implement SymbolTable API. 183ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 184ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruconst UnicodeFunctor* ParseData::lookupMatcher(UChar32 ch) const { 185ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Note that we cannot use data.lookupSet() because the 186ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // set array has not been constructed yet. 187ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru const UnicodeFunctor* set = NULL; 188ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t i = ch - data->variablesBase; 189ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (i >= 0 && i < variablesVector->size()) { 190ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t i = ch - data->variablesBase; 191ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru set = (i < variablesVector->size()) ? 192ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru (UnicodeFunctor*) variablesVector->elementAt(i) : 0; 193ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 194ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return set; 195ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 196ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 197ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 198ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Implement SymbolTable API. Parse out a symbol reference 199ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * name. 200ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 201ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUnicodeString ParseData::parseReference(const UnicodeString& text, 202ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ParsePosition& pos, int32_t limit) const { 203ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t start = pos.getIndex(); 204ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t i = start; 205ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UnicodeString result; 206ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru while (i < limit) { 207ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UChar c = text.charAt(i); 208ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if ((i==start && !u_isIDStart(c)) || !u_isIDPart(c)) { 209ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 210ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 211ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ++i; 212ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 213ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (i == start) { // No valid name chars 214ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return result; // Indicate failure with empty string 215ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 216ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pos.setIndex(i); 217ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru text.extractBetween(start, i, result); 218ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return result; 219ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 220ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 221ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUBool ParseData::isMatcher(UChar32 ch) { 222ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Note that we cannot use data.lookup() because the 223ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // set array has not been constructed yet. 224ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t i = ch - data->variablesBase; 225ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (i >= 0 && i < variablesVector->size()) { 226ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UnicodeFunctor *f = (UnicodeFunctor*) variablesVector->elementAt(i); 227ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return f != NULL && f->toMatcher() != NULL; 228ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 229ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return TRUE; 230ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 231ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 232ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 233ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Return true if the given character is a replacer standin or a plain 234ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * character (non standin). 235ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 236ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUBool ParseData::isReplacer(UChar32 ch) { 237ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Note that we cannot use data.lookup() because the 238ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // set array has not been constructed yet. 239ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int i = ch - data->variablesBase; 240ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (i >= 0 && i < variablesVector->size()) { 241ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UnicodeFunctor *f = (UnicodeFunctor*) variablesVector->elementAt(i); 242ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return f != NULL && f->toReplacer() != NULL; 243ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 244ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return TRUE; 245ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 246ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 247ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//---------------------------------------------------------------------- 248ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// BEGIN RuleHalf 249ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//---------------------------------------------------------------------- 250ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 251ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 252ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * A class representing one side of a rule. This class knows how to 253ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * parse half of a rule. It is tightly coupled to the method 254ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * RuleBasedTransliterator.Parser.parseRule(). 255ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 256ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruclass RuleHalf : public UMemory { 257ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 258ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querupublic: 259ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 260ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UnicodeString text; 261ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 262ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t cursor; // position of cursor in text 263ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t ante; // position of ante context marker '{' in text 264ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t post; // position of post context marker '}' in text 265ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 266ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Record the offset to the cursor either to the left or to the 267ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // right of the key. This is indicated by characters on the output 268ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // side that allow the cursor to be positioned arbitrarily within 269ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // the matching text. For example, abc{def} > | @@@ xyz; changes 270ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // def to xyz and moves the cursor to before abc. Offset characters 271ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // must be at the start or end, and they cannot move the cursor past 272ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // the ante- or postcontext text. Placeholders are only valid in 273ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // output text. The length of the ante and post context is 274ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // determined at runtime, because of supplementals and quantifiers. 275ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t cursorOffset; // only nonzero on output side 276ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 277ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Position of first CURSOR_OFFSET on _right_. This will be -1 278ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // for |@, -2 for |@@, etc., and 1 for @|, 2 for @@|, etc. 279ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t cursorOffsetPos; 280ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 281ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UBool anchorStart; 282ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UBool anchorEnd; 283ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 284ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /** 285ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * The segment number from 1..n of the next '(' we see 286ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * during parsing; 1-based. 287ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 288ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t nextSegmentNumber; 289ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 290ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru TransliteratorParser& parser; 291ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 292ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru //-------------------------------------------------- 293ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Methods 294ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 295ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru RuleHalf(TransliteratorParser& parser); 296ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ~RuleHalf(); 297ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 298ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t parse(const UnicodeString& rule, int32_t pos, int32_t limit, UErrorCode& status); 299ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 300ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t parseSection(const UnicodeString& rule, int32_t pos, int32_t limit, 301ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UnicodeString& buf, 302ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru const UnicodeString& illegal, 303ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UBool isSegment, 304ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UErrorCode& status); 305ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 306ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /** 307ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Remove context. 308ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 309ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru void removeContext(); 310ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 311ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /** 312ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Return true if this half looks like valid output, that is, does not 313ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * contain quantifiers or other special input-only elements. 314ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 315ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UBool isValidOutput(TransliteratorParser& parser); 316ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 317ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /** 318ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Return true if this half looks like valid input, that is, does not 319ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * contain functions or other special output-only elements. 320ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 321ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UBool isValidInput(TransliteratorParser& parser); 322ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 323ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int syntaxError(UErrorCode code, 324ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru const UnicodeString& rule, 325ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t start, 326ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UErrorCode& status) { 327ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return parser.syntaxError(code, rule, start, status); 328ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 329ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 330ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruprivate: 331ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Disallowed methods; no impl. 332ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru RuleHalf(const RuleHalf&); 333ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru RuleHalf& operator=(const RuleHalf&); 334ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}; 335ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 336ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruRuleHalf::RuleHalf(TransliteratorParser& p) : 337ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru parser(p) 338ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{ 339ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cursor = -1; 340ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ante = -1; 341ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru post = -1; 342ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cursorOffset = 0; 343ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cursorOffsetPos = 0; 344ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru anchorStart = anchorEnd = FALSE; 345ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru nextSegmentNumber = 1; 346ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 347ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 348ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruRuleHalf::~RuleHalf() { 349ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 350ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 351ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 352ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Parse one side of a rule, stopping at either the limit, 353ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * the END_OF_RULE character, or an operator. 354ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * @return the index after the terminating character, or 355ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * if limit was reached, limit 356ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 357ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruint32_t RuleHalf::parse(const UnicodeString& rule, int32_t pos, int32_t limit, UErrorCode& status) { 358ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t start = pos; 359ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru text.truncate(0); 360ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pos = parseSection(rule, pos, limit, text, ILLEGAL_TOP, FALSE, status); 361ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 362ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (cursorOffset > 0 && cursor != cursorOffsetPos) { 363ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return syntaxError(U_MISPLACED_CURSOR_OFFSET, rule, start, status); 364ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 365ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 366ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return pos; 367ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 368ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 369ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 370ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Parse a section of one side of a rule, stopping at either 371ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * the limit, the END_OF_RULE character, an operator, or a 372ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * segment close character. This method parses both a 373ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * top-level rule half and a segment within such a rule half. 374ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * It calls itself recursively to parse segments and nested 375ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * segments. 376ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * @param buf buffer into which to accumulate the rule pattern 377ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * characters, either literal characters from the rule or 378ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * standins for UnicodeMatcher objects including segments. 379ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * @param illegal the set of special characters that is illegal during 380ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * this parse. 381ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * @param isSegment if true, then we've already seen a '(' and 382ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * pos on entry points right after it. Accumulate everything 383ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * up to the closing ')', put it in a segment matcher object, 384ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * generate a standin for it, and add the standin to buf. As 385ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * a side effect, update the segments vector with a reference 386ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * to the segment matcher. This works recursively for nested 387ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * segments. If isSegment is false, just accumulate 388ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * characters into buf. 389ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * @return the index after the terminating character, or 390ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * if limit was reached, limit 391ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 392ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruint32_t RuleHalf::parseSection(const UnicodeString& rule, int32_t pos, int32_t limit, 393ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UnicodeString& buf, 394ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru const UnicodeString& illegal, 395ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UBool isSegment, UErrorCode& status) { 396ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t start = pos; 397ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ParsePosition pp; 398ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UnicodeString scratch; 399ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UBool done = FALSE; 400ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t quoteStart = -1; // Most recent 'single quoted string' 401ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t quoteLimit = -1; 402ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t varStart = -1; // Most recent $variableReference 403ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t varLimit = -1; 404ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t bufStart = buf.length(); 405ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 406ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru while (pos < limit && !done) { 407ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Since all syntax characters are in the BMP, fetching 408ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // 16-bit code units suffices here. 409ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UChar c = rule.charAt(pos++); 410b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho if (PatternProps::isWhiteSpace(c)) { 411ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Ignore whitespace. Note that this is not Unicode 412ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // spaces, but Java spaces -- a subset, representing 413ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // whitespace likely to be seen in code. 414ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru continue; 415ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 416ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (u_strchr(HALF_ENDERS, c) != NULL) { 417ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (isSegment) { 418ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Unclosed segment 419ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return syntaxError(U_UNCLOSED_SEGMENT, rule, start, status); 420ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 421ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 422ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 423ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (anchorEnd) { 424ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Text after a presumed end anchor is a syntax err 425ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return syntaxError(U_MALFORMED_VARIABLE_REFERENCE, rule, start, status); 426ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 427ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (UnicodeSet::resemblesPattern(rule, pos-1)) { 428ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pp.setIndex(pos-1); // Backup to opening '[' 429ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru buf.append(parser.parseSet(rule, pp, status)); 430ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (U_FAILURE(status)) { 431ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return syntaxError(U_MALFORMED_SET, rule, start, status); 432ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 433ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pos = pp.getIndex(); 434ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru continue; 435ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 436ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Handle escapes 437ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (c == ESCAPE) { 438ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (pos == limit) { 439ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return syntaxError(U_TRAILING_BACKSLASH, rule, start, status); 440ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 441ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UChar32 escaped = rule.unescapeAt(pos); // pos is already past '\\' 442ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (escaped == (UChar32) -1) { 443ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return syntaxError(U_MALFORMED_UNICODE_ESCAPE, rule, start, status); 444ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 445ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (!parser.checkVariableRange(escaped)) { 446ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return syntaxError(U_VARIABLE_RANGE_OVERLAP, rule, start, status); 447ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 448ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru buf.append(escaped); 449ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru continue; 450ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 451ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Handle quoted matter 452ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (c == QUOTE) { 453ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t iq = rule.indexOf(QUOTE, pos); 454ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (iq == pos) { 455ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru buf.append(c); // Parse [''] outside quotes as ['] 456ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ++pos; 457ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 458ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* This loop picks up a run of quoted text of the 459ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * form 'aaaa' each time through. If this run 460ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * hasn't really ended ('aaaa''bbbb') then it keeps 461ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * looping, each time adding on a new run. When it 462ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * reaches the final quote it breaks. 463ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 464ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru quoteStart = buf.length(); 465ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru for (;;) { 466ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (iq < 0) { 467ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return syntaxError(U_UNTERMINATED_QUOTE, rule, start, status); 468ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 469ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru scratch.truncate(0); 470ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru rule.extractBetween(pos, iq, scratch); 471ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru buf.append(scratch); 472ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pos = iq+1; 473ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (pos < limit && rule.charAt(pos) == QUOTE) { 474ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Parse [''] inside quotes as ['] 475ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru iq = rule.indexOf(QUOTE, pos+1); 476ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Continue looping 477ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 478ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 479ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 480ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 481ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru quoteLimit = buf.length(); 482ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 483ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru for (iq=quoteStart; iq<quoteLimit; ++iq) { 484ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (!parser.checkVariableRange(buf.charAt(iq))) { 485ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return syntaxError(U_VARIABLE_RANGE_OVERLAP, rule, start, status); 486ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 487ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 488ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 489ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru continue; 490ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 491ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 492ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (!parser.checkVariableRange(c)) { 493ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return syntaxError(U_VARIABLE_RANGE_OVERLAP, rule, start, status); 494ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 495ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 496ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (illegal.indexOf(c) >= 0) { 497ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru syntaxError(U_ILLEGAL_CHARACTER, rule, start, status); 498ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 499ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 500ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru switch (c) { 501ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 502ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru //------------------------------------------------------ 503ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Elements allowed within and out of segments 504ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru //------------------------------------------------------ 505ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case ANCHOR_START: 506ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (buf.length() == 0 && !anchorStart) { 507ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru anchorStart = TRUE; 508ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 509ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return syntaxError(U_MISPLACED_ANCHOR_START, 510ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru rule, start, status); 511ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 512ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 513ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case SEGMENT_OPEN: 514ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru { 515ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // bufSegStart is the offset in buf to the first 516ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // character of the segment we are parsing. 517ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t bufSegStart = buf.length(); 518ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 519ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Record segment number now, since nextSegmentNumber 520ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // will be incremented during the call to parseSection 521ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // if there are nested segments. 522ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t segmentNumber = nextSegmentNumber++; // 1-based 523ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 524ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Parse the segment 525ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pos = parseSection(rule, pos, limit, buf, ILLEGAL_SEG, TRUE, status); 526ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 527ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // After parsing a segment, the relevant characters are 528ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // in buf, starting at offset bufSegStart. Extract them 529ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // into a string matcher, and replace them with a 530ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // standin for that matcher. 531ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru StringMatcher* m = 532ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru new StringMatcher(buf, bufSegStart, buf.length(), 533ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru segmentNumber, *parser.curData); 53485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho if (m == NULL) { 53585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho return syntaxError(U_MEMORY_ALLOCATION_ERROR, rule, start, status); 53685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho } 537ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 538ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Record and associate object and segment number 539ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru parser.setSegmentObject(segmentNumber, m, status); 540ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru buf.truncate(bufSegStart); 541ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru buf.append(parser.getSegmentStandin(segmentNumber, status)); 542ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 543ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 544ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case FUNCTION: 545ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case ALT_FUNCTION: 546ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru { 547ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t iref = pos; 548ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru TransliteratorIDParser::SingleID* single = 549ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru TransliteratorIDParser::parseFilterID(rule, iref); 550ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // The next character MUST be a segment open 551ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (single == NULL || 552ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru !ICU_Utility::parseChar(rule, iref, SEGMENT_OPEN)) { 553ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return syntaxError(U_INVALID_FUNCTION, rule, start, status); 554ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 555ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 556ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru Transliterator *t = single->createInstance(); 557ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru delete single; 558ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (t == NULL) { 559ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return syntaxError(U_INVALID_FUNCTION, rule, start, status); 560ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 561ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 562ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // bufSegStart is the offset in buf to the first 563ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // character of the segment we are parsing. 564ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t bufSegStart = buf.length(); 565ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 566ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Parse the segment 567ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pos = parseSection(rule, iref, limit, buf, ILLEGAL_FUNC, TRUE, status); 568ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 569ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // After parsing a segment, the relevant characters are 570ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // in buf, starting at offset bufSegStart. 571ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UnicodeString output; 572ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru buf.extractBetween(bufSegStart, buf.length(), output); 573ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru FunctionReplacer *r = 574ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru new FunctionReplacer(t, new StringReplacer(output, parser.curData)); 57585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho if (r == NULL) { 57685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho return syntaxError(U_MEMORY_ALLOCATION_ERROR, rule, start, status); 57785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho } 578ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 579ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Replace the buffer contents with a stand-in 580ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru buf.truncate(bufSegStart); 581ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru buf.append(parser.generateStandInFor(r, status)); 582ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 583ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 584ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case SymbolTable::SYMBOL_REF: 585ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Handle variable references and segment references "$1" .. "$9" 586ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru { 587ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // A variable reference must be followed immediately 588ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // by a Unicode identifier start and zero or more 589ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Unicode identifier part characters, or by a digit 590ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // 1..9 if it is a segment reference. 591ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (pos == limit) { 592ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // A variable ref character at the end acts as 593ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // an anchor to the context limit, as in perl. 594ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru anchorEnd = TRUE; 595ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 596ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 597ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Parse "$1" "$2" .. "$9" .. (no upper limit) 598ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru c = rule.charAt(pos); 599ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t r = u_digit(c, 10); 600ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (r >= 1 && r <= 9) { 601ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru r = ICU_Utility::parseNumber(rule, pos, 10); 602ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (r < 0) { 603ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return syntaxError(U_UNDEFINED_SEGMENT_REFERENCE, 604ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru rule, start, status); 605ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 606ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru buf.append(parser.getSegmentStandin(r, status)); 607ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 608ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pp.setIndex(pos); 609ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UnicodeString name = parser.parseData-> 610ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru parseReference(rule, pp, limit); 611ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (name.length() == 0) { 612ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // This means the '$' was not followed by a 613ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // valid name. Try to interpret it as an 614ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // end anchor then. If this also doesn't work 615ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // (if we see a following character) then signal 616ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // an error. 617ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru anchorEnd = TRUE; 618ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 619ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 620ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pos = pp.getIndex(); 621ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // If this is a variable definition statement, 622ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // then the LHS variable will be undefined. In 623ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // that case appendVariableDef() will append the 624ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // special placeholder char variableLimit-1. 625ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru varStart = buf.length(); 626ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru parser.appendVariableDef(name, buf, status); 627ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru varLimit = buf.length(); 628ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 629ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 630ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 631ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case DOT: 632ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru buf.append(parser.getDotStandIn(status)); 633ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 634ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case KLEENE_STAR: 635ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case ONE_OR_MORE: 636ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case ZERO_OR_ONE: 637ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Quantifiers. We handle single characters, quoted strings, 638ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // variable references, and segments. 639ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // a+ matches aaa 640ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // 'foo'+ matches foofoofoo 641ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // $v+ matches xyxyxy if $v == xy 642ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // (seg)+ matches segsegseg 643ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru { 644ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (isSegment && buf.length() == bufStart) { 645ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // The */+ immediately follows '(' 646ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return syntaxError(U_MISPLACED_QUANTIFIER, rule, start, status); 647ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 648ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 649ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t qstart, qlimit; 650ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // The */+ follows an isolated character or quote 651ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // or variable reference 652ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (buf.length() == quoteLimit) { 653ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // The */+ follows a 'quoted string' 654ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru qstart = quoteStart; 655ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru qlimit = quoteLimit; 656ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else if (buf.length() == varLimit) { 657ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // The */+ follows a $variableReference 658ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru qstart = varStart; 659ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru qlimit = varLimit; 660ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 661ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // The */+ follows a single character, possibly 662ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // a segment standin 663ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru qstart = buf.length() - 1; 664ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru qlimit = qstart + 1; 665ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 666ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 667ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UnicodeFunctor *m = 668ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru new StringMatcher(buf, qstart, qlimit, 0, *parser.curData); 66985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho if (m == NULL) { 67085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho return syntaxError(U_MEMORY_ALLOCATION_ERROR, rule, start, status); 67185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho } 672ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t min = 0; 673ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t max = Quantifier::MAX; 674ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru switch (c) { 675ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case ONE_OR_MORE: 676ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru min = 1; 677ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 678ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case ZERO_OR_ONE: 679ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru min = 0; 680ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru max = 1; 681ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 682ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // case KLEENE_STAR: 683ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // do nothing -- min, max already set 684ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 685ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru m = new Quantifier(m, min, max); 68685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho if (m == NULL) { 68785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho return syntaxError(U_MEMORY_ALLOCATION_ERROR, rule, start, status); 68885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho } 689ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru buf.truncate(qstart); 690ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru buf.append(parser.generateStandInFor(m, status)); 691ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 692ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 693ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 694ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru //------------------------------------------------------ 695ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Elements allowed ONLY WITHIN segments 696ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru //------------------------------------------------------ 697ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case SEGMENT_CLOSE: 698ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // assert(isSegment); 699ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // We're done parsing a segment. 700ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru done = TRUE; 701ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 702ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 703ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru //------------------------------------------------------ 704ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Elements allowed ONLY OUTSIDE segments 705ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru //------------------------------------------------------ 706ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case CONTEXT_ANTE: 707ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (ante >= 0) { 708ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return syntaxError(U_MULTIPLE_ANTE_CONTEXTS, rule, start, status); 709ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 710ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ante = buf.length(); 711ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 712ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case CONTEXT_POST: 713ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (post >= 0) { 714ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return syntaxError(U_MULTIPLE_POST_CONTEXTS, rule, start, status); 715ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 716ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru post = buf.length(); 717ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 718ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case CURSOR_POS: 719ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (cursor >= 0) { 720ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return syntaxError(U_MULTIPLE_CURSORS, rule, start, status); 721ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 722ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cursor = buf.length(); 723ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 724ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case CURSOR_OFFSET: 725ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (cursorOffset < 0) { 726ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (buf.length() > 0) { 727ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return syntaxError(U_MISPLACED_CURSOR_OFFSET, rule, start, status); 728ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 729ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru --cursorOffset; 730ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else if (cursorOffset > 0) { 731ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (buf.length() != cursorOffsetPos || cursor >= 0) { 732ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return syntaxError(U_MISPLACED_CURSOR_OFFSET, rule, start, status); 733ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 734ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ++cursorOffset; 735ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 736ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (cursor == 0 && buf.length() == 0) { 737ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cursorOffset = -1; 738ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else if (cursor < 0) { 739ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cursorOffsetPos = buf.length(); 740ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru cursorOffset = 1; 741ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 742ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return syntaxError(U_MISPLACED_CURSOR_OFFSET, rule, start, status); 743ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 744ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 745ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 746ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 747ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 748ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru //------------------------------------------------------ 749ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Non-special characters 750ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru //------------------------------------------------------ 751ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru default: 752ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Disallow unquoted characters other than [0-9A-Za-z] 753ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // in the printable ASCII range. These characters are 754ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // reserved for possible future use. 755ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (c >= 0x0021 && c <= 0x007E && 756ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru !((c >= 0x0030/*'0'*/ && c <= 0x0039/*'9'*/) || 757ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru (c >= 0x0041/*'A'*/ && c <= 0x005A/*'Z'*/) || 758ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru (c >= 0x0061/*'a'*/ && c <= 0x007A/*'z'*/))) { 759ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return syntaxError(U_UNQUOTED_SPECIAL, rule, start, status); 760ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 761ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru buf.append(c); 762ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 763ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 764ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 765ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 766ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return pos; 767ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 768ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 769ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 770ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Remove context. 771ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 772ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid RuleHalf::removeContext() { 773ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru //text = text.substring(ante < 0 ? 0 : ante, 774ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // post < 0 ? text.length() : post); 775ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (post >= 0) { 776ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru text.remove(post); 777ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 778ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (ante >= 0) { 779ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru text.removeBetween(0, ante); 780ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 781ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ante = post = -1; 782ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru anchorStart = anchorEnd = FALSE; 783ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 784ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 785ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 786ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Return true if this half looks like valid output, that is, does not 787ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * contain quantifiers or other special input-only elements. 788ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 789ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUBool RuleHalf::isValidOutput(TransliteratorParser& transParser) { 790ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru for (int32_t i=0; i<text.length(); ) { 791ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UChar32 c = text.char32At(i); 792ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru i += UTF_CHAR_LENGTH(c); 793ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (!transParser.parseData->isReplacer(c)) { 794ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return FALSE; 795ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 796ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 797ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return TRUE; 798ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 799ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 800ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 801ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Return true if this half looks like valid input, that is, does not 802ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * contain functions or other special output-only elements. 803ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 804ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUBool RuleHalf::isValidInput(TransliteratorParser& transParser) { 805ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru for (int32_t i=0; i<text.length(); ) { 806ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UChar32 c = text.char32At(i); 807ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru i += UTF_CHAR_LENGTH(c); 808ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (!transParser.parseData->isMatcher(c)) { 809ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return FALSE; 810ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 811ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 812ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return TRUE; 813ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 814ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 815ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//---------------------------------------------------------------------- 816ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// PUBLIC API 817ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//---------------------------------------------------------------------- 818ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 819ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 820ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Constructor. 821ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 822ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruTransliteratorParser::TransliteratorParser(UErrorCode &statusReturn) : 823ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QuerudataVector(statusReturn), 824ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruidBlockVector(statusReturn), 825ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruvariablesVector(statusReturn), 826ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QuerusegmentObjects(statusReturn) 827ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{ 828ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru idBlockVector.setDeleter(uhash_deleteUnicodeString); 829ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru curData = NULL; 830ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru compoundFilter = NULL; 831ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru parseData = NULL; 832ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru variableNames.setValueDeleter(uhash_deleteUnicodeString); 833ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 834ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 835ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 836ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Destructor. 837ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 838ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruTransliteratorParser::~TransliteratorParser() { 839ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru while (!dataVector.isEmpty()) 840ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru delete (TransliterationRuleData*)(dataVector.orphanElementAt(0)); 841ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru delete compoundFilter; 842ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru delete parseData; 843ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru while (!variablesVector.isEmpty()) 844ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru delete (UnicodeFunctor*)variablesVector.orphanElementAt(0); 845ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 846ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 847ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid 848ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruTransliteratorParser::parse(const UnicodeString& rules, 849ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UTransDirection transDirection, 850ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UParseError& pe, 851ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UErrorCode& ec) { 852ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (U_SUCCESS(ec)) { 853ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru parseRules(rules, transDirection, ec); 854ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pe = parseError; 855ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 856ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 857ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 858ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 859ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Return the compound filter parsed by parse(). Caller owns result. 860ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 861ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUnicodeSet* TransliteratorParser::orphanCompoundFilter() { 862ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UnicodeSet* f = compoundFilter; 863ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru compoundFilter = NULL; 864ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return f; 865ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 866ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 867ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//---------------------------------------------------------------------- 868ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// Private implementation 869ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//---------------------------------------------------------------------- 870ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 871ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 872ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Parse the given string as a sequence of rules, separated by newline 873ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * characters ('\n'), and cause this object to implement those rules. Any 874ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * previous rules are discarded. Typically this method is called exactly 875ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * once, during construction. 876ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * @exception IllegalArgumentException if there is a syntax error in the 877ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * rules 878ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 879ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid TransliteratorParser::parseRules(const UnicodeString& rule, 880ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UTransDirection theDirection, 881ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UErrorCode& status) 882ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{ 883ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Clear error struct 884ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru uprv_memset(&parseError, 0, sizeof(parseError)); 885ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru parseError.line = parseError.offset = -1; 886ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 887ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UBool parsingIDs = TRUE; 888ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t ruleCount = 0; 889ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 890ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru while (!dataVector.isEmpty()) { 891ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru delete (TransliterationRuleData*)(dataVector.orphanElementAt(0)); 892ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 893ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (U_FAILURE(status)) { 894ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return; 895ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 896ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 897ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru idBlockVector.removeAllElements(); 898ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru curData = NULL; 899ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru direction = theDirection; 900ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ruleCount = 0; 901ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 902ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru delete compoundFilter; 903ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru compoundFilter = NULL; 904ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 905ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru while (!variablesVector.isEmpty()) { 906ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru delete (UnicodeFunctor*)variablesVector.orphanElementAt(0); 907ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 908ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru variableNames.removeAll(); 909ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru parseData = new ParseData(0, &variablesVector, &variableNames); 910ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (parseData == NULL) { 911ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru status = U_MEMORY_ALLOCATION_ERROR; 912ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return; 913ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 914ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 915ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru dotStandIn = (UChar) -1; 916ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 91785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho UnicodeString *tempstr = NULL; // used for memory allocation error checking 918ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UnicodeString str; // scratch 919ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UnicodeString idBlockResult; 920ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t pos = 0; 921ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t limit = rule.length(); 922ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 923ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // The compound filter offset is an index into idBlockResult. 924ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // If it is 0, then the compound filter occurred at the start, 925ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // and it is the offset to the _start_ of the compound filter 926ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // pattern. Otherwise it is the offset to the _limit_ of the 927ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // compound filter pattern within idBlockResult. 928ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru compoundFilter = NULL; 929ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t compoundFilterOffset = -1; 930ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 931ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru while (pos < limit && U_SUCCESS(status)) { 932ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UChar c = rule.charAt(pos++); 933b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho if (PatternProps::isWhiteSpace(c)) { 934ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Ignore leading whitespace. 935ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru continue; 936ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 937ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Skip lines starting with the comment character 938ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (c == RULE_COMMENT_CHAR) { 939ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pos = rule.indexOf((UChar)0x000A /*\n*/, pos) + 1; 940ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (pos == 0) { 941ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; // No "\n" found; rest of rule is a commnet 942ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 943ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru continue; // Either fall out or restart with next line 944ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 945ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 946ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // skip empty rules 947ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (c == END_OF_RULE) 948ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru continue; 949ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 950ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // keep track of how many rules we've seen 951ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ++ruleCount; 952ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 953ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // We've found the start of a rule or ID. c is its first 954ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // character, and pos points past c. 955ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru --pos; 956ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Look for an ID token. Must have at least ID_TOKEN_LEN + 1 957ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // chars left. 958ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if ((pos + ID_TOKEN_LEN + 1) <= limit && 959ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru rule.compare(pos, ID_TOKEN_LEN, ID_TOKEN) == 0) { 960ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pos += ID_TOKEN_LEN; 961ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru c = rule.charAt(pos); 962b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho while (PatternProps::isWhiteSpace(c) && pos < limit) { 963ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ++pos; 964ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru c = rule.charAt(pos); 965ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 966ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 967ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t p = pos; 968ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 969ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (!parsingIDs) { 970ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (curData != NULL) { 971ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (direction == UTRANS_FORWARD) 972ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru dataVector.addElement(curData, status); 973ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru else 974ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru dataVector.insertElementAt(curData, 0, status); 975ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru curData = NULL; 976ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 977ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru parsingIDs = TRUE; 978ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 979ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 980ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru TransliteratorIDParser::SingleID* id = 981ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru TransliteratorIDParser::parseSingleID(rule, p, direction, status); 982ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (p != pos && ICU_Utility::parseChar(rule, p, END_OF_RULE)) { 983ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Successful ::ID parse. 984ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 985ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (direction == UTRANS_FORWARD) { 986ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru idBlockResult.append(id->canonID).append(END_OF_RULE); 987ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 988ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru idBlockResult.insert(0, END_OF_RULE); 989ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru idBlockResult.insert(0, id->canonID); 990ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 991ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 992ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 993ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Couldn't parse an ID. Try to parse a global filter 994ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t withParens = -1; 995ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UnicodeSet* f = TransliteratorIDParser::parseGlobalFilter(rule, p, direction, withParens, NULL); 996ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (f != NULL) { 997ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (ICU_Utility::parseChar(rule, p, END_OF_RULE) 998ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru && (direction == UTRANS_FORWARD) == (withParens == 0)) 999ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru { 1000ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (compoundFilter != NULL) { 1001ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Multiple compound filters 1002ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru syntaxError(U_MULTIPLE_COMPOUND_FILTERS, rule, pos, status); 1003ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru delete f; 1004ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 1005ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru compoundFilter = f; 1006ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru compoundFilterOffset = ruleCount; 1007ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1008ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 1009ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru delete f; 1010ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1011ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 1012ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Invalid ::id 1013ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Can be parsed as neither an ID nor a global filter 1014ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru syntaxError(U_INVALID_ID, rule, pos, status); 1015ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1016ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1017ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru delete id; 1018ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pos = p; 1019ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 1020ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (parsingIDs) { 102185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho tempstr = new UnicodeString(idBlockResult); 102285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho // NULL pointer check 102385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho if (tempstr == NULL) { 102485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho status = U_MEMORY_ALLOCATION_ERROR; 102585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho return; 102685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho } 1027ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (direction == UTRANS_FORWARD) 102885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho idBlockVector.addElement(tempstr, status); 1029ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru else 103085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho idBlockVector.insertElementAt(tempstr, 0, status); 1031ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru idBlockResult.remove(); 1032ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru parsingIDs = FALSE; 1033ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru curData = new TransliterationRuleData(status); 103485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho // NULL pointer check 103585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho if (curData == NULL) { 103685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho status = U_MEMORY_ALLOCATION_ERROR; 103785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho return; 103885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho } 1039ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru parseData->data = curData; 1040ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1041ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // By default, rules use part of the private use area 1042ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // E000..F8FF for variables and other stand-ins. Currently 1043ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // the range F000..F8FF is typically sufficient. The 'use 1044ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // variable range' pragma allows rule sets to modify this. 1045ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru setVariableRange(0xF000, 0xF8FF, status); 1046ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1047ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1048ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (resemblesPragma(rule, pos, limit)) { 1049ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t ppp = parsePragma(rule, pos, limit, status); 1050ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (ppp < 0) { 1051ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru syntaxError(U_MALFORMED_PRAGMA, rule, pos, status); 1052ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1053ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pos = ppp; 1054ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Parse a rule 1055ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 1056ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pos = parseRule(rule, pos, limit, status); 1057ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1058ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1059ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1060ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1061ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (parsingIDs && idBlockResult.length() > 0) { 106285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho tempstr = new UnicodeString(idBlockResult); 106385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho // NULL pointer check 106485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho if (tempstr == NULL) { 106585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho status = U_MEMORY_ALLOCATION_ERROR; 106685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho return; 106785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho } 1068ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (direction == UTRANS_FORWARD) 106985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho idBlockVector.addElement(tempstr, status); 1070ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru else 107185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho idBlockVector.insertElementAt(tempstr, 0, status); 1072ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1073ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru else if (!parsingIDs && curData != NULL) { 1074ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (direction == UTRANS_FORWARD) 1075ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru dataVector.addElement(curData, status); 1076ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru else 1077ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru dataVector.insertElementAt(curData, 0, status); 1078ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1079ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1080ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (U_SUCCESS(status)) { 1081ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Convert the set vector to an array 1082ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t i, dataVectorSize = dataVector.size(); 1083ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru for (i = 0; i < dataVectorSize; i++) { 1084ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru TransliterationRuleData* data = (TransliterationRuleData*)dataVector.elementAt(i); 1085ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru data->variablesLength = variablesVector.size(); 1086ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (data->variablesLength == 0) { 1087ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru data->variables = 0; 1088ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 1089ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru data->variables = (UnicodeFunctor**)uprv_malloc(data->variablesLength * sizeof(UnicodeFunctor*)); 109085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho // NULL pointer check 109185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho if (data->variables == NULL) { 109285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho status = U_MEMORY_ALLOCATION_ERROR; 109385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho return; 109485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho } 1095ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru data->variablesAreOwned = (i == 0); 1096ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1097ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1098ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru for (int32_t j = 0; j < data->variablesLength; j++) { 1099ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru data->variables[j] = 1100ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ((UnicodeSet*)variablesVector.elementAt(j)); 1101ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1102ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1103ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru data->variableNames.removeAll(); 1104ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t pos = -1; 1105ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru const UHashElement* he = variableNames.nextElement(pos); 1106ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru while (he != NULL) { 110785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho UnicodeString* tempus = (UnicodeString*)(((UnicodeString*)(he->value.pointer))->clone()); 110885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho if (tempus == NULL) { 110985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho status = U_MEMORY_ALLOCATION_ERROR; 111085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho return; 111185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho } 1112ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru data->variableNames.put(*((UnicodeString*)(he->key.pointer)), 111385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho tempus, status); 1114ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru he = variableNames.nextElement(pos); 1115ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1116ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1117ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru variablesVector.removeAllElements(); // keeps them from getting deleted when we succeed 1118ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1119ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Index the rules 1120ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (compoundFilter != NULL) { 1121ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if ((direction == UTRANS_FORWARD && compoundFilterOffset != 1) || 1122ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru (direction == UTRANS_REVERSE && compoundFilterOffset != ruleCount)) { 1123ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru status = U_MISPLACED_COMPOUND_FILTER; 1124ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1125ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1126ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1127ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru for (i = 0; i < dataVectorSize; i++) { 1128ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru TransliterationRuleData* data = (TransliterationRuleData*)dataVector.elementAt(i); 1129ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru data->ruleSet.freeze(parseError, status); 1130ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1131ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (idBlockVector.size() == 1 && ((UnicodeString*)idBlockVector.elementAt(0))->isEmpty()) { 1132ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru idBlockVector.removeElementAt(0); 1133ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1134ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1135ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 1136ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1137ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 1138ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Set the variable range to [start, end] (inclusive). 1139ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 1140ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid TransliteratorParser::setVariableRange(int32_t start, int32_t end, UErrorCode& status) { 1141ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (start > end || start < 0 || end > 0xFFFF) { 1142ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru status = U_MALFORMED_PRAGMA; 1143ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return; 1144ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1145ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1146ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru curData->variablesBase = (UChar) start; 1147ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (dataVector.size() == 0) { 1148ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru variableNext = (UChar) start; 1149ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru variableLimit = (UChar) (end + 1); 1150ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1151ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 1152ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1153ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 1154ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Assert that the given character is NOT within the variable range. 1155ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * If it is, return FALSE. This is neccesary to ensure that the 1156ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * variable range does not overlap characters used in a rule. 1157ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 1158ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUBool TransliteratorParser::checkVariableRange(UChar32 ch) const { 1159ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return !(ch >= curData->variablesBase && ch < variableLimit); 1160ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 1161ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1162ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 1163ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Set the maximum backup to 'backup', in response to a pragma 1164ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * statement. 1165ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 1166ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid TransliteratorParser::pragmaMaximumBackup(int32_t /*backup*/) { 1167ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru //TODO Finish 1168ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 1169ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1170ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 1171ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Begin normalizing all rules using the given mode, in response 1172ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * to a pragma statement. 1173ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 1174ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid TransliteratorParser::pragmaNormalizeRules(UNormalizationMode /*mode*/) { 1175ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru //TODO Finish 1176ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 1177ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1178ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic const UChar PRAGMA_USE[] = {0x75,0x73,0x65,0x20,0}; // "use " 1179ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1180ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic 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 # #~;" 1181ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1182ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic 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 #~;" 1183ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1184ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic const UChar PRAGMA_NFD_RULES[] = {0x7E,0x6E,0x66,0x64,0x20,0x72,0x75,0x6C,0x65,0x73,0x7E,0x3B,0}; // "~nfd rules~;" 1185ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1186ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic const UChar PRAGMA_NFC_RULES[] = {0x7E,0x6E,0x66,0x63,0x20,0x72,0x75,0x6C,0x65,0x73,0x7E,0x3B,0}; // "~nfc rules~;" 1187ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1188ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 1189ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Return true if the given rule looks like a pragma. 1190ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * @param pos offset to the first non-whitespace character 1191ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * of the rule. 1192ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * @param limit pointer past the last character of the rule. 1193ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 1194ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUBool TransliteratorParser::resemblesPragma(const UnicodeString& rule, int32_t pos, int32_t limit) { 1195ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Must start with /use\s/i 1196ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return ICU_Utility::parsePattern(rule, pos, limit, PRAGMA_USE, NULL) >= 0; 1197ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 1198ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1199ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 1200ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Parse a pragma. This method assumes resemblesPragma() has 1201ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * already returned true. 1202ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * @param pos offset to the first non-whitespace character 1203ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * of the rule. 1204ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * @param limit pointer past the last character of the rule. 1205ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * @return the position index after the final ';' of the pragma, 1206ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * or -1 on failure. 1207ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 1208ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruint32_t TransliteratorParser::parsePragma(const UnicodeString& rule, int32_t pos, int32_t limit, UErrorCode& status) { 1209ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t array[2]; 1210ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1211ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // resemblesPragma() has already returned true, so we 1212ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // know that pos points to /use\s/i; we can skip 4 characters 1213ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // immediately 1214ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pos += 4; 1215ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1216ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Here are the pragmas we recognize: 1217ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // use variable range 0xE000 0xEFFF; 1218ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // use maximum backup 16; 1219ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // use nfd rules; 1220ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // use nfc rules; 1221ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int p = ICU_Utility::parsePattern(rule, pos, limit, PRAGMA_VARIABLE_RANGE, array); 1222ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (p >= 0) { 1223ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru setVariableRange(array[0], array[1], status); 1224ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return p; 1225ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1226ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1227ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru p = ICU_Utility::parsePattern(rule, pos, limit, PRAGMA_MAXIMUM_BACKUP, array); 1228ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (p >= 0) { 1229ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pragmaMaximumBackup(array[0]); 1230ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return p; 1231ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1232ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1233ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru p = ICU_Utility::parsePattern(rule, pos, limit, PRAGMA_NFD_RULES, NULL); 1234ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (p >= 0) { 1235ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pragmaNormalizeRules(UNORM_NFD); 1236ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return p; 1237ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1238ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1239ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru p = ICU_Utility::parsePattern(rule, pos, limit, PRAGMA_NFC_RULES, NULL); 1240ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (p >= 0) { 1241ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pragmaNormalizeRules(UNORM_NFC); 1242ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return p; 1243ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1244ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1245ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Syntax error: unable to parse pragma 1246ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return -1; 1247ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 1248ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1249ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 1250ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * MAIN PARSER. Parse the next rule in the given rule string, starting 1251ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * at pos. Return the index after the last character parsed. Do not 1252ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * parse characters at or after limit. 1253ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 1254ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Important: The character at pos must be a non-whitespace character 1255ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * that is not the comment character. 1256ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * 1257ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * This method handles quoting, escaping, and whitespace removal. It 1258ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * parses the end-of-rule character. It recognizes context and cursor 1259ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * indicators. Once it does a lexical breakdown of the rule at pos, it 1260ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * creates a rule object and adds it to our rule list. 1261ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 1262ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruint32_t TransliteratorParser::parseRule(const UnicodeString& rule, int32_t pos, int32_t limit, UErrorCode& status) { 1263ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Locate the left side, operator, and right side 1264ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t start = pos; 1265ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UChar op = 0; 1266ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t i; 1267ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1268ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Set up segments data 1269ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru segmentStandins.truncate(0); 1270ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru segmentObjects.removeAllElements(); 1271ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1272ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Use pointers to automatics to make swapping possible. 1273ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru RuleHalf _left(*this), _right(*this); 1274ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru RuleHalf* left = &_left; 1275ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru RuleHalf* right = &_right; 1276ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1277ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru undefinedVariableName.remove(); 1278ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pos = left->parse(rule, pos, limit, status); 1279ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (U_FAILURE(status)) { 1280ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return start; 1281ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1282ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1283ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (pos == limit || u_strchr(gOPERATORS, (op = rule.charAt(--pos))) == NULL) { 1284ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return syntaxError(U_MISSING_OPERATOR, rule, start, status); 1285ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1286ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ++pos; 1287ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1288ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Found an operator char. Check for forward-reverse operator. 1289ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (op == REVERSE_RULE_OP && 1290ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru (pos < limit && rule.charAt(pos) == FORWARD_RULE_OP)) { 1291ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ++pos; 1292ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru op = FWDREV_RULE_OP; 1293ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1294ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1295ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Translate alternate op characters. 1296ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru switch (op) { 1297ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case ALT_FORWARD_RULE_OP: 1298ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru op = FORWARD_RULE_OP; 1299ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 1300ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case ALT_REVERSE_RULE_OP: 1301ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru op = REVERSE_RULE_OP; 1302ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 1303ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru case ALT_FWDREV_RULE_OP: 1304ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru op = FWDREV_RULE_OP; 1305ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 1306ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1307ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1308ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru pos = right->parse(rule, pos, limit, status); 1309ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (U_FAILURE(status)) { 1310ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return start; 1311ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1312ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1313ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (pos < limit) { 1314ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (rule.charAt(--pos) == END_OF_RULE) { 1315ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ++pos; 1316ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 1317ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // RuleHalf parser must have terminated at an operator 1318ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return syntaxError(U_UNQUOTED_SPECIAL, rule, start, status); 1319ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1320ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1321ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1322ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (op == VARIABLE_DEF_OP) { 1323ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // LHS is the name. RHS is a single character, either a literal 1324ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // or a set (already parsed). If RHS is longer than one 1325ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // character, it is either a multi-character string, or multiple 1326ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // sets, or a mixture of chars and sets -- syntax error. 1327ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1328ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // We expect to see a single undefined variable (the one being 1329ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // defined). 1330ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (undefinedVariableName.length() == 0) { 1331ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // "Missing '$' or duplicate definition" 1332ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return syntaxError(U_BAD_VARIABLE_DEFINITION, rule, start, status); 1333ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1334ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (left->text.length() != 1 || left->text.charAt(0) != variableLimit) { 1335ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // "Malformed LHS" 1336ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return syntaxError(U_MALFORMED_VARIABLE_DEFINITION, rule, start, status); 1337ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1338ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (left->anchorStart || left->anchorEnd || 1339ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru right->anchorStart || right->anchorEnd) { 1340ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return syntaxError(U_MALFORMED_VARIABLE_DEFINITION, rule, start, status); 1341ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1342ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // We allow anything on the right, including an empty string. 1343ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UnicodeString* value = new UnicodeString(right->text); 134485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho // NULL pointer check 134585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho if (value == NULL) { 134685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho return syntaxError(U_MEMORY_ALLOCATION_ERROR, rule, start, status); 134785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho } 1348ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru variableNames.put(undefinedVariableName, value, status); 1349ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ++variableLimit; 1350ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return pos; 1351ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1352ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1353ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // If this is not a variable definition rule, we shouldn't have 1354ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // any undefined variable names. 1355ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (undefinedVariableName.length() != 0) { 1356ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return syntaxError(// "Undefined variable $" + undefinedVariableName, 1357ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru U_UNDEFINED_VARIABLE, 1358ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru rule, start, status); 1359ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1360ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1361ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Verify segments 1362ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (segmentStandins.length() > segmentObjects.size()) { 1363ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru syntaxError(U_UNDEFINED_SEGMENT_REFERENCE, rule, start, status); 1364ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1365ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru for (i=0; i<segmentStandins.length(); ++i) { 1366ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (segmentStandins.charAt(i) == 0) { 1367ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru syntaxError(U_INTERNAL_TRANSLITERATOR_ERROR, rule, start, status); // will never happen 1368ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1369ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1370ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru for (i=0; i<segmentObjects.size(); ++i) { 1371ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (segmentObjects.elementAt(i) == NULL) { 1372ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru syntaxError(U_INTERNAL_TRANSLITERATOR_ERROR, rule, start, status); // will never happen 1373ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1374ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1375ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1376ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // If the direction we want doesn't match the rule 1377ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // direction, do nothing. 1378ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (op != FWDREV_RULE_OP && 1379ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ((direction == UTRANS_FORWARD) != (op == FORWARD_RULE_OP))) { 1380ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return pos; 1381ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1382ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1383ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Transform the rule into a forward rule by swapping the 1384ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // sides if necessary. 1385ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (direction == UTRANS_REVERSE) { 1386ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru left = &_right; 1387ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru right = &_left; 1388ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1389ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1390ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Remove non-applicable elements in forward-reverse 1391ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // rules. Bidirectional rules ignore elements that do not 1392ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // apply. 1393ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (op == FWDREV_RULE_OP) { 1394ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru right->removeContext(); 1395ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru left->cursor = -1; 1396ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru left->cursorOffset = 0; 1397ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1398ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1399ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Normalize context 1400ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (left->ante < 0) { 1401ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru left->ante = 0; 1402ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1403ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (left->post < 0) { 1404ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru left->post = left->text.length(); 1405ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1406ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1407ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Context is only allowed on the input side. Cursors are only 1408ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // allowed on the output side. Segment delimiters can only appear 1409ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // on the left, and references on the right. Cursor offset 1410ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // cannot appear without an explicit cursor. Cursor offset 1411ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // cannot place the cursor outside the limits of the context. 1412ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Anchors are only allowed on the input side. 1413ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (right->ante >= 0 || right->post >= 0 || left->cursor >= 0 || 1414ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru (right->cursorOffset != 0 && right->cursor < 0) || 1415ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // - The following two checks were used to ensure that the 1416ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // - the cursor offset stayed within the ante- or postcontext. 1417ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // - However, with the addition of quantifiers, we have to 1418ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // - allow arbitrary cursor offsets and do runtime checking. 1419ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru //(right->cursorOffset > (left->text.length() - left->post)) || 1420ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru //(-right->cursorOffset > left->ante) || 1421ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru right->anchorStart || right->anchorEnd || 1422ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru !left->isValidInput(*this) || !right->isValidOutput(*this) || 1423ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru left->ante > left->post) { 1424ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1425ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return syntaxError(U_MALFORMED_RULE, rule, start, status); 1426ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1427ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1428ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Flatten segment objects vector to an array 1429ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UnicodeFunctor** segmentsArray = NULL; 1430ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (segmentObjects.size() > 0) { 1431ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru segmentsArray = (UnicodeFunctor **)uprv_malloc(segmentObjects.size() * sizeof(UnicodeFunctor *)); 143285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho // Null pointer check 143385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho if (segmentsArray == NULL) { 143485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho return syntaxError(U_MEMORY_ALLOCATION_ERROR, rule, start, status); 143585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho } 1436ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru segmentObjects.toArray((void**) segmentsArray); 1437ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 143885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho TransliterationRule* temptr = new TransliterationRule( 143985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho left->text, left->ante, left->post, 144085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho right->text, right->cursor, right->cursorOffset, 144185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho segmentsArray, 144285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho segmentObjects.size(), 144385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho left->anchorStart, left->anchorEnd, 144485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho curData, 144585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho status); 144685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho //Null pointer check 144785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho if (temptr == NULL) { 144885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho uprv_free(segmentsArray); 144985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho return syntaxError(U_MEMORY_ALLOCATION_ERROR, rule, start, status); 145085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho } 1451ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 145285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho curData->ruleSet.addRule(temptr, status); 1453ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1454ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return pos; 1455ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 1456ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1457ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 1458ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Called by main parser upon syntax error. Search the rule string 1459ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * for the probable end of the rule. Of course, if the error is that 1460ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * the end of rule marker is missing, then the rule end will not be found. 1461ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * In any case the rule start will be correctly reported. 1462ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * @param msg error description 1463ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * @param rule pattern string 1464ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * @param start position of first character of current rule 1465ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 1466ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruint32_t TransliteratorParser::syntaxError(UErrorCode parseErrorCode, 1467ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru const UnicodeString& rule, 1468ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t pos, 1469ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UErrorCode& status) 1470ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru{ 1471ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru parseError.offset = pos; 1472ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru parseError.line = 0 ; /* we are not using line numbers */ 1473ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1474ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // for pre-context 1475ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru const int32_t LEN = U_PARSE_CONTEXT_LEN - 1; 1476ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t start = uprv_max(pos - LEN, 0); 1477ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t stop = pos; 1478ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1479ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru rule.extract(start,stop-start,parseError.preContext); 1480ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru //null terminate the buffer 1481ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru parseError.preContext[stop-start] = 0; 1482ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1483ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru //for post-context 1484ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru start = pos; 1485ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru stop = uprv_min(pos + LEN, rule.length()); 1486ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1487ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru rule.extract(start,stop-start,parseError.postContext); 1488ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru //null terminate the buffer 1489ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru parseError.postContext[stop-start]= 0; 1490ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1491ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru status = (UErrorCode)parseErrorCode; 1492ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return pos; 1493ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1494ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 1495ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1496ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 1497ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Parse a UnicodeSet out, store it, and return the stand-in character 1498ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * used to represent it. 1499ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 1500ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUChar TransliteratorParser::parseSet(const UnicodeString& rule, 1501ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru ParsePosition& pos, 1502ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UErrorCode& status) { 1503ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UnicodeSet* set = new UnicodeSet(rule, pos, USET_IGNORE_SPACE, parseData, status); 150485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho // Null pointer check 150585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho if (set == NULL) { 150685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho status = U_MEMORY_ALLOCATION_ERROR; 150785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho return (UChar)0x0000; // Return empty character with error. 150885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho } 1509ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru set->compact(); 1510ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return generateStandInFor(set, status); 1511ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 1512ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1513ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 1514ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Generate and return a stand-in for a new UnicodeFunctor. Store 1515ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * the matcher (adopt it). 1516ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 1517ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUChar TransliteratorParser::generateStandInFor(UnicodeFunctor* adopted, UErrorCode& status) { 1518ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // assert(obj != null); 1519ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1520ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Look up previous stand-in, if any. This is a short list 1521ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // (typical n is 0, 1, or 2); linear search is optimal. 1522ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru for (int32_t i=0; i<variablesVector.size(); ++i) { 1523ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (variablesVector.elementAt(i) == adopted) { // [sic] pointer comparison 1524ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return (UChar) (curData->variablesBase + i); 1525ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1526ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1527ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1528ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (variableNext >= variableLimit) { 1529ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru delete adopted; 1530ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru status = U_VARIABLE_RANGE_EXHAUSTED; 1531ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return 0; 1532ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1533ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru variablesVector.addElement(adopted, status); 1534ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return variableNext++; 1535ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 1536ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1537ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 1538ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Return the standin for segment seg (1-based). 1539ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 1540ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUChar TransliteratorParser::getSegmentStandin(int32_t seg, UErrorCode& status) { 1541ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Special character used to indicate an empty spot 1542ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UChar empty = curData->variablesBase - 1; 1543ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru while (segmentStandins.length() < seg) { 1544ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru segmentStandins.append(empty); 1545ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1546ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UChar c = segmentStandins.charAt(seg-1); 1547ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (c == empty) { 1548ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (variableNext >= variableLimit) { 1549ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru status = U_VARIABLE_RANGE_EXHAUSTED; 1550ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return 0; 1551ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1552ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru c = variableNext++; 1553ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Set a placeholder in the master variables vector that will be 1554ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // filled in later by setSegmentObject(). We know that we will get 1555ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // called first because setSegmentObject() will call us. 1556ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru variablesVector.addElement((void*) NULL, status); 1557ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru segmentStandins.setCharAt(seg-1, c); 1558ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1559ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return c; 1560ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 1561ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1562ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 1563ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Set the object for segment seg (1-based). 1564ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 1565ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid TransliteratorParser::setSegmentObject(int32_t seg, StringMatcher* adopted, UErrorCode& status) { 1566ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Since we call parseSection() recursively, nested 1567ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // segments will result in segment i+1 getting parsed 1568ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // and stored before segment i; be careful with the 1569ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // vector handling here. 1570ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (segmentObjects.size() < seg) { 157185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho segmentObjects.setSize(seg, status); 1572ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1573ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t index = getSegmentStandin(seg, status) - curData->variablesBase; 1574ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (segmentObjects.elementAt(seg-1) != NULL || 1575ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru variablesVector.elementAt(index) != NULL) { 1576ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // should never happen 1577ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru status = U_INTERNAL_TRANSLITERATOR_ERROR; 1578ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return; 1579ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1580ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru segmentObjects.setElementAt(adopted, seg-1); 1581ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru variablesVector.setElementAt(adopted, index); 1582ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 1583ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1584ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 1585ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Return the stand-in for the dot set. It is allocated the first 1586ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * time and reused thereafter. 1587ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 1588ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruUChar TransliteratorParser::getDotStandIn(UErrorCode& status) { 1589ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (dotStandIn == (UChar) -1) { 159085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho UnicodeSet* tempus = new UnicodeSet(DOT_SET, status); 159185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho // Null pointer check. 159285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho if (tempus == NULL) { 159385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho status = U_MEMORY_ALLOCATION_ERROR; 159485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho return (UChar)0x0000; 159585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho } 159685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho dotStandIn = generateStandInFor(tempus, status); 1597ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1598ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return dotStandIn; 1599ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 1600ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1601ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 1602ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Append the value of the given variable name to the given 1603ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * UnicodeString. 1604ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 1605ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid TransliteratorParser::appendVariableDef(const UnicodeString& name, 1606ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UnicodeString& buf, 1607ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UErrorCode& status) { 1608ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru const UnicodeString* s = (const UnicodeString*) variableNames.get(name); 1609ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (s == NULL) { 1610ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // We allow one undefined variable so that variable definition 1611ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // statements work. For the first undefined variable we return 1612ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // the special placeholder variableLimit-1, and save the variable 1613ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // name. 1614ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (undefinedVariableName.length() == 0) { 1615ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru undefinedVariableName = name; 1616ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (variableNext >= variableLimit) { 1617ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // throw new RuntimeException("Private use variables exhausted"); 1618ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru status = U_ILLEGAL_ARGUMENT_ERROR; 1619ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return; 1620ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1621ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru buf.append((UChar) --variableLimit); 1622ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 1623ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru //throw new IllegalArgumentException("Undefined variable $" 1624ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // + name); 1625ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru status = U_ILLEGAL_ARGUMENT_ERROR; 1626ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return; 1627ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1628ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } else { 1629ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru buf.append(*s); 1630ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1631ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 1632ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1633ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/** 1634ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * Glue method to get around access restrictions in C++. 1635ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 1636ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru/*Transliterator* TransliteratorParser::createBasicInstance(const UnicodeString& id, const UnicodeString* canonID) { 1637ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return Transliterator::createBasicInstance(id, canonID); 1638ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}*/ 1639ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1640ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_NAMESPACE_END 1641ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1642ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste QueruU_CAPI int32_t 1643ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruutrans_stripRules(const UChar *source, int32_t sourceLen, UChar *target, UErrorCode *status) { 1644ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru U_NAMESPACE_USE 1645ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1646ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru //const UChar *sourceStart = source; 1647ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru const UChar *targetStart = target; 1648ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru const UChar *sourceLimit = source+sourceLen; 1649ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UChar *targetLimit = target+sourceLen; 1650ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UChar32 c = 0; 1651ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UBool quoted = FALSE; 1652ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t index; 1653ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1654ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru uprv_memset(target, 0, sourceLen*U_SIZEOF_UCHAR); 1655ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1656ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* read the rules into the buffer */ 1657ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru while (source < sourceLimit) 1658ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru { 1659ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru index=0; 1660ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru U16_NEXT_UNSAFE(source, index, c); 1661ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru source+=index; 1662ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if(c == QUOTE) { 1663ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru quoted = (UBool)!quoted; 1664ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1665ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru else if (!quoted) { 1666ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (c == RULE_COMMENT_CHAR) { 1667ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* skip comments and all preceding spaces */ 1668ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru while (targetStart < target && *(target - 1) == 0x0020) { 1669ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru target--; 1670ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1671ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru do { 1672ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru c = *(source++); 1673ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1674ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru while (c != CR && c != LF); 1675ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1676ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru else if (c == ESCAPE) { 1677ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UChar32 c2 = *source; 1678ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (c2 == CR || c2 == LF) { 1679ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* A backslash at the end of a line. */ 1680ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* Since we're stripping lines, ignore the backslash. */ 1681ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru source++; 1682ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru continue; 1683ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1684ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (c2 == 0x0075 && source+5 < sourceLimit) { /* \u seen. \U isn't unescaped. */ 1685ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru int32_t escapeOffset = 0; 1686ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru UnicodeString escapedStr(source, 5); 1687ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru c2 = escapedStr.unescapeAt(escapeOffset); 1688ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1689ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (c2 == (UChar32)0xFFFFFFFF || escapeOffset == 0) 1690ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru { 1691ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *status = U_PARSE_ERROR; 1692ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return 0; 1693ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1694b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho if (!PatternProps::isWhiteSpace(c2) && !u_iscntrl(c2) && !u_ispunct(c2)) { 1695ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* It was escaped for a reason. Write what it was suppose to be. */ 1696ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru source+=5; 1697ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru c = c2; 1698ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1699ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1700ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru else if (c2 == QUOTE) { 1701ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* \' seen. Make sure we don't do anything when we see it again. */ 1702ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru quoted = (UBool)!quoted; 1703ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1704ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1705ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1706ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (c == CR || c == LF) 1707ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru { 1708ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* ignore spaces carriage returns, and all leading spaces on the next line. 1709ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru * and line feed unless in the form \uXXXX 1710ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru */ 1711ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru quoted = FALSE; 1712ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru while (source < sourceLimit) { 1713ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru c = *(source); 1714ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (c != CR && c != LF && c != 0x0020) { 1715ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru break; 1716ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1717ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru source++; 1718ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1719ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru continue; 1720ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1721ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1722ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru /* Append UChar * after dissembling if c > 0xffff*/ 1723ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru index=0; 1724ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru U16_APPEND_UNSAFE(target, index, c); 1725ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru target+=index; 1726ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1727ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (target < targetLimit) { 1728ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru *target = 0; 1729ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } 1730ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return (int32_t)(target-targetStart); 1731ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 1732ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 1733ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif /* #if !UCONFIG_NO_TRANSLITERATION */ 1734