2 * Copyright (C) {1999-2003}, International Business Machines Corporation and others. All Rights Reserved.
3 **********************************************************************
4 * Date Name Description
5 * 11/17/99 aliu Creation.
6 **********************************************************************
11 #include "unicode/utypes.h"
13 #if !UCONFIG_NO_TRANSLITERATION
15 #include "unicode/uobject.h"
16 #include "unicode/parseerr.h"
17 #include "unicode/unorm.h"
22 class TransliterationRuleData
;
30 class TransliteratorParser
: public UMemory
{
35 * PUBLIC data member containing the parsed data object, or null if
36 * there were no rules.
38 TransliterationRuleData
* data
;
42 * The block of ::IDs, both at the top and at the bottom.
43 * Inserted into these may be additional rules at the
46 UnicodeString idBlock
;
50 * In a compound RBT, the index at which the RBT rules are
51 * inserted into the ID block. Index 0 means before any IDs
52 * in the block. Index idBlock.length() means after all IDs
53 * in the block. Index is a string index.
58 * PUBLIC data member containing the parsed compound filter, if any.
60 UnicodeSet
* compoundFilter
;
64 // The number of rules parsed. This tells us if there were
65 // any actual transliterator rules, or if there were just ::ID
69 UTransDirection direction
;
72 * We use a single error code during parsing. Rather than pass it
73 * through each API, we keep it here.
78 * Parse error information.
80 UParseError parseError
;
83 * Temporary symbol table used during parsing.
88 * Temporary vector of matcher variables. When parsing is complete, this
89 * is copied into the array data.variables. As with data.variables,
90 * element 0 corresponds to character data.variablesBase.
92 UVector
* variablesVector
;
95 * String of standins for segments. Used during the parsing of a single
96 * rule. segmentStandins.charAt(0) is the standin for "$1" and corresponds
97 * to StringMatcher object segmentObjects.elementAt(0), etc.
99 UnicodeString segmentStandins
;
102 * Vector of StringMatcher objects for segments. Used during the
103 * parsing of a single rule.
104 * segmentStandins.charAt(0) is the standin for "$1" and corresponds
105 * to StringMatcher object segmentObjects.elementAt(0), etc.
107 UVector
* segmentObjects
;
110 * The next available stand-in for variables. This starts at some point in
111 * the private use area (discovered dynamically) and increments up toward
112 * <code>variableLimit</code>. At any point during parsing, available
113 * variables are <code>variableNext..variableLimit-1</code>.
118 * The last available stand-in for variables. This is discovered
119 * dynamically. At any point during parsing, available variables are
120 * <code>variableNext..variableLimit-1</code>.
125 * When we encounter an undefined variable, we do not immediately signal
126 * an error, in case we are defining this variable, e.g., "$a = [a-z];".
127 * Instead, we save the name of the undefined variable, and substitute
128 * in the placeholder char variableLimit - 1, and decrement
131 UnicodeString undefinedVariableName
;
134 * The stand-in character for the 'dot' set, represented by '.' in
135 * patterns. This is allocated the first time it is needed, and
145 TransliteratorParser();
150 ~TransliteratorParser();
153 * Parse the given string as a sequence of rules, separated by newline
154 * characters ('\n'), and cause this object to implement those rules. Any
155 * previous rules are discarded. Typically this method is called exactly
156 * once after construction.
158 * Parse the given rules, in the given direction. After this call
159 * returns, query the public data members for results. The caller
160 * owns the 'data' and 'compoundFilter' data members after this
162 * @param rules rules, separated by ';'
163 * @param direction either FORWARD or REVERSE.
164 * @param pe Struct to recieve information on position
165 * of error if an error is encountered
166 * @param ec Output param set to success/failure code.
168 void parse(const UnicodeString
& rules
,
169 UTransDirection direction
,
174 * Return the compound filter parsed by parse(). Caller owns result.
175 * @return the compound filter parsed by parse().
177 UnicodeSet
* orphanCompoundFilter();
180 * Return the data object parsed by parse(). Caller owns result.
181 * @return the data object parsed by parse().
183 TransliterationRuleData
* orphanData();
188 * Return a representation of this transliterator as source rules.
189 * @param rules Output param to receive the rules.
190 * @param direction either FORWARD or REVERSE.
192 void parseRules(const UnicodeString
& rules
,
193 UTransDirection direction
);
196 * MAIN PARSER. Parse the next rule in the given rule string, starting
197 * at pos. Return the index after the last character parsed. Do not
198 * parse characters at or after limit.
200 * Important: The character at pos must be a non-whitespace character
201 * that is not the comment character.
203 * This method handles quoting, escaping, and whitespace removal. It
204 * parses the end-of-rule character. It recognizes context and cursor
205 * indicators. Once it does a lexical breakdown of the rule at pos, it
206 * creates a rule object and adds it to our rule list.
207 * @param rules Output param to receive the rules.
208 * @param pos the starting position.
209 * @param limit pointer past the last character of the rule.
210 * @return the index after the last character parsed.
212 int32_t parseRule(const UnicodeString
& rule
, int32_t pos
, int32_t limit
);
215 * Set the variable range to [start, end] (inclusive).
216 * @param start the start value of the range.
217 * @param end the end value of the range.
219 void setVariableRange(int32_t start
, int32_t end
);
222 * Assert that the given character is NOT within the variable range.
223 * If it is, return FALSE. This is neccesary to ensure that the
224 * variable range does not overlap characters used in a rule.
225 * @param ch the given character.
226 * @return True, if the given character is NOT within the variable range.
228 UBool
checkVariableRange(UChar32 ch
) const;
231 * Set the maximum backup to 'backup', in response to a pragma
233 * @param backup the new value to be set.
235 void pragmaMaximumBackup(int32_t backup
);
238 * Begin normalizing all rules using the given mode, in response
239 * to a pragma statement.
240 * @param mode the given mode.
242 void pragmaNormalizeRules(UNormalizationMode mode
);
245 * Return true if the given rule looks like a pragma.
246 * @param pos offset to the first non-whitespace character
248 * @param limit pointer past the last character of the rule.
249 * @return true if the given rule looks like a pragma.
251 static UBool
resemblesPragma(const UnicodeString
& rule
, int32_t pos
, int32_t limit
);
254 * Parse a pragma. This method assumes resemblesPragma() has
255 * already returned true.
256 * @param pos offset to the first non-whitespace character
258 * @param limit pointer past the last character of the rule.
259 * @return the position index after the final ';' of the pragma,
262 int32_t parsePragma(const UnicodeString
& rule
, int32_t pos
, int32_t limit
);
265 * Called by main parser upon syntax error. Search the rule string
266 * for the probable end of the rule. Of course, if the error is that
267 * the end of rule marker is missing, then the rule end will not be found.
268 * In any case the rule start will be correctly reported.
269 * @param parseErrorCode error code.
270 * @param msg error description.
271 * @param start position of first character of current rule.
272 * @return start position of first character of current rule.
274 int32_t syntaxError(UErrorCode parseErrorCode
, const UnicodeString
&, int32_t start
);
277 * Parse a UnicodeSet out, store it, and return the stand-in character
278 * used to represent it.
280 * @param rule the rule for UnicodeSet.
281 * @param pos the position in pattern at which to start parsing.
282 * @return the stand-in character used to represent it.
284 UChar
parseSet(const UnicodeString
& rule
,
288 * Generate and return a stand-in for a new UnicodeFunctor. Store
289 * the matcher (adopt it).
290 * @param adopted the UnicodeFunctor to be adopted.
291 * @return a stand-in for a new UnicodeFunctor.
293 UChar
generateStandInFor(UnicodeFunctor
* adopted
);
296 * Return the standin for segment seg (1-based).
297 * @param seg the given segment.
298 * @return the standIn character for the given segment.
300 UChar
getSegmentStandin(int32_t seg
);
303 * Set the object for segment seg (1-based).
304 * @param seg the given segment.
305 * @param adopted the StringMatcher to be adopted.
307 void setSegmentObject(int32_t seg
, StringMatcher
* adopted
);
310 * Return the stand-in for the dot set. It is allocated the first
311 * time and reused thereafter.
312 * @return the stand-in for the dot set.
314 UChar
getDotStandIn();
317 * Append the value of the given variable name to the given
319 * @param name the variable name to be appended.
320 * @param buf the given UnicodeString to append to.
322 void appendVariableDef(const UnicodeString
& name
,
326 * Glue method to get around access restrictions in C++.
328 static Transliterator
* createBasicInstance(const UnicodeString
& id
,
329 const UnicodeString
* canonID
);
331 friend class RuleHalf
;
333 // Disallowed methods; no impl.
337 TransliteratorParser(const TransliteratorParser
&);
340 * Assignment operator
342 TransliteratorParser
& operator=(const TransliteratorParser
&);
347 #endif /* #if !UCONFIG_NO_TRANSLITERATION */