jdk/src/share/classes/java/util/regex/Pattern.java
author sherman
Mon, 19 Dec 2011 14:14:14 -0800
changeset 11287 3db172a5433c
parent 9694 5cf41be509e5
child 12675 3b7d71495649
permissions -rw-r--r--
6990617: Regular expression doesn't match if unicode character next to a digit. Summary: updated RemoveQEQuotation() to deal with this case correctly Reviewed-by: sherman Contributed-by: stephen.flores@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8173
diff changeset
     2
 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4820
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4820
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4820
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4820
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4820
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util.regex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.text.CharacterIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.text.Normalizer;
5610
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
    32
import java.util.Locale;
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
    33
import java.util.Map;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * A compiled representation of a regular expression.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p> A regular expression, specified as a string, must first be compiled into
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * an instance of this class.  The resulting pattern can then be used to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * a {@link Matcher} object that can match arbitrary {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * java.lang.CharSequence </code>character sequences<code>} against the regular
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * expression.  All of the state involved in performing a match resides in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * matcher, so many matchers can share the same pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p> A typical invocation sequence is thus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * Pattern p = Pattern.{@link #compile compile}("a*b");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * Matcher m = p.{@link #matcher matcher}("aaaaab");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * boolean b = m.{@link Matcher#matches matches}();</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <p> A {@link #matches matches} method is defined by this class as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * convenience for when a regular expression is used just once.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * compiles an expression and matches an input sequence against it in a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * invocation.  The statement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * boolean b = Pattern.matches("a*b", "aaaaab");</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * is equivalent to the three statements above, though for repeated matches it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * is less efficient since it does not allow the compiled pattern to be reused.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <p> Instances of this class are immutable and are safe for use by multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * concurrent threads.  Instances of the {@link Matcher} class are not safe for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * such use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <a name="sum">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <h4> Summary of regular-expression constructs </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <table border="0" cellpadding="1" cellspacing="0"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *  summary="Regular expression constructs, and what they match">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <tr align="left">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <th bgcolor="#CCCCFF" align="left" id="construct">Construct</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * <th bgcolor="#CCCCFF" align="left" id="matches">Matches</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <tr><th>&nbsp;</th></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * <tr align="left"><th colspan="2" id="characters">Characters</th></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <tr><td valign="top" headers="construct characters"><i>x</i></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *     <td headers="matches">The character <i>x</i></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * <tr><td valign="top" headers="construct characters"><tt>\\</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *     <td headers="matches">The backslash character</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <tr><td valign="top" headers="construct characters"><tt>\0</tt><i>n</i></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *     <td headers="matches">The character with octal value <tt>0</tt><i>n</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *         (0&nbsp;<tt>&lt;=</tt>&nbsp;<i>n</i>&nbsp;<tt>&lt;=</tt>&nbsp;7)</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * <tr><td valign="top" headers="construct characters"><tt>\0</tt><i>nn</i></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *     <td headers="matches">The character with octal value <tt>0</tt><i>nn</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *         (0&nbsp;<tt>&lt;=</tt>&nbsp;<i>n</i>&nbsp;<tt>&lt;=</tt>&nbsp;7)</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * <tr><td valign="top" headers="construct characters"><tt>\0</tt><i>mnn</i></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *     <td headers="matches">The character with octal value <tt>0</tt><i>mnn</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *         (0&nbsp;<tt>&lt;=</tt>&nbsp;<i>m</i>&nbsp;<tt>&lt;=</tt>&nbsp;3,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *         0&nbsp;<tt>&lt;=</tt>&nbsp;<i>n</i>&nbsp;<tt>&lt;=</tt>&nbsp;7)</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * <tr><td valign="top" headers="construct characters"><tt>\x</tt><i>hh</i></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *     <td headers="matches">The character with hexadecimal&nbsp;value&nbsp;<tt>0x</tt><i>hh</i></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * <tr><td valign="top" headers="construct characters"><tt>&#92;u</tt><i>hhhh</i></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *     <td headers="matches">The character with hexadecimal&nbsp;value&nbsp;<tt>0x</tt><i>hhhh</i></td></tr>
8173
a3a39b98e05a 7014645: Support perl style Unicode hex notation \x{...}
sherman
parents: 7816
diff changeset
   104
 * <tr><td valign="top" headers="construct characters"><tt>&#92;x</tt><i>{h...h}</i></td>
a3a39b98e05a 7014645: Support perl style Unicode hex notation \x{...}
sherman
parents: 7816
diff changeset
   105
 *     <td headers="matches">The character with hexadecimal&nbsp;value&nbsp;<tt>0x</tt><i>h...h</i>
a3a39b98e05a 7014645: Support perl style Unicode hex notation \x{...}
sherman
parents: 7816
diff changeset
   106
 *         ({@link java.lang.Character#MIN_CODE_POINT Character.MIN_CODE_POINT}
a3a39b98e05a 7014645: Support perl style Unicode hex notation \x{...}
sherman
parents: 7816
diff changeset
   107
 *         &nbsp;&lt;=&nbsp;<tt>0x</tt><i>h...h</i>&nbsp;&lt;=&nbsp
a3a39b98e05a 7014645: Support perl style Unicode hex notation \x{...}
sherman
parents: 7816
diff changeset
   108
 *          {@link java.lang.Character#MAX_CODE_POINT Character.MAX_CODE_POINT})</td></tr>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * <tr><td valign="top" headers="matches"><tt>\t</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *     <td headers="matches">The tab character (<tt>'&#92;u0009'</tt>)</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * <tr><td valign="top" headers="construct characters"><tt>\n</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *     <td headers="matches">The newline (line feed) character (<tt>'&#92;u000A'</tt>)</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * <tr><td valign="top" headers="construct characters"><tt>\r</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *     <td headers="matches">The carriage-return character (<tt>'&#92;u000D'</tt>)</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * <tr><td valign="top" headers="construct characters"><tt>\f</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *     <td headers="matches">The form-feed character (<tt>'&#92;u000C'</tt>)</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * <tr><td valign="top" headers="construct characters"><tt>\a</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *     <td headers="matches">The alert (bell) character (<tt>'&#92;u0007'</tt>)</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * <tr><td valign="top" headers="construct characters"><tt>\e</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *     <td headers="matches">The escape character (<tt>'&#92;u001B'</tt>)</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * <tr><td valign="top" headers="construct characters"><tt>\c</tt><i>x</i></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *     <td headers="matches">The control character corresponding to <i>x</i></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * <tr><th>&nbsp;</th></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * <tr align="left"><th colspan="2" id="classes">Character classes</th></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * <tr><td valign="top" headers="construct classes"><tt>[abc]</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 *     <td headers="matches"><tt>a</tt>, <tt>b</tt>, or <tt>c</tt> (simple class)</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * <tr><td valign="top" headers="construct classes"><tt>[^abc]</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 *     <td headers="matches">Any character except <tt>a</tt>, <tt>b</tt>, or <tt>c</tt> (negation)</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * <tr><td valign="top" headers="construct classes"><tt>[a-zA-Z]</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 *     <td headers="matches"><tt>a</tt> through <tt>z</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 *         or <tt>A</tt> through <tt>Z</tt>, inclusive (range)</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * <tr><td valign="top" headers="construct classes"><tt>[a-d[m-p]]</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 *     <td headers="matches"><tt>a</tt> through <tt>d</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 *      or <tt>m</tt> through <tt>p</tt>: <tt>[a-dm-p]</tt> (union)</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * <tr><td valign="top" headers="construct classes"><tt>[a-z&&[def]]</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 *     <td headers="matches"><tt>d</tt>, <tt>e</tt>, or <tt>f</tt> (intersection)</tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * <tr><td valign="top" headers="construct classes"><tt>[a-z&&[^bc]]</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 *     <td headers="matches"><tt>a</tt> through <tt>z</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 *         except for <tt>b</tt> and <tt>c</tt>: <tt>[ad-z]</tt> (subtraction)</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * <tr><td valign="top" headers="construct classes"><tt>[a-z&&[^m-p]]</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 *     <td headers="matches"><tt>a</tt> through <tt>z</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 *          and not <tt>m</tt> through <tt>p</tt>: <tt>[a-lq-z]</tt>(subtraction)</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * <tr><th>&nbsp;</th></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * <tr align="left"><th colspan="2" id="predef">Predefined character classes</th></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * <tr><td valign="top" headers="construct predef"><tt>.</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 *     <td headers="matches">Any character (may or may not match <a href="#lt">line terminators</a>)</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * <tr><td valign="top" headers="construct predef"><tt>\d</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 *     <td headers="matches">A digit: <tt>[0-9]</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * <tr><td valign="top" headers="construct predef"><tt>\D</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 *     <td headers="matches">A non-digit: <tt>[^0-9]</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * <tr><td valign="top" headers="construct predef"><tt>\s</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 *     <td headers="matches">A whitespace character: <tt>[ \t\n\x0B\f\r]</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * <tr><td valign="top" headers="construct predef"><tt>\S</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 *     <td headers="matches">A non-whitespace character: <tt>[^\s]</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * <tr><td valign="top" headers="construct predef"><tt>\w</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 *     <td headers="matches">A word character: <tt>[a-zA-Z_0-9]</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * <tr><td valign="top" headers="construct predef"><tt>\W</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 *     <td headers="matches">A non-word character: <tt>[^\w]</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * <tr><th>&nbsp;</th></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * <tr align="left"><th colspan="2" id="posix">POSIX character classes</b> (US-ASCII only)<b></th></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * <tr><td valign="top" headers="construct posix"><tt>\p{Lower}</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 *     <td headers="matches">A lower-case alphabetic character: <tt>[a-z]</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * <tr><td valign="top" headers="construct posix"><tt>\p{Upper}</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 *     <td headers="matches">An upper-case alphabetic character:<tt>[A-Z]</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * <tr><td valign="top" headers="construct posix"><tt>\p{ASCII}</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 *     <td headers="matches">All ASCII:<tt>[\x00-\x7F]</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 * <tr><td valign="top" headers="construct posix"><tt>\p{Alpha}</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 *     <td headers="matches">An alphabetic character:<tt>[\p{Lower}\p{Upper}]</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 * <tr><td valign="top" headers="construct posix"><tt>\p{Digit}</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 *     <td headers="matches">A decimal digit: <tt>[0-9]</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 * <tr><td valign="top" headers="construct posix"><tt>\p{Alnum}</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 *     <td headers="matches">An alphanumeric character:<tt>[\p{Alpha}\p{Digit}]</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 * <tr><td valign="top" headers="construct posix"><tt>\p{Punct}</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 *     <td headers="matches">Punctuation: One of <tt>!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 *     <!-- <tt>[\!"#\$%&'\(\)\*\+,\-\./:;\<=\>\?@\[\\\]\^_`\{\|\}~]</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 *          <tt>[\X21-\X2F\X31-\X40\X5B-\X60\X7B-\X7E]</tt> -->
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 * <tr><td valign="top" headers="construct posix"><tt>\p{Graph}</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 *     <td headers="matches">A visible character: <tt>[\p{Alnum}\p{Punct}]</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 * <tr><td valign="top" headers="construct posix"><tt>\p{Print}</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 *     <td headers="matches">A printable character: <tt>[\p{Graph}\x20]</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 * <tr><td valign="top" headers="construct posix"><tt>\p{Blank}</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 *     <td headers="matches">A space or a tab: <tt>[ \t]</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 * <tr><td valign="top" headers="construct posix"><tt>\p{Cntrl}</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 *     <td headers="matches">A control character: <tt>[\x00-\x1F\x7F]</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 * <tr><td valign="top" headers="construct posix"><tt>\p{XDigit}</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 *     <td headers="matches">A hexadecimal digit: <tt>[0-9a-fA-F]</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 * <tr><td valign="top" headers="construct posix"><tt>\p{Space}</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 *     <td headers="matches">A whitespace character: <tt>[ \t\n\x0B\f\r]</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 * <tr><th>&nbsp;</th></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
 * <tr align="left"><th colspan="2">java.lang.Character classes (simple <a href="#jcc">java character type</a>)</th></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 * <tr><td valign="top"><tt>\p{javaLowerCase}</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 *     <td>Equivalent to java.lang.Character.isLowerCase()</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 * <tr><td valign="top"><tt>\p{javaUpperCase}</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 *     <td>Equivalent to java.lang.Character.isUpperCase()</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 * <tr><td valign="top"><tt>\p{javaWhitespace}</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 *     <td>Equivalent to java.lang.Character.isWhitespace()</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 * <tr><td valign="top"><tt>\p{javaMirrored}</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 *     <td>Equivalent to java.lang.Character.isMirrored()</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 * <tr><th>&nbsp;</th></tr>
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   209
 * <tr align="left"><th colspan="2" id="unicode">Classes for Unicode scripts, blocks, categories and binary properties</th></tr>
5610
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
   210
 * * <tr><td valign="top" headers="construct unicode"><tt>\p{IsLatin}</tt></td>
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   211
 *     <td headers="matches">A Latin&nbsp;script character (<a href="#usc">script</a>)</td></tr>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 * <tr><td valign="top" headers="construct unicode"><tt>\p{InGreek}</tt></td>
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   213
 *     <td headers="matches">A character in the Greek&nbsp;block (<a href="#ubc">block</a>)</td></tr>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 * <tr><td valign="top" headers="construct unicode"><tt>\p{Lu}</tt></td>
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   215
 *     <td headers="matches">An uppercase letter (<a href="#ucc">category</a>)</td></tr>
9694
5cf41be509e5 7044849: Constructs for Unicode binary properties should be \p{IsXXX} not p{isXXX}
sherman
parents: 9543
diff changeset
   216
 * <tr><td valign="top" headers="construct unicode"><tt>\p{IsAlphabetic}</tt></td>
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   217
 *     <td headers="matches">An alphabetic character (<a href="#ubpc">binary property</a>)</td></tr>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
 * <tr><td valign="top" headers="construct unicode"><tt>\p{Sc}</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
 *     <td headers="matches">A currency symbol</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
 * <tr><td valign="top" headers="construct unicode"><tt>\P{InGreek}</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
 *     <td headers="matches">Any character except one in the Greek block (negation)</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
 * <tr><td valign="top" headers="construct unicode"><tt>[\p{L}&&[^\p{Lu}]]&nbsp;</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
 *     <td headers="matches">Any letter except an uppercase letter (subtraction)</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
 * <tr><th>&nbsp;</th></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
 * <tr align="left"><th colspan="2" id="bounds">Boundary matchers</th></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
 * <tr><td valign="top" headers="construct bounds"><tt>^</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
 *     <td headers="matches">The beginning of a line</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
 * <tr><td valign="top" headers="construct bounds"><tt>$</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
 *     <td headers="matches">The end of a line</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
 * <tr><td valign="top" headers="construct bounds"><tt>\b</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
 *     <td headers="matches">A word boundary</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
 * <tr><td valign="top" headers="construct bounds"><tt>\B</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
 *     <td headers="matches">A non-word boundary</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
 * <tr><td valign="top" headers="construct bounds"><tt>\A</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
 *     <td headers="matches">The beginning of the input</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
 * <tr><td valign="top" headers="construct bounds"><tt>\G</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
 *     <td headers="matches">The end of the previous match</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
 * <tr><td valign="top" headers="construct bounds"><tt>\Z</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
 *     <td headers="matches">The end of the input but for the final
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
 *         <a href="#lt">terminator</a>, if&nbsp;any</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 * <tr><td valign="top" headers="construct bounds"><tt>\z</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
 *     <td headers="matches">The end of the input</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
 * <tr><th>&nbsp;</th></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
 * <tr align="left"><th colspan="2" id="greedy">Greedy quantifiers</th></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
 * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>?</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
 *     <td headers="matches"><i>X</i>, once or not at all</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
 * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>*</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
 *     <td headers="matches"><i>X</i>, zero or more times</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
 * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>+</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
 *     <td headers="matches"><i>X</i>, one or more times</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
 * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>{</tt><i>n</i><tt>}</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
 *     <td headers="matches"><i>X</i>, exactly <i>n</i> times</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
 * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>{</tt><i>n</i><tt>,}</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
 *     <td headers="matches"><i>X</i>, at least <i>n</i> times</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
 * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>{</tt><i>n</i><tt>,</tt><i>m</i><tt>}</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
 *     <td headers="matches"><i>X</i>, at least <i>n</i> but not more than <i>m</i> times</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
 * <tr><th>&nbsp;</th></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
 * <tr align="left"><th colspan="2" id="reluc">Reluctant quantifiers</th></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
 * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>??</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
 *     <td headers="matches"><i>X</i>, once or not at all</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
 * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>*?</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
 *     <td headers="matches"><i>X</i>, zero or more times</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
 * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>+?</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
 *     <td headers="matches"><i>X</i>, one or more times</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
 * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>{</tt><i>n</i><tt>}?</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
 *     <td headers="matches"><i>X</i>, exactly <i>n</i> times</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
 * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>{</tt><i>n</i><tt>,}?</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
 *     <td headers="matches"><i>X</i>, at least <i>n</i> times</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
 * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>{</tt><i>n</i><tt>,</tt><i>m</i><tt>}?</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
 *     <td headers="matches"><i>X</i>, at least <i>n</i> but not more than <i>m</i> times</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
 * <tr><th>&nbsp;</th></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
 * <tr align="left"><th colspan="2" id="poss">Possessive quantifiers</th></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
 * <tr><td valign="top" headers="construct poss"><i>X</i><tt>?+</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
 *     <td headers="matches"><i>X</i>, once or not at all</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
 * <tr><td valign="top" headers="construct poss"><i>X</i><tt>*+</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
 *     <td headers="matches"><i>X</i>, zero or more times</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
 * <tr><td valign="top" headers="construct poss"><i>X</i><tt>++</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
 *     <td headers="matches"><i>X</i>, one or more times</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
 * <tr><td valign="top" headers="construct poss"><i>X</i><tt>{</tt><i>n</i><tt>}+</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
 *     <td headers="matches"><i>X</i>, exactly <i>n</i> times</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
 * <tr><td valign="top" headers="construct poss"><i>X</i><tt>{</tt><i>n</i><tt>,}+</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
 *     <td headers="matches"><i>X</i>, at least <i>n</i> times</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
 * <tr><td valign="top" headers="construct poss"><i>X</i><tt>{</tt><i>n</i><tt>,</tt><i>m</i><tt>}+</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
 *     <td headers="matches"><i>X</i>, at least <i>n</i> but not more than <i>m</i> times</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
 * <tr><th>&nbsp;</th></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
 * <tr align="left"><th colspan="2" id="logical">Logical operators</th></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
 * <tr><td valign="top" headers="construct logical"><i>XY</i></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
 *     <td headers="matches"><i>X</i> followed by <i>Y</i></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
 * <tr><td valign="top" headers="construct logical"><i>X</i><tt>|</tt><i>Y</i></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
 *     <td headers="matches">Either <i>X</i> or <i>Y</i></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
 * <tr><td valign="top" headers="construct logical"><tt>(</tt><i>X</i><tt>)</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
 *     <td headers="matches">X, as a <a href="#cg">capturing group</a></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
 * <tr><th>&nbsp;</th></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
 * <tr align="left"><th colspan="2" id="backref">Back references</th></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
 * <tr><td valign="bottom" headers="construct backref"><tt>\</tt><i>n</i></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
 *     <td valign="bottom" headers="matches">Whatever the <i>n</i><sup>th</sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
 *     <a href="#cg">capturing group</a> matched</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
 *
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   311
 * <tr><td valign="bottom" headers="construct backref"><tt>\</tt><i>k</i>&lt;<i>name</i>&gt;</td>
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   312
 *     <td valign="bottom" headers="matches">Whatever the
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   313
 *     <a href="#groupname">named-capturing group</a> "name" matched</td></tr>
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   314
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
 * <tr><th>&nbsp;</th></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
 * <tr align="left"><th colspan="2" id="quot">Quotation</th></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
 * <tr><td valign="top" headers="construct quot"><tt>\</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
 *     <td headers="matches">Nothing, but quotes the following character</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
 * <tr><td valign="top" headers="construct quot"><tt>\Q</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
 *     <td headers="matches">Nothing, but quotes all characters until <tt>\E</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
 * <tr><td valign="top" headers="construct quot"><tt>\E</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
 *     <td headers="matches">Nothing, but ends quoting started by <tt>\Q</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
 *     <!-- Metachars: !$()*+.<>?[\]^{|} -->
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
 * <tr><th>&nbsp;</th></tr>
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   327
 * <tr align="left"><th colspan="2" id="special">Special constructs (named-capturing and non-capturing)</th></tr>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
 *
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   329
 * <tr><td valign="top" headers="construct special"><tt>(?&lt;<a href="#groupname">name</a>&gt;</tt><i>X</i><tt>)</tt></td>
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   330
 *     <td headers="matches"><i>X</i>, as a named-capturing group</td></tr>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
 * <tr><td valign="top" headers="construct special"><tt>(?:</tt><i>X</i><tt>)</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
 *     <td headers="matches"><i>X</i>, as a non-capturing group</td></tr>
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   333
 * <tr><td valign="top" headers="construct special"><tt>(?idmsuxU-idmsuxU)&nbsp;</tt></td>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
 *     <td headers="matches">Nothing, but turns match flags <a href="#CASE_INSENSITIVE">i</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
 * <a href="#UNIX_LINES">d</a> <a href="#MULTILINE">m</a> <a href="#DOTALL">s</a>
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   336
 * <a href="#UNICODE_CASE">u</a> <a href="#COMMENTS">x</a> <a href="#UNICODE_CHARACTER_CLASS">U</a>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   337
 * on - off</td></tr>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
 * <tr><td valign="top" headers="construct special"><tt>(?idmsux-idmsux:</tt><i>X</i><tt>)</tt>&nbsp;&nbsp;</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
 *     <td headers="matches"><i>X</i>, as a <a href="#cg">non-capturing group</a> with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
 *         given flags <a href="#CASE_INSENSITIVE">i</a> <a href="#UNIX_LINES">d</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
 * <a href="#MULTILINE">m</a> <a href="#DOTALL">s</a> <a href="#UNICODE_CASE">u</a >
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
 * <a href="#COMMENTS">x</a> on - off</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
 * <tr><td valign="top" headers="construct special"><tt>(?=</tt><i>X</i><tt>)</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
 *     <td headers="matches"><i>X</i>, via zero-width positive lookahead</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
 * <tr><td valign="top" headers="construct special"><tt>(?!</tt><i>X</i><tt>)</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
 *     <td headers="matches"><i>X</i>, via zero-width negative lookahead</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
 * <tr><td valign="top" headers="construct special"><tt>(?&lt;=</tt><i>X</i><tt>)</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
 *     <td headers="matches"><i>X</i>, via zero-width positive lookbehind</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
 * <tr><td valign="top" headers="construct special"><tt>(?&lt;!</tt><i>X</i><tt>)</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
 *     <td headers="matches"><i>X</i>, via zero-width negative lookbehind</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
 * <tr><td valign="top" headers="construct special"><tt>(?&gt;</tt><i>X</i><tt>)</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
 *     <td headers="matches"><i>X</i>, as an independent, non-capturing group</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
 * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
 * <hr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
 * <a name="bs">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
 * <h4> Backslashes, escapes, and quoting </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
 * <p> The backslash character (<tt>'\'</tt>) serves to introduce escaped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
 * constructs, as defined in the table above, as well as to quote characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
 * that otherwise would be interpreted as unescaped constructs.  Thus the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
 * expression <tt>\\</tt> matches a single backslash and <tt>\{</tt> matches a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
 * left brace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
 * <p> It is an error to use a backslash prior to any alphabetic character that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
 * does not denote an escaped construct; these are reserved for future
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
 * extensions to the regular-expression language.  A backslash may be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
 * prior to a non-alphabetic character regardless of whether that character is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
 * part of an unescaped construct.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
 * <p> Backslashes within string literals in Java source code are interpreted
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8173
diff changeset
   375
 * as required by
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8173
diff changeset
   376
 * <cite>The Java&trade; Language Specification</cite>
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8173
diff changeset
   377
 * as either Unicode escapes (section 3.3) or other character escapes (section 3.10.6)
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8173
diff changeset
   378
 * It is therefore necessary to double backslashes in string
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
 * literals that represent regular expressions to protect them from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
 * interpretation by the Java bytecode compiler.  The string literal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
 * <tt>"&#92;b"</tt>, for example, matches a single backspace character when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
 * interpreted as a regular expression, while <tt>"&#92;&#92;b"</tt> matches a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
 * word boundary.  The string literal <tt>"&#92;(hello&#92;)"</tt> is illegal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
 * and leads to a compile-time error; in order to match the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
 * <tt>(hello)</tt> the string literal <tt>"&#92;&#92;(hello&#92;&#92;)"</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
 * must be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
 * <a name="cc">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
 * <h4> Character Classes </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
 *    <p> Character classes may appear within other character classes, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
 *    may be composed by the union operator (implicit) and the intersection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
 *    operator (<tt>&amp;&amp;</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
 *    The union operator denotes a class that contains every character that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
 *    in at least one of its operand classes.  The intersection operator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
 *    denotes a class that contains every character that is in both of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
 *    operand classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
 *    <p> The precedence of character-class operators is as follows, from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
 *    highest to lowest:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
 *    <blockquote><table border="0" cellpadding="1" cellspacing="0"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
 *                 summary="Precedence of character class operators.">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
 *      <tr><th>1&nbsp;&nbsp;&nbsp;&nbsp;</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
 *        <td>Literal escape&nbsp;&nbsp;&nbsp;&nbsp;</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
 *        <td><tt>\x</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
 *     <tr><th>2&nbsp;&nbsp;&nbsp;&nbsp;</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
 *        <td>Grouping</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
 *        <td><tt>[...]</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
 *     <tr><th>3&nbsp;&nbsp;&nbsp;&nbsp;</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
 *        <td>Range</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
 *        <td><tt>a-z</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
 *      <tr><th>4&nbsp;&nbsp;&nbsp;&nbsp;</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
 *        <td>Union</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
 *        <td><tt>[a-e][i-u]</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
 *      <tr><th>5&nbsp;&nbsp;&nbsp;&nbsp;</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
 *        <td>Intersection</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
 *        <td><tt>[a-z&&[aeiou]]</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
 *    </table></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
 *    <p> Note that a different set of metacharacters are in effect inside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
 *    a character class than outside a character class. For instance, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
 *    regular expression <tt>.</tt> loses its special meaning inside a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
 *    character class, while the expression <tt>-</tt> becomes a range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
 *    forming metacharacter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
 * <a name="lt">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
 * <h4> Line terminators </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
 * <p> A <i>line terminator</i> is a one- or two-character sequence that marks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
 * the end of a line of the input character sequence.  The following are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
 * recognized as line terminators:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
 *   <li> A newline (line feed) character&nbsp;(<tt>'\n'</tt>),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
 *   <li> A carriage-return character followed immediately by a newline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
 *   character&nbsp;(<tt>"\r\n"</tt>),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
 *   <li> A standalone carriage-return character&nbsp;(<tt>'\r'</tt>),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
 *   <li> A next-line character&nbsp;(<tt>'&#92;u0085'</tt>),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
 *   <li> A line-separator character&nbsp;(<tt>'&#92;u2028'</tt>), or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
 *   <li> A paragraph-separator character&nbsp;(<tt>'&#92;u2029</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
 * <p>If {@link #UNIX_LINES} mode is activated, then the only line terminators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
 * recognized are newline characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
 * <p> The regular expression <tt>.</tt> matches any character except a line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
 * terminator unless the {@link #DOTALL} flag is specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
 * <p> By default, the regular expressions <tt>^</tt> and <tt>$</tt> ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
 * line terminators and only match at the beginning and the end, respectively,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
 * of the entire input sequence. If {@link #MULTILINE} mode is activated then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
 * <tt>^</tt> matches at the beginning of input and after any line terminator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
 * except at the end of input. When in {@link #MULTILINE} mode <tt>$</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
 * matches just before a line terminator or the end of the input sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
 * <a name="cg">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
 * <h4> Groups and capturing </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
 *
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   466
 * <a name="gnumber">
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   467
 * <h5> Group number </h5>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
 * <p> Capturing groups are numbered by counting their opening parentheses from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
 * left to right.  In the expression <tt>((A)(B(C)))</tt>, for example, there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
 * are four such groups: </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
 * <blockquote><table cellpadding=1 cellspacing=0 summary="Capturing group numberings">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
 * <tr><th>1&nbsp;&nbsp;&nbsp;&nbsp;</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
 *     <td><tt>((A)(B(C)))</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
 * <tr><th>2&nbsp;&nbsp;&nbsp;&nbsp;</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
 *     <td><tt>(A)</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
 * <tr><th>3&nbsp;&nbsp;&nbsp;&nbsp;</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
 *     <td><tt>(B(C))</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
 * <tr><th>4&nbsp;&nbsp;&nbsp;&nbsp;</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
 *     <td><tt>(C)</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
 * </table></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
 * <p> Group zero always stands for the entire expression.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
 * <p> Capturing groups are so named because, during a match, each subsequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
 * of the input sequence that matches such a group is saved.  The captured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
 * subsequence may be used later in the expression, via a back reference, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
 * may also be retrieved from the matcher once the match operation is complete.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
 *
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   490
 * <a name="groupname">
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   491
 * <h5> Group name </h5>
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   492
 * <p>A capturing group can also be assigned a "name", a <tt>named-capturing group</tt>,
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   493
 * and then be back-referenced later by the "name". Group names are composed of
4161
679d00486dc6 6878475: Better syntax for the named capture group in regex
sherman
parents: 2291
diff changeset
   494
 * the following characters. The first character must be a <tt>letter</tt>.
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   495
 *
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   496
 * <ul>
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   497
 *   <li> The uppercase letters <tt>'A'</tt> through <tt>'Z'</tt>
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   498
 *        (<tt>'&#92;u0041'</tt>&nbsp;through&nbsp;<tt>'&#92;u005a'</tt>),
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   499
 *   <li> The lowercase letters <tt>'a'</tt> through <tt>'z'</tt>
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   500
 *        (<tt>'&#92;u0061'</tt>&nbsp;through&nbsp;<tt>'&#92;u007a'</tt>),
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   501
 *   <li> The digits <tt>'0'</tt> through <tt>'9'</tt>
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   502
 *        (<tt>'&#92;u0030'</tt>&nbsp;through&nbsp;<tt>'&#92;u0039'</tt>),
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   503
 * </ul>
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   504
 *
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   505
 * <p> A <tt>named-capturing group</tt> is still numbered as described in
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   506
 * <a href="#gnumber">Group number</a>.
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   507
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
 * <p> The captured input associated with a group is always the subsequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
 * that the group most recently matched.  If a group is evaluated a second time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
 * because of quantification then its previously-captured value, if any, will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
 * be retained if the second evaluation fails.  Matching the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
 * <tt>"aba"</tt> against the expression <tt>(a(b)?)+</tt>, for example, leaves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
 * group two set to <tt>"b"</tt>.  All captured input is discarded at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
 * beginning of each match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
 *
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   516
 * <p> Groups beginning with <tt>(?</tt> are either pure, <i>non-capturing</i> groups
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   517
 * that do not capture text and do not count towards the group total, or
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   518
 * <i>named-capturing</i> group.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
 * <h4> Unicode support </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
 * <p> This class is in conformance with Level 1 of <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
 * href="http://www.unicode.org/reports/tr18/"><i>Unicode Technical
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   524
 * Standard #18: Unicode Regular Expression</i></a>, plus RL2.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
 * Canonical Equivalents.
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   526
 * <p>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   527
 * <b>Unicode escape sequences</b> such as <tt>&#92;u2014</tt> in Java source code
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8173
diff changeset
   528
 * are processed as described in section 3.3 of
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8173
diff changeset
   529
 * <cite>The Java&trade; Language Specification</cite>.
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   530
 * Such escape sequences are also implemented directly by the regular-expression
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   531
 * parser so that Unicode escapes can be used in expressions that are read from
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   532
 * files or from the keyboard.  Thus the strings <tt>"&#92;u2014"</tt> and
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   533
 * <tt>"\\u2014"</tt>, while not equal, compile into the same pattern, which
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   534
 * matches the character with hexadecimal value <tt>0x2014</tt>.
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   535
 * <p>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   536
 * A Unicode character can also be represented in a regular-expression by
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   537
 * using its <b>Hex notation</b>(hexadecimal code point value) directly as described in construct
8173
a3a39b98e05a 7014645: Support perl style Unicode hex notation \x{...}
sherman
parents: 7816
diff changeset
   538
 * <tt>&#92;x{...}</tt>, for example a supplementary character U+2011F
a3a39b98e05a 7014645: Support perl style Unicode hex notation \x{...}
sherman
parents: 7816
diff changeset
   539
 * can be specified as <tt>&#92;x{2011F}</tt>, instead of two consecutive
a3a39b98e05a 7014645: Support perl style Unicode hex notation \x{...}
sherman
parents: 7816
diff changeset
   540
 * Unicode escape sequences of the surrogate pair
a3a39b98e05a 7014645: Support perl style Unicode hex notation \x{...}
sherman
parents: 7816
diff changeset
   541
 * <tt>&#92;uD840</tt><tt>&#92;uDD1F</tt>.
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   542
 * <p>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   543
 * Unicode scripts, blocks, categories and binary properties are written with
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   544
 * the <tt>\p</tt> and <tt>\P</tt> constructs as in Perl.
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   545
 * <tt>\p{</tt><i>prop</i><tt>}</tt> matches if
5610
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
   546
 * the input has the property <i>prop</i>, while <tt>\P{</tt><i>prop</i><tt>}</tt>
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
   547
 * does not match if the input has that property.
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
   548
 * <p>
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   549
 * Scripts, blocks, categories and binary properties can be used both inside
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   550
 * and outside of a character class.
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   551
 * <a name="usc">
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   552
 * <p>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   553
 * <b>Scripts</b> are specified either with the prefix {@code Is}, as in
5610
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
   554
 * {@code IsHiragana}, or by using  the {@code script} keyword (or its short
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
   555
 * form {@code sc})as in {@code script=Hiragana} or {@code sc=Hiragana}.
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
   556
 * <p>
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   557
 * The script names supported by <code>Pattern</code> are the valid script names
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   558
 * accepted and defined by
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   559
 * {@link java.lang.Character.UnicodeScript#forName(String) UnicodeScript.forName}.
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   560
 * <a name="ubc">
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   561
 * <p>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   562
 * <b>Blocks</b> are specified with the prefix {@code In}, as in
5610
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
   563
 * {@code InMongolian}, or by using the keyword {@code block} (or its short
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
   564
 * form {@code blk}) as in {@code block=Mongolian} or {@code blk=Mongolian}.
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
   565
 * <p>
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   566
 * The block names supported by <code>Pattern</code> are the valid block names
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   567
 * accepted and defined by
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   568
 * {@link java.lang.Character.UnicodeBlock#forName(String) UnicodeBlock.forName}.
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   569
 * <p>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   570
 * <a name="ucc">
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   571
 * <b>Categories</b> may be specified with the optional prefix {@code Is}:
5610
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
   572
 * Both {@code \p{L}} and {@code \p{IsL}} denote the category of Unicode
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
   573
 * letters. Same as scripts and blocks, categories can also be specified
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
   574
 * by using the keyword {@code general_category} (or its short form
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
   575
 * {@code gc}) as in {@code general_category=Lu} or {@code gc=Lu}.
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
   576
 * <p>
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   577
 * The supported categories are those of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
 * <a href="http://www.unicode.org/unicode/standard/standard.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
 * <i>The Unicode Standard</i></a> in the version specified by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
 * {@link java.lang.Character Character} class. The category names are those
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
 * defined in the Standard, both normative and informative.
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   582
 * <p>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   583
 * <a name="ubpc">
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   584
 * <b>Binary properties</b> are specified with the prefix {@code Is}, as in
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   585
 * {@code IsAlphabetic}. The supported binary properties by <code>Pattern</code>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   586
 * are
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   587
 * <ul>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   588
 *   <li> Alphabetic
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   589
 *   <li> Ideographic
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   590
 *   <li> Letter
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   591
 *   <li> Lowercase
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   592
 *   <li> Uppercase
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   593
 *   <li> Titlecase
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   594
 *   <li> Punctuation
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   595
 *   <Li> Control
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   596
 *   <li> White_Space
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   597
 *   <li> Digit
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   598
 *   <li> Hex_Digit
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   599
 *   <li> Noncharacter_Code_Point
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   600
 *   <li> Assigned
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   601
 * </ul>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   602
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   603
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   604
 * <p>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   605
 * <b>Predefined Character classes</b> and <b>POSIX character classes</b> are in
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   606
 * conformance with the recommendation of <i>Annex C: Compatibility Properties</i>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   607
 * of <a href="http://www.unicode.org/reports/tr18/"><i>Unicode Regular Expression
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   608
 * </i></a>, when {@link #UNICODE_CHARACTER_CLASS} flag is specified.
5610
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
   609
 * <p>
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   610
 * <table border="0" cellpadding="1" cellspacing="0"
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   611
 *  summary="predefined and posix character classes in Unicode mode">
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   612
 * <tr align="left">
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   613
 * <th bgcolor="#CCCCFF" align="left" id="classes">Classes</th>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   614
 * <th bgcolor="#CCCCFF" align="left" id="matches">Matches</th>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   615
 *</tr>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   616
 * <tr><td><tt>\p{Lower}</tt></td>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   617
 *     <td>A lowercase character:<tt>\p{IsLowercase}</tt></td></tr>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   618
 * <tr><td><tt>\p{Upper}</tt></td>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   619
 *     <td>An uppercase character:<tt>\p{IsUppercase}</tt></td></tr>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   620
 * <tr><td><tt>\p{ASCII}</tt></td>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   621
 *     <td>All ASCII:<tt>[\x00-\x7F]</tt></td></tr>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   622
 * <tr><td><tt>\p{Alpha}</tt></td>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   623
 *     <td>An alphabetic character:<tt>\p{IsAlphabetic}</tt></td></tr>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   624
 * <tr><td><tt>\p{Digit}</tt></td>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   625
 *     <td>A decimal digit character:<tt>p{IsDigit}</tt></td></tr>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   626
 * <tr><td><tt>\p{Alnum}</tt></td>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   627
 *     <td>An alphanumeric character:<tt>[\p{IsAlphabetic}\p{IsDigit}]</tt></td></tr>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   628
 * <tr><td><tt>\p{Punct}</tt></td>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   629
 *     <td>A punctuation character:<tt>p{IsPunctuation}</tt></td></tr>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   630
 * <tr><td><tt>\p{Graph}</tt></td>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   631
 *     <td>A visible character: <tt>[^\p{IsWhite_Space}\p{gc=Cc}\p{gc=Cs}\p{gc=Cn}]</tt></td></tr>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   632
 * <tr><td><tt>\p{Print}</tt></td>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   633
 *     <td>A printable character: <tt>[\p{Graph}\p{Blank}&&[^\p{Cntrl}]]</tt></td></tr>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   634
 * <tr><td><tt>\p{Blank}</tt></td>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   635
 *     <td>A space or a tab: <tt>[\p{IsWhite_Space}&&[^\p{gc=Zl}\p{gc=Zp}\x0a\x0b\x0c\x0d\x85]]</tt></td></tr>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   636
 * <tr><td><tt>\p{Cntrl}</tt></td>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   637
 *     <td>A control character: <tt>\p{gc=Cc}</tt></td></tr>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   638
 * <tr><td><tt>\p{XDigit}</tt></td>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   639
 *     <td>A hexadecimal digit: <tt>[\p{gc=Nd}\p{IsHex_Digit}]</tt></td></tr>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   640
 * <tr><td><tt>\p{Space}</tt></td>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   641
 *     <td>A whitespace character:<tt>\p{IsWhite_Space}</tt></td></tr>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   642
 * <tr><td><tt>\d</tt></td>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   643
 *     <td>A digit: <tt>\p{IsDigit}</tt></td></tr>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   644
 * <tr><td><tt>\D</tt></td>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   645
 *     <td>A non-digit: <tt>[^\d]</tt></td></tr>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   646
 * <tr><td><tt>\s</tt></td>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   647
 *     <td>A whitespace character: <tt>\p{IsWhite_Space}</tt></td></tr>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   648
 * <tr><td><tt>\S</tt></td>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   649
 *     <td>A non-whitespace character: <tt>[^\s]</tt></td></tr>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   650
 * <tr><td><tt>\w</tt></td>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   651
 *     <td>A word character: <tt>[\p{Alpha}\p{gc=Mn}\p{gc=Me}\p{gc=Mc}\p{Digit}\p{gc=Pc}]</tt></td></tr>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   652
 * <tr><td><tt>\W</tt></td>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   653
 *     <td>A non-word character: <tt>[^\w]</tt></td></tr>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   654
 * </table>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   655
 * <p>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   656
 * <a name="jcc">
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   657
 * Categories that behave like the java.lang.Character
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
 * boolean is<i>methodname</i> methods (except for the deprecated ones) are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
 * available through the same <tt>\p{</tt><i>prop</i><tt>}</tt> syntax where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
 * the specified property has the name <tt>java<i>methodname</i></tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
 * <h4> Comparison to Perl 5 </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
 * <p>The <code>Pattern</code> engine performs traditional NFA-based matching
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
 * with ordered alternation as occurs in Perl 5.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
 * <p> Perl constructs not supported by this class: </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
 * <ul>
9543
3d4bcfaa8627 7036522: j.u.r.Pattern documentation errors
sherman
parents: 9536
diff changeset
   670
 *    <li><p> Predefined character classes (Unicode character)
3d4bcfaa8627 7036522: j.u.r.Pattern documentation errors
sherman
parents: 9536
diff changeset
   671
 *    <p><tt>\h&nbsp;&nbsp;&nbsp;&nbsp;</tt>A horizontal whitespace
3d4bcfaa8627 7036522: j.u.r.Pattern documentation errors
sherman
parents: 9536
diff changeset
   672
 *    <p><tt>\H&nbsp;&nbsp;&nbsp;&nbsp;</tt>A non horizontal whitespace
3d4bcfaa8627 7036522: j.u.r.Pattern documentation errors
sherman
parents: 9536
diff changeset
   673
 *    <p><tt>\v&nbsp;&nbsp;&nbsp;&nbsp;</tt>A vertical whitespace
3d4bcfaa8627 7036522: j.u.r.Pattern documentation errors
sherman
parents: 9536
diff changeset
   674
 *    <p><tt>\V&nbsp;&nbsp;&nbsp;&nbsp;</tt>A non vertical whitespace
3d4bcfaa8627 7036522: j.u.r.Pattern documentation errors
sherman
parents: 9536
diff changeset
   675
 *    <p><tt>\R&nbsp;&nbsp;&nbsp;&nbsp;</tt>Any Unicode linebreak sequence
3d4bcfaa8627 7036522: j.u.r.Pattern documentation errors
sherman
parents: 9536
diff changeset
   676
 *    <tt>\u005cu000D\u005cu000A|[\u005cu000A\u005cu000B\u005cu000C\u005cu000D\u005cu0085\u005cu2028\u005cu2029]</tt>
3d4bcfaa8627 7036522: j.u.r.Pattern documentation errors
sherman
parents: 9536
diff changeset
   677
 *    <p><tt>\X&nbsp;&nbsp;&nbsp;&nbsp;</tt>Match Unicode
3d4bcfaa8627 7036522: j.u.r.Pattern documentation errors
sherman
parents: 9536
diff changeset
   678
 *    <a href="http://www.unicode.org/reports/tr18/#Default_Grapheme_Clusters">
3d4bcfaa8627 7036522: j.u.r.Pattern documentation errors
sherman
parents: 9536
diff changeset
   679
 *    <i>extended grapheme cluster</i></a>
3d4bcfaa8627 7036522: j.u.r.Pattern documentation errors
sherman
parents: 9536
diff changeset
   680
 *    </p></li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
 *
9543
3d4bcfaa8627 7036522: j.u.r.Pattern documentation errors
sherman
parents: 9536
diff changeset
   682
 *    <li><p> The backreference constructs, <tt>\g{</tt><i>n</i><tt>}</tt> for
3d4bcfaa8627 7036522: j.u.r.Pattern documentation errors
sherman
parents: 9536
diff changeset
   683
 *    the <i>n</i><sup>th</sup><a href="#cg">capturing group</a> and
3d4bcfaa8627 7036522: j.u.r.Pattern documentation errors
sherman
parents: 9536
diff changeset
   684
 *    <tt>\g{</tt><i>name</i><tt>}</tt> for
3d4bcfaa8627 7036522: j.u.r.Pattern documentation errors
sherman
parents: 9536
diff changeset
   685
 *    <a href="#groupname">named-capturing group</a>.
3d4bcfaa8627 7036522: j.u.r.Pattern documentation errors
sherman
parents: 9536
diff changeset
   686
 *    </p></li>
3d4bcfaa8627 7036522: j.u.r.Pattern documentation errors
sherman
parents: 9536
diff changeset
   687
 *
3d4bcfaa8627 7036522: j.u.r.Pattern documentation errors
sherman
parents: 9536
diff changeset
   688
 *    <li><p> The named character construct, <tt>\N{</tt><i>name</i><tt>}</tt>
3d4bcfaa8627 7036522: j.u.r.Pattern documentation errors
sherman
parents: 9536
diff changeset
   689
 *    for a Unicode character by its name.
3d4bcfaa8627 7036522: j.u.r.Pattern documentation errors
sherman
parents: 9536
diff changeset
   690
 *    </p></li>
3d4bcfaa8627 7036522: j.u.r.Pattern documentation errors
sherman
parents: 9536
diff changeset
   691
 *
3d4bcfaa8627 7036522: j.u.r.Pattern documentation errors
sherman
parents: 9536
diff changeset
   692
 *    <li><p> The conditional constructs
3d4bcfaa8627 7036522: j.u.r.Pattern documentation errors
sherman
parents: 9536
diff changeset
   693
 *    <tt>(?(</tt><i>condition</i><tt>)</tt><i>X</i><tt>)</tt> and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
 *    <tt>(?(</tt><i>condition</i><tt>)</tt><i>X</i><tt>|</tt><i>Y</i><tt>)</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
 *    </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
 *    <li><p> The embedded code constructs <tt>(?{</tt><i>code</i><tt>})</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
 *    and <tt>(??{</tt><i>code</i><tt>})</tt>,</p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
 *    <li><p> The embedded comment syntax <tt>(?#comment)</tt>, and </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
 *    <li><p> The preprocessing operations <tt>\l</tt> <tt>&#92;u</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
 *    <tt>\L</tt>, and <tt>\U</tt>.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
 * <p> Constructs supported by this class but not by Perl: </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
 *    <li><p> Character-class union and intersection as described
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
 *    <a href="#cc">above</a>.</p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
 * <p> Notable differences from Perl: </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
 *    <li><p> In Perl, <tt>\1</tt> through <tt>\9</tt> are always interpreted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
 *    as back references; a backslash-escaped number greater than <tt>9</tt> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
 *    treated as a back reference if at least that many subexpressions exist,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
 *    otherwise it is interpreted, if possible, as an octal escape.  In this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
 *    class octal escapes must always begin with a zero. In this class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
 *    <tt>\1</tt> through <tt>\9</tt> are always interpreted as back
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
 *    references, and a larger number is accepted as a back reference if at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
 *    least that many subexpressions exist at that point in the regular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
 *    expression, otherwise the parser will drop digits until the number is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
 *    smaller or equal to the existing number of groups or it is one digit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
 *    </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
 *    <li><p> Perl uses the <tt>g</tt> flag to request a match that resumes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
 *    where the last match left off.  This functionality is provided implicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
 *    by the {@link Matcher} class: Repeated invocations of the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
 *    Matcher#find find} method will resume where the last match left off,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
 *    unless the matcher is reset.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
 *    <li><p> In Perl, embedded flags at the top level of an expression affect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
 *    the whole expression.  In this class, embedded flags always take effect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
 *    at the point at which they appear, whether they are at the top level or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
 *    within a group; in the latter case, flags are restored at the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
 *    group just as in Perl.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
 * <p> For a more precise description of the behavior of regular expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
 * constructs, please see <a href="http://www.oreilly.com/catalog/regex3/">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
 * <i>Mastering Regular Expressions, 3nd Edition</i>, Jeffrey E. F. Friedl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
 * O'Reilly and Associates, 2006.</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
 * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
 * @see java.lang.String#split(String, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
 * @see java.lang.String#split(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
 * @author      Mike McCloskey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
 * @author      Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
 * @author      JSR-51 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
 * @spec        JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
public final class Pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    implements java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * Regular expression modifier values.  Instead of being passed as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * arguments, they can also be passed as inline modifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * For example, the following statements have the same effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * RegExp r1 = RegExp.compile("abc", Pattern.I|Pattern.M);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * RegExp r2 = RegExp.compile("(?im)abc", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * The flags are duplicated so that the familiar Perl match flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * names are available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * Enables Unix lines mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * <p> In this mode, only the <tt>'\n'</tt> line terminator is recognized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * in the behavior of <tt>.</tt>, <tt>^</tt>, and <tt>$</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * <p> Unix lines mode can also be enabled via the embedded flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * expression&nbsp;<tt>(?d)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    public static final int UNIX_LINES = 0x01;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * Enables case-insensitive matching.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * <p> By default, case-insensitive matching assumes that only characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * in the US-ASCII charset are being matched.  Unicode-aware
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * case-insensitive matching can be enabled by specifying the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * #UNICODE_CASE} flag in conjunction with this flag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * <p> Case-insensitive matching can also be enabled via the embedded flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * expression&nbsp;<tt>(?i)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * <p> Specifying this flag may impose a slight performance penalty.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    public static final int CASE_INSENSITIVE = 0x02;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * Permits whitespace and comments in pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * <p> In this mode, whitespace is ignored, and embedded comments starting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * with <tt>#</tt> are ignored until the end of a line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * <p> Comments mode can also be enabled via the embedded flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * expression&nbsp;<tt>(?x)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    public static final int COMMENTS = 0x04;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * Enables multiline mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * <p> In multiline mode the expressions <tt>^</tt> and <tt>$</tt> match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * just after or just before, respectively, a line terminator or the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * the input sequence.  By default these expressions only match at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * beginning and the end of the entire input sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * <p> Multiline mode can also be enabled via the embedded flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * expression&nbsp;<tt>(?m)</tt>.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    public static final int MULTILINE = 0x08;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * Enables literal parsing of the pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * <p> When this flag is specified then the input string that specifies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * the pattern is treated as a sequence of literal characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * Metacharacters or escape sequences in the input sequence will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * given no special meaning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * <p>The flags CASE_INSENSITIVE and UNICODE_CASE retain their impact on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * matching when used in conjunction with this flag. The other flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * become superfluous.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * <p> There is no embedded flag character for enabling literal parsing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    public static final int LITERAL = 0x10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * Enables dotall mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * <p> In dotall mode, the expression <tt>.</tt> matches any character,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * including a line terminator.  By default this expression does not match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * line terminators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * <p> Dotall mode can also be enabled via the embedded flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * expression&nbsp;<tt>(?s)</tt>.  (The <tt>s</tt> is a mnemonic for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * "single-line" mode, which is what this is called in Perl.)  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    public static final int DOTALL = 0x20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * Enables Unicode-aware case folding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * <p> When this flag is specified then case-insensitive matching, when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * enabled by the {@link #CASE_INSENSITIVE} flag, is done in a manner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * consistent with the Unicode Standard.  By default, case-insensitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * matching assumes that only characters in the US-ASCII charset are being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * matched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * <p> Unicode-aware case folding can also be enabled via the embedded flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * expression&nbsp;<tt>(?u)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * <p> Specifying this flag may impose a performance penalty.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    public static final int UNICODE_CASE = 0x40;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * Enables canonical equivalence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * <p> When this flag is specified then two characters will be considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * to match if, and only if, their full canonical decompositions match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * The expression <tt>"a&#92;u030A"</tt>, for example, will match the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * string <tt>"&#92;u00E5"</tt> when this flag is specified.  By default,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * matching does not take canonical equivalence into account.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * <p> There is no embedded flag character for enabling canonical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * equivalence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * <p> Specifying this flag may impose a performance penalty.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    public static final int CANON_EQ = 0x80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   892
    /**
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   893
     * Enables the Unicode version of <i>Predefined character classes</i> and
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   894
     * <i>POSIX character classes</i>.
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   895
     *
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   896
     * <p> When this flag is specified then the (US-ASCII only)
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   897
     * <i>Predefined character classes</i> and <i>POSIX character classes</i>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   898
     * are in conformance with
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   899
     * <a href="http://www.unicode.org/reports/tr18/"><i>Unicode Technical
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   900
     * Standard #18: Unicode Regular Expression</i></a>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   901
     * <i>Annex C: Compatibility Properties</i>.
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   902
     * <p>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   903
     * The UNICODE_CHARACTER_CLASS mode can also be enabled via the embedded
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   904
     * flag expression&nbsp;<tt>(?U)</tt>.
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   905
     * <p>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   906
     * The flag implies UNICODE_CASE, that is, it enables Unicode-aware case
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   907
     * folding.
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   908
     * <p>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   909
     * Specifying this flag may impose a performance penalty.  </p>
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   910
     * @since 1.7
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   911
     */
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   912
    public static final int UNICODE_CHARACTER_CLASS = 0x100;
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
   913
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    /* Pattern has only two serialized components: The pattern string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * and the flags, which are all that is needed to recompile the pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * when it is deserialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    /** use serialVersionUID from Merlin b59 for interoperability */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
    private static final long serialVersionUID = 5073258162644648461L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * The original regular-expression pattern string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    private String pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * The original pattern flags.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
    private int flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * Boolean indicating this Pattern is compiled; this is necessary in order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * to lazily compile deserialized Patterns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
    private transient volatile boolean compiled = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * The normalized pattern string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
    private transient String normalizedPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * The starting point of state machine for the find operation.  This allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * a match to start anywhere in the input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    transient Node root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * The root of object tree for a match operation.  The pattern is matched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * at the beginning.  This may include a find that uses BnM or a First
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
    transient Node matchRoot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * Temporary storage used by parsing pattern slice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    transient int[] buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    /**
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   966
     * Map the "name" of the "named capturing group" to its group id
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   967
     * node.
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   968
     */
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   969
    transient volatile Map<String, Integer> namedGroups;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   970
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   971
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * Temporary storage used while parsing group references.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
    transient GroupHead[] groupNodes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * Temporary null terminated code point array used by pattern compiling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
    private transient int[] temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * The number of capturing groups in this Pattern. Used by matchers to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * allocate storage needed to perform a match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    transient int capturingGroupCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * The local variable count used by parsing tree. Used by matchers to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * allocate storage needed to perform a match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    transient int localCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * Index into the pattern string that keeps track of how much has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     * parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    private transient int cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * Holds the length of the pattern string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
    private transient int patternLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
    /**
4820
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  1005
     * If the Start node might possibly match supplementary characters.
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  1006
     * It is set to true during compiling if
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  1007
     * (1) There is supplementary char in pattern, or
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  1008
     * (2) There is complement node of Category or Block
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  1009
     */
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  1010
    private transient boolean hasSupplementary;
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  1011
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  1012
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * Compiles the given regular expression into a pattern.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * @param  regex
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     *         The expression to be compiled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * @throws  PatternSyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     *          If the expression's syntax is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
    public static Pattern compile(String regex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        return new Pattern(regex, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * Compiles the given regular expression into a pattern with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * flags.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * @param  regex
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     *         The expression to be compiled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * @param  flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     *         Match flags, a bit mask that may include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     *         {@link #CASE_INSENSITIVE}, {@link #MULTILINE}, {@link #DOTALL},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     *         {@link #UNICODE_CASE}, {@link #CANON_EQ}, {@link #UNIX_LINES},
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  1036
     *         {@link #LITERAL}, {@link #UNICODE_CHARACTER_CLASS}
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  1037
     *         and {@link #COMMENTS}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     *          If bit values other than those corresponding to the defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     *          match flags are set in <tt>flags</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     * @throws  PatternSyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     *          If the expression's syntax is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
    public static Pattern compile(String regex, int flags) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        return new Pattern(regex, flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * Returns the regular expression from which this pattern was compiled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * @return  The source of this pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
    public String pattern() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        return pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * <p>Returns the string representation of this pattern. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * is the regular expression from which this pattern was
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * compiled.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * @return  The string representation of this pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        return pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     * Creates a matcher that will match the given input against this pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * @param  input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     *         The character sequence to be matched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * @return  A new matcher for this pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
    public Matcher matcher(CharSequence input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        if (!compiled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                if (!compiled)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                    compile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        Matcher m = new Matcher(this, input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        return m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * Returns this pattern's match flags.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * @return  The match flags specified when this pattern was compiled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
    public int flags() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        return flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * Compiles the given regular expression and attempts to match the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     * input against it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * <p> An invocation of this convenience method of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     * Pattern.matches(regex, input);</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * behaves in exactly the same way as the expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     * Pattern.compile(regex).matcher(input).matches()</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     * <p> If a pattern is to be used multiple times, compiling it once and reusing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     * it will be more efficient than invoking this method each time.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     * @param  regex
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     *         The expression to be compiled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     * @param  input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     *         The character sequence to be matched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * @throws  PatternSyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     *          If the expression's syntax is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
    public static boolean matches(String regex, CharSequence input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        Pattern p = Pattern.compile(regex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        Matcher m = p.matcher(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        return m.matches();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * Splits the given input sequence around matches of this pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     * <p> The array returned by this method contains each substring of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     * input sequence that is terminated by another subsequence that matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * this pattern or is terminated by the end of the input sequence.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     * substrings in the array are in the order in which they occur in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     * input.  If this pattern does not match any subsequence of the input then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     * the resulting array has just one element, namely the input sequence in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     * string form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * <p> The <tt>limit</tt> parameter controls the number of times the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * pattern is applied and therefore affects the length of the resulting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * array.  If the limit <i>n</i> is greater than zero then the pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     * will be applied at most <i>n</i>&nbsp;-&nbsp;1 times, the array's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     * length will be no greater than <i>n</i>, and the array's last entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * will contain all input beyond the last matched delimiter.  If <i>n</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * is non-positive then the pattern will be applied as many times as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * possible and the array can have any length.  If <i>n</i> is zero then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * the pattern will be applied as many times as possible, the array can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     * have any length, and trailing empty strings will be discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * <p> The input <tt>"boo:and:foo"</tt>, for example, yields the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     * results with these parameters:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * <blockquote><table cellpadding=1 cellspacing=0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     *              summary="Split examples showing regex, limit, and result">
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * <tr><th><P align="left"><i>Regex&nbsp;&nbsp;&nbsp;&nbsp;</i></th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     *     <th><P align="left"><i>Limit&nbsp;&nbsp;&nbsp;&nbsp;</i></th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     *     <th><P align="left"><i>Result&nbsp;&nbsp;&nbsp;&nbsp;</i></th></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * <tr><td align=center>:</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     *     <td align=center>2</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     *     <td><tt>{ "boo", "and:foo" }</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * <tr><td align=center>:</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     *     <td align=center>5</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     *     <td><tt>{ "boo", "and", "foo" }</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * <tr><td align=center>:</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     *     <td align=center>-2</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     *     <td><tt>{ "boo", "and", "foo" }</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * <tr><td align=center>o</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     *     <td align=center>5</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     *     <td><tt>{ "b", "", ":and:f", "", "" }</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     * <tr><td align=center>o</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     *     <td align=center>-2</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     *     <td><tt>{ "b", "", ":and:f", "", "" }</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     * <tr><td align=center>o</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     *     <td align=center>0</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     *     <td><tt>{ "b", "", ":and:f" }</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     * </table></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * @param  input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     *         The character sequence to be split
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     * @param  limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     *         The result threshold, as described above
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * @return  The array of strings computed by splitting the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     *          around matches of this pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
    public String[] split(CharSequence input, int limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        boolean matchLimited = limit > 0;
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5627
diff changeset
  1196
        ArrayList<String> matchList = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        Matcher m = matcher(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        // Add segments before each match found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        while(m.find()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
            if (!matchLimited || matchList.size() < limit - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                String match = input.subSequence(index, m.start()).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
                matchList.add(match);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
                index = m.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
            } else if (matchList.size() == limit - 1) { // last one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
                String match = input.subSequence(index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                                                 input.length()).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                matchList.add(match);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                index = m.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        // If no match was found, return this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        if (index == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
            return new String[] {input.toString()};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        // Add remaining segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        if (!matchLimited || matchList.size() < limit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
            matchList.add(input.subSequence(index, input.length()).toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        // Construct result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        int resultSize = matchList.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        if (limit == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
            while (resultSize > 0 && matchList.get(resultSize-1).equals(""))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
                resultSize--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        String[] result = new String[resultSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        return matchList.subList(0, resultSize).toArray(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     * Splits the given input sequence around matches of this pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     * <p> This method works as if by invoking the two-argument {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     * #split(java.lang.CharSequence, int) split} method with the given input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     * sequence and a limit argument of zero.  Trailing empty strings are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     * therefore not included in the resulting array. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     * <p> The input <tt>"boo:and:foo"</tt>, for example, yields the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     * results with these expressions:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     * <blockquote><table cellpadding=1 cellspacing=0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     *              summary="Split examples showing regex and result">
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     * <tr><th><P align="left"><i>Regex&nbsp;&nbsp;&nbsp;&nbsp;</i></th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     *     <th><P align="left"><i>Result</i></th></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     * <tr><td align=center>:</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
     *     <td><tt>{ "boo", "and", "foo" }</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
     * <tr><td align=center>o</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
     *     <td><tt>{ "b", "", ":and:f" }</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     * </table></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     * @param  input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     *         The character sequence to be split
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     * @return  The array of strings computed by splitting the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     *          around matches of this pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
    public String[] split(CharSequence input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        return split(input, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     * Returns a literal pattern <code>String</code> for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * <code>String</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * <p>This method produces a <code>String</code> that can be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     * create a <code>Pattern</code> that would match the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     * <code>s</code> as if it were a literal pattern.</p> Metacharacters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     * or escape sequences in the input sequence will be given no special
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     * meaning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     * @param  s The string to be literalized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     * @return  A literal string replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
    public static String quote(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        int slashEIndex = s.indexOf("\\E");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        if (slashEIndex == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
            return "\\Q" + s + "\\E";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        StringBuilder sb = new StringBuilder(s.length() * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        sb.append("\\Q");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        slashEIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        int current = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
        while ((slashEIndex = s.indexOf("\\E", current)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
            sb.append(s.substring(current, slashEIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
            current = slashEIndex + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
            sb.append("\\E\\\\E\\Q");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
        sb.append(s.substring(current, s.length()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
        sb.append("\\E");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     * Recompile the Pattern instance from a stream.  The original pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     * string is read in and the object tree is recompiled from it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
        // Read in all fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        // Initialize counts
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
        capturingGroupCount = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        localCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        // if length > 0, the Pattern is lazily compiled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        compiled = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
        if (pattern.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            root = new Start(lastAccept);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
            matchRoot = lastAccept;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
            compiled = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     * This private constructor is used to create all Patterns. The pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     * string and match flags are all that is needed to completely describe
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     * a Pattern. An empty pattern string results in an object tree with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     * only a Start node and a LastNode node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
    private Pattern(String p, int f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
        pattern = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
        flags = f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  1328
        // to use UNICODE_CASE if UNICODE_CHARACTER_CLASS present
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  1329
        if ((flags & UNICODE_CHARACTER_CLASS) != 0)
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  1330
            flags |= UNICODE_CASE;
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  1331
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        // Reset group index count
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
        capturingGroupCount = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        localCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        if (pattern.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
            compile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
            root = new Start(lastAccept);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
            matchRoot = lastAccept;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     * The pattern is converted to normalizedD form and then a pure group
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     * is constructed to match canonical equivalences of the characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
    private void normalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
        boolean inCharClass = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
        int lastCodePoint = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        // Convert pattern into normalizedD form
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        normalizedPattern = Normalizer.normalize(pattern, Normalizer.Form.NFD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        patternLength = normalizedPattern.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        // Modify pattern to match canonical equivalences
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
        StringBuilder newPattern = new StringBuilder(patternLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        for(int i=0; i<patternLength; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
            int c = normalizedPattern.codePointAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
            StringBuilder sequenceBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
            if ((Character.getType(c) == Character.NON_SPACING_MARK)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
                && (lastCodePoint != -1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                sequenceBuffer = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                sequenceBuffer.appendCodePoint(lastCodePoint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
                sequenceBuffer.appendCodePoint(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
                while(Character.getType(c) == Character.NON_SPACING_MARK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
                    i += Character.charCount(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                    if (i >= patternLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                    c = normalizedPattern.codePointAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
                    sequenceBuffer.appendCodePoint(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                String ea = produceEquivalentAlternation(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
                                               sequenceBuffer.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                newPattern.setLength(newPattern.length()-Character.charCount(lastCodePoint));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                newPattern.append("(?:").append(ea).append(")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
            } else if (c == '[' && lastCodePoint != '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                i = normalizeCharClass(newPattern, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                newPattern.appendCodePoint(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
            lastCodePoint = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
            i += Character.charCount(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
        normalizedPattern = newPattern.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     * Complete the character class being parsed and add a set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     * of alternations to it that will match the canonical equivalences
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     * of the characters within the class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
    private int normalizeCharClass(StringBuilder newPattern, int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        StringBuilder charClass = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
        StringBuilder eq = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        int lastCodePoint = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        String result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
        charClass.append("[");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
        while(true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
            int c = normalizedPattern.codePointAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
            StringBuilder sequenceBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
            if (c == ']' && lastCodePoint != '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                charClass.append((char)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
            } else if (Character.getType(c) == Character.NON_SPACING_MARK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
                sequenceBuffer = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
                sequenceBuffer.appendCodePoint(lastCodePoint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
                while(Character.getType(c) == Character.NON_SPACING_MARK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
                    sequenceBuffer.appendCodePoint(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
                    i += Character.charCount(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
                    if (i >= normalizedPattern.length())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
                    c = normalizedPattern.codePointAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
                String ea = produceEquivalentAlternation(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
                                                  sequenceBuffer.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
                charClass.setLength(charClass.length()-Character.charCount(lastCodePoint));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
                if (eq == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
                    eq = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
                eq.append('|');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
                eq.append(ea);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
                charClass.appendCodePoint(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
            if (i == normalizedPattern.length())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
                throw error("Unclosed character class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
            lastCodePoint = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
        if (eq != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
            result = "(?:"+charClass.toString()+eq.toString()+")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
            result = charClass.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
        newPattern.append(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
     * Given a specific sequence composed of a regular character and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
     * combining marks that follow it, produce the alternation that will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
     * match all canonical equivalences of that sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
    private String produceEquivalentAlternation(String source) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
        int len = countChars(source, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
        if (source.length() == len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
            // source has one character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
            return source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
        String base = source.substring(0,len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
        String combiningMarks = source.substring(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
        String[] perms = producePermutations(combiningMarks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
        StringBuilder result = new StringBuilder(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
        // Add combined permutations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
        for(int x=0; x<perms.length; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
            String next = base + perms[x];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
            if (x>0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
                result.append("|"+next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
            next = composeOneStep(next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
            if (next != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
                result.append("|"+produceEquivalentAlternation(next));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
        return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
     * Returns an array of strings that have all the possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
     * permutations of the characters in the input string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
     * This is used to get a list of all possible orderings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
     * of a set of combining marks. Note that some of the permutations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
     * are invalid because of combining class collisions, and these
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
     * possibilities must be removed because they are not canonically
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
     * equivalent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
    private String[] producePermutations(String input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
        if (input.length() == countChars(input, 0, 1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
            return new String[] {input};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
        if (input.length() == countChars(input, 0, 2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
            int c0 = Character.codePointAt(input, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
            int c1 = Character.codePointAt(input, Character.charCount(c0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
            if (getClass(c1) == getClass(c0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
                return new String[] {input};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
            String[] result = new String[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
            result[0] = input;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
            StringBuilder sb = new StringBuilder(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
            sb.appendCodePoint(c1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
            sb.appendCodePoint(c0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
            result[1] = sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
        int length = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
        int nCodePoints = countCodePoints(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        for(int x=1; x<nCodePoints; x++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
            length = length * (x+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
        String[] temp = new String[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
        int combClass[] = new int[nCodePoints];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
        for(int x=0, i=0; x<nCodePoints; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
            int c = Character.codePointAt(input, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
            combClass[x] = getClass(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
            i +=  Character.charCount(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
        // For each char, take it out and add the permutations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
        // of the remaining chars
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
        int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
        // offset maintains the index in code units.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
loop:   for(int x=0, offset=0; x<nCodePoints; x++, offset+=len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
            len = countChars(input, offset, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
            boolean skip = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
            for(int y=x-1; y>=0; y--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
                if (combClass[y] == combClass[x]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
                    continue loop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
            StringBuilder sb = new StringBuilder(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
            String otherChars = sb.delete(offset, offset+len).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
            String[] subResult = producePermutations(otherChars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
            String prefix = input.substring(offset, offset+len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
            for(int y=0; y<subResult.length; y++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
                temp[index++] =  prefix + subResult[y];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
        String[] result = new String[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
        for (int x=0; x<index; x++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
            result[x] = temp[x];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
    private int getClass(int c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
        return sun.text.Normalizer.getCombiningClass(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
     * Attempts to compose input by combining the first character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
     * with the first combining mark following it. Returns a String
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
     * that is the composition of the leading character with its first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
     * combining mark followed by the remaining combining marks. Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
     * null if the first two characters cannot be further composed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
    private String composeOneStep(String input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
        int len = countChars(input, 0, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
        String firstTwoCharacters = input.substring(0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
        String result = Normalizer.normalize(firstTwoCharacters, Normalizer.Form.NFC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
        if (result.equals(firstTwoCharacters))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
            String remainder = input.substring(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
            return result + remainder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
     * Preprocess any \Q...\E sequences in `temp', meta-quoting them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
     * See the description of `quotemeta' in perlfunc(1).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
    private void RemoveQEQuoting() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
        final int pLen = patternLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
        while (i < pLen-1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
            if (temp[i] != '\\')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
                i += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
            else if (temp[i + 1] != 'Q')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
                i += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
        if (i >= pLen - 1)    // No \Q sequence found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
        int j = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
        i += 2;
11287
3db172a5433c 6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents: 9694
diff changeset
  1586
        int[] newtemp = new int[j + 3*(pLen-i) + 2];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
        System.arraycopy(temp, 0, newtemp, 0, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
        boolean inQuote = true;
11287
3db172a5433c 6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents: 9694
diff changeset
  1590
        boolean beginQuote = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
        while (i < pLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
            int c = temp[i++];
11287
3db172a5433c 6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents: 9694
diff changeset
  1593
            if (!ASCII.isAscii(c) || ASCII.isAlpha(c)) {
3db172a5433c 6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents: 9694
diff changeset
  1594
                newtemp[j++] = c;
3db172a5433c 6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents: 9694
diff changeset
  1595
            } else if (ASCII.isDigit(c)) {
3db172a5433c 6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents: 9694
diff changeset
  1596
                if (beginQuote) {
3db172a5433c 6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents: 9694
diff changeset
  1597
                    /*
3db172a5433c 6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents: 9694
diff changeset
  1598
                     * A unicode escape \[0xu] could be before this quote,
3db172a5433c 6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents: 9694
diff changeset
  1599
                     * and we don't want this numeric char to processed as
3db172a5433c 6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents: 9694
diff changeset
  1600
                     * part of the escape.
3db172a5433c 6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents: 9694
diff changeset
  1601
                     */
3db172a5433c 6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents: 9694
diff changeset
  1602
                    newtemp[j++] = '\\';
3db172a5433c 6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents: 9694
diff changeset
  1603
                    newtemp[j++] = 'x';
3db172a5433c 6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents: 9694
diff changeset
  1604
                    newtemp[j++] = '3';
3db172a5433c 6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents: 9694
diff changeset
  1605
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
                newtemp[j++] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
            } else if (c != '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
                if (inQuote) newtemp[j++] = '\\';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
                newtemp[j++] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
            } else if (inQuote) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
                if (temp[i] == 'E') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
                    i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
                    inQuote = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
                    newtemp[j++] = '\\';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
                    newtemp[j++] = '\\';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
                if (temp[i] == 'Q') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
                    i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
                    inQuote = true;
11287
3db172a5433c 6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents: 9694
diff changeset
  1622
                    beginQuote = true;
3db172a5433c 6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents: 9694
diff changeset
  1623
                    continue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
                    newtemp[j++] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
                    if (i != pLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
                        newtemp[j++] = temp[i++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
            }
11287
3db172a5433c 6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents: 9694
diff changeset
  1630
3db172a5433c 6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents: 9694
diff changeset
  1631
            beginQuote = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
        patternLength = j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
        temp = Arrays.copyOf(newtemp, j + 2); // double zero termination
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
     * Copies regular expression to an int array and invokes the parsing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
     * of the expression which will create the object tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
    private void compile() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
        // Handle canonical equivalences
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
        if (has(CANON_EQ) && !has(LITERAL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
            normalize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
            normalizedPattern = pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
        patternLength = normalizedPattern.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
        // Copy pattern to int array for convenience
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
        // Use double zero to terminate pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
        temp = new int[patternLength + 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
4820
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  1655
        hasSupplementary = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
        int c, count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
        // Convert all chars into code points
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
        for (int x = 0; x < patternLength; x += Character.charCount(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
            c = normalizedPattern.codePointAt(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
            if (isSupplementary(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
                hasSupplementary = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
            temp[count++] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
        patternLength = count;   // patternLength now in code points
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
        if (! has(LITERAL))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
            RemoveQEQuoting();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
        // Allocate all temporary objects here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
        buffer = new int[32];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
        groupNodes = new GroupHead[10];
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1674
        namedGroups = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
        if (has(LITERAL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
            // Literal pattern handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
            matchRoot = newSlice(temp, patternLength, hasSupplementary);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
            matchRoot.next = lastAccept;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
            // Start recursive descent parsing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
            matchRoot = expr(lastAccept);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
            // Check extra pattern characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
            if (patternLength != cursor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
                if (peek() == ')') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
                    throw error("Unmatched closing ')'");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
                    throw error("Unexpected internal error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
        // Peephole optimization
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
        if (matchRoot instanceof Slice) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
            root = BnM.optimize(matchRoot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
            if (root == matchRoot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
                root = hasSupplementary ? new StartS(matchRoot) : new Start(matchRoot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
        } else if (matchRoot instanceof Begin || matchRoot instanceof First) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
            root = matchRoot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
            root = hasSupplementary ? new StartS(matchRoot) : new Start(matchRoot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
        // Release temporary storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
        temp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
        buffer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
        groupNodes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
        patternLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
        compiled = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1713
    Map<String, Integer> namedGroups() {
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1714
        if (namedGroups == null)
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5627
diff changeset
  1715
            namedGroups = new HashMap<>(2);
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1716
        return namedGroups;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1717
    }
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1718
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
     * Used to print out a subtree of the Pattern to help with debugging.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
    private static void printObjectTree(Node node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
        while(node != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
            if (node instanceof Prolog) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
                System.out.println(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
                printObjectTree(((Prolog)node).loop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
                System.out.println("**** end contents prolog loop");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
            } else if (node instanceof Loop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
                System.out.println(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
                printObjectTree(((Loop)node).body);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
                System.out.println("**** end contents Loop body");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
            } else if (node instanceof Curly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
                System.out.println(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
                printObjectTree(((Curly)node).atom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
                System.out.println("**** end contents Curly body");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
            } else if (node instanceof GroupCurly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
                System.out.println(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
                printObjectTree(((GroupCurly)node).atom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
                System.out.println("**** end contents GroupCurly body");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
            } else if (node instanceof GroupTail) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
                System.out.println(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
                System.out.println("Tail next is "+node.next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
                System.out.println(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
            node = node.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
            if (node != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
                System.out.println("->next:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
            if (node == Pattern.accept) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
                System.out.println("Accept Node");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
                node = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
     * Used to accumulate information about a subtree of the object graph
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
     * so that optimizations can be applied to the subtree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
    static final class TreeInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
        int minLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
        int maxLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
        boolean maxValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
        boolean deterministic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
        TreeInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
            reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
        void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
            minLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
            maxLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
            maxValid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
            deterministic = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
     * The following private methods are mainly used to improve the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
     * readability of the code. In order to let the Java compiler easily
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
     * inline them, we should not put many assertions or error checks in them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
     * Indicates whether a particular flag is set or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
    private boolean has(int f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
        return (flags & f) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
     * Match next character, signal error if failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
    private void accept(int ch, String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
        int testChar = temp[cursor++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
        if (has(COMMENTS))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
            testChar = parsePastWhitespace(testChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
        if (ch != testChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
            throw error(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
     * Mark the end of pattern with a specific character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
    private void mark(int c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
        temp[patternLength] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
     * Peek the next character, and do not advance the cursor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
    private int peek() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
        int ch = temp[cursor];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
        if (has(COMMENTS))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
            ch = peekPastWhitespace(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
        return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
     * Read the next character, and advance the cursor by one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
    private int read() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
        int ch = temp[cursor++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
        if (has(COMMENTS))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
            ch = parsePastWhitespace(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
        return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
     * Read the next character, and advance the cursor by one,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
     * ignoring the COMMENTS setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
    private int readEscaped() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
        int ch = temp[cursor++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
        return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
     * Advance the cursor by one, and peek the next character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
    private int next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
        int ch = temp[++cursor];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
        if (has(COMMENTS))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
            ch = peekPastWhitespace(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
        return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
     * Advance the cursor by one, and peek the next character,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
     * ignoring the COMMENTS setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
    private int nextEscaped() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
        int ch = temp[++cursor];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
        return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
     * If in xmode peek past whitespace and comments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
    private int peekPastWhitespace(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
        while (ASCII.isSpace(ch) || ch == '#') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
            while (ASCII.isSpace(ch))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
                ch = temp[++cursor];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
            if (ch == '#') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
                ch = peekPastLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
        return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
     * If in xmode parse past whitespace and comments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
    private int parsePastWhitespace(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
        while (ASCII.isSpace(ch) || ch == '#') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
            while (ASCII.isSpace(ch))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
                ch = temp[cursor++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
            if (ch == '#')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
                ch = parsePastLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
        return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
     * xmode parse past comment to end of line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
    private int parsePastLine() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
        int ch = temp[cursor++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
        while (ch != 0 && !isLineSeparator(ch))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
            ch = temp[cursor++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
        return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
     * xmode peek past comment to end of line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
    private int peekPastLine() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
        int ch = temp[++cursor];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
        while (ch != 0 && !isLineSeparator(ch))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
            ch = temp[++cursor];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
        return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
     * Determines if character is a line separator in the current mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
    private boolean isLineSeparator(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
        if (has(UNIX_LINES)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
            return ch == '\n';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
            return (ch == '\n' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
                    ch == '\r' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
                    (ch|1) == '\u2029' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
                    ch == '\u0085');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
     * Read the character after the next one, and advance the cursor by two.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
    private int skip() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
        int i = cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
        int ch = temp[i+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
        cursor = i + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
        return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
     * Unread one next character, and retreat cursor by one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
    private void unread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
        cursor--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
     * Internal method used for handling all syntax errors. The pattern is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
     * displayed with a pointer to aid in locating the syntax error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
    private PatternSyntaxException error(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
        return new PatternSyntaxException(s, normalizedPattern,  cursor - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
     * Determines if there is any supplementary character or unpaired
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
     * surrogate in the specified range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
    private boolean findSupplementary(int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
        for (int i = start; i < end; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
            if (isSupplementary(temp[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
     * Determines if the specified code point is a supplementary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
     * character or unpaired surrogate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
    private static final boolean isSupplementary(int ch) {
4820
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  1961
        return ch >= Character.MIN_SUPPLEMENTARY_CODE_POINT ||
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  1962
               Character.isSurrogate((char)ch);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
     *  The following methods handle the main parsing. They are sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
     *  according to their precedence order, the lowest one first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
     * The expression is parsed with branch nodes added for alternations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
     * This may be called recursively to parse sub expressions that may
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
     * contain alternations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
    private Node expr(Node end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
        Node prev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
        Node firstTail = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
        Node branchConn = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
            Node node = sequence(end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
            Node nodeTail = root;      //double return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
            if (prev == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
                prev = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
                firstTail = nodeTail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
                // Branch
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
                if (branchConn == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
                    branchConn = new BranchConn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
                    branchConn.next = end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
                if (node == end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
                    // if the node returned from sequence() is "end"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
                    // we have an empty expr, set a null atom into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
                    // the branch to indicate to go "next" directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
                    node = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
                    // the "tail.next" of each atom goes to branchConn
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
                    nodeTail.next = branchConn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
                if (prev instanceof Branch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
                    ((Branch)prev).add(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
                    if (prev == end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
                        prev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
                        // replace the "end" with "branchConn" at its tail.next
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
                        // when put the "prev" into the branch as the first atom.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
                        firstTail.next = branchConn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
                    prev = new Branch(prev, node, branchConn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
            if (peek() != '|') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
                return prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
            next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
     * Parsing of sequences between alternations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
    private Node sequence(Node end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
        Node head = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
        Node tail = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
        Node node = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
    LOOP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
            int ch = peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
            switch (ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
            case '(':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
                // Because group handles its own closure,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
                // we need to treat it differently
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
                node = group0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
                // Check for comment or flag group
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
                if (node == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
                if (head == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
                    head = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
                    tail.next = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
                // Double return: Tail was returned in root
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
                tail = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
            case '[':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
                node = clazz(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
            case '\\':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
                ch = nextEscaped();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
                if (ch == 'p' || ch == 'P') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
                    boolean oneLetter = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
                    boolean comp = (ch == 'P');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
                    ch = next(); // Consume { if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
                    if (ch != '{') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
                        unread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
                        oneLetter = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
                    }
4820
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  2060
                    node = family(oneLetter, comp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
                    unread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
                    node = atom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
            case '^':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
                next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
                if (has(MULTILINE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
                    if (has(UNIX_LINES))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
                        node = new UnixCaret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
                        node = new Caret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
                    node = new Begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
            case '$':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
                next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
                if (has(UNIX_LINES))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
                    node = new UnixDollar(has(MULTILINE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
                    node = new Dollar(has(MULTILINE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
            case '.':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
                next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
                if (has(DOTALL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
                    node = new All();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
                    if (has(UNIX_LINES))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
                        node = new UnixDot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
                        node = new Dot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
            case '|':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
            case ')':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
                break LOOP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
            case ']': // Now interpreting dangling ] and } as literals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
            case '}':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
                node = atom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
            case '?':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
            case '*':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
            case '+':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
                next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
                throw error("Dangling meta character '" + ((char)ch) + "'");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
            case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
                if (cursor >= patternLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
                    break LOOP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
                // Fall through
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
                node = atom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
            node = closure(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
            if (head == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
                head = tail = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
                tail.next = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
                tail = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
        if (head == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
            return end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
        tail.next = end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
        root = tail;      //double return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
        return head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
     * Parse and add a new Single or Slice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
    private Node atom() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
        int first = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
        int prev = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
        boolean hasSupplementary = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
        int ch = peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
            switch (ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
            case '*':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
            case '+':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
            case '?':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
            case '{':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
                if (first > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
                    cursor = prev;    // Unwind one character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
                    first--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
            case '$':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
            case '.':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
            case '^':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
            case '(':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
            case '[':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
            case '|':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
            case ')':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
            case '\\':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
                ch = nextEscaped();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
                if (ch == 'p' || ch == 'P') { // Property
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
                    if (first > 0) { // Slice is waiting; handle it first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
                        unread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
                    } else { // No slice; just return the family node
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
                        boolean comp = (ch == 'P');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
                        boolean oneLetter = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
                        ch = next(); // Consume { if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
                        if (ch != '{')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
                            unread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
                        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
                            oneLetter = false;
4820
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  2176
                        return family(oneLetter, comp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
                unread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
                prev = cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
                ch = escape(false, first == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
                if (ch >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
                    append(ch, first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
                    first++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
                    if (isSupplementary(ch)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
                        hasSupplementary = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
                    ch = peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
                } else if (first == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
                    return root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
                // Unwind meta escape sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
                cursor = prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
            case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
                if (cursor >= patternLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
                // Fall through
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
                prev = cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
                append(ch, first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
                first++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
                if (isSupplementary(ch)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
                    hasSupplementary = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
                ch = next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
        if (first == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
            return newSingle(buffer[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
            return newSlice(buffer, first, hasSupplementary);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
    private void append(int ch, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
        if (len >= buffer.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
            int[] tmp = new int[len+len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
            System.arraycopy(buffer, 0, tmp, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
            buffer = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
        buffer[len] = ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
     * Parses a backref greedily, taking as many numbers as it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
     * can. The first digit is always treated as a backref, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
     * multi digit numbers are only treated as a backref if at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
     * least that many backrefs exist at this point in the regex.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
    private Node ref(int refNum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
        boolean done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
        while(!done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
            int ch = peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
            switch(ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
            case '0':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
            case '1':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
            case '2':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
            case '3':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
            case '4':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
            case '5':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
            case '6':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
            case '7':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
            case '8':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
            case '9':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
                int newRefNum = (refNum * 10) + (ch - '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
                // Add another number if it doesn't make a group
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
                // that doesn't exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
                if (capturingGroupCount - 1 < newRefNum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
                    done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
                refNum = newRefNum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
                read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
                done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
        if (has(CASE_INSENSITIVE))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
            return new CIBackRef(refNum, has(UNICODE_CASE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
            return new BackRef(refNum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
     * Parses an escape sequence to determine the actual value that needs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
     * to be matched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
     * If -1 is returned and create was true a new object was added to the tree
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
     * to handle the escape sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
     * If the returned value is greater than zero, it is the value that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
     * matches the escape sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
    private int escape(boolean inclass, boolean create) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
        int ch = skip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
        switch (ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
        case '0':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
            return o();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
        case '1':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
        case '2':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
        case '3':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
        case '4':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
        case '5':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
        case '6':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
        case '7':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
        case '8':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
        case '9':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
            if (inclass) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
            if (create) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
                root = ref((ch - '0'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
        case 'A':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
            if (inclass) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
            if (create) root = new Begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
        case 'B':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
            if (inclass) break;
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2304
            if (create) root = new Bound(Bound.NONE, has(UNICODE_CHARACTER_CLASS));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
        case 'C':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
        case 'D':
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2309
            if (create) root = has(UNICODE_CHARACTER_CLASS)
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2310
                               ? new Utype(UnicodeProp.DIGIT).complement()
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2311
                               : new Ctype(ASCII.DIGIT).complement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
        case 'E':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
        case 'F':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
        case 'G':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
            if (inclass) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
            if (create) root = new LastMatch();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
        case 'H':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
        case 'I':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
        case 'J':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
        case 'K':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
        case 'L':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
        case 'M':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
        case 'N':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
        case 'O':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
        case 'P':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
        case 'Q':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
        case 'R':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
        case 'S':
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2333
            if (create) root = has(UNICODE_CHARACTER_CLASS)
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2334
                               ? new Utype(UnicodeProp.WHITE_SPACE).complement()
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2335
                               : new Ctype(ASCII.SPACE).complement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
        case 'T':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
        case 'U':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
        case 'V':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
        case 'W':
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2342
            if (create) root = has(UNICODE_CHARACTER_CLASS)
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2343
                               ? new Utype(UnicodeProp.WORD).complement()
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2344
                               : new Ctype(ASCII.WORD).complement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
        case 'X':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
        case 'Y':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
        case 'Z':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
            if (inclass) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
            if (create) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
                if (has(UNIX_LINES))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
                    root = new UnixDollar(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
                    root = new Dollar(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
        case 'a':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
            return '\007';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
        case 'b':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
            if (inclass) break;
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2362
            if (create) root = new Bound(Bound.BOTH, has(UNICODE_CHARACTER_CLASS));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
        case 'c':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
            return c();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
        case 'd':
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2367
            if (create) root = has(UNICODE_CHARACTER_CLASS)
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2368
                               ? new Utype(UnicodeProp.DIGIT)
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2369
                               : new Ctype(ASCII.DIGIT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
        case 'e':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
            return '\033';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
        case 'f':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
            return '\f';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
        case 'g':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
        case 'h':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
        case 'i':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
        case 'j':
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2379
            break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
        case 'k':
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2381
            if (inclass)
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2382
                break;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2383
            if (read() != '<')
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2384
                throw error("\\k is not followed by '<' for named capturing group");
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2385
            String name = groupname(read());
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2386
            if (!namedGroups().containsKey(name))
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2387
                throw error("(named capturing group <"+ name+"> does not exit");
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2388
            if (create) {
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2389
                if (has(CASE_INSENSITIVE))
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2390
                    root = new CIBackRef(namedGroups().get(name), has(UNICODE_CASE));
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2391
                else
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2392
                    root = new BackRef(namedGroups().get(name));
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2393
            }
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2394
            return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
        case 'l':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
        case 'm':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
        case 'n':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
            return '\n';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
        case 'o':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
        case 'p':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
        case 'q':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
        case 'r':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
            return '\r';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
        case 's':
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2407
            if (create) root = has(UNICODE_CHARACTER_CLASS)
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2408
                               ? new Utype(UnicodeProp.WHITE_SPACE)
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2409
                               : new Ctype(ASCII.SPACE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
        case 't':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
            return '\t';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
        case 'u':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
            return u();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
        case 'v':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
            return '\013';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
        case 'w':
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2418
            if (create) root = has(UNICODE_CHARACTER_CLASS)
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2419
                               ? new Utype(UnicodeProp.WORD)
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2420
                               : new Ctype(ASCII.WORD);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
        case 'x':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
            return x();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
        case 'y':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
        case 'z':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
            if (inclass) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
            if (create) root = new End();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
            return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
        throw error("Illegal/unsupported escape sequence");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
     * Parse a character class, and return the node that matches it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
     * Consumes a ] on the way out if consume is true. Usually consume
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
     * is true except for the case of [abc&&def] where def is a separate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
     * right hand node with "understood" brackets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
    private CharProperty clazz(boolean consume) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
        CharProperty prev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
        CharProperty node = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
        BitClass bits = new BitClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
        boolean include = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
        boolean firstInClass = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
        int ch = next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
            switch (ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
                case '^':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
                    // Negates if first char in a class, otherwise literal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
                    if (firstInClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
                        if (temp[cursor-1] != '[')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
                        ch = next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
                        include = !include;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
                        // ^ not first in class, treat as literal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
                case '[':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
                    firstInClass = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
                    node = clazz(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
                    if (prev == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
                        prev = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
                        prev = union(prev, node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
                    ch = peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
                case '&':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
                    firstInClass = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
                    ch = next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
                    if (ch == '&') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
                        ch = next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
                        CharProperty rightNode = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
                        while (ch != ']' && ch != '&') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
                            if (ch == '[') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
                                if (rightNode == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
                                    rightNode = clazz(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
                                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
                                    rightNode = union(rightNode, clazz(true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
                            } else { // abc&&def
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
                                unread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
                                rightNode = clazz(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
                            ch = peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
                        if (rightNode != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
                            node = rightNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
                        if (prev == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
                            if (rightNode == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
                                throw error("Bad class syntax");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
                            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
                                prev = rightNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
                            prev = intersection(prev, node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
                        // treat as a literal &
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
                        unread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
                case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
                    firstInClass = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
                    if (cursor >= patternLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
                        throw error("Unclosed character class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
                case ']':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
                    firstInClass = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
                    if (prev != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
                        if (consume)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
                            next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
                        return prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
                    firstInClass = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
            node = range(bits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
            if (include) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
                if (prev == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
                    prev = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
                    if (prev != node)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
                        prev = union(prev, node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
                if (prev == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
                    prev = node.complement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
                    if (prev != node)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
                        prev = setDifference(prev, node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
            ch = peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
    private CharProperty bitsOrSingle(BitClass bits, int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
        /* Bits can only handle codepoints in [u+0000-u+00ff] range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
           Use "single" node instead of bits when dealing with unicode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
           case folding for codepoints listed below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
           (1)Uppercase out of range: u+00ff, u+00b5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
              toUpperCase(u+00ff) -> u+0178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
              toUpperCase(u+00b5) -> u+039c
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
           (2)LatinSmallLetterLongS u+17f
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
              toUpperCase(u+017f) -> u+0053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
           (3)LatinSmallLetterDotlessI u+131
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
              toUpperCase(u+0131) -> u+0049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
           (4)LatinCapitalLetterIWithDotAbove u+0130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
              toLowerCase(u+0130) -> u+0069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
           (5)KelvinSign u+212a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
              toLowerCase(u+212a) ==> u+006B
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
           (6)AngstromSign u+212b
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
              toLowerCase(u+212b) ==> u+00e5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
        int d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
        if (ch < 256 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
            !(has(CASE_INSENSITIVE) && has(UNICODE_CASE) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
              (ch == 0xff || ch == 0xb5 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
               ch == 0x49 || ch == 0x69 ||  //I and i
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
               ch == 0x53 || ch == 0x73 ||  //S and s
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
               ch == 0x4b || ch == 0x6b ||  //K and k
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
               ch == 0xc5 || ch == 0xe5)))  //A+ring
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
            return bits.add(ch, flags());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
        return newSingle(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
     * Parse a single character or a character range in a character class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
     * and return its representative node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
    private CharProperty range(BitClass bits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
        int ch = peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
        if (ch == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
            ch = nextEscaped();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
            if (ch == 'p' || ch == 'P') { // A property
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
                boolean comp = (ch == 'P');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
                boolean oneLetter = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
                // Consume { if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
                ch = next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
                if (ch != '{')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
                    unread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
                    oneLetter = false;
4820
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  2591
                return family(oneLetter, comp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
            } else { // ordinary escape
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
                unread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
                ch = escape(true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
                if (ch == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
                    return (CharProperty) root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
            ch = single();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
        if (ch >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
            if (peek() == '-') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
                int endRange = temp[cursor+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
                if (endRange == '[') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
                    return bitsOrSingle(bits, ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
                if (endRange != ']') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
                    next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
                    int m = single();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
                    if (m < ch)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
                        throw error("Illegal character range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
                    if (has(CASE_INSENSITIVE))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
                        return caseInsensitiveRangeFor(ch, m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
                        return rangeFor(ch, m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
            return bitsOrSingle(bits, ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
        throw error("Unexpected character '"+((char)ch)+"'");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
    private int single() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
        int ch = peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
        switch (ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
        case '\\':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
            return escape(true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
            next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
            return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
     * Parses a Unicode character family and returns its representative node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
     */
4820
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  2637
    private CharProperty family(boolean singleLetter,
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  2638
                                boolean maybeComplement)
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  2639
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
        next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
        String name;
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2642
        CharProperty node = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
        if (singleLetter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
            int c = temp[cursor];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
            if (!Character.isSupplementaryCodePoint(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
                name = String.valueOf((char)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
                name = new String(temp, cursor, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
            read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
            int i = cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
            mark('}');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
            while(read() != '}') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
            mark('\000');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
            int j = cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
            if (j > patternLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
                throw error("Unclosed character family");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
            if (i + 1 >= j)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
                throw error("Empty character family");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
            name = new String(temp, i, j-i-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
5610
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2666
        int i = name.indexOf('=');
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2667
        if (i != -1) {
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2668
            // property construct \p{name=value}
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2669
            String value = name.substring(i + 1);
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2670
            name = name.substring(0, i).toLowerCase(Locale.ENGLISH);
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2671
            if ("sc".equals(name) || "script".equals(name)) {
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2672
                node = unicodeScriptPropertyFor(value);
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2673
            } else if ("blk".equals(name) || "block".equals(name)) {
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2674
                node = unicodeBlockPropertyFor(value);
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2675
            } else if ("gc".equals(name) || "general_category".equals(name)) {
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2676
                node = charPropertyNodeFor(value);
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2677
            } else {
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2678
                throw error("Unknown Unicode property {name=<" + name + ">, "
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2679
                             + "value=<" + value + ">}");
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2680
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
        } else {
5610
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2682
            if (name.startsWith("In")) {
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2683
                // \p{inBlockName}
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2684
                node = unicodeBlockPropertyFor(name.substring(2));
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2685
            } else if (name.startsWith("Is")) {
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2686
                // \p{isGeneralCategory} and \p{isScriptName}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
                name = name.substring(2);
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2688
                UnicodeProp uprop = UnicodeProp.forName(name);
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2689
                if (uprop != null)
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2690
                    node = new Utype(uprop);
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2691
                if (node == null)
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2692
                    node = CharPropertyNames.charPropertyFor(name);
5610
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2693
                if (node == null)
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2694
                    node = unicodeScriptPropertyFor(name);
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2695
            } else {
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2696
                if (has(UNICODE_CHARACTER_CLASS)) {
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2697
                    UnicodeProp uprop = UnicodeProp.forPOSIXName(name);
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2698
                    if (uprop != null)
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2699
                        node = new Utype(uprop);
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2700
                }
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2701
                if (node == null)
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2702
                    node = charPropertyNodeFor(name);
5610
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2703
            }
4820
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  2704
        }
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  2705
        if (maybeComplement) {
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  2706
            if (node instanceof Category || node instanceof Block)
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  2707
                hasSupplementary = true;
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  2708
            node = node.complement();
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  2709
        }
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  2710
        return node;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
5610
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2713
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2714
    /**
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2715
     * Returns a CharProperty matching all characters belong to
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2716
     * a UnicodeScript.
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2717
     */
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2718
    private CharProperty unicodeScriptPropertyFor(String name) {
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2719
        final Character.UnicodeScript script;
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2720
        try {
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2721
            script = Character.UnicodeScript.forName(name);
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2722
        } catch (IllegalArgumentException iae) {
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2723
            throw error("Unknown character script name {" + name + "}");
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2724
        }
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2725
        return new Script(script);
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2726
    }
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2727
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
     * Returns a CharProperty matching all characters in a UnicodeBlock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
    private CharProperty unicodeBlockPropertyFor(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
        final Character.UnicodeBlock block;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
            block = Character.UnicodeBlock.forName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
        } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
            throw error("Unknown character block name {" + name + "}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
        }
4820
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  2738
        return new Block(block);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
     * Returns a CharProperty matching all characters in a named property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
    private CharProperty charPropertyNodeFor(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
        CharProperty p = CharPropertyNames.charPropertyFor(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
        if (p == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
            throw error("Unknown character property name {" + name + "}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
        return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
    /**
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2752
     * Parses and returns the name of a "named capturing group", the trailing
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2753
     * ">" is consumed after parsing.
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2754
     */
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2755
    private String groupname(int ch) {
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2756
        StringBuilder sb = new StringBuilder();
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2757
        sb.append(Character.toChars(ch));
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2758
        while (ASCII.isLower(ch=read()) || ASCII.isUpper(ch) ||
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2759
               ASCII.isDigit(ch)) {
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2760
            sb.append(Character.toChars(ch));
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2761
        }
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2762
        if (sb.length() == 0)
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2763
            throw error("named capturing group has 0 length name");
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2764
        if (ch != '>')
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2765
            throw error("named capturing group is missing trailing '>'");
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2766
        return sb.toString();
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2767
    }
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2768
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2769
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
     * Parses a group and returns the head node of a set of nodes that process
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
     * the group. Sometimes a double return system is used where the tail is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
     * returned in root.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
    private Node group0() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
        boolean capturingGroup = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
        Node head = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
        Node tail = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
        int save = flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
        root = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
        int ch = next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
        if (ch == '?') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
            ch = skip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
            switch (ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
            case ':':   //  (?:xxx) pure group
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
                head = createGroup(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
                tail = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
                head.next = expr(tail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
            case '=':   // (?=xxx) and (?!xxx) lookahead
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
            case '!':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
                head = createGroup(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
                tail = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
                head.next = expr(tail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
                if (ch == '=') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
                    head = tail = new Pos(head);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
                    head = tail = new Neg(head);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
            case '>':   // (?>xxx)  independent group
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
                head = createGroup(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
                tail = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
                head.next = expr(tail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
                head = tail = new Ques(head, INDEPENDENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
            case '<':   // (?<xxx)  look behind
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
                ch = read();
4161
679d00486dc6 6878475: Better syntax for the named capture group in regex
sherman
parents: 2291
diff changeset
  2808
                if (ASCII.isLower(ch) || ASCII.isUpper(ch)) {
2290
3a3bde061968 6817475: named-capturing group name started with digit causes PSE exception
sherman
parents: 2070
diff changeset
  2809
                    // named captured group
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2810
                    String name = groupname(ch);
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2811
                    if (namedGroups().containsKey(name))
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2812
                        throw error("Named capturing group <" + name
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2813
                                    + "> is already defined");
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2814
                    capturingGroup = true;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2815
                    head = createGroup(false);
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2816
                    tail = root;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2817
                    namedGroups().put(name, capturingGroupCount-1);
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2818
                    head.next = expr(tail);
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2819
                    break;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2820
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
                int start = cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
                head = createGroup(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
                tail = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
                head.next = expr(tail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
                tail.next = lookbehindEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
                TreeInfo info = new TreeInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
                head.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
                if (info.maxValid == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
                    throw error("Look-behind group does not have "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
                                + "an obvious maximum length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
                boolean hasSupplementary = findSupplementary(start, patternLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
                if (ch == '=') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
                    head = tail = (hasSupplementary ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
                                   new BehindS(head, info.maxLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
                                               info.minLength) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
                                   new Behind(head, info.maxLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
                                              info.minLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
                } else if (ch == '!') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
                    head = tail = (hasSupplementary ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
                                   new NotBehindS(head, info.maxLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
                                                  info.minLength) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
                                   new NotBehind(head, info.maxLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
                                                 info.minLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
                    throw error("Unknown look-behind group");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
            case '$':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
            case '@':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
                throw error("Unknown group type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
            default:    // (?xxx:) inlined match flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
                unread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
                addFlag();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
                ch = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
                if (ch == ')') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
                    return null;    // Inline modifier only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
                if (ch != ':') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
                    throw error("Unknown inline modifier");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
                head = createGroup(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
                tail = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
                head.next = expr(tail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
        } else { // (xxx) a regular group
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
            capturingGroup = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
            head = createGroup(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
            tail = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
            head.next = expr(tail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
        accept(')', "Unclosed group");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
        flags = save;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
        // Check for quantifiers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
        Node node = closure(head);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
        if (node == head) { // No closure
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
            root = tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
            return node;    // Dual return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
        if (head == tail) { // Zero length assertion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
            root = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
            return node;    // Dual return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
        if (node instanceof Ques) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
            Ques ques = (Ques) node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
            if (ques.type == POSSESSIVE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
                root = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
                return node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2894
            tail.next = new BranchConn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
            tail = tail.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
            if (ques.type == GREEDY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
                head = new Branch(head, null, tail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
            } else { // Reluctant quantifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
                head = new Branch(null, head, tail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
            root = tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
            return head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
        } else if (node instanceof Curly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
            Curly curly = (Curly) node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
            if (curly.type == POSSESSIVE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
                root = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
                return node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
            // Discover if the group is deterministic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
            TreeInfo info = new TreeInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
            if (head.study(info)) { // Deterministic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
                GroupTail temp = (GroupTail) tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
                head = root = new GroupCurly(head.next, curly.cmin,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
                                   curly.cmax, curly.type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
                                   ((GroupTail)tail).localIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
                                   ((GroupTail)tail).groupIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
                                             capturingGroup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
                return head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
            } else { // Non-deterministic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
                int temp = ((GroupHead) head).localIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
                Loop loop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
                if (curly.type == GREEDY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
                    loop = new Loop(this.localCount, temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
                else  // Reluctant Curly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
                    loop = new LazyLoop(this.localCount, temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
                Prolog prolog = new Prolog(loop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
                this.localCount += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
                loop.cmin = curly.cmin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
                loop.cmax = curly.cmax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
                loop.body = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
                tail.next = loop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
                root = loop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
                return prolog; // Dual return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
        throw error("Internal logic error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
     * Create group head and tail nodes using double return. If the group is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
     * created with anonymous true then it is a pure group and should not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
     * affect group counting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
    private Node createGroup(boolean anonymous) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
        int localIndex = localCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
        int groupIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
        if (!anonymous)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
            groupIndex = capturingGroupCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
        GroupHead head = new GroupHead(localIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
        root = new GroupTail(localIndex, groupIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
        if (!anonymous && groupIndex < 10)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
            groupNodes[groupIndex] = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
        return head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
     * Parses inlined match flags and set them appropriately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
    private void addFlag() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
        int ch = peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
            switch (ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
            case 'i':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
                flags |= CASE_INSENSITIVE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
            case 'm':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
                flags |= MULTILINE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
            case 's':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
                flags |= DOTALL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
            case 'd':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
                flags |= UNIX_LINES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
            case 'u':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
                flags |= UNICODE_CASE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
            case 'c':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
                flags |= CANON_EQ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
            case 'x':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
                flags |= COMMENTS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
                break;
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2984
            case 'U':
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2985
                flags |= (UNICODE_CHARACTER_CLASS | UNICODE_CASE);
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  2986
                break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
            case '-': // subFlag then fall through
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
                ch = next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
                subFlag();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2990
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2991
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
            ch = next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
     * Parses the second part of inlined match flags and turns off
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
     * flags appropriately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
    private void subFlag() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
        int ch = peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3004
            switch (ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
            case 'i':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
                flags &= ~CASE_INSENSITIVE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
            case 'm':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
                flags &= ~MULTILINE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
            case 's':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
                flags &= ~DOTALL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
            case 'd':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
                flags &= ~UNIX_LINES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
            case 'u':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
                flags &= ~UNICODE_CASE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
            case 'c':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
                flags &= ~CANON_EQ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
            case 'x':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
                flags &= ~COMMENTS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3025
                break;
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  3026
            case 'U':
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  3027
                flags &= ~(UNICODE_CHARACTER_CLASS | UNICODE_CASE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3029
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3030
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3031
            ch = next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3032
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3033
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3035
    static final int MAX_REPS   = 0x7FFFFFFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3037
    static final int GREEDY     = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3039
    static final int LAZY       = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3041
    static final int POSSESSIVE = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3042
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3043
    static final int INDEPENDENT = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3045
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3046
     * Processes repetition. If the next character peeked is a quantifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3047
     * then new nodes must be appended to handle the repetition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3048
     * Prev could be a single or a group, so it could be a chain of nodes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3049
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3050
    private Node closure(Node prev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
        Node atom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3052
        int ch = peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3053
        switch (ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3054
        case '?':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3055
            ch = next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3056
            if (ch == '?') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3057
                next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3058
                return new Ques(prev, LAZY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3059
            } else if (ch == '+') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3060
                next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3061
                return new Ques(prev, POSSESSIVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3062
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3063
            return new Ques(prev, GREEDY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3064
        case '*':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3065
            ch = next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3066
            if (ch == '?') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3067
                next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3068
                return new Curly(prev, 0, MAX_REPS, LAZY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3069
            } else if (ch == '+') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3070
                next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3071
                return new Curly(prev, 0, MAX_REPS, POSSESSIVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3072
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3073
            return new Curly(prev, 0, MAX_REPS, GREEDY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3074
        case '+':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3075
            ch = next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
            if (ch == '?') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
                next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
                return new Curly(prev, 1, MAX_REPS, LAZY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
            } else if (ch == '+') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3080
                next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3081
                return new Curly(prev, 1, MAX_REPS, POSSESSIVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3083
            return new Curly(prev, 1, MAX_REPS, GREEDY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3084
        case '{':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3085
            ch = temp[cursor+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3086
            if (ASCII.isDigit(ch)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3087
                skip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3088
                int cmin = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3089
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3090
                    cmin = cmin * 10 + (ch - '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
                } while (ASCII.isDigit(ch = read()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
                int cmax = cmin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3093
                if (ch == ',') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
                    ch = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
                    cmax = MAX_REPS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
                    if (ch != '}') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
                        cmax = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
                        while (ASCII.isDigit(ch)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
                            cmax = cmax * 10 + (ch - '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
                            ch = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
                if (ch != '}')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
                    throw error("Unclosed counted closure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3106
                if (((cmin) | (cmax) | (cmax - cmin)) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3107
                    throw error("Illegal repetition range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3108
                Curly curly;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3109
                ch = peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3110
                if (ch == '?') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3111
                    next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3112
                    curly = new Curly(prev, cmin, cmax, LAZY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3113
                } else if (ch == '+') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3114
                    next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3115
                    curly = new Curly(prev, cmin, cmax, POSSESSIVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3116
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3117
                    curly = new Curly(prev, cmin, cmax, GREEDY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3118
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3119
                return curly;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3120
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3121
                throw error("Illegal repetition");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3123
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3124
            return prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3127
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3129
     *  Utility method for parsing control escape sequences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3131
    private int c() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3132
        if (cursor < patternLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3133
            return read() ^ 64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3135
        throw error("Illegal control escape sequence");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3139
     *  Utility method for parsing octal escape sequences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3141
    private int o() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3142
        int n = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3143
        if (((n-'0')|('7'-n)) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3144
            int m = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3145
            if (((m-'0')|('7'-m)) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3146
                int o = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3147
                if ((((o-'0')|('7'-o)) >= 0) && (((n-'0')|('3'-n)) >= 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3148
                    return (n - '0') * 64 + (m - '0') * 8 + (o - '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3149
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3150
                unread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3151
                return (n - '0') * 8 + (m - '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3153
            unread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3154
            return (n - '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3156
        throw error("Illegal octal escape sequence");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3158
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3160
     *  Utility method for parsing hexadecimal escape sequences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3162
    private int x() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3163
        int n = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3164
        if (ASCII.isHexDigit(n)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3165
            int m = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3166
            if (ASCII.isHexDigit(m)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3167
                return ASCII.toDigit(n) * 16 + ASCII.toDigit(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3168
            }
8173
a3a39b98e05a 7014645: Support perl style Unicode hex notation \x{...}
sherman
parents: 7816
diff changeset
  3169
        } else if (n == '{' && ASCII.isHexDigit(peek())) {
a3a39b98e05a 7014645: Support perl style Unicode hex notation \x{...}
sherman
parents: 7816
diff changeset
  3170
            int ch = 0;
a3a39b98e05a 7014645: Support perl style Unicode hex notation \x{...}
sherman
parents: 7816
diff changeset
  3171
            while (ASCII.isHexDigit(n = read())) {
a3a39b98e05a 7014645: Support perl style Unicode hex notation \x{...}
sherman
parents: 7816
diff changeset
  3172
                ch = (ch << 4) + ASCII.toDigit(n);
a3a39b98e05a 7014645: Support perl style Unicode hex notation \x{...}
sherman
parents: 7816
diff changeset
  3173
                if (ch > Character.MAX_CODE_POINT)
a3a39b98e05a 7014645: Support perl style Unicode hex notation \x{...}
sherman
parents: 7816
diff changeset
  3174
                    throw error("Hexadecimal codepoint is too big");
a3a39b98e05a 7014645: Support perl style Unicode hex notation \x{...}
sherman
parents: 7816
diff changeset
  3175
            }
a3a39b98e05a 7014645: Support perl style Unicode hex notation \x{...}
sherman
parents: 7816
diff changeset
  3176
            if (n != '}')
a3a39b98e05a 7014645: Support perl style Unicode hex notation \x{...}
sherman
parents: 7816
diff changeset
  3177
                throw error("Unclosed hexadecimal escape sequence");
a3a39b98e05a 7014645: Support perl style Unicode hex notation \x{...}
sherman
parents: 7816
diff changeset
  3178
            return ch;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3180
        throw error("Illegal hexadecimal escape sequence");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3182
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3184
     *  Utility method for parsing unicode escape sequences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3185
     */
404
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3186
    private int cursor() {
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3187
        return cursor;
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3188
    }
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3189
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3190
    private void setcursor(int pos) {
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3191
        cursor = pos;
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3192
    }
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3193
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3194
    private int uxxxx() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3195
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3196
        for (int i = 0; i < 4; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3197
            int ch = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3198
            if (!ASCII.isHexDigit(ch)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3199
                throw error("Illegal Unicode escape sequence");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3201
            n = n * 16 + ASCII.toDigit(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3203
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3205
404
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3206
    private int u() {
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3207
        int n = uxxxx();
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3208
        if (Character.isHighSurrogate((char)n)) {
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3209
            int cur = cursor();
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3210
            if (read() == '\\' && read() == 'u') {
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3211
                int n2 = uxxxx();
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3212
                if (Character.isLowSurrogate((char)n2))
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3213
                    return Character.toCodePoint((char)n, (char)n2);
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3214
            }
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3215
            setcursor(cur);
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3216
        }
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3217
        return n;
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3218
    }
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3219
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3220
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3221
    // Utility methods for code point support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3222
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3223
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3224
    private static final int countChars(CharSequence seq, int index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3225
                                        int lengthInCodePoints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3226
        // optimization
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3227
        if (lengthInCodePoints == 1 && !Character.isHighSurrogate(seq.charAt(index))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3228
            assert (index >= 0 && index < seq.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3229
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3231
        int length = seq.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3232
        int x = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3233
        if (lengthInCodePoints >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3234
            assert (index >= 0 && index < length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3235
            for (int i = 0; x < length && i < lengthInCodePoints; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3236
                if (Character.isHighSurrogate(seq.charAt(x++))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3237
                    if (x < length && Character.isLowSurrogate(seq.charAt(x))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3238
                        x++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3239
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3240
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3242
            return x - index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3244
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3245
        assert (index >= 0 && index <= length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3246
        if (index == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3247
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3249
        int len = -lengthInCodePoints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3250
        for (int i = 0; x > 0 && i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3251
            if (Character.isLowSurrogate(seq.charAt(--x))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3252
                if (x > 0 && Character.isHighSurrogate(seq.charAt(x-1))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3253
                    x--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3254
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3255
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3257
        return index - x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3259
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3260
    private static final int countCodePoints(CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3261
        int length = seq.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3262
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3263
        for (int i = 0; i < length; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3264
            n++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3265
            if (Character.isHighSurrogate(seq.charAt(i++))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3266
                if (i < length && Character.isLowSurrogate(seq.charAt(i))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3267
                    i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3268
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3271
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3273
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3275
     *  Creates a bit vector for matching Latin-1 values. A normal BitClass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3276
     *  never matches values above Latin-1, and a complemented BitClass always
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3277
     *  matches values above Latin-1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3279
    private static final class BitClass extends BmpCharProperty {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3280
        final boolean[] bits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3281
        BitClass() { bits = new boolean[256]; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3282
        private BitClass(boolean[] bits) { this.bits = bits; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3283
        BitClass add(int c, int flags) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3284
            assert c >= 0 && c <= 255;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3285
            if ((flags & CASE_INSENSITIVE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3286
                if (ASCII.isAscii(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3287
                    bits[ASCII.toUpper(c)] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3288
                    bits[ASCII.toLower(c)] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3289
                } else if ((flags & UNICODE_CASE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3290
                    bits[Character.toLowerCase(c)] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3291
                    bits[Character.toUpperCase(c)] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3292
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3293
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3294
            bits[c] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3295
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3297
        boolean isSatisfiedBy(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3298
            return ch < 256 && bits[ch];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3301
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3302
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3303
     *  Returns a suitably optimized, single character matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3305
    private CharProperty newSingle(final int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3306
        if (has(CASE_INSENSITIVE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3307
            int lower, upper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3308
            if (has(UNICODE_CASE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3309
                upper = Character.toUpperCase(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3310
                lower = Character.toLowerCase(upper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3311
                if (upper != lower)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3312
                    return new SingleU(lower);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3313
            } else if (ASCII.isAscii(ch)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3314
                lower = ASCII.toLower(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3315
                upper = ASCII.toUpper(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3316
                if (lower != upper)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3317
                    return new SingleI(lower, upper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3318
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3320
        if (isSupplementary(ch))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3321
            return new SingleS(ch);    // Match a given Unicode character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3322
        return new Single(ch);         // Match a given BMP character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3324
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3325
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3326
     *  Utility method for creating a string slice matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3328
    private Node newSlice(int[] buf, int count, boolean hasSupplementary) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3329
        int[] tmp = new int[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3330
        if (has(CASE_INSENSITIVE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3331
            if (has(UNICODE_CASE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3332
                for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3333
                    tmp[i] = Character.toLowerCase(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3334
                                 Character.toUpperCase(buf[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3335
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3336
                return hasSupplementary? new SliceUS(tmp) : new SliceU(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3338
            for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3339
                tmp[i] = ASCII.toLower(buf[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3340
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3341
            return hasSupplementary? new SliceIS(tmp) : new SliceI(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3343
        for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3344
            tmp[i] = buf[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3346
        return hasSupplementary ? new SliceS(tmp) : new Slice(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3348
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3349
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3350
     * The following classes are the building components of the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3351
     * tree that represents a compiled regular expression. The object tree
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3352
     * is made of individual elements that handle constructs in the Pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3353
     * Each type of object knows how to match its equivalent construct with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3354
     * the match() method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3355
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3356
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3357
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3358
     * Base class for all node classes. Subclasses should override the match()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3359
     * method as appropriate. This class is an accepting node, so its match()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3360
     * always returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3362
    static class Node extends Object {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3363
        Node next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3364
        Node() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3365
            next = Pattern.accept;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3367
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3368
         * This method implements the classic accept node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3369
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3370
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3371
            matcher.last = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3372
            matcher.groups[0] = matcher.first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3373
            matcher.groups[1] = matcher.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3374
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3376
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3377
         * This method is good for all zero length assertions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3378
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3379
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3380
            if (next != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3381
                return next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3382
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3383
                return info.deterministic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3384
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3387
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3388
    static class LastNode extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3389
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3390
         * This method implements the classic accept node with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3391
         * the addition of a check to see if the match occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3392
         * using all of the input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3393
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3394
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3395
            if (matcher.acceptMode == Matcher.ENDANCHOR && i != matcher.to)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3396
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3397
            matcher.last = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3398
            matcher.groups[0] = matcher.first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3399
            matcher.groups[1] = matcher.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3400
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3403
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3404
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3405
     * Used for REs that can start anywhere within the input string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3406
     * This basically tries to match repeatedly at each spot in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3407
     * input string, moving forward after each try. An anchored search
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3408
     * or a BnM will bypass this node completely.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3409
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3410
    static class Start extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3411
        int minLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3412
        Start(Node node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3413
            this.next = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3414
            TreeInfo info = new TreeInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3415
            next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3416
            minLength = info.minLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3418
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3419
            if (i > matcher.to - minLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3420
                matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3421
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3422
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3423
            int guard = matcher.to - minLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3424
            for (; i <= guard; i++) {
4820
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3425
                if (next.match(matcher, i, seq)) {
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3426
                    matcher.first = i;
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3427
                    matcher.groups[0] = matcher.first;
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3428
                    matcher.groups[1] = matcher.last;
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3429
                    return true;
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3430
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3431
            }
4820
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3432
            matcher.hitEnd = true;
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3433
            return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3434
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3435
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3436
            next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3437
            info.maxValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3438
            info.deterministic = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3439
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3442
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3443
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3444
     * StartS supports supplementary characters, including unpaired surrogates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3445
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3446
    static final class StartS extends Start {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3447
        StartS(Node node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3448
            super(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3450
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3451
            if (i > matcher.to - minLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3452
                matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3453
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3454
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3455
            int guard = matcher.to - minLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3456
            while (i <= guard) {
4820
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3457
                //if ((ret = next.match(matcher, i, seq)) || i == guard)
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3458
                if (next.match(matcher, i, seq)) {
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3459
                    matcher.first = i;
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3460
                    matcher.groups[0] = matcher.first;
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3461
                    matcher.groups[1] = matcher.last;
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3462
                    return true;
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3463
                }
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3464
                if (i == guard)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3465
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3466
                // Optimization to move to the next character. This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3467
                // faster than countChars(seq, i, 1).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3468
                if (Character.isHighSurrogate(seq.charAt(i++))) {
4820
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3469
                    if (i < seq.length() &&
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3470
                        Character.isLowSurrogate(seq.charAt(i))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3471
                        i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3472
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3473
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3474
            }
4820
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3475
            matcher.hitEnd = true;
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3476
            return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3479
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3480
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3481
     * Node to anchor at the beginning of input. This object implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3482
     * match for a \A sequence, and the caret anchor will use this if not in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3483
     * multiline mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3484
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3485
    static final class Begin extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3486
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3487
            int fromIndex = (matcher.anchoringBounds) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3488
                matcher.from : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3489
            if (i == fromIndex && next.match(matcher, i, seq)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3490
                matcher.first = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3491
                matcher.groups[0] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3492
                matcher.groups[1] = matcher.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3493
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3494
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3495
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3496
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3498
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3499
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3500
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3501
     * Node to anchor at the end of input. This is the absolute end, so this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3502
     * should not match at the last newline before the end as $ will.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3503
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3504
    static final class End extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3505
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3506
            int endIndex = (matcher.anchoringBounds) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3507
                matcher.to : matcher.getTextLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3508
            if (i == endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3509
                matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3510
                return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3511
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3512
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3514
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3515
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3516
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3517
     * Node to anchor at the beginning of a line. This is essentially the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3518
     * object to match for the multiline ^.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3519
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3520
    static final class Caret extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3521
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3522
            int startIndex = matcher.from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3523
            int endIndex = matcher.to;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3524
            if (!matcher.anchoringBounds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3525
                startIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3526
                endIndex = matcher.getTextLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3527
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3528
            // Perl does not match ^ at end of input even after newline
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3529
            if (i == endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3530
                matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3531
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3532
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3533
            if (i > startIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3534
                char ch = seq.charAt(i-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3535
                if (ch != '\n' && ch != '\r'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3536
                    && (ch|1) != '\u2029'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3537
                    && ch != '\u0085' ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3538
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3539
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3540
                // Should treat /r/n as one newline
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3541
                if (ch == '\r' && seq.charAt(i) == '\n')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3542
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3543
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3544
            return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3545
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3546
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3547
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3548
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3549
     * Node to anchor at the beginning of a line when in unixdot mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3550
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3551
    static final class UnixCaret extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3552
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3553
            int startIndex = matcher.from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3554
            int endIndex = matcher.to;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3555
            if (!matcher.anchoringBounds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3556
                startIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3557
                endIndex = matcher.getTextLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3558
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3559
            // Perl does not match ^ at end of input even after newline
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3560
            if (i == endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3561
                matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3562
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3563
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3564
            if (i > startIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3565
                char ch = seq.charAt(i-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3566
                if (ch != '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3567
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3568
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3569
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3570
            return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3571
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3572
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3573
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3574
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3575
     * Node to match the location where the last match ended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3576
     * This is used for the \G construct.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3577
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3578
    static final class LastMatch extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3579
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3580
            if (i != matcher.oldLast)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3581
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3582
            return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3583
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3584
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3585
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3586
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3587
     * Node to anchor at the end of a line or the end of input based on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3588
     * multiline mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3589
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3590
     * When not in multiline mode, the $ can only match at the very end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3591
     * of the input, unless the input ends in a line terminator in which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3592
     * it matches right before the last line terminator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3593
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3594
     * Note that \r\n is considered an atomic line terminator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3595
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3596
     * Like ^ the $ operator matches at a position, it does not match the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3597
     * line terminators themselves.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3598
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3599
    static final class Dollar extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3600
        boolean multiline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3601
        Dollar(boolean mul) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3602
            multiline = mul;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3604
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3605
            int endIndex = (matcher.anchoringBounds) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3606
                matcher.to : matcher.getTextLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3607
            if (!multiline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3608
                if (i < endIndex - 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3609
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3610
                if (i == endIndex - 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3611
                    char ch = seq.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3612
                    if (ch != '\r')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3613
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3614
                    ch = seq.charAt(i + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3615
                    if (ch != '\n')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3616
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3617
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3618
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3619
            // Matches before any line terminator; also matches at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3620
            // end of input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3621
            // Before line terminator:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3622
            // If multiline, we match here no matter what
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3623
            // If not multiline, fall through so that the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3624
            // is marked as hit; this must be a /r/n or a /n
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3625
            // at the very end so the end was hit; more input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3626
            // could make this not match here
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3627
            if (i < endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3628
                char ch = seq.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3629
                 if (ch == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3630
                     // No match between \r\n
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3631
                     if (i > 0 && seq.charAt(i-1) == '\r')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3632
                         return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3633
                     if (multiline)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3634
                         return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3635
                 } else if (ch == '\r' || ch == '\u0085' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3636
                            (ch|1) == '\u2029') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3637
                     if (multiline)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3638
                         return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3639
                 } else { // No line terminator, no match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3640
                     return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3641
                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3642
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3643
            // Matched at current end so hit end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3644
            matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3645
            // If a $ matches because of end of input, then more input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3646
            // could cause it to fail!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3647
            matcher.requireEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3648
            return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3649
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3650
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3651
            next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3652
            return info.deterministic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3653
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3654
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3655
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3656
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3657
     * Node to anchor at the end of a line or the end of input based on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3658
     * multiline mode when in unix lines mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3659
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3660
    static final class UnixDollar extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3661
        boolean multiline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3662
        UnixDollar(boolean mul) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3663
            multiline = mul;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3664
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3665
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3666
            int endIndex = (matcher.anchoringBounds) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3667
                matcher.to : matcher.getTextLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3668
            if (i < endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3669
                char ch = seq.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3670
                if (ch == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3671
                    // If not multiline, then only possible to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3672
                    // match at very end or one before end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3673
                    if (multiline == false && i != endIndex - 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3674
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3675
                    // If multiline return next.match without setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3676
                    // matcher.hitEnd
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3677
                    if (multiline)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3678
                        return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3679
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3680
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3681
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3682
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3683
            // Matching because at the end or 1 before the end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3684
            // more input could change this so set hitEnd
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3685
            matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3686
            // If a $ matches because of end of input, then more input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3687
            // could cause it to fail!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3688
            matcher.requireEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3689
            return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3690
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3691
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3692
            next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3693
            return info.deterministic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3694
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3695
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3696
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3697
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3698
     * Abstract node class to match one character satisfying some
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3699
     * boolean property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3700
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3701
    private static abstract class CharProperty extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3702
        abstract boolean isSatisfiedBy(int ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3703
        CharProperty complement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3704
            return new CharProperty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3705
                    boolean isSatisfiedBy(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3706
                        return ! CharProperty.this.isSatisfiedBy(ch);}};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3707
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3708
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3709
            if (i < matcher.to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3710
                int ch = Character.codePointAt(seq, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3711
                return isSatisfiedBy(ch)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3712
                    && next.match(matcher, i+Character.charCount(ch), seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3713
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3714
                matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3715
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3716
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3717
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3718
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3719
            info.minLength++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3720
            info.maxLength++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3721
            return next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3722
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3723
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3724
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3725
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3726
     * Optimized version of CharProperty that works only for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3727
     * properties never satisfied by Supplementary characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3728
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3729
    private static abstract class BmpCharProperty extends CharProperty {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3730
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3731
            if (i < matcher.to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3732
                return isSatisfiedBy(seq.charAt(i))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3733
                    && next.match(matcher, i+1, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3734
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3735
                matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3736
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3737
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3738
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3739
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3740
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3741
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3742
     * Node class that matches a Supplementary Unicode character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3743
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3744
    static final class SingleS extends CharProperty {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3745
        final int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3746
        SingleS(int c) { this.c = c; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3747
        boolean isSatisfiedBy(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3748
            return ch == c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3749
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3750
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3751
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3752
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3753
     * Optimization -- matches a given BMP character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3754
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3755
    static final class Single extends BmpCharProperty {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3756
        final int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3757
        Single(int c) { this.c = c; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3758
        boolean isSatisfiedBy(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3759
            return ch == c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3760
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3761
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3762
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3763
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3764
     * Case insensitive matches a given BMP character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3765
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3766
    static final class SingleI extends BmpCharProperty {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3767
        final int lower;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3768
        final int upper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3769
        SingleI(int lower, int upper) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3770
            this.lower = lower;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3771
            this.upper = upper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3772
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3773
        boolean isSatisfiedBy(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3774
            return ch == lower || ch == upper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3776
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3777
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3778
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3779
     * Unicode case insensitive matches a given Unicode character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3780
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3781
    static final class SingleU extends CharProperty {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3782
        final int lower;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3783
        SingleU(int lower) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3784
            this.lower = lower;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3785
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3786
        boolean isSatisfiedBy(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3787
            return lower == ch ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3788
                lower == Character.toLowerCase(Character.toUpperCase(ch));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3789
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3790
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3791
4820
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3792
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3793
    /**
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3794
     * Node class that matches a Unicode block.
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3795
     */
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3796
    static final class Block extends CharProperty {
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3797
        final Character.UnicodeBlock block;
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3798
        Block(Character.UnicodeBlock block) {
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3799
            this.block = block;
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3800
        }
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3801
        boolean isSatisfiedBy(int ch) {
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3802
            return block == Character.UnicodeBlock.of(ch);
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3803
        }
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3804
    }
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3805
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3806
    /**
5610
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  3807
     * Node class that matches a Unicode script
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  3808
     */
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  3809
    static final class Script extends CharProperty {
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  3810
        final Character.UnicodeScript script;
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  3811
        Script(Character.UnicodeScript script) {
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  3812
            this.script = script;
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  3813
        }
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  3814
        boolean isSatisfiedBy(int ch) {
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  3815
            return script == Character.UnicodeScript.of(ch);
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  3816
        }
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  3817
    }
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  3818
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  3819
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3820
     * Node class that matches a Unicode category.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3821
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3822
    static final class Category extends CharProperty {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3823
        final int typeMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3824
        Category(int typeMask) { this.typeMask = typeMask; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3825
        boolean isSatisfiedBy(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3826
            return (typeMask & (1 << Character.getType(ch))) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3827
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3828
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3829
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3830
    /**
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  3831
     * Node class that matches a Unicode "type"
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  3832
     */
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  3833
    static final class Utype extends CharProperty {
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  3834
        final UnicodeProp uprop;
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  3835
        Utype(UnicodeProp uprop) { this.uprop = uprop; }
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  3836
        boolean isSatisfiedBy(int ch) {
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  3837
            return uprop.is(ch);
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  3838
        }
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  3839
    }
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  3840
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  3841
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  3842
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3843
     * Node class that matches a POSIX type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3844
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3845
    static final class Ctype extends BmpCharProperty {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3846
        final int ctype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3847
        Ctype(int ctype) { this.ctype = ctype; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3848
        boolean isSatisfiedBy(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3849
            return ch < 128 && ASCII.isType(ch, ctype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3850
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3851
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3852
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3853
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3854
     * Base class for all Slice nodes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3855
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3856
    static class SliceNode extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3857
        int[] buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3858
        SliceNode(int[] buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3859
            buffer = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3860
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3861
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3862
            info.minLength += buffer.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3863
            info.maxLength += buffer.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3864
            return next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3865
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3866
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3867
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3868
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3869
     * Node class for a case sensitive/BMP-only sequence of literal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3870
     * characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3871
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3872
    static final class Slice extends SliceNode {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3873
        Slice(int[] buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3874
            super(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3875
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3876
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3877
            int[] buf = buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3878
            int len = buf.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3879
            for (int j=0; j<len; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3880
                if ((i+j) >= matcher.to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3881
                    matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3882
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3883
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3884
                if (buf[j] != seq.charAt(i+j))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3885
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3886
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3887
            return next.match(matcher, i+len, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3888
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3889
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3890
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3891
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3892
     * Node class for a case_insensitive/BMP-only sequence of literal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3893
     * characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3894
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3895
    static class SliceI extends SliceNode {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3896
        SliceI(int[] buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3897
            super(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3898
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3899
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3900
            int[] buf = buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3901
            int len = buf.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3902
            for (int j=0; j<len; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3903
                if ((i+j) >= matcher.to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3904
                    matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3905
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3906
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3907
                int c = seq.charAt(i+j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3908
                if (buf[j] != c &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3909
                    buf[j] != ASCII.toLower(c))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3910
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3911
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3912
            return next.match(matcher, i+len, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3913
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3914
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3915
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3916
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3917
     * Node class for a unicode_case_insensitive/BMP-only sequence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3918
     * literal characters. Uses unicode case folding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3919
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3920
    static final class SliceU extends SliceNode {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3921
        SliceU(int[] buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3922
            super(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3923
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3924
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3925
            int[] buf = buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3926
            int len = buf.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3927
            for (int j=0; j<len; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3928
                if ((i+j) >= matcher.to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3929
                    matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3930
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3931
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3932
                int c = seq.charAt(i+j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3933
                if (buf[j] != c &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3934
                    buf[j] != Character.toLowerCase(Character.toUpperCase(c)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3935
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3936
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3937
            return next.match(matcher, i+len, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3938
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3939
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3940
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3941
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3942
     * Node class for a case sensitive sequence of literal characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3943
     * including supplementary characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3944
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3945
    static final class SliceS extends SliceNode {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3946
        SliceS(int[] buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3947
            super(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3948
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3949
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3950
            int[] buf = buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3951
            int x = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3952
            for (int j = 0; j < buf.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3953
                if (x >= matcher.to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3954
                    matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3955
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3956
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3957
                int c = Character.codePointAt(seq, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3958
                if (buf[j] != c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3959
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3960
                x += Character.charCount(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3961
                if (x > matcher.to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3962
                    matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3963
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3964
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3965
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3966
            return next.match(matcher, x, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3967
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3968
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3969
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3970
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3971
     * Node class for a case insensitive sequence of literal characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3972
     * including supplementary characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3973
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3974
    static class SliceIS extends SliceNode {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3975
        SliceIS(int[] buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3976
            super(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3977
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3978
        int toLower(int c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3979
            return ASCII.toLower(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3980
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3981
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3982
            int[] buf = buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3983
            int x = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3984
            for (int j = 0; j < buf.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3985
                if (x >= matcher.to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3986
                    matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3987
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3988
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3989
                int c = Character.codePointAt(seq, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3990
                if (buf[j] != c && buf[j] != toLower(c))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3991
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3992
                x += Character.charCount(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3993
                if (x > matcher.to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3994
                    matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3995
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3996
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3997
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3998
            return next.match(matcher, x, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3999
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4000
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4002
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4003
     * Node class for a case insensitive sequence of literal characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4004
     * Uses unicode case folding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4005
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4006
    static final class SliceUS extends SliceIS {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4007
        SliceUS(int[] buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4008
            super(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4009
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4010
        int toLower(int c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4011
            return Character.toLowerCase(Character.toUpperCase(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4012
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4013
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4014
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4015
    private static boolean inRange(int lower, int ch, int upper) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4016
        return lower <= ch && ch <= upper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4017
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4019
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4020
     * Returns node for matching characters within an explicit value range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4021
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4022
    private static CharProperty rangeFor(final int lower,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4023
                                         final int upper) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4024
        return new CharProperty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4025
                boolean isSatisfiedBy(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4026
                    return inRange(lower, ch, upper);}};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4027
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4028
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4029
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4030
     * Returns node for matching characters within an explicit value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4031
     * range in a case insensitive manner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4032
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4033
    private CharProperty caseInsensitiveRangeFor(final int lower,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4034
                                                 final int upper) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4035
        if (has(UNICODE_CASE))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4036
            return new CharProperty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4037
                boolean isSatisfiedBy(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4038
                    if (inRange(lower, ch, upper))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4039
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4040
                    int up = Character.toUpperCase(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4041
                    return inRange(lower, up, upper) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4042
                           inRange(lower, Character.toLowerCase(up), upper);}};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4043
        return new CharProperty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4044
            boolean isSatisfiedBy(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4045
                return inRange(lower, ch, upper) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4046
                    ASCII.isAscii(ch) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4047
                        (inRange(lower, ASCII.toUpper(ch), upper) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4048
                         inRange(lower, ASCII.toLower(ch), upper));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4049
            }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4050
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4052
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4053
     * Implements the Unicode category ALL and the dot metacharacter when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4054
     * in dotall mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4055
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4056
    static final class All extends CharProperty {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4057
        boolean isSatisfiedBy(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4058
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4059
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4060
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4062
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4063
     * Node class for the dot metacharacter when dotall is not enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4064
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4065
    static final class Dot extends CharProperty {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4066
        boolean isSatisfiedBy(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4067
            return (ch != '\n' && ch != '\r'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4068
                    && (ch|1) != '\u2029'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4069
                    && ch != '\u0085');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4070
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4071
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4073
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4074
     * Node class for the dot metacharacter when dotall is not enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4075
     * but UNIX_LINES is enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4076
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4077
    static final class UnixDot extends CharProperty {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4078
        boolean isSatisfiedBy(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4079
            return ch != '\n';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4080
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4081
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4083
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4084
     * The 0 or 1 quantifier. This one class implements all three types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4085
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4086
    static final class Ques extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4087
        Node atom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4088
        int type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4089
        Ques(Node node, int type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4090
            this.atom = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4091
            this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4092
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4093
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4094
            switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4095
            case GREEDY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4096
                return (atom.match(matcher, i, seq) && next.match(matcher, matcher.last, seq))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4097
                    || next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4098
            case LAZY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4099
                return next.match(matcher, i, seq)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4100
                    || (atom.match(matcher, i, seq) && next.match(matcher, matcher.last, seq));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4101
            case POSSESSIVE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4102
                if (atom.match(matcher, i, seq)) i = matcher.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4103
                return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4104
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4105
                return atom.match(matcher, i, seq) && next.match(matcher, matcher.last, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4108
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4109
            if (type != INDEPENDENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4110
                int minL = info.minLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4111
                atom.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4112
                info.minLength = minL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4113
                info.deterministic = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4114
                return next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4115
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4116
                atom.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4117
                return next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4123
     * Handles the curly-brace style repetition with a specified minimum and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4124
     * maximum occurrences. The * quantifier is handled as a special case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4125
     * This class handles the three types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4127
    static final class Curly extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4128
        Node atom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4129
        int type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4130
        int cmin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4131
        int cmax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4133
        Curly(Node node, int cmin, int cmax, int type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4134
            this.atom = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4135
            this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4136
            this.cmin = cmin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4137
            this.cmax = cmax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4139
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4140
            int j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4141
            for (j = 0; j < cmin; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4142
                if (atom.match(matcher, i, seq)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4143
                    i = matcher.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4144
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4145
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4146
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4148
            if (type == GREEDY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4149
                return match0(matcher, i, j, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4150
            else if (type == LAZY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4151
                return match1(matcher, i, j, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4152
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4153
                return match2(matcher, i, j, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4155
        // Greedy match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4156
        // i is the index to start matching at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4157
        // j is the number of atoms that have matched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4158
        boolean match0(Matcher matcher, int i, int j, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4159
            if (j >= cmax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4160
                // We have matched the maximum... continue with the rest of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4161
                // the regular expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4162
                return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4164
            int backLimit = j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4165
            while (atom.match(matcher, i, seq)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4166
                // k is the length of this match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4167
                int k = matcher.last - i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4168
                if (k == 0) // Zero length match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4169
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4170
                // Move up index and number matched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4171
                i = matcher.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4172
                j++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4173
                // We are greedy so match as many as we can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4174
                while (j < cmax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4175
                    if (!atom.match(matcher, i, seq))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4176
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4177
                    if (i + k != matcher.last) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4178
                        if (match0(matcher, matcher.last, j+1, seq))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4179
                            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4180
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4181
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4182
                    i += k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4183
                    j++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4184
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4185
                // Handle backing off if match fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4186
                while (j >= backLimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4187
                   if (next.match(matcher, i, seq))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4188
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4189
                    i -= k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4190
                    j--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4191
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4192
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4194
            return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4196
        // Reluctant match. At this point, the minimum has been satisfied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4197
        // i is the index to start matching at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4198
        // j is the number of atoms that have matched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4199
        boolean match1(Matcher matcher, int i, int j, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4200
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4201
                // Try finishing match without consuming any more
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4202
                if (next.match(matcher, i, seq))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4203
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4204
                // At the maximum, no match found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4205
                if (j >= cmax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4206
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4207
                // Okay, must try one more atom
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4208
                if (!atom.match(matcher, i, seq))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4209
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4210
                // If we haven't moved forward then must break out
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4211
                if (i == matcher.last)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4212
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4213
                // Move up index and number matched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4214
                i = matcher.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4215
                j++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4216
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4218
        boolean match2(Matcher matcher, int i, int j, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4219
            for (; j < cmax; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4220
                if (!atom.match(matcher, i, seq))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4221
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4222
                if (i == matcher.last)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4223
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4224
                i = matcher.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4225
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4226
            return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4228
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4229
            // Save original info
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4230
            int minL = info.minLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4231
            int maxL = info.maxLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4232
            boolean maxV = info.maxValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4233
            boolean detm = info.deterministic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4234
            info.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4236
            atom.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4237
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4238
            int temp = info.minLength * cmin + minL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4239
            if (temp < minL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4240
                temp = 0xFFFFFFF; // arbitrary large number
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4242
            info.minLength = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4243
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4244
            if (maxV & info.maxValid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4245
                temp = info.maxLength * cmax + maxL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4246
                info.maxLength = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4247
                if (temp < maxL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4248
                    info.maxValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4249
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4250
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4251
                info.maxValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4253
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4254
            if (info.deterministic && cmin == cmax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4255
                info.deterministic = detm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4256
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4257
                info.deterministic = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4258
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4259
            return next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4262
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4263
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4264
     * Handles the curly-brace style repetition with a specified minimum and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4265
     * maximum occurrences in deterministic cases. This is an iterative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4266
     * optimization over the Prolog and Loop system which would handle this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4267
     * in a recursive way. The * quantifier is handled as a special case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4268
     * If capture is true then this class saves group settings and ensures
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4269
     * that groups are unset when backing off of a group match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4270
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4271
    static final class GroupCurly extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4272
        Node atom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4273
        int type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4274
        int cmin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4275
        int cmax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4276
        int localIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4277
        int groupIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4278
        boolean capture;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4280
        GroupCurly(Node node, int cmin, int cmax, int type, int local,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4281
                   int group, boolean capture) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4282
            this.atom = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4283
            this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4284
            this.cmin = cmin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4285
            this.cmax = cmax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4286
            this.localIndex = local;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4287
            this.groupIndex = group;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4288
            this.capture = capture;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4290
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4291
            int[] groups = matcher.groups;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4292
            int[] locals = matcher.locals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4293
            int save0 = locals[localIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4294
            int save1 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4295
            int save2 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4296
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4297
            if (capture) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4298
                save1 = groups[groupIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4299
                save2 = groups[groupIndex+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4300
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4301
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4302
            // Notify GroupTail there is no need to setup group info
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4303
            // because it will be set here
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4304
            locals[localIndex] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4305
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4306
            boolean ret = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4307
            for (int j = 0; j < cmin; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4308
                if (atom.match(matcher, i, seq)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4309
                    if (capture) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4310
                        groups[groupIndex] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4311
                        groups[groupIndex+1] = matcher.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4312
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4313
                    i = matcher.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4314
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4315
                    ret = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4316
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4317
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4318
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4319
            if (ret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4320
                if (type == GREEDY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4321
                    ret = match0(matcher, i, cmin, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4322
                } else if (type == LAZY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4323
                    ret = match1(matcher, i, cmin, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4324
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4325
                    ret = match2(matcher, i, cmin, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4326
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4327
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4328
            if (!ret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4329
                locals[localIndex] = save0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4330
                if (capture) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4331
                    groups[groupIndex] = save1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4332
                    groups[groupIndex+1] = save2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4333
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4334
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4335
            return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4337
        // Aggressive group match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4338
        boolean match0(Matcher matcher, int i, int j, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4339
            int[] groups = matcher.groups;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4340
            int save0 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4341
            int save1 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4342
            if (capture) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4343
                save0 = groups[groupIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4344
                save1 = groups[groupIndex+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4345
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4346
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4347
                if (j >= cmax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4348
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4349
                if (!atom.match(matcher, i, seq))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4350
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4351
                int k = matcher.last - i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4352
                if (k <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4353
                    if (capture) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4354
                        groups[groupIndex] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4355
                        groups[groupIndex+1] = i + k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4356
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4357
                    i = i + k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4358
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4359
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4360
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4361
                    if (capture) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4362
                        groups[groupIndex] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4363
                        groups[groupIndex+1] = i + k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4364
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4365
                    i = i + k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4366
                    if (++j >= cmax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4367
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4368
                    if (!atom.match(matcher, i, seq))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4369
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4370
                    if (i + k != matcher.last) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4371
                        if (match0(matcher, i, j, seq))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4372
                            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4373
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4374
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4375
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4376
                while (j > cmin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4377
                    if (next.match(matcher, i, seq)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4378
                        if (capture) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4379
                            groups[groupIndex+1] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4380
                            groups[groupIndex] = i - k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4381
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4382
                        i = i - k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4383
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4384
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4385
                    // backing off
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4386
                    if (capture) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4387
                        groups[groupIndex+1] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4388
                        groups[groupIndex] = i - k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4389
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4390
                    i = i - k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4391
                    j--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4392
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4393
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4394
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4395
            if (capture) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4396
                groups[groupIndex] = save0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4397
                groups[groupIndex+1] = save1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4398
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4399
            return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4401
        // Reluctant matching
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4402
        boolean match1(Matcher matcher, int i, int j, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4403
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4404
                if (next.match(matcher, i, seq))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4405
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4406
                if (j >= cmax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4407
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4408
                if (!atom.match(matcher, i, seq))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4409
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4410
                if (i == matcher.last)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4411
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4412
                if (capture) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4413
                    matcher.groups[groupIndex] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4414
                    matcher.groups[groupIndex+1] = matcher.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4415
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4416
                i = matcher.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4417
                j++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4418
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4420
        // Possessive matching
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4421
        boolean match2(Matcher matcher, int i, int j, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4422
            for (; j < cmax; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4423
                if (!atom.match(matcher, i, seq)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4424
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4425
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4426
                if (capture) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4427
                    matcher.groups[groupIndex] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4428
                    matcher.groups[groupIndex+1] = matcher.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4429
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4430
                if (i == matcher.last) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4431
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4432
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4433
                i = matcher.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4434
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4435
            return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4437
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4438
            // Save original info
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4439
            int minL = info.minLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4440
            int maxL = info.maxLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4441
            boolean maxV = info.maxValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4442
            boolean detm = info.deterministic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4443
            info.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4444
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4445
            atom.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4446
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4447
            int temp = info.minLength * cmin + minL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4448
            if (temp < minL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4449
                temp = 0xFFFFFFF; // Arbitrary large number
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4450
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4451
            info.minLength = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4452
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4453
            if (maxV & info.maxValid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4454
                temp = info.maxLength * cmax + maxL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4455
                info.maxLength = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4456
                if (temp < maxL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4457
                    info.maxValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4458
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4459
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4460
                info.maxValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4461
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4462
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4463
            if (info.deterministic && cmin == cmax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4464
                info.deterministic = detm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4465
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4466
                info.deterministic = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4467
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4468
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4469
            return next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4470
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4472
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4473
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4474
     * A Guard node at the end of each atom node in a Branch. It
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4475
     * serves the purpose of chaining the "match" operation to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4476
     * "next" but not the "study", so we can collect the TreeInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4477
     * of each atom node without including the TreeInfo of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4478
     * "next".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4479
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4480
    static final class BranchConn extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4481
        BranchConn() {};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4482
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4483
            return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4485
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4486
            return info.deterministic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4489
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4490
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4491
     * Handles the branching of alternations. Note this is also used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4492
     * the ? quantifier to branch between the case where it matches once
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4493
     * and where it does not occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4494
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4495
    static final class Branch extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4496
        Node[] atoms = new Node[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4497
        int size = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4498
        Node conn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4499
        Branch(Node first, Node second, Node branchConn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4500
            conn = branchConn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4501
            atoms[0] = first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4502
            atoms[1] = second;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4503
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4504
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4505
        void add(Node node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4506
            if (size >= atoms.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4507
                Node[] tmp = new Node[atoms.length*2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4508
                System.arraycopy(atoms, 0, tmp, 0, atoms.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4509
                atoms = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4510
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4511
            atoms[size++] = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4512
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4513
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4514
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4515
            for (int n = 0; n < size; n++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4516
                if (atoms[n] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4517
                    if (conn.next.match(matcher, i, seq))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4518
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4519
                } else if (atoms[n].match(matcher, i, seq)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4520
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4521
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4522
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4523
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4524
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4525
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4526
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4527
            int minL = info.minLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4528
            int maxL = info.maxLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4529
            boolean maxV = info.maxValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4530
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4531
            int minL2 = Integer.MAX_VALUE; //arbitrary large enough num
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4532
            int maxL2 = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4533
            for (int n = 0; n < size; n++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4534
                info.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4535
                if (atoms[n] != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4536
                    atoms[n].study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4537
                minL2 = Math.min(minL2, info.minLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4538
                maxL2 = Math.max(maxL2, info.maxLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4539
                maxV = (maxV & info.maxValid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4540
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4541
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4542
            minL += minL2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4543
            maxL += maxL2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4544
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4545
            info.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4546
            conn.next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4547
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4548
            info.minLength += minL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4549
            info.maxLength += maxL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4550
            info.maxValid &= maxV;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4551
            info.deterministic = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4552
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4553
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4555
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4556
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4557
     * The GroupHead saves the location where the group begins in the locals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4558
     * and restores them when the match is done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4559
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4560
     * The matchRef is used when a reference to this group is accessed later
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4561
     * in the expression. The locals will have a negative value in them to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4562
     * indicate that we do not want to unset the group if the reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4563
     * doesn't match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4564
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4565
    static final class GroupHead extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4566
        int localIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4567
        GroupHead(int localCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4568
            localIndex = localCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4569
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4570
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4571
            int save = matcher.locals[localIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4572
            matcher.locals[localIndex] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4573
            boolean ret = next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4574
            matcher.locals[localIndex] = save;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4575
            return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4577
        boolean matchRef(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4578
            int save = matcher.locals[localIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4579
            matcher.locals[localIndex] = ~i; // HACK
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4580
            boolean ret = next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4581
            matcher.locals[localIndex] = save;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4582
            return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4583
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4584
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4585
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4586
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4587
     * Recursive reference to a group in the regular expression. It calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4588
     * matchRef because if the reference fails to match we would not unset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4589
     * the group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4590
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4591
    static final class GroupRef extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4592
        GroupHead head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4593
        GroupRef(GroupHead head) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4594
            this.head = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4595
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4596
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4597
            return head.matchRef(matcher, i, seq)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4598
                && next.match(matcher, matcher.last, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4599
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4600
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4601
            info.maxValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4602
            info.deterministic = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4603
            return next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4604
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4605
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4606
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4607
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4608
     * The GroupTail handles the setting of group beginning and ending
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4609
     * locations when groups are successfully matched. It must also be able to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4610
     * unset groups that have to be backed off of.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4611
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4612
     * The GroupTail node is also used when a previous group is referenced,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4613
     * and in that case no group information needs to be set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4614
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4615
    static final class GroupTail extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4616
        int localIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4617
        int groupIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4618
        GroupTail(int localCount, int groupCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4619
            localIndex = localCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4620
            groupIndex = groupCount + groupCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4621
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4622
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4623
            int tmp = matcher.locals[localIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4624
            if (tmp >= 0) { // This is the normal group case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4625
                // Save the group so we can unset it if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4626
                // backs off of a match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4627
                int groupStart = matcher.groups[groupIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4628
                int groupEnd = matcher.groups[groupIndex+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4629
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4630
                matcher.groups[groupIndex] = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4631
                matcher.groups[groupIndex+1] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4632
                if (next.match(matcher, i, seq)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4633
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4634
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4635
                matcher.groups[groupIndex] = groupStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4636
                matcher.groups[groupIndex+1] = groupEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4637
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4638
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4639
                // This is a group reference case. We don't need to save any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4640
                // group info because it isn't really a group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4641
                matcher.last = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4642
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4643
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4644
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4645
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4646
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4647
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4648
     * This sets up a loop to handle a recursive quantifier structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4649
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4650
    static final class Prolog extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4651
        Loop loop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4652
        Prolog(Loop loop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4653
            this.loop = loop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4654
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4655
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4656
            return loop.matchInit(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4657
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4658
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4659
            return loop.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4660
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4661
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4662
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4663
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4664
     * Handles the repetition count for a greedy Curly. The matchInit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4665
     * is called from the Prolog to save the index of where the group
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4666
     * beginning is stored. A zero length group check occurs in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4667
     * normal match but is skipped in the matchInit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4668
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4669
    static class Loop extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4670
        Node body;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4671
        int countIndex; // local count index in matcher locals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4672
        int beginIndex; // group beginning index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4673
        int cmin, cmax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4674
        Loop(int countIndex, int beginIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4675
            this.countIndex = countIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4676
            this.beginIndex = beginIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4677
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4678
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4679
            // Avoid infinite loop in zero-length case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4680
            if (i > matcher.locals[beginIndex]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4681
                int count = matcher.locals[countIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4682
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4683
                // This block is for before we reach the minimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4684
                // iterations required for the loop to match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4685
                if (count < cmin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4686
                    matcher.locals[countIndex] = count + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4687
                    boolean b = body.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4688
                    // If match failed we must backtrack, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4689
                    // the loop count should NOT be incremented
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4690
                    if (!b)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4691
                        matcher.locals[countIndex] = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4692
                    // Return success or failure since we are under
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4693
                    // minimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4694
                    return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4695
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4696
                // This block is for after we have the minimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4697
                // iterations required for the loop to match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4698
                if (count < cmax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4699
                    matcher.locals[countIndex] = count + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4700
                    boolean b = body.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4701
                    // If match failed we must backtrack, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4702
                    // the loop count should NOT be incremented
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4703
                    if (!b)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4704
                        matcher.locals[countIndex] = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4705
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4706
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4707
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4708
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4709
            return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4710
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4711
        boolean matchInit(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4712
            int save = matcher.locals[countIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4713
            boolean ret = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4714
            if (0 < cmin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4715
                matcher.locals[countIndex] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4716
                ret = body.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4717
            } else if (0 < cmax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4718
                matcher.locals[countIndex] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4719
                ret = body.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4720
                if (ret == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4721
                    ret = next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4722
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4723
                ret = next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4724
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4725
            matcher.locals[countIndex] = save;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4726
            return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4727
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4728
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4729
            info.maxValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4730
            info.deterministic = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4731
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4732
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4733
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4734
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4735
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4736
     * Handles the repetition count for a reluctant Curly. The matchInit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4737
     * is called from the Prolog to save the index of where the group
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4738
     * beginning is stored. A zero length group check occurs in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4739
     * normal match but is skipped in the matchInit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4740
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4741
    static final class LazyLoop extends Loop {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4742
        LazyLoop(int countIndex, int beginIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4743
            super(countIndex, beginIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4744
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4745
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4746
            // Check for zero length group
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4747
            if (i > matcher.locals[beginIndex]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4748
                int count = matcher.locals[countIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4749
                if (count < cmin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4750
                    matcher.locals[countIndex] = count + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4751
                    boolean result = body.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4752
                    // If match failed we must backtrack, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4753
                    // the loop count should NOT be incremented
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4754
                    if (!result)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4755
                        matcher.locals[countIndex] = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4756
                    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4757
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4758
                if (next.match(matcher, i, seq))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4759
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4760
                if (count < cmax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4761
                    matcher.locals[countIndex] = count + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4762
                    boolean result = body.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4763
                    // If match failed we must backtrack, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4764
                    // the loop count should NOT be incremented
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4765
                    if (!result)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4766
                        matcher.locals[countIndex] = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4767
                    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4768
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4769
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4770
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4771
            return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4772
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4773
        boolean matchInit(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4774
            int save = matcher.locals[countIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4775
            boolean ret = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4776
            if (0 < cmin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4777
                matcher.locals[countIndex] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4778
                ret = body.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4779
            } else if (next.match(matcher, i, seq)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4780
                ret = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4781
            } else if (0 < cmax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4782
                matcher.locals[countIndex] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4783
                ret = body.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4784
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4785
            matcher.locals[countIndex] = save;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4786
            return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4787
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4788
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4789
            info.maxValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4790
            info.deterministic = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4791
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4792
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4793
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4794
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4795
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4796
     * Refers to a group in the regular expression. Attempts to match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4797
     * whatever the group referred to last matched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4798
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4799
    static class BackRef extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4800
        int groupIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4801
        BackRef(int groupCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4802
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4803
            groupIndex = groupCount + groupCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4804
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4805
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4806
            int j = matcher.groups[groupIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4807
            int k = matcher.groups[groupIndex+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4808
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4809
            int groupSize = k - j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4810
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4811
            // If the referenced group didn't match, neither can this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4812
            if (j < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4813
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4814
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4815
            // If there isn't enough input left no match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4816
            if (i + groupSize > matcher.to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4817
                matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4818
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4819
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4820
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4821
            // Check each new char to make sure it matches what the group
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4822
            // referenced matched last time around
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4823
            for (int index=0; index<groupSize; index++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4824
                if (seq.charAt(i+index) != seq.charAt(j+index))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4825
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4826
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4827
            return next.match(matcher, i+groupSize, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4829
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4830
            info.maxValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4831
            return next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4832
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4833
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4834
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4835
    static class CIBackRef extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4836
        int groupIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4837
        boolean doUnicodeCase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4838
        CIBackRef(int groupCount, boolean doUnicodeCase) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4839
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4840
            groupIndex = groupCount + groupCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4841
            this.doUnicodeCase = doUnicodeCase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4843
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4844
            int j = matcher.groups[groupIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4845
            int k = matcher.groups[groupIndex+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4846
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4847
            int groupSize = k - j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4848
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4849
            // If the referenced group didn't match, neither can this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4850
            if (j < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4851
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4852
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4853
            // If there isn't enough input left no match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4854
            if (i + groupSize > matcher.to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4855
                matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4856
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4857
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4858
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4859
            // Check each new char to make sure it matches what the group
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4860
            // referenced matched last time around
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4861
            int x = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4862
            for (int index=0; index<groupSize; index++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4863
                int c1 = Character.codePointAt(seq, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4864
                int c2 = Character.codePointAt(seq, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4865
                if (c1 != c2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4866
                    if (doUnicodeCase) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4867
                        int cc1 = Character.toUpperCase(c1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4868
                        int cc2 = Character.toUpperCase(c2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4869
                        if (cc1 != cc2 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4870
                            Character.toLowerCase(cc1) !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4871
                            Character.toLowerCase(cc2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4872
                            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4873
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4874
                        if (ASCII.toLower(c1) != ASCII.toLower(c2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4875
                            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4876
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4877
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4878
                x += Character.charCount(c1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4879
                j += Character.charCount(c2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4880
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4881
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4882
            return next.match(matcher, i+groupSize, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4883
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4884
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4885
            info.maxValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4886
            return next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4887
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4888
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4889
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4890
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4891
     * Searches until the next instance of its atom. This is useful for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4892
     * finding the atom efficiently without passing an instance of it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4893
     * (greedy problem) and without a lot of wasted search time (reluctant
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4894
     * problem).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4895
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4896
    static final class First extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4897
        Node atom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4898
        First(Node node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4899
            this.atom = BnM.optimize(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4900
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4901
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4902
            if (atom instanceof BnM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4903
                return atom.match(matcher, i, seq)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4904
                    && next.match(matcher, matcher.last, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4905
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4906
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4907
                if (i > matcher.to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4908
                    matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4909
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4910
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4911
                if (atom.match(matcher, i, seq)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4912
                    return next.match(matcher, matcher.last, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4913
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4914
                i += countChars(seq, i, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4915
                matcher.first++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4916
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4917
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4918
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4919
            atom.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4920
            info.maxValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4921
            info.deterministic = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4922
            return next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4923
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4924
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4925
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4926
    static final class Conditional extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4927
        Node cond, yes, not;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4928
        Conditional(Node cond, Node yes, Node not) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4929
            this.cond = cond;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4930
            this.yes = yes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4931
            this.not = not;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4932
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4933
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4934
            if (cond.match(matcher, i, seq)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4935
                return yes.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4936
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4937
                return not.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4938
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4939
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4940
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4941
            int minL = info.minLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4942
            int maxL = info.maxLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4943
            boolean maxV = info.maxValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4944
            info.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4945
            yes.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4946
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4947
            int minL2 = info.minLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4948
            int maxL2 = info.maxLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4949
            boolean maxV2 = info.maxValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4950
            info.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4951
            not.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4952
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4953
            info.minLength = minL + Math.min(minL2, info.minLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4954
            info.maxLength = maxL + Math.max(maxL2, info.maxLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4955
            info.maxValid = (maxV & maxV2 & info.maxValid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4956
            info.deterministic = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4957
            return next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4958
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4959
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4960
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4961
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4962
     * Zero width positive lookahead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4963
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4964
    static final class Pos extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4965
        Node cond;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4966
        Pos(Node cond) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4967
            this.cond = cond;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4968
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4969
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4970
            int savedTo = matcher.to;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4971
            boolean conditionMatched = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4972
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4973
            // Relax transparent region boundaries for lookahead
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4974
            if (matcher.transparentBounds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4975
                matcher.to = matcher.getTextLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4976
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4977
                conditionMatched = cond.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4978
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4979
                // Reinstate region boundaries
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4980
                matcher.to = savedTo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4981
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4982
            return conditionMatched && next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4983
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4984
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4985
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4986
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4987
     * Zero width negative lookahead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4988
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4989
    static final class Neg extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4990
        Node cond;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4991
        Neg(Node cond) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4992
            this.cond = cond;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4993
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4994
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4995
            int savedTo = matcher.to;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4996
            boolean conditionMatched = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4997
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4998
            // Relax transparent region boundaries for lookahead
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4999
            if (matcher.transparentBounds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5000
                matcher.to = matcher.getTextLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5001
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5002
                if (i < matcher.to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5003
                    conditionMatched = !cond.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5004
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5005
                    // If a negative lookahead succeeds then more input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5006
                    // could cause it to fail!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5007
                    matcher.requireEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5008
                    conditionMatched = !cond.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5009
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5010
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5011
                // Reinstate region boundaries
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5012
                matcher.to = savedTo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5013
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5014
            return conditionMatched && next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5015
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5016
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5018
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5019
     * For use with lookbehinds; matches the position where the lookbehind
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5020
     * was encountered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5021
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5022
    static Node lookbehindEnd = new Node() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5023
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5024
            return i == matcher.lookbehindTo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5025
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5026
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5027
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5028
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5029
     * Zero width positive lookbehind.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5030
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5031
    static class Behind extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5032
        Node cond;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5033
        int rmax, rmin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5034
        Behind(Node cond, int rmax, int rmin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5035
            this.cond = cond;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5036
            this.rmax = rmax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5037
            this.rmin = rmin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5038
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5040
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5041
            int savedFrom = matcher.from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5042
            boolean conditionMatched = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5043
            int startIndex = (!matcher.transparentBounds) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5044
                             matcher.from : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5045
            int from = Math.max(i - rmax, startIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5046
            // Set end boundary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5047
            int savedLBT = matcher.lookbehindTo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5048
            matcher.lookbehindTo = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5049
            // Relax transparent region boundaries for lookbehind
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5050
            if (matcher.transparentBounds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5051
                matcher.from = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5052
            for (int j = i - rmin; !conditionMatched && j >= from; j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5053
                conditionMatched = cond.match(matcher, j, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5054
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5055
            matcher.from = savedFrom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5056
            matcher.lookbehindTo = savedLBT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5057
            return conditionMatched && next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5058
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5059
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5060
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5061
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5062
     * Zero width positive lookbehind, including supplementary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5063
     * characters or unpaired surrogates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5064
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5065
    static final class BehindS extends Behind {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5066
        BehindS(Node cond, int rmax, int rmin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5067
            super(cond, rmax, rmin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5068
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5069
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5070
            int rmaxChars = countChars(seq, i, -rmax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5071
            int rminChars = countChars(seq, i, -rmin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5072
            int savedFrom = matcher.from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5073
            int startIndex = (!matcher.transparentBounds) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5074
                             matcher.from : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5075
            boolean conditionMatched = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5076
            int from = Math.max(i - rmaxChars, startIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5077
            // Set end boundary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5078
            int savedLBT = matcher.lookbehindTo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5079
            matcher.lookbehindTo = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5080
            // Relax transparent region boundaries for lookbehind
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5081
            if (matcher.transparentBounds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5082
                matcher.from = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5084
            for (int j = i - rminChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5085
                 !conditionMatched && j >= from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5086
                 j -= j>from ? countChars(seq, j, -1) : 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5087
                conditionMatched = cond.match(matcher, j, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5088
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5089
            matcher.from = savedFrom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5090
            matcher.lookbehindTo = savedLBT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5091
            return conditionMatched && next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5092
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5093
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5095
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5096
     * Zero width negative lookbehind.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5097
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5098
    static class NotBehind extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5099
        Node cond;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5100
        int rmax, rmin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5101
        NotBehind(Node cond, int rmax, int rmin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5102
            this.cond = cond;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5103
            this.rmax = rmax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5104
            this.rmin = rmin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5107
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5108
            int savedLBT = matcher.lookbehindTo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5109
            int savedFrom = matcher.from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5110
            boolean conditionMatched = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5111
            int startIndex = (!matcher.transparentBounds) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5112
                             matcher.from : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5113
            int from = Math.max(i - rmax, startIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5114
            matcher.lookbehindTo = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5115
            // Relax transparent region boundaries for lookbehind
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5116
            if (matcher.transparentBounds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5117
                matcher.from = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5118
            for (int j = i - rmin; !conditionMatched && j >= from; j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5119
                conditionMatched = cond.match(matcher, j, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5121
            // Reinstate region boundaries
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5122
            matcher.from = savedFrom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5123
            matcher.lookbehindTo = savedLBT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5124
            return !conditionMatched && next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5127
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5129
     * Zero width negative lookbehind, including supplementary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5130
     * characters or unpaired surrogates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5132
    static final class NotBehindS extends NotBehind {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5133
        NotBehindS(Node cond, int rmax, int rmin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5134
            super(cond, rmax, rmin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5136
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5137
            int rmaxChars = countChars(seq, i, -rmax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5138
            int rminChars = countChars(seq, i, -rmin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5139
            int savedFrom = matcher.from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5140
            int savedLBT = matcher.lookbehindTo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5141
            boolean conditionMatched = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5142
            int startIndex = (!matcher.transparentBounds) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5143
                             matcher.from : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5144
            int from = Math.max(i - rmaxChars, startIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5145
            matcher.lookbehindTo = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5146
            // Relax transparent region boundaries for lookbehind
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5147
            if (matcher.transparentBounds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5148
                matcher.from = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5149
            for (int j = i - rminChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5150
                 !conditionMatched && j >= from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5151
                 j -= j>from ? countChars(seq, j, -1) : 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5152
                conditionMatched = cond.match(matcher, j, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5154
            //Reinstate region boundaries
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5155
            matcher.from = savedFrom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5156
            matcher.lookbehindTo = savedLBT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5157
            return !conditionMatched && next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5162
     * Returns the set union of two CharProperty nodes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5164
    private static CharProperty union(final CharProperty lhs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5165
                                      final CharProperty rhs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5166
        return new CharProperty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5167
                boolean isSatisfiedBy(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5168
                    return lhs.isSatisfiedBy(ch) || rhs.isSatisfiedBy(ch);}};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5170
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5172
     * Returns the set intersection of two CharProperty nodes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5174
    private static CharProperty intersection(final CharProperty lhs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5175
                                             final CharProperty rhs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5176
        return new CharProperty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5177
                boolean isSatisfiedBy(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5178
                    return lhs.isSatisfiedBy(ch) && rhs.isSatisfiedBy(ch);}};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5182
     * Returns the set difference of two CharProperty nodes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5184
    private static CharProperty setDifference(final CharProperty lhs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5185
                                              final CharProperty rhs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5186
        return new CharProperty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5187
                boolean isSatisfiedBy(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5188
                    return ! rhs.isSatisfiedBy(ch) && lhs.isSatisfiedBy(ch);}};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5190
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5192
     * Handles word boundaries. Includes a field to allow this one class to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5193
     * deal with the different types of word boundaries we can match. The word
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5194
     * characters include underscores, letters, and digits. Non spacing marks
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5195
     * can are also part of a word if they have a base character, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5196
     * they are ignored for purposes of finding word boundaries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5198
    static final class Bound extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5199
        static int LEFT = 0x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5200
        static int RIGHT= 0x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5201
        static int BOTH = 0x3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5202
        static int NONE = 0x4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5203
        int type;
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  5204
        boolean useUWORD;
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  5205
        Bound(int n, boolean useUWORD) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5206
            type = n;
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  5207
            this.useUWORD = useUWORD;
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  5208
        }
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  5209
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  5210
        boolean isWord(int ch) {
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  5211
            return useUWORD ? UnicodeProp.WORD.is(ch)
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  5212
                            : (ch == '_' || Character.isLetterOrDigit(ch));
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  5213
        }
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  5214
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5215
        int check(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5216
            int ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5217
            boolean left = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5218
            int startIndex = matcher.from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5219
            int endIndex = matcher.to;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5220
            if (matcher.transparentBounds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5221
                startIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5222
                endIndex = matcher.getTextLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5223
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5224
            if (i > startIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5225
                ch = Character.codePointBefore(seq, i);
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  5226
                left = (isWord(ch) ||
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5227
                    ((Character.getType(ch) == Character.NON_SPACING_MARK)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5228
                     && hasBaseCharacter(matcher, i-1, seq)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5230
            boolean right = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5231
            if (i < endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5232
                ch = Character.codePointAt(seq, i);
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  5233
                right = (isWord(ch) ||
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5234
                    ((Character.getType(ch) == Character.NON_SPACING_MARK)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5235
                     && hasBaseCharacter(matcher, i, seq)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5236
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5237
                // Tried to access char past the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5238
                matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5239
                // The addition of another char could wreck a boundary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5240
                matcher.requireEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5242
            return ((left ^ right) ? (right ? LEFT : RIGHT) : NONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5244
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5245
            return (check(matcher, i, seq) & type) > 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5246
                && next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5249
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5251
     * Non spacing marks only count as word characters in bounds calculations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5252
     * if they have a base character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5254
    private static boolean hasBaseCharacter(Matcher matcher, int i,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5255
                                            CharSequence seq)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5256
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5257
        int start = (!matcher.transparentBounds) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5258
            matcher.from : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5259
        for (int x=i; x >= start; x--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5260
            int ch = Character.codePointAt(seq, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5261
            if (Character.isLetterOrDigit(ch))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5262
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5263
            if (Character.getType(ch) == Character.NON_SPACING_MARK)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5264
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5265
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5267
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5269
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5271
     * Attempts to match a slice in the input using the Boyer-Moore string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5272
     * matching algorithm. The algorithm is based on the idea that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5273
     * pattern can be shifted farther ahead in the search text if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5274
     * matched right to left.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5275
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5276
     * The pattern is compared to the input one character at a time, from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5277
     * the rightmost character in the pattern to the left. If the characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5278
     * all match the pattern has been found. If a character does not match,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5279
     * the pattern is shifted right a distance that is the maximum of two
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5280
     * functions, the bad character shift and the good suffix shift. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5281
     * shift moves the attempted match position through the input more
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5282
     * quickly than a naive one position at a time check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5283
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5284
     * The bad character shift is based on the character from the text that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5285
     * did not match. If the character does not appear in the pattern, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5286
     * pattern can be shifted completely beyond the bad character. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5287
     * character does occur in the pattern, the pattern can be shifted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5288
     * line the pattern up with the next occurrence of that character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5289
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5290
     * The good suffix shift is based on the idea that some subset on the right
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5291
     * side of the pattern has matched. When a bad character is found, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5292
     * pattern can be shifted right by the pattern length if the subset does
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5293
     * not occur again in pattern, or by the amount of distance to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5294
     * next occurrence of the subset in the pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5296
     * Boyer-Moore search methods adapted from code by Amy Yu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5298
    static class BnM extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5299
        int[] buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5300
        int[] lastOcc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5301
        int[] optoSft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5302
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5303
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5304
         * Pre calculates arrays needed to generate the bad character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5305
         * shift and the good suffix shift. Only the last seven bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5306
         * are used to see if chars match; This keeps the tables small
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5307
         * and covers the heavily used ASCII range, but occasionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5308
         * results in an aliased match for the bad character shift.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5309
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5310
        static Node optimize(Node node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5311
            if (!(node instanceof Slice)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5312
                return node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5313
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5314
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5315
            int[] src = ((Slice) node).buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5316
            int patternLength = src.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5317
            // The BM algorithm requires a bit of overhead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5318
            // If the pattern is short don't use it, since
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5319
            // a shift larger than the pattern length cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5320
            // be used anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5321
            if (patternLength < 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5322
                return node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5323
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5324
            int i, j, k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5325
            int[] lastOcc = new int[128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5326
            int[] optoSft = new int[patternLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5327
            // Precalculate part of the bad character shift
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5328
            // It is a table for where in the pattern each
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5329
            // lower 7-bit value occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5330
            for (i = 0; i < patternLength; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5331
                lastOcc[src[i]&0x7F] = i + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5332
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5333
            // Precalculate the good suffix shift
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5334
            // i is the shift amount being considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5335
NEXT:       for (i = patternLength; i > 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5336
                // j is the beginning index of suffix being considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5337
                for (j = patternLength - 1; j >= i; j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5338
                    // Testing for good suffix
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5339
                    if (src[j] == src[j-i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5340
                        // src[j..len] is a good suffix
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5341
                        optoSft[j-1] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5342
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5343
                        // No match. The array has already been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5344
                        // filled up with correct values before.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5345
                        continue NEXT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5346
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5347
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5348
                // This fills up the remaining of optoSft
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5349
                // any suffix can not have larger shift amount
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5350
                // then its sub-suffix. Why???
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5351
                while (j > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5352
                    optoSft[--j] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5353
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5354
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5355
            // Set the guard value because of unicode compression
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5356
            optoSft[patternLength-1] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5357
            if (node instanceof SliceS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5358
                return new BnMS(src, lastOcc, optoSft, node.next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5359
            return new BnM(src, lastOcc, optoSft, node.next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5361
        BnM(int[] src, int[] lastOcc, int[] optoSft, Node next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5362
            this.buffer = src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5363
            this.lastOcc = lastOcc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5364
            this.optoSft = optoSft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5365
            this.next = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5367
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5368
            int[] src = buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5369
            int patternLength = src.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5370
            int last = matcher.to - patternLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5371
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5372
            // Loop over all possible match positions in text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5373
NEXT:       while (i <= last) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5374
                // Loop over pattern from right to left
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5375
                for (int j = patternLength - 1; j >= 0; j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5376
                    int ch = seq.charAt(i+j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5377
                    if (ch != src[j]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5378
                        // Shift search to the right by the maximum of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5379
                        // bad character shift and the good suffix shift
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5380
                        i += Math.max(j + 1 - lastOcc[ch&0x7F], optoSft[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5381
                        continue NEXT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5382
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5383
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5384
                // Entire pattern matched starting at i
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5385
                matcher.first = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5386
                boolean ret = next.match(matcher, i + patternLength, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5387
                if (ret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5388
                    matcher.first = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5389
                    matcher.groups[0] = matcher.first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5390
                    matcher.groups[1] = matcher.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5391
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5392
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5393
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5394
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5395
            // BnM is only used as the leading node in the unanchored case,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5396
            // and it replaced its Start() which always searches to the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5397
            // if it doesn't find what it's looking for, so hitEnd is true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5398
            matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5399
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5401
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5402
            info.minLength += buffer.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5403
            info.maxValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5404
            return next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5408
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5409
     * Supplementary support version of BnM(). Unpaired surrogates are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5410
     * also handled by this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5411
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5412
    static final class BnMS extends BnM {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5413
        int lengthInChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5414
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5415
        BnMS(int[] src, int[] lastOcc, int[] optoSft, Node next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5416
            super(src, lastOcc, optoSft, next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5417
            for (int x = 0; x < buffer.length; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5418
                lengthInChars += Character.charCount(buffer[x]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5419
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5421
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5422
            int[] src = buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5423
            int patternLength = src.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5424
            int last = matcher.to - lengthInChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5425
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5426
            // Loop over all possible match positions in text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5427
NEXT:       while (i <= last) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5428
                // Loop over pattern from right to left
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5429
                int ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5430
                for (int j = countChars(seq, i, patternLength), x = patternLength - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5431
                     j > 0; j -= Character.charCount(ch), x--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5432
                    ch = Character.codePointBefore(seq, i+j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5433
                    if (ch != src[x]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5434
                        // Shift search to the right by the maximum of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5435
                        // bad character shift and the good suffix shift
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5436
                        int n = Math.max(x + 1 - lastOcc[ch&0x7F], optoSft[x]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5437
                        i += countChars(seq, i, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5438
                        continue NEXT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5439
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5440
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5441
                // Entire pattern matched starting at i
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5442
                matcher.first = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5443
                boolean ret = next.match(matcher, i + lengthInChars, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5444
                if (ret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5445
                    matcher.first = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5446
                    matcher.groups[0] = matcher.first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5447
                    matcher.groups[1] = matcher.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5448
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5449
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5450
                i += countChars(seq, i, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5451
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5452
            matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5453
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5456
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5457
///////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5458
///////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5459
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5460
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5461
     *  This must be the very first initializer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5462
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5463
    static Node accept = new Node();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5464
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5465
    static Node lastAccept = new LastNode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5466
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5467
    private static class CharPropertyNames {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5468
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5469
        static CharProperty charPropertyFor(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5470
            CharPropertyFactory m = map.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5471
            return m == null ? null : m.make();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5473
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5474
        private static abstract class CharPropertyFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5475
            abstract CharProperty make();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5477
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5478
        private static void defCategory(String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5479
                                        final int typeMask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5480
            map.put(name, new CharPropertyFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5481
                    CharProperty make() { return new Category(typeMask);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5483
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5484
        private static void defRange(String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5485
                                     final int lower, final int upper) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5486
            map.put(name, new CharPropertyFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5487
                    CharProperty make() { return rangeFor(lower, upper);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5488
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5489
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5490
        private static void defCtype(String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5491
                                     final int ctype) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5492
            map.put(name, new CharPropertyFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5493
                    CharProperty make() { return new Ctype(ctype);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5494
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5495
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5496
        private static abstract class CloneableProperty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5497
            extends CharProperty implements Cloneable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5498
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5499
            public CloneableProperty clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5500
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5501
                    return (CloneableProperty) super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5502
                } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5503
                    throw new AssertionError(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5504
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5505
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5506
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5507
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5508
        private static void defClone(String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5509
                                     final CloneableProperty p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5510
            map.put(name, new CharPropertyFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5511
                    CharProperty make() { return p.clone();}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5512
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5513
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5514
        private static final HashMap<String, CharPropertyFactory> map
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5627
diff changeset
  5515
            = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5516
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5517
        static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5518
            // Unicode character property aliases, defined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5519
            // http://www.unicode.org/Public/UNIDATA/PropertyValueAliases.txt
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5520
            defCategory("Cn", 1<<Character.UNASSIGNED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5521
            defCategory("Lu", 1<<Character.UPPERCASE_LETTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5522
            defCategory("Ll", 1<<Character.LOWERCASE_LETTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5523
            defCategory("Lt", 1<<Character.TITLECASE_LETTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5524
            defCategory("Lm", 1<<Character.MODIFIER_LETTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5525
            defCategory("Lo", 1<<Character.OTHER_LETTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5526
            defCategory("Mn", 1<<Character.NON_SPACING_MARK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5527
            defCategory("Me", 1<<Character.ENCLOSING_MARK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5528
            defCategory("Mc", 1<<Character.COMBINING_SPACING_MARK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5529
            defCategory("Nd", 1<<Character.DECIMAL_DIGIT_NUMBER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5530
            defCategory("Nl", 1<<Character.LETTER_NUMBER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5531
            defCategory("No", 1<<Character.OTHER_NUMBER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5532
            defCategory("Zs", 1<<Character.SPACE_SEPARATOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5533
            defCategory("Zl", 1<<Character.LINE_SEPARATOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5534
            defCategory("Zp", 1<<Character.PARAGRAPH_SEPARATOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5535
            defCategory("Cc", 1<<Character.CONTROL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5536
            defCategory("Cf", 1<<Character.FORMAT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5537
            defCategory("Co", 1<<Character.PRIVATE_USE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5538
            defCategory("Cs", 1<<Character.SURROGATE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5539
            defCategory("Pd", 1<<Character.DASH_PUNCTUATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5540
            defCategory("Ps", 1<<Character.START_PUNCTUATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5541
            defCategory("Pe", 1<<Character.END_PUNCTUATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5542
            defCategory("Pc", 1<<Character.CONNECTOR_PUNCTUATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5543
            defCategory("Po", 1<<Character.OTHER_PUNCTUATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5544
            defCategory("Sm", 1<<Character.MATH_SYMBOL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5545
            defCategory("Sc", 1<<Character.CURRENCY_SYMBOL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5546
            defCategory("Sk", 1<<Character.MODIFIER_SYMBOL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5547
            defCategory("So", 1<<Character.OTHER_SYMBOL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5548
            defCategory("Pi", 1<<Character.INITIAL_QUOTE_PUNCTUATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5549
            defCategory("Pf", 1<<Character.FINAL_QUOTE_PUNCTUATION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5550
            defCategory("L", ((1<<Character.UPPERCASE_LETTER) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5551
                              (1<<Character.LOWERCASE_LETTER) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5552
                              (1<<Character.TITLECASE_LETTER) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5553
                              (1<<Character.MODIFIER_LETTER)  |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5554
                              (1<<Character.OTHER_LETTER)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5555
            defCategory("M", ((1<<Character.NON_SPACING_MARK) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5556
                              (1<<Character.ENCLOSING_MARK)   |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5557
                              (1<<Character.COMBINING_SPACING_MARK)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5558
            defCategory("N", ((1<<Character.DECIMAL_DIGIT_NUMBER) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5559
                              (1<<Character.LETTER_NUMBER)        |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5560
                              (1<<Character.OTHER_NUMBER)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5561
            defCategory("Z", ((1<<Character.SPACE_SEPARATOR) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5562
                              (1<<Character.LINE_SEPARATOR)  |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5563
                              (1<<Character.PARAGRAPH_SEPARATOR)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5564
            defCategory("C", ((1<<Character.CONTROL)     |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5565
                              (1<<Character.FORMAT)      |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5566
                              (1<<Character.PRIVATE_USE) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5567
                              (1<<Character.SURROGATE))); // Other
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5568
            defCategory("P", ((1<<Character.DASH_PUNCTUATION)      |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5569
                              (1<<Character.START_PUNCTUATION)     |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5570
                              (1<<Character.END_PUNCTUATION)       |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5571
                              (1<<Character.CONNECTOR_PUNCTUATION) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5572
                              (1<<Character.OTHER_PUNCTUATION)     |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5573
                              (1<<Character.INITIAL_QUOTE_PUNCTUATION) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5574
                              (1<<Character.FINAL_QUOTE_PUNCTUATION)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5575
            defCategory("S", ((1<<Character.MATH_SYMBOL)     |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5576
                              (1<<Character.CURRENCY_SYMBOL) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5577
                              (1<<Character.MODIFIER_SYMBOL) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5578
                              (1<<Character.OTHER_SYMBOL)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5579
            defCategory("LC", ((1<<Character.UPPERCASE_LETTER) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5580
                               (1<<Character.LOWERCASE_LETTER) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5581
                               (1<<Character.TITLECASE_LETTER)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5582
            defCategory("LD", ((1<<Character.UPPERCASE_LETTER) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5583
                               (1<<Character.LOWERCASE_LETTER) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5584
                               (1<<Character.TITLECASE_LETTER) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5585
                               (1<<Character.MODIFIER_LETTER)  |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5586
                               (1<<Character.OTHER_LETTER)     |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5587
                               (1<<Character.DECIMAL_DIGIT_NUMBER)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5588
            defRange("L1", 0x00, 0xFF); // Latin-1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5589
            map.put("all", new CharPropertyFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5590
                    CharProperty make() { return new All(); }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5591
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5592
            // Posix regular expression character classes, defined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5593
            // http://www.unix.org/onlinepubs/009695399/basedefs/xbd_chap09.html
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5594
            defRange("ASCII", 0x00, 0x7F);   // ASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5595
            defCtype("Alnum", ASCII.ALNUM);  // Alphanumeric characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5596
            defCtype("Alpha", ASCII.ALPHA);  // Alphabetic characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5597
            defCtype("Blank", ASCII.BLANK);  // Space and tab characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5598
            defCtype("Cntrl", ASCII.CNTRL);  // Control characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5599
            defRange("Digit", '0', '9');     // Numeric characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5600
            defCtype("Graph", ASCII.GRAPH);  // printable and visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5601
            defRange("Lower", 'a', 'z');     // Lower-case alphabetic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5602
            defRange("Print", 0x20, 0x7E);   // Printable characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5603
            defCtype("Punct", ASCII.PUNCT);  // Punctuation characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5604
            defCtype("Space", ASCII.SPACE);  // Space characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5605
            defRange("Upper", 'A', 'Z');     // Upper-case alphabetic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5606
            defCtype("XDigit",ASCII.XDIGIT); // hexadecimal digits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5607
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5608
            // Java character properties, defined by methods in Character.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5609
            defClone("javaLowerCase", new CloneableProperty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5610
                boolean isSatisfiedBy(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5611
                    return Character.isLowerCase(ch);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5612
            defClone("javaUpperCase", new CloneableProperty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5613
                boolean isSatisfiedBy(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5614
                    return Character.isUpperCase(ch);}});
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  5615
            defClone("javaAlphabetic", new CloneableProperty() {
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  5616
                boolean isSatisfiedBy(int ch) {
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  5617
                    return Character.isAlphabetic(ch);}});
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  5618
            defClone("javaIdeographic", new CloneableProperty() {
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  5619
                boolean isSatisfiedBy(int ch) {
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  5620
                    return Character.isIdeographic(ch);}});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5621
            defClone("javaTitleCase", new CloneableProperty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5622
                boolean isSatisfiedBy(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5623
                    return Character.isTitleCase(ch);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5624
            defClone("javaDigit", new CloneableProperty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5625
                boolean isSatisfiedBy(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5626
                    return Character.isDigit(ch);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5627
            defClone("javaDefined", new CloneableProperty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5628
                boolean isSatisfiedBy(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5629
                    return Character.isDefined(ch);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5630
            defClone("javaLetter", new CloneableProperty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5631
                boolean isSatisfiedBy(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5632
                    return Character.isLetter(ch);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5633
            defClone("javaLetterOrDigit", new CloneableProperty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5634
                boolean isSatisfiedBy(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5635
                    return Character.isLetterOrDigit(ch);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5636
            defClone("javaJavaIdentifierStart", new CloneableProperty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5637
                boolean isSatisfiedBy(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5638
                    return Character.isJavaIdentifierStart(ch);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5639
            defClone("javaJavaIdentifierPart", new CloneableProperty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5640
                boolean isSatisfiedBy(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5641
                    return Character.isJavaIdentifierPart(ch);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5642
            defClone("javaUnicodeIdentifierStart", new CloneableProperty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5643
                boolean isSatisfiedBy(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5644
                    return Character.isUnicodeIdentifierStart(ch);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5645
            defClone("javaUnicodeIdentifierPart", new CloneableProperty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5646
                boolean isSatisfiedBy(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5647
                    return Character.isUnicodeIdentifierPart(ch);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5648
            defClone("javaIdentifierIgnorable", new CloneableProperty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5649
                boolean isSatisfiedBy(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5650
                    return Character.isIdentifierIgnorable(ch);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5651
            defClone("javaSpaceChar", new CloneableProperty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5652
                boolean isSatisfiedBy(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5653
                    return Character.isSpaceChar(ch);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5654
            defClone("javaWhitespace", new CloneableProperty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5655
                boolean isSatisfiedBy(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5656
                    return Character.isWhitespace(ch);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5657
            defClone("javaISOControl", new CloneableProperty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5658
                boolean isSatisfiedBy(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5659
                    return Character.isISOControl(ch);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5660
            defClone("javaMirrored", new CloneableProperty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5661
                boolean isSatisfiedBy(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5662
                    return Character.isMirrored(ch);}});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5663
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5664
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5665
}