home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / i18n / rbbi.h < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-27  |  17.3 KB  |  410 lines

  1. /*
  2. **********************************************************************
  3. *   Copyright (C) 1999 Alan Liu and others. All rights reserved.
  4. **********************************************************************
  5. *   Date        Name        Description
  6. *   10/22/99    alan        Creation.
  7. **********************************************************************
  8. */
  9.  
  10. #ifndef RBBI_H
  11. #define RBBI_H
  12.  
  13. /**
  14.  * <p>A subclass of BreakIterator whose behavior is specified using a list of rules.</p>
  15.  * 
  16.  * <p>There are two kinds of rules, which are separated by semicolons: <i>substitutions</i>
  17.  * and <i>regular expressions.</i></p>
  18.  * 
  19.  * <p>A substitution rule defines a name that can be used in place of an expression. It
  20.  * consists of a name, which is a string of characters contained in angle brackets, an equals
  21.  * sign, and an expression. (There can be no whitespace on either side of the equals sign.)
  22.  * To keep its syntactic meaning intact, the expression must be enclosed in parentheses or
  23.  * square brackets. A substitution is visible after its definition, and is filled in using
  24.  * simple textual substitution. Substitution definitions can contain other substitutions, as
  25.  * long as those substitutions have been defined first. Substitutions are generally used to
  26.  * make the regular expressions (which can get quite complex) shorted and easier to read.
  27.  * They typically define either character categories or commonly-used subexpressions.</p>
  28.  * 
  29.  * <p>There is one special substitution.  If the description defines a substitution
  30.  * called "<ignore>", the expression must be a [] expression, and the
  31.  * expression defines a set of characters (the "<em>ignore characters</em>") that
  32.  * will be transparent to the BreakIterator.  A sequence of characters will break the
  33.  * same way it would if any ignore characters it contains are taken out.  Break
  34.  * positions never occur befoer ignore characters.</p>
  35.  * 
  36.  * <p>A regular expression uses a subset of the normal Unix regular-expression syntax, and
  37.  * defines a sequence of characters to be kept together. With one significant exception, the
  38.  * iterator uses a longest-possible-match algorithm when matching text to regular
  39.  * expressions. The iterator also treats descriptions containing multiple regular expressions
  40.  * as if they were ORed together (i.e., as if they were separated by |).</p>
  41.  * 
  42.  * <p>The special characters recognized by the regular-expression parser are as follows:</p>
  43.  * 
  44.  * <blockquote>
  45.  *   <table border="1" width="100%">
  46.  *     <tr>
  47.  *       <td width="6%">*</td>
  48.  *       <td width="94%">Specifies that the expression preceding the asterisk may occur any number
  49.  *       of times (including not at all).</td>
  50.  *     </tr>
  51.  *     <tr>
  52.  *       <td width="6%">{}</td>
  53.  *       <td width="94%">Encloses a sequence of characters that is optional.</td>
  54.  *     </tr>
  55.  *     <tr>
  56.  *       <td width="6%">()</td>
  57.  *       <td width="94%">Encloses a sequence of characters.  If followed by *, the sequence
  58.  *       repeats.  Otherwise, the parentheses are just a grouping device and a way to delimit
  59.  *       the ends of expressions containing |.</td>
  60.  *     </tr>
  61.  *     <tr>
  62.  *       <td width="6%">|</td>
  63.  *       <td width="94%">Separates two alternative sequences of characters.  Either one
  64.  *       sequence or the other, but not both, matches this expression.  The | character can
  65.  *       only occur inside ().</td>
  66.  *     </tr>
  67.  *     <tr>
  68.  *       <td width="6%">.</td>
  69.  *       <td width="94%">Matches any character.</td>
  70.  *     </tr>
  71.  *     <tr>
  72.  *       <td width="6%">*?</td>
  73.  *       <td width="94%">Specifies a non-greedy asterisk.  *? works the same way as *, except
  74.  *       when there is overlap between the last group of characters in the expression preceding the
  75.  *       * and the first group of characters following the *.  When there is this kind of
  76.  *       overlap, * will match the longest sequence of characters that match the expression before
  77.  *       the *, and *? will match the shortest sequence of characters matching the expression
  78.  *       before the *?.  For example, if you have "xxyxyyyxyxyxxyxyxyy" in the text,
  79.  *       "x[xy]*x" will match through to the last x (i.e., "<strong>xxyxyyyxyxyxxyxyx</strong>yy",
  80.  *       but "x[xy]*?x" will only match the first two xes ("<strong>xx</strong>yxyyyxyxyxxyxyxyy").</td>
  81.  *     </tr>
  82.  *     <tr>
  83.  *       <td width="6%">[]</td>
  84.  *       <td width="94%">Specifies a group of alternative characters.  A [] expression will
  85.  *       match any single character that is specified in the [] expression.  For more on the
  86.  *       syntax of [] expressions, see below.</td>
  87.  *     </tr>
  88.  *     <tr>
  89.  *       <td width="6%">/</td>
  90.  *       <td width="94%">Specifies where the break position should go if text matches this
  91.  *       expression.  (e.g., "[a-z]*/[:Zs:]*1" will match if the iterator sees a run
  92.  *       of letters, followed by a run of whitespace, followed by a digit, but the break position
  93.  *       will actually go before the whitespace).  Expressions that don't contain / put the
  94.  *       break position at the end of the matching text.</td>
  95.  *     </tr>
  96.  *     <tr>
  97.  *       <td width="6%">\</td>
  98.  *       <td width="94%">Escape character.  The \ itself is ignored, but causes the next
  99.  *       character to be treated as literal character.  This has no effect for many
  100.  *       characters, but for the characters listed above, this deprives them of their special
  101.  *       meaning.  (There are no special escape sequences for Unicode characters, or tabs and
  102.  *       newlines; these are all handled by a higher-level protocol.  In a Java string,
  103.  *       "\n" will be converted to a literal newline character by the time the
  104.  *       regular-expression parser sees it.  Of course, this means that \ sequences that are
  105.  *       visible to the regexp parser must be written as \\ when inside a Java string.)  All
  106.  *       characters in the ASCII range except for letters, digits, and control characters are
  107.  *       reserved characters to the parser and must be preceded by \ even if they currently don't
  108.  *       mean anything.</td>
  109.  *     </tr>
  110.  *     <tr>
  111.  *       <td width="6%">!</td>
  112.  *       <td width="94%">If ! appears at the beginning of a regular expression, it tells the regexp
  113.  *       parser that this expression specifies the backwards-iteration behavior of the iterator,
  114.  *       and not its normal iteration behavior.  This is generally only used in situations
  115.  *       where the automatically-generated backwards-iteration brhavior doesn't produce
  116.  *       satisfactory results and must be supplemented with extra client-specified rules.</td>
  117.  *     </tr>
  118.  *     <tr>
  119.  *       <td width="6%"><em>(all others)</em></td>
  120.  *       <td width="94%">All other characters are treated as literal characters, which must match
  121.  *       the corresponding character(s) in the text exactly.</td>
  122.  *     </tr>
  123.  *   </table>
  124.  * </blockquote>
  125.  * 
  126.  * <p>Within a [] expression, a number of other special characters can be used to specify
  127.  * groups of characters:</p>
  128.  * 
  129.  * <blockquote>
  130.  *   <table border="1" width="100%">
  131.  *     <tr>
  132.  *       <td width="6%">-</td>
  133.  *       <td width="94%">Specifies a range of matching characters.  For example
  134.  *       "[a-p]" matches all lowercase Latin letters from a to p (inclusive).  The -
  135.  *       sign specifies ranges of continuous Unicode numeric values, not ranges of characters in a
  136.  *       language's alphabetical order: "[a-z]" doesn't include capital letters, nor does
  137.  *       it include accented letters such as a-umlaut.</td>
  138.  *     </tr>
  139.  *     <tr>
  140.  *       <td width="6%">::</td>
  141.  *       <td width="94%">A pair of colons containing a one- or two-letter code matches all
  142.  *       characters in the corresponding Unicode category.  The two-letter codes are the same
  143.  *       as the two-letter codes in the Unicode database (for example, "[:Sc::Sm:]"
  144.  *       matches all currency symbols and all math symbols).  Specifying a one-letter code is
  145.  *       the same as specifying all two-letter codes that begin with that letter (for example,
  146.  *       "[:L:]" matches all letters, and is equivalent to
  147.  *       "[:Lu::Ll::Lo::Lm::Lt:]").  Anything other than a valid two-letter Unicode
  148.  *       category code or a single letter that begins a Unicode category code is illegal within
  149.  *       colons.</td>
  150.  *     </tr>
  151.  *     <tr>
  152.  *       <td width="6%">[]</td>
  153.  *       <td width="94%">[] expressions can nest.  This has no effect, except when used in
  154.  *       conjunction with the ^ token.</td>
  155.  *     </tr>
  156.  *     <tr>
  157.  *       <td width="6%">^</td>
  158.  *       <td width="94%">Excludes the character (or the characters in the [] expression) following
  159.  *       it from the group of characters.  For example, "[a-z^p]" matches all Latin
  160.  *       lowercase letters except p.  "[:L:^[\u4e00-\u9fff]]" matches all letters
  161.  *       except the Han ideographs.</td>
  162.  *     </tr>
  163.  *     <tr>
  164.  *       <td width="6%"><em>(all others)</em></td>
  165.  *       <td width="94%">All other characters are treated as literal characters.  (For
  166.  *       example, "[aeiou]" specifies just the letters a, e, i, o, and u.)</td>
  167.  *     </tr>
  168.  *   </table>
  169.  * </blockquote>
  170.  * 
  171.  * <p>For a more complete explanation, see <a
  172.  * href="http://www.ibm.com/java/education/boundaries/boundaries.html">http://www.ibm.com/java/education/boundaries/boundaries.html</a>.
  173.  *   For examples, see the resource data (which is annotated).</p>
  174.  *
  175.  * @author Richard Gillam
  176.  */
  177. class RuleBasedBreakIterator {
  178.  
  179. protected:
  180.  
  181.     /**
  182.      * A token used as a character-category value to identify ignore characters
  183.      */
  184.     static int8_t IGNORE;
  185.  
  186. private:
  187.  
  188.     /**
  189.      * The state number of the starting state
  190.      */
  191.     static int16_t START_STATE;
  192.  
  193.     /**
  194.      * The state-transition value indicating "stop"
  195.      */
  196.     static int16_t STOP_STATE;
  197.  
  198.     /**
  199.      * The textual description this iterator was created from
  200.      */
  201.     UnicodeString description;
  202.  
  203.     /**
  204.      * A table that indexes from character values to character category numbers
  205.      */
  206.     CompactByteArray charCategoryTable;
  207.  
  208.     /**
  209.      * The table of state transitions used for forward iteration
  210.      */
  211.     int16_t* stateTable;
  212.  
  213.     /**
  214.      * The table of state transitions used to sync up the iterator with the
  215.      * text in backwards and random-access iteration
  216.      */
  217.     int16_t* backwardsStateTable;
  218.  
  219.     /**
  220.      * A list of flags indicating which states in the state table are accepting
  221.      * ("end") states
  222.      */
  223.     bool_t* endStates;
  224.  
  225.     /**
  226.      * The number of character categories (and, thus, the number of columns in
  227.      * the state tables)
  228.      */
  229.     int32_t numCategories;
  230.  
  231.     /**
  232.      * The character iterator through which this BreakIterator accesses the text
  233.      */
  234.     CharacterIterator text;
  235.  
  236.     //=======================================================================
  237.     // constructors
  238.     //=======================================================================
  239.  
  240. public:
  241.  
  242.     /**
  243.      * Constructs a RuleBasedBreakIterator according to the description
  244.      * provided.  If the description is malformed, throws an
  245.      * IllegalArgumentException.  Normally, instead of constructing a
  246.      * RuleBasedBreakIterator directory, you'll use the factory methods
  247.      * on BreakIterator to create one indirectly from a description
  248.      * in the framework's resource files.  You'd use this when you want
  249.      * special behavior not provided by the built-in iterators.
  250.      */
  251.     RuleBasedBreakIterator(UnicodeString description);
  252.  
  253.     //=======================================================================
  254.     // boilerplate
  255.     //=======================================================================
  256. public:
  257.  
  258.     /**
  259.      * Clones this iterator.
  260.      * @return A newly-constructed RuleBasedBreakIterator with the same
  261.      * behavior as this one.
  262.      */
  263.     virtual Object clone();
  264.  
  265.     /**
  266.      * Returns true if both BreakIterators are of the same class, have the same
  267.      * rules, and iterate over the same text.
  268.      */
  269.     virtual bool_t equals(Object that);
  270.  
  271.     /**
  272.      * Returns the description used to create this iterator
  273.      */
  274.     virtual UnicodeString toString();
  275.  
  276.     /**
  277.      * Compute a hashcode for this BreakIterator
  278.      * @return A hash code
  279.      */
  280.     virtual int32_t hashCode();
  281.     //=======================================================================
  282.     // BreakIterator overrides
  283.     //=======================================================================
  284.     /**
  285.      * Sets the current iteration position to the beginning of the text.
  286.      * (i.e., the CharacterIterator's starting offset).
  287.      * @return The offset of the beginning of the text.
  288.      */
  289.     virtual int32_t first();
  290.  
  291.     /**
  292.      * Sets the current iteration position to the end of the text.
  293.      * (i.e., the CharacterIterator's ending offset).
  294.      * @return The text's past-the-end offset.
  295.      */
  296.     virtual int32_t last();
  297.  
  298.     /**
  299.      * Advances the iterator either forward or backward the specified number of steps.
  300.      * Negative values move backward, and positive values move forward.  This is
  301.      * equivalent to repeatedly calling next() or previous().
  302.      * @param n The number of steps to move.  The sign indicates the direction
  303.      * (negative is backwards, and positive is forwards).
  304.      * @return The character offset of the boundary position n boundaries away from
  305.      * the current one.
  306.      */
  307.     virtual int32_t next(int32_t n);
  308.  
  309.     /**
  310.      * Advances the iterator to the next boundary position.
  311.      * @return The position of the first boundary after this one.
  312.      */
  313.     virtual int32_t next();
  314.  
  315.     /**
  316.      * Advances the iterator backwards, to the last boundary preceding this one.
  317.      * @return The position of the last boundary position preceding this one.
  318.      */
  319.     virtual int32_t previous();
  320.  
  321.     /**
  322.      * Sets the iterator to refer to the first boundary position following
  323.      * the specified position.
  324.      * @offset The position from which to begin searching for a break position.
  325.      * @return The position of the first break after the current position.
  326.      */
  327.     virtual int32_t following(int32_t offset);
  328.  
  329.     /**
  330.      * Sets the iterator to refer to the last boundary position before the
  331.      * specified position.
  332.      * @offset The position to begin searching for a break from.
  333.      * @return The position of the last boundary before the starting position.
  334.      */
  335.     virtual int32_t preceding(int32_t offset);
  336.  
  337.     /**
  338.      * Returns true if the specfied position is a boundary position.  As a side
  339.      * effect, leaves the iterator pointing to the first boundary position at
  340.      * or after "offset".
  341.      * @param offset the offset to check.
  342.      * @return True if "offset" is a boundary position.
  343.      */
  344.     virtual bool_t isBoundary(int32_t offset);
  345.  
  346.     /**
  347.      * Returns the current iteration position.
  348.      * @return The current iteration position.
  349.      */
  350.     virtual int32_t current();
  351.  
  352.     /**
  353.      * Return a CharacterIterator over the text being analyzed.  This version
  354.      * of this method returns the actual CharacterIterator we're using internally.
  355.      * Changing the state of this iterator can have undefined consequences.  If
  356.      * you need to change it, clone it first.
  357.      * @return An iterator over the text being analyzed.
  358.      */
  359.     virtual CharacterIterator getText();
  360.  
  361.     /**
  362.      * Set the iterator to analyze a new piece of text.  This function resets
  363.      * the current iteration position to the beginning of the text.
  364.      * @param newText An iterator over the text to analyze.
  365.      */
  366.     virtual void setText(CharacterIterator newText);
  367.     //=======================================================================
  368.     // implementation
  369.     //=======================================================================
  370. protected:
  371.  
  372.     /**
  373.      * This method is the actual implementation of the next() method.  All iteration
  374.      * vectors through here.  This method initializes the state machine to state 1
  375.      * and advances through the text character by character until we reach the end
  376.      * of the text or the state machine transitions to state 0.  We update our return
  377.      * value every time the state machine passes through a possible end state.
  378.      */
  379.     virtual int32_t handleNext();
  380.  
  381.     /**
  382.      * This method backs the iterator back up to a "safe position" in the text.
  383.      * This is a position that we know, without any context, must be a break position.
  384.      * The various calling methods then iterate forward from this safe position to
  385.      * the appropriate position to return.  (For more information, see the description
  386.      * of buildBackwardsStateTable() in RuleBasedBreakIterator.Builder.)
  387.      */
  388.     virtual int32_t handlePrevious();
  389.  
  390.     /**
  391.      * Looks up a character's category (i.e., its category for breaking purposes,
  392.      * not its Unicode category)
  393.      */
  394.     virtual int32_t lookupCategory(UChar c);
  395.  
  396.     /**
  397.      * Given a current state and a character category, looks up the
  398.      * next state to transition to in the state table.
  399.      */
  400.     virtual int32_t lookupState(int32_t state, int32_t category);
  401.  
  402.     /**
  403.      * Given a current state and a character category, looks up the
  404.      * next state to transition to in the backwards state table.
  405.      */
  406.     virtual int32_t lookupBackwardState(int32_t state, int32_t category);
  407. };
  408.  
  409. #endif
  410.