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