]> granicus.if.org Git - icu/commitdiff
ICU-13832 Transliterator: move rule syntax docs from internal class to public (#150)
authorMarkus Scherer <markus.icu@gmail.com>
Thu, 20 Sep 2018 04:51:49 +0000 (21:51 -0700)
committerShane Carr <shane@unicode.org>
Thu, 27 Sep 2018 21:27:41 +0000 (14:27 -0700)
icu4c/source/i18n/rbt.h
icu4c/source/i18n/unicode/translit.h
icu4c/source/test/intltest/cpdtrtst.h
icu4j/main/classes/translit/src/com/ibm/icu/text/RuleBasedTransliterator.java
icu4j/main/classes/translit/src/com/ibm/icu/text/Transliterator.java

index b998c694c2303307d58d919b41abb9c3a3cbbd4b..671149f66ef7c16bad8992cf596243098ed321b4 100644 (file)
@@ -29,262 +29,10 @@ class TransliterationRuleData;
 
 /**
  * <code>RuleBasedTransliterator</code> is a transliterator
- * that reads a set of rules in order to determine how to perform
- * translations. Rule sets are stored in resource bundles indexed by
- * name. Rules within a rule set are separated by semicolons (';').
- * To include a literal semicolon, prefix it with a backslash ('\').
- * Whitespace, as defined by <code>Character.isWhitespace()</code>,
- * is ignored. If the first non-blank character on a line is '#',
- * the entire line is ignored as a comment. </p>
- * 
- * <p>Each set of rules consists of two groups, one forward, and one
- * reverse. This is a convention that is not enforced; rules for one
- * direction may be omitted, with the result that translations in
- * that direction will not modify the source text. In addition,
- * bidirectional forward-reverse rules may be specified for
- * symmetrical transformations.</p>
- * 
- * <p><b>Rule syntax</b> </p>
- * 
- * <p>Rule statements take one of the following forms: </p>
- * 
- * <dl>
- *     <dt><code>$alefmadda=\u0622;</code></dt>
- *     <dd><strong>Variable definition.</strong> The name on the
- *         left is assigned the text on the right. In this example,
- *         after this statement, instances of the left hand name,
- *         &quot;<code>$alefmadda</code>&quot;, will be replaced by
- *         the Unicode character U+0622. Variable names must begin
- *         with a letter and consist only of letters, digits, and
- *         underscores. Case is significant. Duplicate names cause
- *         an exception to be thrown, that is, variables cannot be
- *         redefined. The right hand side may contain well-formed
- *         text of any length, including no text at all (&quot;<code>$empty=;</code>&quot;).
- *         The right hand side may contain embedded <code>UnicodeSet</code>
- *         patterns, for example, &quot;<code>$softvowel=[eiyEIY]</code>&quot;.</dd>
- *     <dd>&nbsp;</dd>
- *     <dt><code>ai&gt;$alefmadda;</code></dt>
- *     <dd><strong>Forward translation rule.</strong> This rule
- *         states that the string on the left will be changed to the
- *         string on the right when performing forward
- *         transliteration.</dd>
- *     <dt>&nbsp;</dt>
- *     <dt><code>ai<$alefmadda;</code></dt>
- *     <dd><strong>Reverse translation rule.</strong> This rule
- *         states that the string on the right will be changed to
- *         the string on the left when performing reverse
- *         transliteration.</dd>
- * </dl>
- * 
- * <dl>
- *     <dt><code>ai<>$alefmadda;</code></dt>
- *     <dd><strong>Bidirectional translation rule.</strong> This
- *         rule states that the string on the right will be changed
- *         to the string on the left when performing forward
- *         transliteration, and vice versa when performing reverse
- *         transliteration.</dd>
- * </dl>
- * 
- * <p>Translation rules consist of a <em>match pattern</em> and an <em>output
- * string</em>. The match pattern consists of literal characters,
- * optionally preceded by context, and optionally followed by
- * context. Context characters, like literal pattern characters,
- * must be matched in the text being transliterated. However, unlike
- * literal pattern characters, they are not replaced by the output
- * text. For example, the pattern &quot;<code>abc{def}</code>&quot;
- * indicates the characters &quot;<code>def</code>&quot; must be
- * preceded by &quot;<code>abc</code>&quot; for a successful match.
- * If there is a successful match, &quot;<code>def</code>&quot; will
- * be replaced, but not &quot;<code>abc</code>&quot;. The final '<code>}</code>'
- * is optional, so &quot;<code>abc{def</code>&quot; is equivalent to
- * &quot;<code>abc{def}</code>&quot;. Another example is &quot;<code>{123}456</code>&quot;
- * (or &quot;<code>123}456</code>&quot;) in which the literal
- * pattern &quot;<code>123</code>&quot; must be followed by &quot;<code>456</code>&quot;.
- * </p>
- * 
- * <p>The output string of a forward or reverse rule consists of
- * characters to replace the literal pattern characters. If the
- * output string contains the character '<code>|</code>', this is
- * taken to indicate the location of the <em>cursor</em> after
- * replacement. The cursor is the point in the text at which the
- * next replacement, if any, will be applied. The cursor is usually
- * placed within the replacement text; however, it can actually be
- * placed into the precending or following context by using the
- * special character '<code>@</code>'. Examples:</p>
- * 
- * <blockquote>
- *     <p><code>a {foo} z &gt; | @ bar; # foo -&gt; bar, move cursor
- *     before a<br>
- *     {foo} xyz &gt; bar @@|; #&nbsp;foo -&gt; bar, cursor between
- *     y and z</code></p>
- * </blockquote>
- * 
- * <p><b>UnicodeSet</b></p>
- * 
- * <p><code>UnicodeSet</code> patterns may appear anywhere that
- * makes sense. They may appear in variable definitions.
- * Contrariwise, <code>UnicodeSet</code> patterns may themselves
- * contain variable references, such as &quot;<code>$a=[a-z];$not_a=[^$a]</code>&quot;,
- * or &quot;<code>$range=a-z;$ll=[$range]</code>&quot;.</p>
- * 
- * <p><code>UnicodeSet</code> patterns may also be embedded directly
- * into rule strings. Thus, the following two rules are equivalent:</p>
- * 
- * <blockquote>
- *     <p><code>$vowel=[aeiou]; $vowel&gt;'*'; # One way to do this<br>
- *     [aeiou]&gt;'*';
- *     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#
- *     Another way</code></p>
- * </blockquote>
- * 
- * <p>See {@link UnicodeSet} for more documentation and examples.</p>
- * 
- * <p><b>Segments</b></p>
- * 
- * <p>Segments of the input string can be matched and copied to the
- * output string. This makes certain sets of rules simpler and more
- * general, and makes reordering possible. For example:</p>
- * 
- * <blockquote>
- *     <p><code>([a-z]) &gt; $1 $1;
- *     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#
- *     double lowercase letters<br>
- *     ([:Lu:]) ([:Ll:]) &gt; $2 $1; # reverse order of Lu-Ll pairs</code></p>
- * </blockquote>
- * 
- * <p>The segment of the input string to be copied is delimited by
- * &quot;<code>(</code>&quot; and &quot;<code>)</code>&quot;. Up to
- * nine segments may be defined. Segments may not overlap. In the
- * output string, &quot;<code>$1</code>&quot; through &quot;<code>$9</code>&quot;
- * represent the input string segments, in left-to-right order of
- * definition.</p>
- * 
- * <p><b>Anchors</b></p>
- * 
- * <p>Patterns can be anchored to the beginning or the end of the text. This is done with the
- * special characters '<code>^</code>' and '<code>$</code>'. For example:</p>
- * 
- * <blockquote>
- *   <p><code>^ a&nbsp;&nbsp; &gt; 'BEG_A'; &nbsp;&nbsp;# match 'a' at start of text<br>
- *   &nbsp; a&nbsp;&nbsp; &gt; 'A';&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # match other instances
- *   of 'a'<br>
- *   &nbsp; z $ &gt; 'END_Z'; &nbsp;&nbsp;# match 'z' at end of text<br>
- *   &nbsp; z&nbsp;&nbsp; &gt; 'Z';&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # match other instances
- *   of 'z'</code></p>
- * </blockquote>
- * 
- * <p>It is also possible to match the beginning or the end of the text using a <code>UnicodeSet</code>.
- * This is done by including a virtual anchor character '<code>$</code>' at the end of the
- * set pattern. Although this is usually the match chafacter for the end anchor, the set will
- * match either the beginning or the end of the text, depending on its placement. For
- * example:</p>
- * 
- * <blockquote>
- *   <p><code>$x = [a-z$]; &nbsp;&nbsp;# match 'a' through 'z' OR anchor<br>
- *   $x 1&nbsp;&nbsp;&nbsp; &gt; 2;&nbsp;&nbsp; # match '1' after a-z or at the start<br>
- *   &nbsp;&nbsp; 3 $x &gt; 4; &nbsp;&nbsp;# match '3' before a-z or at the end</code></p>
- * </blockquote>
- * 
- * <p><b>Example</b> </p>
- * 
- * <p>The following example rules illustrate many of the features of
- * the rule language. </p>
- * 
- * <table border="0" cellpadding="4">
- *     <tr>
- *         <td valign="top">Rule 1.</td>
- *         <td valign="top" nowrap><code>abc{def}&gt;x|y</code></td>
- *     </tr>
- *     <tr>
- *         <td valign="top">Rule 2.</td>
- *         <td valign="top" nowrap><code>xyz&gt;r</code></td>
- *     </tr>
- *     <tr>
- *         <td valign="top">Rule 3.</td>
- *         <td valign="top" nowrap><code>yz&gt;q</code></td>
- *     </tr>
- * </table>
- * 
- * <p>Applying these rules to the string &quot;<code>adefabcdefz</code>&quot;
- * yields the following results: </p>
- * 
- * <table border="0" cellpadding="4">
- *     <tr>
- *         <td valign="top" nowrap><code>|adefabcdefz</code></td>
- *         <td valign="top">Initial state, no rules match. Advance
- *         cursor.</td>
- *     </tr>
- *     <tr>
- *         <td valign="top" nowrap><code>a|defabcdefz</code></td>
- *         <td valign="top">Still no match. Rule 1 does not match
- *         because the preceding context is not present.</td>
- *     </tr>
- *     <tr>
- *         <td valign="top" nowrap><code>ad|efabcdefz</code></td>
- *         <td valign="top">Still no match. Keep advancing until
- *         there is a match...</td>
- *     </tr>
- *     <tr>
- *         <td valign="top" nowrap><code>ade|fabcdefz</code></td>
- *         <td valign="top">...</td>
- *     </tr>
- *     <tr>
- *         <td valign="top" nowrap><code>adef|abcdefz</code></td>
- *         <td valign="top">...</td>
- *     </tr>
- *     <tr>
- *         <td valign="top" nowrap><code>adefa|bcdefz</code></td>
- *         <td valign="top">...</td>
- *     </tr>
- *     <tr>
- *         <td valign="top" nowrap><code>adefab|cdefz</code></td>
- *         <td valign="top">...</td>
- *     </tr>
- *     <tr>
- *         <td valign="top" nowrap><code>adefabc|defz</code></td>
- *         <td valign="top">Rule 1 matches; replace &quot;<code>def</code>&quot;
- *         with &quot;<code>xy</code>&quot; and back up the cursor
- *         to before the '<code>y</code>'.</td>
- *     </tr>
- *     <tr>
- *         <td valign="top" nowrap><code>adefabcx|yz</code></td>
- *         <td valign="top">Although &quot;<code>xyz</code>&quot; is
- *         present, rule 2 does not match because the cursor is
- *         before the '<code>y</code>', not before the '<code>x</code>'.
- *         Rule 3 does match. Replace &quot;<code>yz</code>&quot;
- *         with &quot;<code>q</code>&quot;.</td>
- *     </tr>
- *     <tr>
- *         <td valign="top" nowrap><code>adefabcxq|</code></td>
- *         <td valign="top">The cursor is at the end;
- *         transliteration is complete.</td>
- *     </tr>
- * </table>
- * 
- * <p>The order of rules is significant. If multiple rules may match
- * at some point, the first matching rule is applied. </p>
- * 
- * <p>Forward and reverse rules may have an empty output string.
- * Otherwise, an empty left or right hand side of any statement is a
- * syntax error. </p>
- * 
- * <p>Single quotes are used to quote any character other than a
- * digit or letter. To specify a single quote itself, inside or
- * outside of quotes, use two single quotes in a row. For example,
- * the rule &quot;<code>'&gt;'&gt;o''clock</code>&quot; changes the
- * string &quot;<code>&gt;</code>&quot; to the string &quot;<code>o'clock</code>&quot;.
- * </p>
- * 
- * <p><b>Notes</b> </p>
- * 
- * <p>While a RuleBasedTransliterator is being built, it checks that
- * the rules are added in proper order. For example, if the rule
- * &quot;a&gt;x&quot; is followed by the rule &quot;ab&gt;y&quot;,
- * then the second rule will throw an exception. The reason is that
- * the second rule can never be triggered, since the first rule
- * always matches anything it matches. In other words, the first
- * rule <em>masks</em> the second rule. </p>
- * 
+ * built from a set of rules as defined for
+ * Transliterator::createFromRules().
+ * See the C++ class Transliterator documentation for the rule syntax.
+ *
  * @author Alan Liu
  * @internal Use transliterator factory methods instead since this class will be removed in that release.
  */
index ebb9575a9f5ddb94bcecf751b8d447e9260464ea..6b4888145f1be51364b0500af25c080bad9f9c22 100644 (file)
 #include "unicode/utypes.h"
 
 /**
- * \file 
+ * \file
  * \brief C++ API: Tranforms text from one format to another.
  */
+
 #if !UCONFIG_NO_TRANSLITERATION
 
 #include "unicode/uobject.h"
@@ -31,7 +31,6 @@ U_NAMESPACE_BEGIN
 
 class UnicodeFilter;
 class UnicodeSet;
-class CompoundTransliterator;
 class TransliteratorParser;
 class NormalizationTransliterator;
 class TransliteratorIDParser;
@@ -97,18 +96,20 @@ class TransliteratorIDParser;
  * contents of the buffer may show text being modified as each new
  * character arrives.
  *
- * <p>Consider the simple `RuleBasedTransliterator`:
- *
+ * <p>Consider the simple rule-based Transliterator:
+ * <pre>
  *     th>{theta}
  *     t>{tau}
+ * </pre>
  *
  * When the user types 't', nothing will happen, since the
  * transliterator is waiting to see if the next character is 'h'.  To
  * remedy this, we introduce the notion of a cursor, marked by a '|'
  * in the output string:
- *
+ * <pre>
  *     t>|{tau}
  *     {tau}h>{theta}
+ * </pre>
  *
  * Now when the user types 't', tau appears, and if the next character
  * is 'h', the tau changes to a theta.  This is accomplished by
@@ -130,7 +131,7 @@ class TransliteratorIDParser;
  * which the transliterator last stopped, either because it reached
  * the end, or because it required more characters to disambiguate
  * between possible inputs.  The <code>CURSOR</code> can also be
- * explicitly set by rules in a <code>RuleBasedTransliterator</code>.
+ * explicitly set by rules in a rule-based Transliterator.
  * Any characters before the <code>CURSOR</code> index are frozen;
  * future keyboard transliteration calls within this input sequence
  * will not change them.  New text is inserted at the
@@ -232,6 +233,255 @@ class TransliteratorIDParser;
  * if the performance of these methods can be improved over the
  * performance obtained by the default implementations in this class.
  *
+ * <p><b>Rule syntax</b>
+ *
+ * <p>A set of rules determines how to perform translations.
+ * Rules within a rule set are separated by semicolons (';').
+ * To include a literal semicolon, prefix it with a backslash ('\').
+ * Unicode Pattern_White_Space is ignored.
+ * If the first non-blank character on a line is '#',
+ * the entire line is ignored as a comment.
+ *
+ * <p>Each set of rules consists of two groups, one forward, and one
+ * reverse. This is a convention that is not enforced; rules for one
+ * direction may be omitted, with the result that translations in
+ * that direction will not modify the source text. In addition,
+ * bidirectional forward-reverse rules may be specified for
+ * symmetrical transformations.
+ *
+ * <p>Note: Another description of the Transliterator rule syntax is available in
+ * <a href="https://www.unicode.org/reports/tr35/tr35-general.html#Transform_Rules_Syntax">section
+ * Transform Rules Syntax of UTS #35: Unicode LDML</a>.
+ * The rules are shown there using arrow symbols ← and → and ↔.
+ * ICU supports both those and the equivalent ASCII symbols &lt; and &gt; and &lt;&gt;.
+ *
+ * <p>Rule statements take one of the following forms:
+ *
+ * <dl>
+ *     <dt><code>$alefmadda=\\u0622;</code></dt>
+ *     <dd><strong>Variable definition.</strong> The name on the
+ *         left is assigned the text on the right. In this example,
+ *         after this statement, instances of the left hand name,
+ *         &quot;<code>$alefmadda</code>&quot;, will be replaced by
+ *         the Unicode character U+0622. Variable names must begin
+ *         with a letter and consist only of letters, digits, and
+ *         underscores. Case is significant. Duplicate names cause
+ *         an exception to be thrown, that is, variables cannot be
+ *         redefined. The right hand side may contain well-formed
+ *         text of any length, including no text at all (&quot;<code>$empty=;</code>&quot;).
+ *         The right hand side may contain embedded <code>UnicodeSet</code>
+ *         patterns, for example, &quot;<code>$softvowel=[eiyEIY]</code>&quot;.</dd>
+ *     <dt><code>ai&gt;$alefmadda;</code></dt>
+ *     <dd><strong>Forward translation rule.</strong> This rule
+ *         states that the string on the left will be changed to the
+ *         string on the right when performing forward
+ *         transliteration.</dd>
+ *     <dt><code>ai&lt;$alefmadda;</code></dt>
+ *     <dd><strong>Reverse translation rule.</strong> This rule
+ *         states that the string on the right will be changed to
+ *         the string on the left when performing reverse
+ *         transliteration.</dd>
+ * </dl>
+ *
+ * <dl>
+ *     <dt><code>ai&lt;&gt;$alefmadda;</code></dt>
+ *     <dd><strong>Bidirectional translation rule.</strong> This
+ *         rule states that the string on the right will be changed
+ *         to the string on the left when performing forward
+ *         transliteration, and vice versa when performing reverse
+ *         transliteration.</dd>
+ * </dl>
+ *
+ * <p>Translation rules consist of a <em>match pattern</em> and an <em>output
+ * string</em>. The match pattern consists of literal characters,
+ * optionally preceded by context, and optionally followed by
+ * context. Context characters, like literal pattern characters,
+ * must be matched in the text being transliterated. However, unlike
+ * literal pattern characters, they are not replaced by the output
+ * text. For example, the pattern &quot;<code>abc{def}</code>&quot;
+ * indicates the characters &quot;<code>def</code>&quot; must be
+ * preceded by &quot;<code>abc</code>&quot; for a successful match.
+ * If there is a successful match, &quot;<code>def</code>&quot; will
+ * be replaced, but not &quot;<code>abc</code>&quot;. The final '<code>}</code>'
+ * is optional, so &quot;<code>abc{def</code>&quot; is equivalent to
+ * &quot;<code>abc{def}</code>&quot;. Another example is &quot;<code>{123}456</code>&quot;
+ * (or &quot;<code>123}456</code>&quot;) in which the literal
+ * pattern &quot;<code>123</code>&quot; must be followed by &quot;<code>456</code>&quot;.
+ *
+ * <p>The output string of a forward or reverse rule consists of
+ * characters to replace the literal pattern characters. If the
+ * output string contains the character '<code>|</code>', this is
+ * taken to indicate the location of the <em>cursor</em> after
+ * replacement. The cursor is the point in the text at which the
+ * next replacement, if any, will be applied. The cursor is usually
+ * placed within the replacement text; however, it can actually be
+ * placed into the precending or following context by using the
+ * special character '@'. Examples:
+ *
+ * <pre>
+ *     a {foo} z &gt; | @ bar; # foo -&gt; bar, move cursor before a
+ *     {foo} xyz &gt; bar @@|; #&nbsp;foo -&gt; bar, cursor between y and z
+ * </pre>
+ *
+ * <p><b>UnicodeSet</b>
+ *
+ * <p><code>UnicodeSet</code> patterns may appear anywhere that
+ * makes sense. They may appear in variable definitions.
+ * Contrariwise, <code>UnicodeSet</code> patterns may themselves
+ * contain variable references, such as &quot;<code>$a=[a-z];$not_a=[^$a]</code>&quot;,
+ * or &quot;<code>$range=a-z;$ll=[$range]</code>&quot;.
+ *
+ * <p><code>UnicodeSet</code> patterns may also be embedded directly
+ * into rule strings. Thus, the following two rules are equivalent:
+ *
+ * <pre>
+ *     $vowel=[aeiou]; $vowel&gt;'*'; # One way to do this
+ *     [aeiou]&gt;'*'; # Another way
+ * </pre>
+ *
+ * <p>See {@link UnicodeSet} for more documentation and examples.
+ *
+ * <p><b>Segments</b>
+ *
+ * <p>Segments of the input string can be matched and copied to the
+ * output string. This makes certain sets of rules simpler and more
+ * general, and makes reordering possible. For example:
+ *
+ * <pre>
+ *     ([a-z]) &gt; $1 $1; # double lowercase letters
+ *     ([:Lu:]) ([:Ll:]) &gt; $2 $1; # reverse order of Lu-Ll pairs
+ * </pre>
+ *
+ * <p>The segment of the input string to be copied is delimited by
+ * &quot;<code>(</code>&quot; and &quot;<code>)</code>&quot;. Up to
+ * nine segments may be defined. Segments may not overlap. In the
+ * output string, &quot;<code>$1</code>&quot; through &quot;<code>$9</code>&quot;
+ * represent the input string segments, in left-to-right order of
+ * definition.
+ *
+ * <p><b>Anchors</b>
+ *
+ * <p>Patterns can be anchored to the beginning or the end of the text. This is done with the
+ * special characters '<code>^</code>' and '<code>$</code>'. For example:
+ *
+ * <pre>
+ *   ^ a&nbsp;&nbsp; &gt; 'BEG_A'; &nbsp;&nbsp;# match 'a' at start of text
+ *   &nbsp; a&nbsp;&nbsp; &gt; 'A'; # match other instances of 'a'
+ *   &nbsp; z $ &gt; 'END_Z'; &nbsp;&nbsp;# match 'z' at end of text
+ *   &nbsp; z&nbsp;&nbsp; &gt; 'Z';&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # match other instances of 'z'
+ * </pre>
+ *
+ * <p>It is also possible to match the beginning or the end of the text using a <code>UnicodeSet</code>.
+ * This is done by including a virtual anchor character '<code>$</code>' at the end of the
+ * set pattern. Although this is usually the match chafacter for the end anchor, the set will
+ * match either the beginning or the end of the text, depending on its placement. For
+ * example:
+ *
+ * <pre>
+ *   $x = [a-z$]; &nbsp;&nbsp;# match 'a' through 'z' OR anchor
+ *   $x 1&nbsp;&nbsp;&nbsp; &gt; 2;&nbsp;&nbsp; # match '1' after a-z or at the start
+ *   &nbsp;&nbsp; 3 $x &gt; 4; &nbsp;&nbsp;# match '3' before a-z or at the end
+ * </pre>
+ *
+ * <p><b>Example</b>
+ *
+ * <p>The following example rules illustrate many of the features of
+ * the rule language.
+ *
+ * <table border="0" cellpadding="4">
+ *     <tr>
+ *         <td style="vertical-align: top;">Rule 1.</td>
+ *         <td style="vertical-align: top; write-space: nowrap;"><code>abc{def}&gt;x|y</code></td>
+ *     </tr>
+ *     <tr>
+ *         <td style="vertical-align: top;">Rule 2.</td>
+ *         <td style="vertical-align: top; write-space: nowrap;"><code>xyz&gt;r</code></td>
+ *     </tr>
+ *     <tr>
+ *         <td style="vertical-align: top;">Rule 3.</td>
+ *         <td style="vertical-align: top; write-space: nowrap;"><code>yz&gt;q</code></td>
+ *     </tr>
+ * </table>
+ *
+ * <p>Applying these rules to the string &quot;<code>adefabcdefz</code>&quot;
+ * yields the following results:
+ *
+ * <table border="0" cellpadding="4">
+ *     <tr>
+ *         <td style="vertical-align: top; write-space: nowrap;"><code>|adefabcdefz</code></td>
+ *         <td style="vertical-align: top;">Initial state, no rules match. Advance
+ *         cursor.</td>
+ *     </tr>
+ *     <tr>
+ *         <td style="vertical-align: top; write-space: nowrap;"><code>a|defabcdefz</code></td>
+ *         <td style="vertical-align: top;">Still no match. Rule 1 does not match
+ *         because the preceding context is not present.</td>
+ *     </tr>
+ *     <tr>
+ *         <td style="vertical-align: top; write-space: nowrap;"><code>ad|efabcdefz</code></td>
+ *         <td style="vertical-align: top;">Still no match. Keep advancing until
+ *         there is a match...</td>
+ *     </tr>
+ *     <tr>
+ *         <td style="vertical-align: top; write-space: nowrap;"><code>ade|fabcdefz</code></td>
+ *         <td style="vertical-align: top;">...</td>
+ *     </tr>
+ *     <tr>
+ *         <td style="vertical-align: top; write-space: nowrap;"><code>adef|abcdefz</code></td>
+ *         <td style="vertical-align: top;">...</td>
+ *     </tr>
+ *     <tr>
+ *         <td style="vertical-align: top; write-space: nowrap;"><code>adefa|bcdefz</code></td>
+ *         <td style="vertical-align: top;">...</td>
+ *     </tr>
+ *     <tr>
+ *         <td style="vertical-align: top; write-space: nowrap;"><code>adefab|cdefz</code></td>
+ *         <td style="vertical-align: top;">...</td>
+ *     </tr>
+ *     <tr>
+ *         <td style="vertical-align: top; write-space: nowrap;"><code>adefabc|defz</code></td>
+ *         <td style="vertical-align: top;">Rule 1 matches; replace &quot;<code>def</code>&quot;
+ *         with &quot;<code>xy</code>&quot; and back up the cursor
+ *         to before the '<code>y</code>'.</td>
+ *     </tr>
+ *     <tr>
+ *         <td style="vertical-align: top; write-space: nowrap;"><code>adefabcx|yz</code></td>
+ *         <td style="vertical-align: top;">Although &quot;<code>xyz</code>&quot; is
+ *         present, rule 2 does not match because the cursor is
+ *         before the '<code>y</code>', not before the '<code>x</code>'.
+ *         Rule 3 does match. Replace &quot;<code>yz</code>&quot;
+ *         with &quot;<code>q</code>&quot;.</td>
+ *     </tr>
+ *     <tr>
+ *         <td style="vertical-align: top; write-space: nowrap;"><code>adefabcxq|</code></td>
+ *         <td style="vertical-align: top;">The cursor is at the end;
+ *         transliteration is complete.</td>
+ *     </tr>
+ * </table>
+ *
+ * <p>The order of rules is significant. If multiple rules may match
+ * at some point, the first matching rule is applied.
+ *
+ * <p>Forward and reverse rules may have an empty output string.
+ * Otherwise, an empty left or right hand side of any statement is a
+ * syntax error.
+ *
+ * <p>Single quotes are used to quote any character other than a
+ * digit or letter. To specify a single quote itself, inside or
+ * outside of quotes, use two single quotes in a row. For example,
+ * the rule &quot;<code>'&gt;'&gt;o''clock</code>&quot; changes the
+ * string &quot;<code>&gt;</code>&quot; to the string &quot;<code>o'clock</code>&quot;.
+ *
+ * <p><b>Notes</b>
+ *
+ * <p>While a Transliterator is being built from rules, it checks that
+ * the rules are added in proper order. For example, if the rule
+ * &quot;a&gt;x&quot; is followed by the rule &quot;ab&gt;y&quot;,
+ * then the second rule will throw an exception. The reason is that
+ * the second rule can never be triggered, since the first rule
+ * always matches anything it matches. In other words, the first
+ * rule <em>masks</em> the second rule.
+ *
  * @author Alan Liu
  * @stable ICU 2.0
  */
@@ -627,7 +877,7 @@ public:
     /**
      * Transliterate a substring of text, as specified by index, taking filters
      * into account.  This method is for subclasses that need to delegate to
-     * another transliterator, such as CompoundTransliterator.
+     * another transliterator.
      * @param text the text to be transliterated
      * @param index the position indices
      * @param incremental if TRUE, then assume more characters may be inserted
@@ -841,17 +1091,19 @@ public:
 
     /**
      * Returns a <code>Transliterator</code> object constructed from
-     * the given rule string.  This will be a RuleBasedTransliterator,
+     * the given rule string.  This will be a rule-based Transliterator,
      * if the rule string contains only rules, or a
-     * CompoundTransliterator, if it contains ID blocks, or a
-     * NullTransliterator, if it contains ID blocks which parse as
+     * compound Transliterator, if it contains ID blocks, or a
+     * null Transliterator, if it contains ID blocks which parse as
      * empty for the given direction.
+     *
      * @param ID            the id for the transliterator.
      * @param rules         rules, separated by ';'
      * @param dir           either FORWARD or REVERSE.
-     * @param parseError    Struct to recieve information on position
+     * @param parseError    Struct to receive information on position
      *                      of error if an error is encountered
      * @param status        Output param set to success/failure code.
+     * @return a newly created Transliterator
      * @stable ICU 2.0
      */
     static Transliterator* U_EXPORT2 createFromRules(const UnicodeString& ID,
index e723619ad36cb1d7809116a2794a6ff2853d4fc5..1733f1a6e425aaecff5b0c8a7afd6e39f096e105 100644 (file)
@@ -20,6 +20,7 @@
 #if !UCONFIG_NO_TRANSLITERATION
 
 #include "unicode/translit.h"
+#include "cpdtrans.h"
 #include "intltest.h"
 
 /**
index 97a51fdd2f2b458d4c679c78705904bb9f4a6c59..be3beb6fdbd745cedb132718a78c747e448db83d 100644 (file)
@@ -13,259 +13,9 @@ import java.util.Map;
 
 /**
  * <code>RuleBasedTransliterator</code> is a transliterator
- * that reads a set of rules in order to determine how to perform
- * translations. Rule sets are stored in resource bundles indexed by
- * name. Rules within a rule set are separated by semicolons (';').
- * To include a literal semicolon, prefix it with a backslash ('\').
- * Unicode Pattern_White_Space is ignored.
- * If the first non-blank character on a line is '#',
- * the entire line is ignored as a comment.
- *
- * <p>Each set of rules consists of two groups, one forward, and one
- * reverse. This is a convention that is not enforced; rules for one
- * direction may be omitted, with the result that translations in
- * that direction will not modify the source text. In addition,
- * bidirectional forward-reverse rules may be specified for
- * symmetrical transformations.
- *
- * <p><b>Rule syntax</b>
- *
- * <p>Rule statements take one of the following forms:
- *
- * <dl>
- *     <dt><code>$alefmadda=\u0622;</code></dt>
- *     <dd><strong>Variable definition.</strong> The name on the
- *         left is assigned the text on the right. In this example,
- *         after this statement, instances of the left hand name,
- *         &quot;<code>$alefmadda</code>&quot;, will be replaced by
- *         the Unicode character U+0622. Variable names must begin
- *         with a letter and consist only of letters, digits, and
- *         underscores. Case is significant. Duplicate names cause
- *         an exception to be thrown, that is, variables cannot be
- *         redefined. The right hand side may contain well-formed
- *         text of any length, including no text at all (&quot;<code>$empty=;</code>&quot;).
- *         The right hand side may contain embedded <code>UnicodeSet</code>
- *         patterns, for example, &quot;<code>$softvowel=[eiyEIY]</code>&quot;.</dd>
- *     <dd>&nbsp;</dd>
- *     <dt><code>ai&gt;$alefmadda;</code></dt>
- *     <dd><strong>Forward translation rule.</strong> This rule
- *         states that the string on the left will be changed to the
- *         string on the right when performing forward
- *         transliteration.</dd>
- *     <dt>&nbsp;</dt>
- *     <dt><code>ai&lt;$alefmadda;</code></dt>
- *     <dd><strong>Reverse translation rule.</strong> This rule
- *         states that the string on the right will be changed to
- *         the string on the left when performing reverse
- *         transliteration.</dd>
- * </dl>
- *
- * <dl>
- *     <dt><code>ai&lt;&gt;$alefmadda;</code></dt>
- *     <dd><strong>Bidirectional translation rule.</strong> This
- *         rule states that the string on the right will be changed
- *         to the string on the left when performing forward
- *         transliteration, and vice versa when performing reverse
- *         transliteration.</dd>
- * </dl>
- *
- * <p>Translation rules consist of a <em>match pattern</em> and an <em>output
- * string</em>. The match pattern consists of literal characters,
- * optionally preceded by context, and optionally followed by
- * context. Context characters, like literal pattern characters,
- * must be matched in the text being transliterated. However, unlike
- * literal pattern characters, they are not replaced by the output
- * text. For example, the pattern &quot;<code>abc{def}</code>&quot;
- * indicates the characters &quot;<code>def</code>&quot; must be
- * preceded by &quot;<code>abc</code>&quot; for a successful match.
- * If there is a successful match, &quot;<code>def</code>&quot; will
- * be replaced, but not &quot;<code>abc</code>&quot;. The final '<code>}</code>'
- * is optional, so &quot;<code>abc{def</code>&quot; is equivalent to
- * &quot;<code>abc{def}</code>&quot;. Another example is &quot;<code>{123}456</code>&quot;
- * (or &quot;<code>123}456</code>&quot;) in which the literal
- * pattern &quot;<code>123</code>&quot; must be followed by &quot;<code>456</code>&quot;.
- *
- * <p>The output string of a forward or reverse rule consists of
- * characters to replace the literal pattern characters. If the
- * output string contains the character '<code>|</code>', this is
- * taken to indicate the location of the <em>cursor</em> after
- * replacement. The cursor is the point in the text at which the
- * next replacement, if any, will be applied. The cursor is usually
- * placed within the replacement text; however, it can actually be
- * placed into the precending or following context by using the
- * special character '<code>@</code>'. Examples:
- *
- * <blockquote>
- *     <p><code>a {foo} z &gt; | @ bar; # foo -&gt; bar, move cursor
- *     before a<br>
- *     {foo} xyz &gt; bar @@|; #&nbsp;foo -&gt; bar, cursor between
- *     y and z</code>
- * </blockquote>
- *
- * <p><b>UnicodeSet</b>
- *
- * <p><code>UnicodeSet</code> patterns may appear anywhere that
- * makes sense. They may appear in variable definitions.
- * Contrariwise, <code>UnicodeSet</code> patterns may themselves
- * contain variable references, such as &quot;<code>$a=[a-z];$not_a=[^$a]</code>&quot;,
- * or &quot;<code>$range=a-z;$ll=[$range]</code>&quot;.
- *
- * <p><code>UnicodeSet</code> patterns may also be embedded directly
- * into rule strings. Thus, the following two rules are equivalent:
- *
- * <blockquote>
- *     <p><code>$vowel=[aeiou]; $vowel&gt;'*'; # One way to do this<br>
- *     [aeiou]&gt;'*';
- *     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#
- *     Another way</code>
- * </blockquote>
- *
- * <p>See {@link UnicodeSet} for more documentation and examples.
- *
- * <p><b>Segments</b>
- *
- * <p>Segments of the input string can be matched and copied to the
- * output string. This makes certain sets of rules simpler and more
- * general, and makes reordering possible. For example:
- *
- * <blockquote>
- *     <p><code>([a-z]) &gt; $1 $1;
- *     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#
- *     double lowercase letters<br>
- *     ([:Lu:]) ([:Ll:]) &gt; $2 $1; # reverse order of Lu-Ll pairs</code>
- * </blockquote>
- *
- * <p>The segment of the input string to be copied is delimited by
- * &quot;<code>(</code>&quot; and &quot;<code>)</code>&quot;. Up to
- * nine segments may be defined. Segments may not overlap. In the
- * output string, &quot;<code>$1</code>&quot; through &quot;<code>$9</code>&quot;
- * represent the input string segments, in left-to-right order of
- * definition.
- *
- * <p><b>Anchors</b>
- *
- * <p>Patterns can be anchored to the beginning or the end of the text. This is done with the
- * special characters '<code>^</code>' and '<code>$</code>'. For example:
- *
- * <blockquote>
- *   <p><code>^ a&nbsp;&nbsp; &gt; 'BEG_A'; &nbsp;&nbsp;# match 'a' at start of text<br>
- *   &nbsp; a&nbsp;&nbsp; &gt; 'A';&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # match other instances
- *   of 'a'<br>
- *   &nbsp; z $ &gt; 'END_Z'; &nbsp;&nbsp;# match 'z' at end of text<br>
- *   &nbsp; z&nbsp;&nbsp; &gt; 'Z';&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # match other instances
- *   of 'z'</code>
- * </blockquote>
- *
- * <p>It is also possible to match the beginning or the end of the text using a <code>UnicodeSet</code>.
- * This is done by including a virtual anchor character '<code>$</code>' at the end of the
- * set pattern. Although this is usually the match chafacter for the end anchor, the set will
- * match either the beginning or the end of the text, depending on its placement. For
- * example:
- *
- * <blockquote>
- *   <p><code>$x = [a-z$]; &nbsp;&nbsp;# match 'a' through 'z' OR anchor<br>
- *   $x 1&nbsp;&nbsp;&nbsp; &gt; 2;&nbsp;&nbsp; # match '1' after a-z or at the start<br>
- *   &nbsp;&nbsp; 3 $x &gt; 4; &nbsp;&nbsp;# match '3' before a-z or at the end</code>
- * </blockquote>
- *
- * <p><b>Example</b>
- *
- * <p>The following example rules illustrate many of the features of
- * the rule language.
- *
- * <table border="0" cellpadding="4">
- *     <tr>
- *         <td style="vertical-align: top;">Rule 1.</td>
- *         <td style="vertical-align: top; write-space: nowrap;"><code>abc{def}&gt;x|y</code></td>
- *     </tr>
- *     <tr>
- *         <td style="vertical-align: top;">Rule 2.</td>
- *         <td style="vertical-align: top; write-space: nowrap;"><code>xyz&gt;r</code></td>
- *     </tr>
- *     <tr>
- *         <td style="vertical-align: top;">Rule 3.</td>
- *         <td style="vertical-align: top; write-space: nowrap;"><code>yz&gt;q</code></td>
- *     </tr>
- * </table>
- *
- * <p>Applying these rules to the string &quot;<code>adefabcdefz</code>&quot;
- * yields the following results:
- *
- * <table border="0" cellpadding="4">
- *     <tr>
- *         <td style="vertical-align: top; write-space: nowrap;"><code>|adefabcdefz</code></td>
- *         <td style="vertical-align: top;">Initial state, no rules match. Advance
- *         cursor.</td>
- *     </tr>
- *     <tr>
- *         <td style="vertical-align: top; write-space: nowrap;"><code>a|defabcdefz</code></td>
- *         <td style="vertical-align: top;">Still no match. Rule 1 does not match
- *         because the preceding context is not present.</td>
- *     </tr>
- *     <tr>
- *         <td style="vertical-align: top; write-space: nowrap;"><code>ad|efabcdefz</code></td>
- *         <td style="vertical-align: top;">Still no match. Keep advancing until
- *         there is a match...</td>
- *     </tr>
- *     <tr>
- *         <td style="vertical-align: top; write-space: nowrap;"><code>ade|fabcdefz</code></td>
- *         <td style="vertical-align: top;">...</td>
- *     </tr>
- *     <tr>
- *         <td style="vertical-align: top; write-space: nowrap;"><code>adef|abcdefz</code></td>
- *         <td style="vertical-align: top;">...</td>
- *     </tr>
- *     <tr>
- *         <td style="vertical-align: top; write-space: nowrap;"><code>adefa|bcdefz</code></td>
- *         <td style="vertical-align: top;">...</td>
- *     </tr>
- *     <tr>
- *         <td style="vertical-align: top; write-space: nowrap;"><code>adefab|cdefz</code></td>
- *         <td style="vertical-align: top;">...</td>
- *     </tr>
- *     <tr>
- *         <td style="vertical-align: top; write-space: nowrap;"><code>adefabc|defz</code></td>
- *         <td style="vertical-align: top;">Rule 1 matches; replace &quot;<code>def</code>&quot;
- *         with &quot;<code>xy</code>&quot; and back up the cursor
- *         to before the '<code>y</code>'.</td>
- *     </tr>
- *     <tr>
- *         <td style="vertical-align: top; write-space: nowrap;"><code>adefabcx|yz</code></td>
- *         <td style="vertical-align: top;">Although &quot;<code>xyz</code>&quot; is
- *         present, rule 2 does not match because the cursor is
- *         before the '<code>y</code>', not before the '<code>x</code>'.
- *         Rule 3 does match. Replace &quot;<code>yz</code>&quot;
- *         with &quot;<code>q</code>&quot;.</td>
- *     </tr>
- *     <tr>
- *         <td style="vertical-align: top; write-space: nowrap;"><code>adefabcxq|</code></td>
- *         <td style="vertical-align: top;">The cursor is at the end;
- *         transliteration is complete.</td>
- *     </tr>
- * </table>
- *
- * <p>The order of rules is significant. If multiple rules may match
- * at some point, the first matching rule is applied.
- *
- * <p>Forward and reverse rules may have an empty output string.
- * Otherwise, an empty left or right hand side of any statement is a
- * syntax error.
- *
- * <p>Single quotes are used to quote any character other than a
- * digit or letter. To specify a single quote itself, inside or
- * outside of quotes, use two single quotes in a row. For example,
- * the rule &quot;<code>'&gt;'&gt;o''clock</code>&quot; changes the
- * string &quot;<code>&gt;</code>&quot; to the string &quot;<code>o'clock</code>&quot;.
- *
- * <p><b>Notes</b>
- *
- * <p>While a RuleBasedTransliterator is being built, it checks that
- * the rules are added in proper order. For example, if the rule
- * &quot;a&gt;x&quot; is followed by the rule &quot;ab&gt;y&quot;,
- * then the second rule will throw an exception. The reason is that
- * the second rule can never be triggered, since the first rule
- * always matches anything it matches. In other words, the first
- * rule <em>masks</em> the second rule.
+ * built from a set of rules as defined for
+ * {@link Transliterator#createFromRules(String, String, int)}.
+ * See the class {@link Transliterator} documentation for the rule syntax.
  *
  * @author Alan Liu
  * @internal
@@ -369,7 +119,7 @@ public class RuleBasedTransliterator extends Transliterator {
 
     static class Data {
         public Data() {
-            variableNames = new HashMap<String, char[]>();
+            variableNames = new HashMap<>();
             ruleSet = new TransliterationRuleSet();
         }
 
@@ -487,5 +237,3 @@ public class RuleBasedTransliterator extends Transliterator {
         return new RuleBasedTransliterator(getID(), data, filter);
     }
 }
-
-
index b8f82558e27d2212ddfcb79071b9dc354f57a1a9..01be8a96dff339296c7554337d13dc6c201f31f7 100644 (file)
@@ -83,7 +83,7 @@ import com.ibm.icu.util.UResourceBundle;
  * modified as each new character arrives.
  *
  * <p>
- * Consider the simple <code>RuleBasedTransliterator</code>:
+ * Consider the simple rule-based Transliterator:
  *
  * <blockquote><code>
  * th&gt;{theta}<br>
@@ -110,8 +110,8 @@ import com.ibm.icu.util.UResourceBundle;
  * that the transliterator will look at. It is advanced as text becomes committed (but it is not the committed index;
  * that's the <code>cursor</code>). The <code>cursor</code> index, described above, marks the point at which the
  * transliterator last stopped, either because it reached the end, or because it required more characters to
- * disambiguate between possible inputs. The <code>cursor</code> can also be explicitly set by rules in a
- * <code>RuleBasedTransliterator</code>. Any characters before the <code>cursor</code> index are frozen; future keyboard
+ * disambiguate between possible inputs. The <code>cursor</code> can also be explicitly set by rules.
+ * Any characters before the <code>cursor</code> index are frozen; future keyboard
  * transliteration calls within this input sequence will not change them. New text is inserted at the <code>limit</code>
  * index, which marks the end of the substring that the transliterator looks at.
  *
@@ -222,13 +222,262 @@ import com.ibm.icu.util.UResourceBundle;
  * <code>transliterate()</code> method taking a <code>String</code> and <code>StringBuffer</code> if the performance of
  * these methods can be improved over the performance obtained by the default implementations in this class.
  *
+ * <p><b>Rule syntax</b>
+ *
+ * <p>A set of rules determines how to perform translations.
+ * Rules within a rule set are separated by semicolons (';').
+ * To include a literal semicolon, prefix it with a backslash ('\').
+ * Unicode Pattern_White_Space is ignored.
+ * If the first non-blank character on a line is '#',
+ * the entire line is ignored as a comment.
+ *
+ * <p>Each set of rules consists of two groups, one forward, and one
+ * reverse. This is a convention that is not enforced; rules for one
+ * direction may be omitted, with the result that translations in
+ * that direction will not modify the source text. In addition,
+ * bidirectional forward-reverse rules may be specified for
+ * symmetrical transformations.
+ *
+ * <p>Note: Another description of the Transliterator rule syntax is available in
+ * <a href="https://www.unicode.org/reports/tr35/tr35-general.html#Transform_Rules_Syntax">section
+ * Transform Rules Syntax of UTS #35: Unicode LDML</a>.
+ * The rules are shown there using arrow symbols ← and → and ↔.
+ * ICU supports both those and the equivalent ASCII symbols &lt; and &gt; and &lt;&gt;.
+ *
+ * <p>Rule statements take one of the following forms:
+ *
+ * <dl>
+ *     <dt><code>$alefmadda=\\u0622;</code></dt>
+ *     <dd><strong>Variable definition.</strong> The name on the
+ *         left is assigned the text on the right. In this example,
+ *         after this statement, instances of the left hand name,
+ *         &quot;<code>$alefmadda</code>&quot;, will be replaced by
+ *         the Unicode character U+0622. Variable names must begin
+ *         with a letter and consist only of letters, digits, and
+ *         underscores. Case is significant. Duplicate names cause
+ *         an exception to be thrown, that is, variables cannot be
+ *         redefined. The right hand side may contain well-formed
+ *         text of any length, including no text at all (&quot;<code>$empty=;</code>&quot;).
+ *         The right hand side may contain embedded <code>UnicodeSet</code>
+ *         patterns, for example, &quot;<code>$softvowel=[eiyEIY]</code>&quot;.</dd>
+ *     <dt><code>ai&gt;$alefmadda;</code></dt>
+ *     <dd><strong>Forward translation rule.</strong> This rule
+ *         states that the string on the left will be changed to the
+ *         string on the right when performing forward
+ *         transliteration.</dd>
+ *     <dt><code>ai&lt;$alefmadda;</code></dt>
+ *     <dd><strong>Reverse translation rule.</strong> This rule
+ *         states that the string on the right will be changed to
+ *         the string on the left when performing reverse
+ *         transliteration.</dd>
+ * </dl>
+ *
+ * <dl>
+ *     <dt><code>ai&lt;&gt;$alefmadda;</code></dt>
+ *     <dd><strong>Bidirectional translation rule.</strong> This
+ *         rule states that the string on the right will be changed
+ *         to the string on the left when performing forward
+ *         transliteration, and vice versa when performing reverse
+ *         transliteration.</dd>
+ * </dl>
+ *
+ * <p>Translation rules consist of a <em>match pattern</em> and an <em>output
+ * string</em>. The match pattern consists of literal characters,
+ * optionally preceded by context, and optionally followed by
+ * context. Context characters, like literal pattern characters,
+ * must be matched in the text being transliterated. However, unlike
+ * literal pattern characters, they are not replaced by the output
+ * text. For example, the pattern &quot;<code>abc{def}</code>&quot;
+ * indicates the characters &quot;<code>def</code>&quot; must be
+ * preceded by &quot;<code>abc</code>&quot; for a successful match.
+ * If there is a successful match, &quot;<code>def</code>&quot; will
+ * be replaced, but not &quot;<code>abc</code>&quot;. The final '<code>}</code>'
+ * is optional, so &quot;<code>abc{def</code>&quot; is equivalent to
+ * &quot;<code>abc{def}</code>&quot;. Another example is &quot;<code>{123}456</code>&quot;
+ * (or &quot;<code>123}456</code>&quot;) in which the literal
+ * pattern &quot;<code>123</code>&quot; must be followed by &quot;<code>456</code>&quot;.
+ *
+ * <p>The output string of a forward or reverse rule consists of
+ * characters to replace the literal pattern characters. If the
+ * output string contains the character '<code>|</code>', this is
+ * taken to indicate the location of the <em>cursor</em> after
+ * replacement. The cursor is the point in the text at which the
+ * next replacement, if any, will be applied. The cursor is usually
+ * placed within the replacement text; however, it can actually be
+ * placed into the precending or following context by using the
+ * special character '@'. Examples:
+ *
+ * <pre>
+ *     a {foo} z &gt; | @ bar; # foo -&gt; bar, move cursor before a
+ *     {foo} xyz &gt; bar @@|; #&nbsp;foo -&gt; bar, cursor between y and z
+ * </pre>
+ *
+ * <p><b>UnicodeSet</b>
+ *
+ * <p><code>UnicodeSet</code> patterns may appear anywhere that
+ * makes sense. They may appear in variable definitions.
+ * Contrariwise, <code>UnicodeSet</code> patterns may themselves
+ * contain variable references, such as &quot;<code>$a=[a-z];$not_a=[^$a]</code>&quot;,
+ * or &quot;<code>$range=a-z;$ll=[$range]</code>&quot;.
+ *
+ * <p><code>UnicodeSet</code> patterns may also be embedded directly
+ * into rule strings. Thus, the following two rules are equivalent:
+ *
+ * <pre>
+ *     $vowel=[aeiou]; $vowel&gt;'*'; # One way to do this
+ *     [aeiou]&gt;'*'; # Another way
+ * </pre>
+ *
+ * <p>See {@link UnicodeSet} for more documentation and examples.
+ *
+ * <p><b>Segments</b>
+ *
+ * <p>Segments of the input string can be matched and copied to the
+ * output string. This makes certain sets of rules simpler and more
+ * general, and makes reordering possible. For example:
+ *
+ * <pre>
+ *     ([a-z]) &gt; $1 $1; # double lowercase letters
+ *     ([:Lu:]) ([:Ll:]) &gt; $2 $1; # reverse order of Lu-Ll pairs
+ * </pre>
+ *
+ * <p>The segment of the input string to be copied is delimited by
+ * &quot;<code>(</code>&quot; and &quot;<code>)</code>&quot;. Up to
+ * nine segments may be defined. Segments may not overlap. In the
+ * output string, &quot;<code>$1</code>&quot; through &quot;<code>$9</code>&quot;
+ * represent the input string segments, in left-to-right order of
+ * definition.
+ *
+ * <p><b>Anchors</b>
+ *
+ * <p>Patterns can be anchored to the beginning or the end of the text. This is done with the
+ * special characters '<code>^</code>' and '<code>$</code>'. For example:
+ *
+ * <pre>
+ *   ^ a&nbsp;&nbsp; &gt; 'BEG_A'; &nbsp;&nbsp;# match 'a' at start of text
+ *   &nbsp; a&nbsp;&nbsp; &gt; 'A'; # match other instances of 'a'
+ *   &nbsp; z $ &gt; 'END_Z'; &nbsp;&nbsp;# match 'z' at end of text
+ *   &nbsp; z&nbsp;&nbsp; &gt; 'Z';&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # match other instances of 'z'
+ * </pre>
+ *
+ * <p>It is also possible to match the beginning or the end of the text using a <code>UnicodeSet</code>.
+ * This is done by including a virtual anchor character '<code>$</code>' at the end of the
+ * set pattern. Although this is usually the match chafacter for the end anchor, the set will
+ * match either the beginning or the end of the text, depending on its placement. For
+ * example:
+ *
+ * <pre>
+ *   $x = [a-z$]; &nbsp;&nbsp;# match 'a' through 'z' OR anchor
+ *   $x 1&nbsp;&nbsp;&nbsp; &gt; 2;&nbsp;&nbsp; # match '1' after a-z or at the start
+ *   &nbsp;&nbsp; 3 $x &gt; 4; &nbsp;&nbsp;# match '3' before a-z or at the end
+ * </pre>
+ *
+ * <p><b>Example</b>
+ *
+ * <p>The following example rules illustrate many of the features of
+ * the rule language.
+ *
+ * <table border="0" cellpadding="4">
+ *     <tr>
+ *         <td style="vertical-align: top;">Rule 1.</td>
+ *         <td style="vertical-align: top; write-space: nowrap;"><code>abc{def}&gt;x|y</code></td>
+ *     </tr>
+ *     <tr>
+ *         <td style="vertical-align: top;">Rule 2.</td>
+ *         <td style="vertical-align: top; write-space: nowrap;"><code>xyz&gt;r</code></td>
+ *     </tr>
+ *     <tr>
+ *         <td style="vertical-align: top;">Rule 3.</td>
+ *         <td style="vertical-align: top; write-space: nowrap;"><code>yz&gt;q</code></td>
+ *     </tr>
+ * </table>
+ *
+ * <p>Applying these rules to the string &quot;<code>adefabcdefz</code>&quot;
+ * yields the following results:
+ *
+ * <table border="0" cellpadding="4">
+ *     <tr>
+ *         <td style="vertical-align: top; write-space: nowrap;"><code>|adefabcdefz</code></td>
+ *         <td style="vertical-align: top;">Initial state, no rules match. Advance
+ *         cursor.</td>
+ *     </tr>
+ *     <tr>
+ *         <td style="vertical-align: top; write-space: nowrap;"><code>a|defabcdefz</code></td>
+ *         <td style="vertical-align: top;">Still no match. Rule 1 does not match
+ *         because the preceding context is not present.</td>
+ *     </tr>
+ *     <tr>
+ *         <td style="vertical-align: top; write-space: nowrap;"><code>ad|efabcdefz</code></td>
+ *         <td style="vertical-align: top;">Still no match. Keep advancing until
+ *         there is a match...</td>
+ *     </tr>
+ *     <tr>
+ *         <td style="vertical-align: top; write-space: nowrap;"><code>ade|fabcdefz</code></td>
+ *         <td style="vertical-align: top;">...</td>
+ *     </tr>
+ *     <tr>
+ *         <td style="vertical-align: top; write-space: nowrap;"><code>adef|abcdefz</code></td>
+ *         <td style="vertical-align: top;">...</td>
+ *     </tr>
+ *     <tr>
+ *         <td style="vertical-align: top; write-space: nowrap;"><code>adefa|bcdefz</code></td>
+ *         <td style="vertical-align: top;">...</td>
+ *     </tr>
+ *     <tr>
+ *         <td style="vertical-align: top; write-space: nowrap;"><code>adefab|cdefz</code></td>
+ *         <td style="vertical-align: top;">...</td>
+ *     </tr>
+ *     <tr>
+ *         <td style="vertical-align: top; write-space: nowrap;"><code>adefabc|defz</code></td>
+ *         <td style="vertical-align: top;">Rule 1 matches; replace &quot;<code>def</code>&quot;
+ *         with &quot;<code>xy</code>&quot; and back up the cursor
+ *         to before the '<code>y</code>'.</td>
+ *     </tr>
+ *     <tr>
+ *         <td style="vertical-align: top; write-space: nowrap;"><code>adefabcx|yz</code></td>
+ *         <td style="vertical-align: top;">Although &quot;<code>xyz</code>&quot; is
+ *         present, rule 2 does not match because the cursor is
+ *         before the '<code>y</code>', not before the '<code>x</code>'.
+ *         Rule 3 does match. Replace &quot;<code>yz</code>&quot;
+ *         with &quot;<code>q</code>&quot;.</td>
+ *     </tr>
+ *     <tr>
+ *         <td style="vertical-align: top; write-space: nowrap;"><code>adefabcxq|</code></td>
+ *         <td style="vertical-align: top;">The cursor is at the end;
+ *         transliteration is complete.</td>
+ *     </tr>
+ * </table>
+ *
+ * <p>The order of rules is significant. If multiple rules may match
+ * at some point, the first matching rule is applied.
+ *
+ * <p>Forward and reverse rules may have an empty output string.
+ * Otherwise, an empty left or right hand side of any statement is a
+ * syntax error.
+ *
+ * <p>Single quotes are used to quote any character other than a
+ * digit or letter. To specify a single quote itself, inside or
+ * outside of quotes, use two single quotes in a row. For example,
+ * the rule &quot;<code>'&gt;'&gt;o''clock</code>&quot; changes the
+ * string &quot;<code>&gt;</code>&quot; to the string &quot;<code>o'clock</code>&quot;.
+ *
+ * <p><b>Notes</b>
+ *
+ * <p>While a Transliterator is being built from rules, it checks that
+ * the rules are added in proper order. For example, if the rule
+ * &quot;a&gt;x&quot; is followed by the rule &quot;ab&gt;y&quot;,
+ * then the second rule will throw an exception. The reason is that
+ * the second rule can never be triggered, since the first rule
+ * always matches anything it matches. In other words, the first
+ * rule <em>masks</em> the second rule.
+ *
  * @author Alan Liu
  * @stable ICU 2.0
  */
 public abstract class Transliterator implements StringTransform  {
     /**
      * Direction constant indicating the forward direction in a transliterator,
-     * e.g., the forward rules of a RuleBasedTransliterator.  An "A-B"
+     * e.g., the forward rules of a rule-based Transliterator.  An "A-B"
      * transliterator transliterates A to B when operating in the forward
      * direction, and B to A when operating in the reverse direction.
      * @stable ICU 2.0
@@ -237,7 +486,7 @@ public abstract class Transliterator implements StringTransform  {
 
     /**
      * Direction constant indicating the reverse direction in a transliterator,
-     * e.g., the reverse rules of a RuleBasedTransliterator.  An "A-B"
+     * e.g., the reverse rules of a rule-based Transliterator.  An "A-B"
      * transliterator transliterates A to B when operating in the forward
      * direction, and B to A when operating in the reverse direction.
      * @stable ICU 2.0
@@ -1102,7 +1351,7 @@ public abstract class Transliterator implements StringTransform  {
     /**
      * Transliterate a substring of text, as specified by index, taking filters
      * into account.  This method is for subclasses that need to delegate to
-     * another transliterator, such as CompoundTransliterator.
+     * another transliterator.
      * @param text the text to be transliterated
      * @param index the position indices
      * @param incremental if TRUE, then assume more characters may be inserted
@@ -1400,11 +1649,17 @@ public abstract class Transliterator implements StringTransform  {
 
     /**
      * Returns a <code>Transliterator</code> object constructed from
-     * the given rule string.  This will be a RuleBasedTransliterator,
+     * the given rule string.  This will be a rule-based Transliterator,
      * if the rule string contains only rules, or a
-     * CompoundTransliterator, if it contains ID blocks, or a
-     * NullTransliterator, if it contains ID blocks which parse as
+     * compound Transliterator, if it contains ID blocks, or a
+     * null Transliterator, if it contains ID blocks which parse as
      * empty for the given direction.
+     *
+     * @param ID the id for the transliterator.
+     * @param rules rules, separated by ';'
+     * @param dir either FORWARD or REVERSE.
+     * @return a newly created Transliterator
+     * @throws IllegalArgumentException if there is a problem with the ID or the rules
      * @stable ICU 2.0
      */
     public static final Transliterator createFromRules(String ID, String rules, int dir) {