rt/emul/compact/src/main/java/java/util/regex/Pattern.java
author Jaroslav Tulach <jtulach@netbeans.org>
Mon, 07 Oct 2013 16:17:21 +0200
changeset 1350 f14e9730d4e9
parent 1348 bca65655b36b
permissions -rw-r--r--
ReExp builds on top of mini emul Character
jtulach@1348
     1
/*
jtulach@1348
     2
 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
jtulach@1348
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jtulach@1348
     4
 *
jtulach@1348
     5
 * This code is free software; you can redistribute it and/or modify it
jtulach@1348
     6
 * under the terms of the GNU General Public License version 2 only, as
jtulach@1348
     7
 * published by the Free Software Foundation.  Oracle designates this
jtulach@1348
     8
 * particular file as subject to the "Classpath" exception as provided
jtulach@1348
     9
 * by Oracle in the LICENSE file that accompanied this code.
jtulach@1348
    10
 *
jtulach@1348
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jtulach@1348
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jtulach@1348
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jtulach@1348
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jtulach@1348
    15
 * accompanied this code).
jtulach@1348
    16
 *
jtulach@1348
    17
 * You should have received a copy of the GNU General Public License version
jtulach@1348
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jtulach@1348
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jtulach@1348
    20
 *
jtulach@1348
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jtulach@1348
    22
 * or visit www.oracle.com if you need additional information or have any
jtulach@1348
    23
 * questions.
jtulach@1348
    24
 */
jtulach@1348
    25
jtulach@1348
    26
package java.util.regex;
jtulach@1348
    27
jtulach@1348
    28
import java.util.Locale;
jtulach@1348
    29
import java.util.Map;
jtulach@1348
    30
import java.util.ArrayList;
jtulach@1348
    31
import java.util.HashMap;
jtulach@1348
    32
import java.util.Arrays;
jtulach@1348
    33
jtulach@1348
    34
jtulach@1348
    35
/**
jtulach@1348
    36
 * A compiled representation of a regular expression.
jtulach@1348
    37
 *
jtulach@1348
    38
 * <p> A regular expression, specified as a string, must first be compiled into
jtulach@1348
    39
 * an instance of this class.  The resulting pattern can then be used to create
jtulach@1348
    40
 * a {@link Matcher} object that can match arbitrary {@link
jtulach@1348
    41
 * java.lang.CharSequence </code>character sequences<code>} against the regular
jtulach@1348
    42
 * expression.  All of the state involved in performing a match resides in the
jtulach@1348
    43
 * matcher, so many matchers can share the same pattern.
jtulach@1348
    44
 *
jtulach@1348
    45
 * <p> A typical invocation sequence is thus
jtulach@1348
    46
 *
jtulach@1348
    47
 * <blockquote><pre>
jtulach@1348
    48
 * Pattern p = Pattern.{@link #compile compile}("a*b");
jtulach@1348
    49
 * Matcher m = p.{@link #matcher matcher}("aaaaab");
jtulach@1348
    50
 * boolean b = m.{@link Matcher#matches matches}();</pre></blockquote>
jtulach@1348
    51
 *
jtulach@1348
    52
 * <p> A {@link #matches matches} method is defined by this class as a
jtulach@1348
    53
 * convenience for when a regular expression is used just once.  This method
jtulach@1348
    54
 * compiles an expression and matches an input sequence against it in a single
jtulach@1348
    55
 * invocation.  The statement
jtulach@1348
    56
 *
jtulach@1348
    57
 * <blockquote><pre>
jtulach@1348
    58
 * boolean b = Pattern.matches("a*b", "aaaaab");</pre></blockquote>
jtulach@1348
    59
 *
jtulach@1348
    60
 * is equivalent to the three statements above, though for repeated matches it
jtulach@1348
    61
 * is less efficient since it does not allow the compiled pattern to be reused.
jtulach@1348
    62
 *
jtulach@1348
    63
 * <p> Instances of this class are immutable and are safe for use by multiple
jtulach@1348
    64
 * concurrent threads.  Instances of the {@link Matcher} class are not safe for
jtulach@1348
    65
 * such use.
jtulach@1348
    66
 *
jtulach@1348
    67
 *
jtulach@1348
    68
 * <a name="sum">
jtulach@1348
    69
 * <h4> Summary of regular-expression constructs </h4>
jtulach@1348
    70
 *
jtulach@1348
    71
 * <table border="0" cellpadding="1" cellspacing="0"
jtulach@1348
    72
 *  summary="Regular expression constructs, and what they match">
jtulach@1348
    73
 *
jtulach@1348
    74
 * <tr align="left">
jtulach@1348
    75
 * <th bgcolor="#CCCCFF" align="left" id="construct">Construct</th>
jtulach@1348
    76
 * <th bgcolor="#CCCCFF" align="left" id="matches">Matches</th>
jtulach@1348
    77
 * </tr>
jtulach@1348
    78
 *
jtulach@1348
    79
 * <tr><th>&nbsp;</th></tr>
jtulach@1348
    80
 * <tr align="left"><th colspan="2" id="characters">Characters</th></tr>
jtulach@1348
    81
 *
jtulach@1348
    82
 * <tr><td valign="top" headers="construct characters"><i>x</i></td>
jtulach@1348
    83
 *     <td headers="matches">The character <i>x</i></td></tr>
jtulach@1348
    84
 * <tr><td valign="top" headers="construct characters"><tt>\\</tt></td>
jtulach@1348
    85
 *     <td headers="matches">The backslash character</td></tr>
jtulach@1348
    86
 * <tr><td valign="top" headers="construct characters"><tt>\0</tt><i>n</i></td>
jtulach@1348
    87
 *     <td headers="matches">The character with octal value <tt>0</tt><i>n</i>
jtulach@1348
    88
 *         (0&nbsp;<tt>&lt;=</tt>&nbsp;<i>n</i>&nbsp;<tt>&lt;=</tt>&nbsp;7)</td></tr>
jtulach@1348
    89
 * <tr><td valign="top" headers="construct characters"><tt>\0</tt><i>nn</i></td>
jtulach@1348
    90
 *     <td headers="matches">The character with octal value <tt>0</tt><i>nn</i>
jtulach@1348
    91
 *         (0&nbsp;<tt>&lt;=</tt>&nbsp;<i>n</i>&nbsp;<tt>&lt;=</tt>&nbsp;7)</td></tr>
jtulach@1348
    92
 * <tr><td valign="top" headers="construct characters"><tt>\0</tt><i>mnn</i></td>
jtulach@1348
    93
 *     <td headers="matches">The character with octal value <tt>0</tt><i>mnn</i>
jtulach@1348
    94
 *         (0&nbsp;<tt>&lt;=</tt>&nbsp;<i>m</i>&nbsp;<tt>&lt;=</tt>&nbsp;3,
jtulach@1348
    95
 *         0&nbsp;<tt>&lt;=</tt>&nbsp;<i>n</i>&nbsp;<tt>&lt;=</tt>&nbsp;7)</td></tr>
jtulach@1348
    96
 * <tr><td valign="top" headers="construct characters"><tt>\x</tt><i>hh</i></td>
jtulach@1348
    97
 *     <td headers="matches">The character with hexadecimal&nbsp;value&nbsp;<tt>0x</tt><i>hh</i></td></tr>
jtulach@1348
    98
 * <tr><td valign="top" headers="construct characters"><tt>&#92;u</tt><i>hhhh</i></td>
jtulach@1348
    99
 *     <td headers="matches">The character with hexadecimal&nbsp;value&nbsp;<tt>0x</tt><i>hhhh</i></td></tr>
jtulach@1348
   100
 * <tr><td valign="top" headers="construct characters"><tt>&#92;x</tt><i>{h...h}</i></td>
jtulach@1348
   101
 *     <td headers="matches">The character with hexadecimal&nbsp;value&nbsp;<tt>0x</tt><i>h...h</i>
jtulach@1348
   102
 *         ({@link java.lang.Character#MIN_CODE_POINT Character.MIN_CODE_POINT}
jtulach@1348
   103
 *         &nbsp;&lt;=&nbsp;<tt>0x</tt><i>h...h</i>&nbsp;&lt;=&nbsp
jtulach@1348
   104
 *          {@link java.lang.Character#MAX_CODE_POINT Character.MAX_CODE_POINT})</td></tr>
jtulach@1348
   105
 * <tr><td valign="top" headers="matches"><tt>\t</tt></td>
jtulach@1348
   106
 *     <td headers="matches">The tab character (<tt>'&#92;u0009'</tt>)</td></tr>
jtulach@1348
   107
 * <tr><td valign="top" headers="construct characters"><tt>\n</tt></td>
jtulach@1348
   108
 *     <td headers="matches">The newline (line feed) character (<tt>'&#92;u000A'</tt>)</td></tr>
jtulach@1348
   109
 * <tr><td valign="top" headers="construct characters"><tt>\r</tt></td>
jtulach@1348
   110
 *     <td headers="matches">The carriage-return character (<tt>'&#92;u000D'</tt>)</td></tr>
jtulach@1348
   111
 * <tr><td valign="top" headers="construct characters"><tt>\f</tt></td>
jtulach@1348
   112
 *     <td headers="matches">The form-feed character (<tt>'&#92;u000C'</tt>)</td></tr>
jtulach@1348
   113
 * <tr><td valign="top" headers="construct characters"><tt>\a</tt></td>
jtulach@1348
   114
 *     <td headers="matches">The alert (bell) character (<tt>'&#92;u0007'</tt>)</td></tr>
jtulach@1348
   115
 * <tr><td valign="top" headers="construct characters"><tt>\e</tt></td>
jtulach@1348
   116
 *     <td headers="matches">The escape character (<tt>'&#92;u001B'</tt>)</td></tr>
jtulach@1348
   117
 * <tr><td valign="top" headers="construct characters"><tt>\c</tt><i>x</i></td>
jtulach@1348
   118
 *     <td headers="matches">The control character corresponding to <i>x</i></td></tr>
jtulach@1348
   119
 *
jtulach@1348
   120
 * <tr><th>&nbsp;</th></tr>
jtulach@1348
   121
 * <tr align="left"><th colspan="2" id="classes">Character classes</th></tr>
jtulach@1348
   122
 *
jtulach@1348
   123
 * <tr><td valign="top" headers="construct classes"><tt>[abc]</tt></td>
jtulach@1348
   124
 *     <td headers="matches"><tt>a</tt>, <tt>b</tt>, or <tt>c</tt> (simple class)</td></tr>
jtulach@1348
   125
 * <tr><td valign="top" headers="construct classes"><tt>[^abc]</tt></td>
jtulach@1348
   126
 *     <td headers="matches">Any character except <tt>a</tt>, <tt>b</tt>, or <tt>c</tt> (negation)</td></tr>
jtulach@1348
   127
 * <tr><td valign="top" headers="construct classes"><tt>[a-zA-Z]</tt></td>
jtulach@1348
   128
 *     <td headers="matches"><tt>a</tt> through <tt>z</tt>
jtulach@1348
   129
 *         or <tt>A</tt> through <tt>Z</tt>, inclusive (range)</td></tr>
jtulach@1348
   130
 * <tr><td valign="top" headers="construct classes"><tt>[a-d[m-p]]</tt></td>
jtulach@1348
   131
 *     <td headers="matches"><tt>a</tt> through <tt>d</tt>,
jtulach@1348
   132
 *      or <tt>m</tt> through <tt>p</tt>: <tt>[a-dm-p]</tt> (union)</td></tr>
jtulach@1348
   133
 * <tr><td valign="top" headers="construct classes"><tt>[a-z&&[def]]</tt></td>
jtulach@1348
   134
 *     <td headers="matches"><tt>d</tt>, <tt>e</tt>, or <tt>f</tt> (intersection)</tr>
jtulach@1348
   135
 * <tr><td valign="top" headers="construct classes"><tt>[a-z&&[^bc]]</tt></td>
jtulach@1348
   136
 *     <td headers="matches"><tt>a</tt> through <tt>z</tt>,
jtulach@1348
   137
 *         except for <tt>b</tt> and <tt>c</tt>: <tt>[ad-z]</tt> (subtraction)</td></tr>
jtulach@1348
   138
 * <tr><td valign="top" headers="construct classes"><tt>[a-z&&[^m-p]]</tt></td>
jtulach@1348
   139
 *     <td headers="matches"><tt>a</tt> through <tt>z</tt>,
jtulach@1348
   140
 *          and not <tt>m</tt> through <tt>p</tt>: <tt>[a-lq-z]</tt>(subtraction)</td></tr>
jtulach@1348
   141
 * <tr><th>&nbsp;</th></tr>
jtulach@1348
   142
 *
jtulach@1348
   143
 * <tr align="left"><th colspan="2" id="predef">Predefined character classes</th></tr>
jtulach@1348
   144
 *
jtulach@1348
   145
 * <tr><td valign="top" headers="construct predef"><tt>.</tt></td>
jtulach@1348
   146
 *     <td headers="matches">Any character (may or may not match <a href="#lt">line terminators</a>)</td></tr>
jtulach@1348
   147
 * <tr><td valign="top" headers="construct predef"><tt>\d</tt></td>
jtulach@1348
   148
 *     <td headers="matches">A digit: <tt>[0-9]</tt></td></tr>
jtulach@1348
   149
 * <tr><td valign="top" headers="construct predef"><tt>\D</tt></td>
jtulach@1348
   150
 *     <td headers="matches">A non-digit: <tt>[^0-9]</tt></td></tr>
jtulach@1348
   151
 * <tr><td valign="top" headers="construct predef"><tt>\s</tt></td>
jtulach@1348
   152
 *     <td headers="matches">A whitespace character: <tt>[ \t\n\x0B\f\r]</tt></td></tr>
jtulach@1348
   153
 * <tr><td valign="top" headers="construct predef"><tt>\S</tt></td>
jtulach@1348
   154
 *     <td headers="matches">A non-whitespace character: <tt>[^\s]</tt></td></tr>
jtulach@1348
   155
 * <tr><td valign="top" headers="construct predef"><tt>\w</tt></td>
jtulach@1348
   156
 *     <td headers="matches">A word character: <tt>[a-zA-Z_0-9]</tt></td></tr>
jtulach@1348
   157
 * <tr><td valign="top" headers="construct predef"><tt>\W</tt></td>
jtulach@1348
   158
 *     <td headers="matches">A non-word character: <tt>[^\w]</tt></td></tr>
jtulach@1348
   159
 *
jtulach@1348
   160
 * <tr><th>&nbsp;</th></tr>
jtulach@1348
   161
 * <tr align="left"><th colspan="2" id="posix">POSIX character classes</b> (US-ASCII only)<b></th></tr>
jtulach@1348
   162
 *
jtulach@1348
   163
 * <tr><td valign="top" headers="construct posix"><tt>\p{Lower}</tt></td>
jtulach@1348
   164
 *     <td headers="matches">A lower-case alphabetic character: <tt>[a-z]</tt></td></tr>
jtulach@1348
   165
 * <tr><td valign="top" headers="construct posix"><tt>\p{Upper}</tt></td>
jtulach@1348
   166
 *     <td headers="matches">An upper-case alphabetic character:<tt>[A-Z]</tt></td></tr>
jtulach@1348
   167
 * <tr><td valign="top" headers="construct posix"><tt>\p{ASCII}</tt></td>
jtulach@1348
   168
 *     <td headers="matches">All ASCII:<tt>[\x00-\x7F]</tt></td></tr>
jtulach@1348
   169
 * <tr><td valign="top" headers="construct posix"><tt>\p{Alpha}</tt></td>
jtulach@1348
   170
 *     <td headers="matches">An alphabetic character:<tt>[\p{Lower}\p{Upper}]</tt></td></tr>
jtulach@1348
   171
 * <tr><td valign="top" headers="construct posix"><tt>\p{Digit}</tt></td>
jtulach@1348
   172
 *     <td headers="matches">A decimal digit: <tt>[0-9]</tt></td></tr>
jtulach@1348
   173
 * <tr><td valign="top" headers="construct posix"><tt>\p{Alnum}</tt></td>
jtulach@1348
   174
 *     <td headers="matches">An alphanumeric character:<tt>[\p{Alpha}\p{Digit}]</tt></td></tr>
jtulach@1348
   175
 * <tr><td valign="top" headers="construct posix"><tt>\p{Punct}</tt></td>
jtulach@1348
   176
 *     <td headers="matches">Punctuation: One of <tt>!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~</tt></td></tr>
jtulach@1348
   177
 *     <!-- <tt>[\!"#\$%&'\(\)\*\+,\-\./:;\<=\>\?@\[\\\]\^_`\{\|\}~]</tt>
jtulach@1348
   178
 *          <tt>[\X21-\X2F\X31-\X40\X5B-\X60\X7B-\X7E]</tt> -->
jtulach@1348
   179
 * <tr><td valign="top" headers="construct posix"><tt>\p{Graph}</tt></td>
jtulach@1348
   180
 *     <td headers="matches">A visible character: <tt>[\p{Alnum}\p{Punct}]</tt></td></tr>
jtulach@1348
   181
 * <tr><td valign="top" headers="construct posix"><tt>\p{Print}</tt></td>
jtulach@1348
   182
 *     <td headers="matches">A printable character: <tt>[\p{Graph}\x20]</tt></td></tr>
jtulach@1348
   183
 * <tr><td valign="top" headers="construct posix"><tt>\p{Blank}</tt></td>
jtulach@1348
   184
 *     <td headers="matches">A space or a tab: <tt>[ \t]</tt></td></tr>
jtulach@1348
   185
 * <tr><td valign="top" headers="construct posix"><tt>\p{Cntrl}</tt></td>
jtulach@1348
   186
 *     <td headers="matches">A control character: <tt>[\x00-\x1F\x7F]</tt></td></tr>
jtulach@1348
   187
 * <tr><td valign="top" headers="construct posix"><tt>\p{XDigit}</tt></td>
jtulach@1348
   188
 *     <td headers="matches">A hexadecimal digit: <tt>[0-9a-fA-F]</tt></td></tr>
jtulach@1348
   189
 * <tr><td valign="top" headers="construct posix"><tt>\p{Space}</tt></td>
jtulach@1348
   190
 *     <td headers="matches">A whitespace character: <tt>[ \t\n\x0B\f\r]</tt></td></tr>
jtulach@1348
   191
 *
jtulach@1348
   192
 * <tr><th>&nbsp;</th></tr>
jtulach@1348
   193
 * <tr align="left"><th colspan="2">java.lang.Character classes (simple <a href="#jcc">java character type</a>)</th></tr>
jtulach@1348
   194
 *
jtulach@1348
   195
 * <tr><td valign="top"><tt>\p{javaLowerCase}</tt></td>
jtulach@1348
   196
 *     <td>Equivalent to java.lang.Character.isLowerCase()</td></tr>
jtulach@1348
   197
 * <tr><td valign="top"><tt>\p{javaUpperCase}</tt></td>
jtulach@1348
   198
 *     <td>Equivalent to java.lang.Character.isUpperCase()</td></tr>
jtulach@1348
   199
 * <tr><td valign="top"><tt>\p{javaWhitespace}</tt></td>
jtulach@1348
   200
 *     <td>Equivalent to java.lang.Character.isWhitespace()</td></tr>
jtulach@1348
   201
 * <tr><td valign="top"><tt>\p{javaMirrored}</tt></td>
jtulach@1348
   202
 *     <td>Equivalent to java.lang.Character.isMirrored()</td></tr>
jtulach@1348
   203
 *
jtulach@1348
   204
 * <tr><th>&nbsp;</th></tr>
jtulach@1348
   205
 * <tr align="left"><th colspan="2" id="unicode">Classes for Unicode scripts, blocks, categories and binary properties</th></tr>
jtulach@1348
   206
 * * <tr><td valign="top" headers="construct unicode"><tt>\p{IsLatin}</tt></td>
jtulach@1348
   207
 *     <td headers="matches">A Latin&nbsp;script character (<a href="#usc">script</a>)</td></tr>
jtulach@1348
   208
 * <tr><td valign="top" headers="construct unicode"><tt>\p{InGreek}</tt></td>
jtulach@1348
   209
 *     <td headers="matches">A character in the Greek&nbsp;block (<a href="#ubc">block</a>)</td></tr>
jtulach@1348
   210
 * <tr><td valign="top" headers="construct unicode"><tt>\p{Lu}</tt></td>
jtulach@1348
   211
 *     <td headers="matches">An uppercase letter (<a href="#ucc">category</a>)</td></tr>
jtulach@1348
   212
 * <tr><td valign="top" headers="construct unicode"><tt>\p{IsAlphabetic}</tt></td>
jtulach@1348
   213
 *     <td headers="matches">An alphabetic character (<a href="#ubpc">binary property</a>)</td></tr>
jtulach@1348
   214
 * <tr><td valign="top" headers="construct unicode"><tt>\p{Sc}</tt></td>
jtulach@1348
   215
 *     <td headers="matches">A currency symbol</td></tr>
jtulach@1348
   216
 * <tr><td valign="top" headers="construct unicode"><tt>\P{InGreek}</tt></td>
jtulach@1348
   217
 *     <td headers="matches">Any character except one in the Greek block (negation)</td></tr>
jtulach@1348
   218
 * <tr><td valign="top" headers="construct unicode"><tt>[\p{L}&&[^\p{Lu}]]&nbsp;</tt></td>
jtulach@1348
   219
 *     <td headers="matches">Any letter except an uppercase letter (subtraction)</td></tr>
jtulach@1348
   220
 *
jtulach@1348
   221
 * <tr><th>&nbsp;</th></tr>
jtulach@1348
   222
 * <tr align="left"><th colspan="2" id="bounds">Boundary matchers</th></tr>
jtulach@1348
   223
 *
jtulach@1348
   224
 * <tr><td valign="top" headers="construct bounds"><tt>^</tt></td>
jtulach@1348
   225
 *     <td headers="matches">The beginning of a line</td></tr>
jtulach@1348
   226
 * <tr><td valign="top" headers="construct bounds"><tt>$</tt></td>
jtulach@1348
   227
 *     <td headers="matches">The end of a line</td></tr>
jtulach@1348
   228
 * <tr><td valign="top" headers="construct bounds"><tt>\b</tt></td>
jtulach@1348
   229
 *     <td headers="matches">A word boundary</td></tr>
jtulach@1348
   230
 * <tr><td valign="top" headers="construct bounds"><tt>\B</tt></td>
jtulach@1348
   231
 *     <td headers="matches">A non-word boundary</td></tr>
jtulach@1348
   232
 * <tr><td valign="top" headers="construct bounds"><tt>\A</tt></td>
jtulach@1348
   233
 *     <td headers="matches">The beginning of the input</td></tr>
jtulach@1348
   234
 * <tr><td valign="top" headers="construct bounds"><tt>\G</tt></td>
jtulach@1348
   235
 *     <td headers="matches">The end of the previous match</td></tr>
jtulach@1348
   236
 * <tr><td valign="top" headers="construct bounds"><tt>\Z</tt></td>
jtulach@1348
   237
 *     <td headers="matches">The end of the input but for the final
jtulach@1348
   238
 *         <a href="#lt">terminator</a>, if&nbsp;any</td></tr>
jtulach@1348
   239
 * <tr><td valign="top" headers="construct bounds"><tt>\z</tt></td>
jtulach@1348
   240
 *     <td headers="matches">The end of the input</td></tr>
jtulach@1348
   241
 *
jtulach@1348
   242
 * <tr><th>&nbsp;</th></tr>
jtulach@1348
   243
 * <tr align="left"><th colspan="2" id="greedy">Greedy quantifiers</th></tr>
jtulach@1348
   244
 *
jtulach@1348
   245
 * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>?</tt></td>
jtulach@1348
   246
 *     <td headers="matches"><i>X</i>, once or not at all</td></tr>
jtulach@1348
   247
 * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>*</tt></td>
jtulach@1348
   248
 *     <td headers="matches"><i>X</i>, zero or more times</td></tr>
jtulach@1348
   249
 * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>+</tt></td>
jtulach@1348
   250
 *     <td headers="matches"><i>X</i>, one or more times</td></tr>
jtulach@1348
   251
 * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>{</tt><i>n</i><tt>}</tt></td>
jtulach@1348
   252
 *     <td headers="matches"><i>X</i>, exactly <i>n</i> times</td></tr>
jtulach@1348
   253
 * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>{</tt><i>n</i><tt>,}</tt></td>
jtulach@1348
   254
 *     <td headers="matches"><i>X</i>, at least <i>n</i> times</td></tr>
jtulach@1348
   255
 * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>{</tt><i>n</i><tt>,</tt><i>m</i><tt>}</tt></td>
jtulach@1348
   256
 *     <td headers="matches"><i>X</i>, at least <i>n</i> but not more than <i>m</i> times</td></tr>
jtulach@1348
   257
 *
jtulach@1348
   258
 * <tr><th>&nbsp;</th></tr>
jtulach@1348
   259
 * <tr align="left"><th colspan="2" id="reluc">Reluctant quantifiers</th></tr>
jtulach@1348
   260
 *
jtulach@1348
   261
 * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>??</tt></td>
jtulach@1348
   262
 *     <td headers="matches"><i>X</i>, once or not at all</td></tr>
jtulach@1348
   263
 * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>*?</tt></td>
jtulach@1348
   264
 *     <td headers="matches"><i>X</i>, zero or more times</td></tr>
jtulach@1348
   265
 * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>+?</tt></td>
jtulach@1348
   266
 *     <td headers="matches"><i>X</i>, one or more times</td></tr>
jtulach@1348
   267
 * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>{</tt><i>n</i><tt>}?</tt></td>
jtulach@1348
   268
 *     <td headers="matches"><i>X</i>, exactly <i>n</i> times</td></tr>
jtulach@1348
   269
 * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>{</tt><i>n</i><tt>,}?</tt></td>
jtulach@1348
   270
 *     <td headers="matches"><i>X</i>, at least <i>n</i> times</td></tr>
jtulach@1348
   271
 * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>{</tt><i>n</i><tt>,</tt><i>m</i><tt>}?</tt></td>
jtulach@1348
   272
 *     <td headers="matches"><i>X</i>, at least <i>n</i> but not more than <i>m</i> times</td></tr>
jtulach@1348
   273
 *
jtulach@1348
   274
 * <tr><th>&nbsp;</th></tr>
jtulach@1348
   275
 * <tr align="left"><th colspan="2" id="poss">Possessive quantifiers</th></tr>
jtulach@1348
   276
 *
jtulach@1348
   277
 * <tr><td valign="top" headers="construct poss"><i>X</i><tt>?+</tt></td>
jtulach@1348
   278
 *     <td headers="matches"><i>X</i>, once or not at all</td></tr>
jtulach@1348
   279
 * <tr><td valign="top" headers="construct poss"><i>X</i><tt>*+</tt></td>
jtulach@1348
   280
 *     <td headers="matches"><i>X</i>, zero or more times</td></tr>
jtulach@1348
   281
 * <tr><td valign="top" headers="construct poss"><i>X</i><tt>++</tt></td>
jtulach@1348
   282
 *     <td headers="matches"><i>X</i>, one or more times</td></tr>
jtulach@1348
   283
 * <tr><td valign="top" headers="construct poss"><i>X</i><tt>{</tt><i>n</i><tt>}+</tt></td>
jtulach@1348
   284
 *     <td headers="matches"><i>X</i>, exactly <i>n</i> times</td></tr>
jtulach@1348
   285
 * <tr><td valign="top" headers="construct poss"><i>X</i><tt>{</tt><i>n</i><tt>,}+</tt></td>
jtulach@1348
   286
 *     <td headers="matches"><i>X</i>, at least <i>n</i> times</td></tr>
jtulach@1348
   287
 * <tr><td valign="top" headers="construct poss"><i>X</i><tt>{</tt><i>n</i><tt>,</tt><i>m</i><tt>}+</tt></td>
jtulach@1348
   288
 *     <td headers="matches"><i>X</i>, at least <i>n</i> but not more than <i>m</i> times</td></tr>
jtulach@1348
   289
 *
jtulach@1348
   290
 * <tr><th>&nbsp;</th></tr>
jtulach@1348
   291
 * <tr align="left"><th colspan="2" id="logical">Logical operators</th></tr>
jtulach@1348
   292
 *
jtulach@1348
   293
 * <tr><td valign="top" headers="construct logical"><i>XY</i></td>
jtulach@1348
   294
 *     <td headers="matches"><i>X</i> followed by <i>Y</i></td></tr>
jtulach@1348
   295
 * <tr><td valign="top" headers="construct logical"><i>X</i><tt>|</tt><i>Y</i></td>
jtulach@1348
   296
 *     <td headers="matches">Either <i>X</i> or <i>Y</i></td></tr>
jtulach@1348
   297
 * <tr><td valign="top" headers="construct logical"><tt>(</tt><i>X</i><tt>)</tt></td>
jtulach@1348
   298
 *     <td headers="matches">X, as a <a href="#cg">capturing group</a></td></tr>
jtulach@1348
   299
 *
jtulach@1348
   300
 * <tr><th>&nbsp;</th></tr>
jtulach@1348
   301
 * <tr align="left"><th colspan="2" id="backref">Back references</th></tr>
jtulach@1348
   302
 *
jtulach@1348
   303
 * <tr><td valign="bottom" headers="construct backref"><tt>\</tt><i>n</i></td>
jtulach@1348
   304
 *     <td valign="bottom" headers="matches">Whatever the <i>n</i><sup>th</sup>
jtulach@1348
   305
 *     <a href="#cg">capturing group</a> matched</td></tr>
jtulach@1348
   306
 *
jtulach@1348
   307
 * <tr><td valign="bottom" headers="construct backref"><tt>\</tt><i>k</i>&lt;<i>name</i>&gt;</td>
jtulach@1348
   308
 *     <td valign="bottom" headers="matches">Whatever the
jtulach@1348
   309
 *     <a href="#groupname">named-capturing group</a> "name" matched</td></tr>
jtulach@1348
   310
 *
jtulach@1348
   311
 * <tr><th>&nbsp;</th></tr>
jtulach@1348
   312
 * <tr align="left"><th colspan="2" id="quot">Quotation</th></tr>
jtulach@1348
   313
 *
jtulach@1348
   314
 * <tr><td valign="top" headers="construct quot"><tt>\</tt></td>
jtulach@1348
   315
 *     <td headers="matches">Nothing, but quotes the following character</td></tr>
jtulach@1348
   316
 * <tr><td valign="top" headers="construct quot"><tt>\Q</tt></td>
jtulach@1348
   317
 *     <td headers="matches">Nothing, but quotes all characters until <tt>\E</tt></td></tr>
jtulach@1348
   318
 * <tr><td valign="top" headers="construct quot"><tt>\E</tt></td>
jtulach@1348
   319
 *     <td headers="matches">Nothing, but ends quoting started by <tt>\Q</tt></td></tr>
jtulach@1348
   320
 *     <!-- Metachars: !$()*+.<>?[\]^{|} -->
jtulach@1348
   321
 *
jtulach@1348
   322
 * <tr><th>&nbsp;</th></tr>
jtulach@1348
   323
 * <tr align="left"><th colspan="2" id="special">Special constructs (named-capturing and non-capturing)</th></tr>
jtulach@1348
   324
 *
jtulach@1348
   325
 * <tr><td valign="top" headers="construct special"><tt>(?&lt;<a href="#groupname">name</a>&gt;</tt><i>X</i><tt>)</tt></td>
jtulach@1348
   326
 *     <td headers="matches"><i>X</i>, as a named-capturing group</td></tr>
jtulach@1348
   327
 * <tr><td valign="top" headers="construct special"><tt>(?:</tt><i>X</i><tt>)</tt></td>
jtulach@1348
   328
 *     <td headers="matches"><i>X</i>, as a non-capturing group</td></tr>
jtulach@1348
   329
 * <tr><td valign="top" headers="construct special"><tt>(?idmsuxU-idmsuxU)&nbsp;</tt></td>
jtulach@1348
   330
 *     <td headers="matches">Nothing, but turns match flags <a href="#CASE_INSENSITIVE">i</a>
jtulach@1348
   331
 * <a href="#UNIX_LINES">d</a> <a href="#MULTILINE">m</a> <a href="#DOTALL">s</a>
jtulach@1348
   332
 * <a href="#UNICODE_CASE">u</a> <a href="#COMMENTS">x</a> <a href="#UNICODE_CHARACTER_CLASS">U</a>
jtulach@1348
   333
 * on - off</td></tr>
jtulach@1348
   334
 * <tr><td valign="top" headers="construct special"><tt>(?idmsux-idmsux:</tt><i>X</i><tt>)</tt>&nbsp;&nbsp;</td>
jtulach@1348
   335
 *     <td headers="matches"><i>X</i>, as a <a href="#cg">non-capturing group</a> with the
jtulach@1348
   336
 *         given flags <a href="#CASE_INSENSITIVE">i</a> <a href="#UNIX_LINES">d</a>
jtulach@1348
   337
 * <a href="#MULTILINE">m</a> <a href="#DOTALL">s</a> <a href="#UNICODE_CASE">u</a >
jtulach@1348
   338
 * <a href="#COMMENTS">x</a> on - off</td></tr>
jtulach@1348
   339
 * <tr><td valign="top" headers="construct special"><tt>(?=</tt><i>X</i><tt>)</tt></td>
jtulach@1348
   340
 *     <td headers="matches"><i>X</i>, via zero-width positive lookahead</td></tr>
jtulach@1348
   341
 * <tr><td valign="top" headers="construct special"><tt>(?!</tt><i>X</i><tt>)</tt></td>
jtulach@1348
   342
 *     <td headers="matches"><i>X</i>, via zero-width negative lookahead</td></tr>
jtulach@1348
   343
 * <tr><td valign="top" headers="construct special"><tt>(?&lt;=</tt><i>X</i><tt>)</tt></td>
jtulach@1348
   344
 *     <td headers="matches"><i>X</i>, via zero-width positive lookbehind</td></tr>
jtulach@1348
   345
 * <tr><td valign="top" headers="construct special"><tt>(?&lt;!</tt><i>X</i><tt>)</tt></td>
jtulach@1348
   346
 *     <td headers="matches"><i>X</i>, via zero-width negative lookbehind</td></tr>
jtulach@1348
   347
 * <tr><td valign="top" headers="construct special"><tt>(?&gt;</tt><i>X</i><tt>)</tt></td>
jtulach@1348
   348
 *     <td headers="matches"><i>X</i>, as an independent, non-capturing group</td></tr>
jtulach@1348
   349
 *
jtulach@1348
   350
 * </table>
jtulach@1348
   351
 *
jtulach@1348
   352
 * <hr>
jtulach@1348
   353
 *
jtulach@1348
   354
 *
jtulach@1348
   355
 * <a name="bs">
jtulach@1348
   356
 * <h4> Backslashes, escapes, and quoting </h4>
jtulach@1348
   357
 *
jtulach@1348
   358
 * <p> The backslash character (<tt>'\'</tt>) serves to introduce escaped
jtulach@1348
   359
 * constructs, as defined in the table above, as well as to quote characters
jtulach@1348
   360
 * that otherwise would be interpreted as unescaped constructs.  Thus the
jtulach@1348
   361
 * expression <tt>\\</tt> matches a single backslash and <tt>\{</tt> matches a
jtulach@1348
   362
 * left brace.
jtulach@1348
   363
 *
jtulach@1348
   364
 * <p> It is an error to use a backslash prior to any alphabetic character that
jtulach@1348
   365
 * does not denote an escaped construct; these are reserved for future
jtulach@1348
   366
 * extensions to the regular-expression language.  A backslash may be used
jtulach@1348
   367
 * prior to a non-alphabetic character regardless of whether that character is
jtulach@1348
   368
 * part of an unescaped construct.
jtulach@1348
   369
 *
jtulach@1348
   370
 * <p> Backslashes within string literals in Java source code are interpreted
jtulach@1348
   371
 * as required by
jtulach@1348
   372
 * <cite>The Java&trade; Language Specification</cite>
jtulach@1348
   373
 * as either Unicode escapes (section 3.3) or other character escapes (section 3.10.6)
jtulach@1348
   374
 * It is therefore necessary to double backslashes in string
jtulach@1348
   375
 * literals that represent regular expressions to protect them from
jtulach@1348
   376
 * interpretation by the Java bytecode compiler.  The string literal
jtulach@1348
   377
 * <tt>"&#92;b"</tt>, for example, matches a single backspace character when
jtulach@1348
   378
 * interpreted as a regular expression, while <tt>"&#92;&#92;b"</tt> matches a
jtulach@1348
   379
 * word boundary.  The string literal <tt>"&#92;(hello&#92;)"</tt> is illegal
jtulach@1348
   380
 * and leads to a compile-time error; in order to match the string
jtulach@1348
   381
 * <tt>(hello)</tt> the string literal <tt>"&#92;&#92;(hello&#92;&#92;)"</tt>
jtulach@1348
   382
 * must be used.
jtulach@1348
   383
 *
jtulach@1348
   384
 * <a name="cc">
jtulach@1348
   385
 * <h4> Character Classes </h4>
jtulach@1348
   386
 *
jtulach@1348
   387
 *    <p> Character classes may appear within other character classes, and
jtulach@1348
   388
 *    may be composed by the union operator (implicit) and the intersection
jtulach@1348
   389
 *    operator (<tt>&amp;&amp;</tt>).
jtulach@1348
   390
 *    The union operator denotes a class that contains every character that is
jtulach@1348
   391
 *    in at least one of its operand classes.  The intersection operator
jtulach@1348
   392
 *    denotes a class that contains every character that is in both of its
jtulach@1348
   393
 *    operand classes.
jtulach@1348
   394
 *
jtulach@1348
   395
 *    <p> The precedence of character-class operators is as follows, from
jtulach@1348
   396
 *    highest to lowest:
jtulach@1348
   397
 *
jtulach@1348
   398
 *    <blockquote><table border="0" cellpadding="1" cellspacing="0"
jtulach@1348
   399
 *                 summary="Precedence of character class operators.">
jtulach@1348
   400
 *      <tr><th>1&nbsp;&nbsp;&nbsp;&nbsp;</th>
jtulach@1348
   401
 *        <td>Literal escape&nbsp;&nbsp;&nbsp;&nbsp;</td>
jtulach@1348
   402
 *        <td><tt>\x</tt></td></tr>
jtulach@1348
   403
 *     <tr><th>2&nbsp;&nbsp;&nbsp;&nbsp;</th>
jtulach@1348
   404
 *        <td>Grouping</td>
jtulach@1348
   405
 *        <td><tt>[...]</tt></td></tr>
jtulach@1348
   406
 *     <tr><th>3&nbsp;&nbsp;&nbsp;&nbsp;</th>
jtulach@1348
   407
 *        <td>Range</td>
jtulach@1348
   408
 *        <td><tt>a-z</tt></td></tr>
jtulach@1348
   409
 *      <tr><th>4&nbsp;&nbsp;&nbsp;&nbsp;</th>
jtulach@1348
   410
 *        <td>Union</td>
jtulach@1348
   411
 *        <td><tt>[a-e][i-u]</tt></td></tr>
jtulach@1348
   412
 *      <tr><th>5&nbsp;&nbsp;&nbsp;&nbsp;</th>
jtulach@1348
   413
 *        <td>Intersection</td>
jtulach@1348
   414
 *        <td><tt>[a-z&&[aeiou]]</tt></td></tr>
jtulach@1348
   415
 *    </table></blockquote>
jtulach@1348
   416
 *
jtulach@1348
   417
 *    <p> Note that a different set of metacharacters are in effect inside
jtulach@1348
   418
 *    a character class than outside a character class. For instance, the
jtulach@1348
   419
 *    regular expression <tt>.</tt> loses its special meaning inside a
jtulach@1348
   420
 *    character class, while the expression <tt>-</tt> becomes a range
jtulach@1348
   421
 *    forming metacharacter.
jtulach@1348
   422
 *
jtulach@1348
   423
 * <a name="lt">
jtulach@1348
   424
 * <h4> Line terminators </h4>
jtulach@1348
   425
 *
jtulach@1348
   426
 * <p> A <i>line terminator</i> is a one- or two-character sequence that marks
jtulach@1348
   427
 * the end of a line of the input character sequence.  The following are
jtulach@1348
   428
 * recognized as line terminators:
jtulach@1348
   429
 *
jtulach@1348
   430
 * <ul>
jtulach@1348
   431
 *
jtulach@1348
   432
 *   <li> A newline (line feed) character&nbsp;(<tt>'\n'</tt>),
jtulach@1348
   433
 *
jtulach@1348
   434
 *   <li> A carriage-return character followed immediately by a newline
jtulach@1348
   435
 *   character&nbsp;(<tt>"\r\n"</tt>),
jtulach@1348
   436
 *
jtulach@1348
   437
 *   <li> A standalone carriage-return character&nbsp;(<tt>'\r'</tt>),
jtulach@1348
   438
 *
jtulach@1348
   439
 *   <li> A next-line character&nbsp;(<tt>'&#92;u0085'</tt>),
jtulach@1348
   440
 *
jtulach@1348
   441
 *   <li> A line-separator character&nbsp;(<tt>'&#92;u2028'</tt>), or
jtulach@1348
   442
 *
jtulach@1348
   443
 *   <li> A paragraph-separator character&nbsp;(<tt>'&#92;u2029</tt>).
jtulach@1348
   444
 *
jtulach@1348
   445
 * </ul>
jtulach@1348
   446
 * <p>If {@link #UNIX_LINES} mode is activated, then the only line terminators
jtulach@1348
   447
 * recognized are newline characters.
jtulach@1348
   448
 *
jtulach@1348
   449
 * <p> The regular expression <tt>.</tt> matches any character except a line
jtulach@1348
   450
 * terminator unless the {@link #DOTALL} flag is specified.
jtulach@1348
   451
 *
jtulach@1348
   452
 * <p> By default, the regular expressions <tt>^</tt> and <tt>$</tt> ignore
jtulach@1348
   453
 * line terminators and only match at the beginning and the end, respectively,
jtulach@1348
   454
 * of the entire input sequence. If {@link #MULTILINE} mode is activated then
jtulach@1348
   455
 * <tt>^</tt> matches at the beginning of input and after any line terminator
jtulach@1348
   456
 * except at the end of input. When in {@link #MULTILINE} mode <tt>$</tt>
jtulach@1348
   457
 * matches just before a line terminator or the end of the input sequence.
jtulach@1348
   458
 *
jtulach@1348
   459
 * <a name="cg">
jtulach@1348
   460
 * <h4> Groups and capturing </h4>
jtulach@1348
   461
 *
jtulach@1348
   462
 * <a name="gnumber">
jtulach@1348
   463
 * <h5> Group number </h5>
jtulach@1348
   464
 * <p> Capturing groups are numbered by counting their opening parentheses from
jtulach@1348
   465
 * left to right.  In the expression <tt>((A)(B(C)))</tt>, for example, there
jtulach@1348
   466
 * are four such groups: </p>
jtulach@1348
   467
 *
jtulach@1348
   468
 * <blockquote><table cellpadding=1 cellspacing=0 summary="Capturing group numberings">
jtulach@1348
   469
 * <tr><th>1&nbsp;&nbsp;&nbsp;&nbsp;</th>
jtulach@1348
   470
 *     <td><tt>((A)(B(C)))</tt></td></tr>
jtulach@1348
   471
 * <tr><th>2&nbsp;&nbsp;&nbsp;&nbsp;</th>
jtulach@1348
   472
 *     <td><tt>(A)</tt></td></tr>
jtulach@1348
   473
 * <tr><th>3&nbsp;&nbsp;&nbsp;&nbsp;</th>
jtulach@1348
   474
 *     <td><tt>(B(C))</tt></td></tr>
jtulach@1348
   475
 * <tr><th>4&nbsp;&nbsp;&nbsp;&nbsp;</th>
jtulach@1348
   476
 *     <td><tt>(C)</tt></td></tr>
jtulach@1348
   477
 * </table></blockquote>
jtulach@1348
   478
 *
jtulach@1348
   479
 * <p> Group zero always stands for the entire expression.
jtulach@1348
   480
 *
jtulach@1348
   481
 * <p> Capturing groups are so named because, during a match, each subsequence
jtulach@1348
   482
 * of the input sequence that matches such a group is saved.  The captured
jtulach@1348
   483
 * subsequence may be used later in the expression, via a back reference, and
jtulach@1348
   484
 * may also be retrieved from the matcher once the match operation is complete.
jtulach@1348
   485
 *
jtulach@1348
   486
 * <a name="groupname">
jtulach@1348
   487
 * <h5> Group name </h5>
jtulach@1348
   488
 * <p>A capturing group can also be assigned a "name", a <tt>named-capturing group</tt>,
jtulach@1348
   489
 * and then be back-referenced later by the "name". Group names are composed of
jtulach@1348
   490
 * the following characters. The first character must be a <tt>letter</tt>.
jtulach@1348
   491
 *
jtulach@1348
   492
 * <ul>
jtulach@1348
   493
 *   <li> The uppercase letters <tt>'A'</tt> through <tt>'Z'</tt>
jtulach@1348
   494
 *        (<tt>'&#92;u0041'</tt>&nbsp;through&nbsp;<tt>'&#92;u005a'</tt>),
jtulach@1348
   495
 *   <li> The lowercase letters <tt>'a'</tt> through <tt>'z'</tt>
jtulach@1348
   496
 *        (<tt>'&#92;u0061'</tt>&nbsp;through&nbsp;<tt>'&#92;u007a'</tt>),
jtulach@1348
   497
 *   <li> The digits <tt>'0'</tt> through <tt>'9'</tt>
jtulach@1348
   498
 *        (<tt>'&#92;u0030'</tt>&nbsp;through&nbsp;<tt>'&#92;u0039'</tt>),
jtulach@1348
   499
 * </ul>
jtulach@1348
   500
 *
jtulach@1348
   501
 * <p> A <tt>named-capturing group</tt> is still numbered as described in
jtulach@1348
   502
 * <a href="#gnumber">Group number</a>.
jtulach@1348
   503
 *
jtulach@1348
   504
 * <p> The captured input associated with a group is always the subsequence
jtulach@1348
   505
 * that the group most recently matched.  If a group is evaluated a second time
jtulach@1348
   506
 * because of quantification then its previously-captured value, if any, will
jtulach@1348
   507
 * be retained if the second evaluation fails.  Matching the string
jtulach@1348
   508
 * <tt>"aba"</tt> against the expression <tt>(a(b)?)+</tt>, for example, leaves
jtulach@1348
   509
 * group two set to <tt>"b"</tt>.  All captured input is discarded at the
jtulach@1348
   510
 * beginning of each match.
jtulach@1348
   511
 *
jtulach@1348
   512
 * <p> Groups beginning with <tt>(?</tt> are either pure, <i>non-capturing</i> groups
jtulach@1348
   513
 * that do not capture text and do not count towards the group total, or
jtulach@1348
   514
 * <i>named-capturing</i> group.
jtulach@1348
   515
 *
jtulach@1348
   516
 * <h4> Unicode support </h4>
jtulach@1348
   517
 *
jtulach@1348
   518
 * <p> This class is in conformance with Level 1 of <a
jtulach@1348
   519
 * href="http://www.unicode.org/reports/tr18/"><i>Unicode Technical
jtulach@1348
   520
 * Standard #18: Unicode Regular Expression</i></a>, plus RL2.1
jtulach@1348
   521
 * Canonical Equivalents.
jtulach@1348
   522
 * <p>
jtulach@1348
   523
 * <b>Unicode escape sequences</b> such as <tt>&#92;u2014</tt> in Java source code
jtulach@1348
   524
 * are processed as described in section 3.3 of
jtulach@1348
   525
 * <cite>The Java&trade; Language Specification</cite>.
jtulach@1348
   526
 * Such escape sequences are also implemented directly by the regular-expression
jtulach@1348
   527
 * parser so that Unicode escapes can be used in expressions that are read from
jtulach@1348
   528
 * files or from the keyboard.  Thus the strings <tt>"&#92;u2014"</tt> and
jtulach@1348
   529
 * <tt>"\\u2014"</tt>, while not equal, compile into the same pattern, which
jtulach@1348
   530
 * matches the character with hexadecimal value <tt>0x2014</tt>.
jtulach@1348
   531
 * <p>
jtulach@1348
   532
 * A Unicode character can also be represented in a regular-expression by
jtulach@1348
   533
 * using its <b>Hex notation</b>(hexadecimal code point value) directly as described in construct
jtulach@1348
   534
 * <tt>&#92;x{...}</tt>, for example a supplementary character U+2011F
jtulach@1348
   535
 * can be specified as <tt>&#92;x{2011F}</tt>, instead of two consecutive
jtulach@1348
   536
 * Unicode escape sequences of the surrogate pair
jtulach@1348
   537
 * <tt>&#92;uD840</tt><tt>&#92;uDD1F</tt>.
jtulach@1348
   538
 * <p>
jtulach@1348
   539
 * Unicode scripts, blocks, categories and binary properties are written with
jtulach@1348
   540
 * the <tt>\p</tt> and <tt>\P</tt> constructs as in Perl.
jtulach@1348
   541
 * <tt>\p{</tt><i>prop</i><tt>}</tt> matches if
jtulach@1348
   542
 * the input has the property <i>prop</i>, while <tt>\P{</tt><i>prop</i><tt>}</tt>
jtulach@1348
   543
 * does not match if the input has that property.
jtulach@1348
   544
 * <p>
jtulach@1348
   545
 * Scripts, blocks, categories and binary properties can be used both inside
jtulach@1348
   546
 * and outside of a character class.
jtulach@1348
   547
 * <a name="usc">
jtulach@1348
   548
 * <p>
jtulach@1348
   549
 * <b>Scripts</b> are specified either with the prefix {@code Is}, as in
jtulach@1348
   550
 * {@code IsHiragana}, or by using  the {@code script} keyword (or its short
jtulach@1348
   551
 * form {@code sc})as in {@code script=Hiragana} or {@code sc=Hiragana}.
jtulach@1348
   552
 * <p>
jtulach@1348
   553
 * The script names supported by <code>Pattern</code> are the valid script names
jtulach@1348
   554
 * accepted and defined by
jtulach@1348
   555
 * {@link java.lang.Character.UnicodeScript#forName(String) UnicodeScript.forName}.
jtulach@1348
   556
 * <a name="ubc">
jtulach@1348
   557
 * <p>
jtulach@1348
   558
 * <b>Blocks</b> are specified with the prefix {@code In}, as in
jtulach@1348
   559
 * {@code InMongolian}, or by using the keyword {@code block} (or its short
jtulach@1348
   560
 * form {@code blk}) as in {@code block=Mongolian} or {@code blk=Mongolian}.
jtulach@1348
   561
 * <p>
jtulach@1348
   562
 * The block names supported by <code>Pattern</code> are the valid block names
jtulach@1348
   563
 * accepted and defined by
jtulach@1348
   564
 * {@link java.lang.Character.UnicodeBlock#forName(String) UnicodeBlock.forName}.
jtulach@1348
   565
 * <p>
jtulach@1348
   566
 * <a name="ucc">
jtulach@1348
   567
 * <b>Categories</b> may be specified with the optional prefix {@code Is}:
jtulach@1348
   568
 * Both {@code \p{L}} and {@code \p{IsL}} denote the category of Unicode
jtulach@1348
   569
 * letters. Same as scripts and blocks, categories can also be specified
jtulach@1348
   570
 * by using the keyword {@code general_category} (or its short form
jtulach@1348
   571
 * {@code gc}) as in {@code general_category=Lu} or {@code gc=Lu}.
jtulach@1348
   572
 * <p>
jtulach@1348
   573
 * The supported categories are those of
jtulach@1348
   574
 * <a href="http://www.unicode.org/unicode/standard/standard.html">
jtulach@1348
   575
 * <i>The Unicode Standard</i></a> in the version specified by the
jtulach@1348
   576
 * {@link java.lang.Character Character} class. The category names are those
jtulach@1348
   577
 * defined in the Standard, both normative and informative.
jtulach@1348
   578
 * <p>
jtulach@1348
   579
 * <a name="ubpc">
jtulach@1348
   580
 * <b>Binary properties</b> are specified with the prefix {@code Is}, as in
jtulach@1348
   581
 * {@code IsAlphabetic}. The supported binary properties by <code>Pattern</code>
jtulach@1348
   582
 * are
jtulach@1348
   583
 * <ul>
jtulach@1348
   584
 *   <li> Alphabetic
jtulach@1348
   585
 *   <li> Ideographic
jtulach@1348
   586
 *   <li> Letter
jtulach@1348
   587
 *   <li> Lowercase
jtulach@1348
   588
 *   <li> Uppercase
jtulach@1348
   589
 *   <li> Titlecase
jtulach@1348
   590
 *   <li> Punctuation
jtulach@1348
   591
 *   <Li> Control
jtulach@1348
   592
 *   <li> White_Space
jtulach@1348
   593
 *   <li> Digit
jtulach@1348
   594
 *   <li> Hex_Digit
jtulach@1348
   595
 *   <li> Noncharacter_Code_Point
jtulach@1348
   596
 *   <li> Assigned
jtulach@1348
   597
 * </ul>
jtulach@1348
   598
jtulach@1348
   599
jtulach@1348
   600
 * <p>
jtulach@1348
   601
 * <b>Predefined Character classes</b> and <b>POSIX character classes</b> are in
jtulach@1348
   602
 * conformance with the recommendation of <i>Annex C: Compatibility Properties</i>
jtulach@1348
   603
 * of <a href="http://www.unicode.org/reports/tr18/"><i>Unicode Regular Expression
jtulach@1348
   604
 * </i></a>, when {@link #UNICODE_CHARACTER_CLASS} flag is specified.
jtulach@1348
   605
 * <p>
jtulach@1348
   606
 * <table border="0" cellpadding="1" cellspacing="0"
jtulach@1348
   607
 *  summary="predefined and posix character classes in Unicode mode">
jtulach@1348
   608
 * <tr align="left">
jtulach@1348
   609
 * <th bgcolor="#CCCCFF" align="left" id="classes">Classes</th>
jtulach@1348
   610
 * <th bgcolor="#CCCCFF" align="left" id="matches">Matches</th>
jtulach@1348
   611
 *</tr>
jtulach@1348
   612
 * <tr><td><tt>\p{Lower}</tt></td>
jtulach@1348
   613
 *     <td>A lowercase character:<tt>\p{IsLowercase}</tt></td></tr>
jtulach@1348
   614
 * <tr><td><tt>\p{Upper}</tt></td>
jtulach@1348
   615
 *     <td>An uppercase character:<tt>\p{IsUppercase}</tt></td></tr>
jtulach@1348
   616
 * <tr><td><tt>\p{ASCII}</tt></td>
jtulach@1348
   617
 *     <td>All ASCII:<tt>[\x00-\x7F]</tt></td></tr>
jtulach@1348
   618
 * <tr><td><tt>\p{Alpha}</tt></td>
jtulach@1348
   619
 *     <td>An alphabetic character:<tt>\p{IsAlphabetic}</tt></td></tr>
jtulach@1348
   620
 * <tr><td><tt>\p{Digit}</tt></td>
jtulach@1348
   621
 *     <td>A decimal digit character:<tt>p{IsDigit}</tt></td></tr>
jtulach@1348
   622
 * <tr><td><tt>\p{Alnum}</tt></td>
jtulach@1348
   623
 *     <td>An alphanumeric character:<tt>[\p{IsAlphabetic}\p{IsDigit}]</tt></td></tr>
jtulach@1348
   624
 * <tr><td><tt>\p{Punct}</tt></td>
jtulach@1348
   625
 *     <td>A punctuation character:<tt>p{IsPunctuation}</tt></td></tr>
jtulach@1348
   626
 * <tr><td><tt>\p{Graph}</tt></td>
jtulach@1348
   627
 *     <td>A visible character: <tt>[^\p{IsWhite_Space}\p{gc=Cc}\p{gc=Cs}\p{gc=Cn}]</tt></td></tr>
jtulach@1348
   628
 * <tr><td><tt>\p{Print}</tt></td>
jtulach@1348
   629
 *     <td>A printable character: <tt>[\p{Graph}\p{Blank}&&[^\p{Cntrl}]]</tt></td></tr>
jtulach@1348
   630
 * <tr><td><tt>\p{Blank}</tt></td>
jtulach@1348
   631
 *     <td>A space or a tab: <tt>[\p{IsWhite_Space}&&[^\p{gc=Zl}\p{gc=Zp}\x0a\x0b\x0c\x0d\x85]]</tt></td></tr>
jtulach@1348
   632
 * <tr><td><tt>\p{Cntrl}</tt></td>
jtulach@1348
   633
 *     <td>A control character: <tt>\p{gc=Cc}</tt></td></tr>
jtulach@1348
   634
 * <tr><td><tt>\p{XDigit}</tt></td>
jtulach@1348
   635
 *     <td>A hexadecimal digit: <tt>[\p{gc=Nd}\p{IsHex_Digit}]</tt></td></tr>
jtulach@1348
   636
 * <tr><td><tt>\p{Space}</tt></td>
jtulach@1348
   637
 *     <td>A whitespace character:<tt>\p{IsWhite_Space}</tt></td></tr>
jtulach@1348
   638
 * <tr><td><tt>\d</tt></td>
jtulach@1348
   639
 *     <td>A digit: <tt>\p{IsDigit}</tt></td></tr>
jtulach@1348
   640
 * <tr><td><tt>\D</tt></td>
jtulach@1348
   641
 *     <td>A non-digit: <tt>[^\d]</tt></td></tr>
jtulach@1348
   642
 * <tr><td><tt>\s</tt></td>
jtulach@1348
   643
 *     <td>A whitespace character: <tt>\p{IsWhite_Space}</tt></td></tr>
jtulach@1348
   644
 * <tr><td><tt>\S</tt></td>
jtulach@1348
   645
 *     <td>A non-whitespace character: <tt>[^\s]</tt></td></tr>
jtulach@1348
   646
 * <tr><td><tt>\w</tt></td>
jtulach@1348
   647
 *     <td>A word character: <tt>[\p{Alpha}\p{gc=Mn}\p{gc=Me}\p{gc=Mc}\p{Digit}\p{gc=Pc}]</tt></td></tr>
jtulach@1348
   648
 * <tr><td><tt>\W</tt></td>
jtulach@1348
   649
 *     <td>A non-word character: <tt>[^\w]</tt></td></tr>
jtulach@1348
   650
 * </table>
jtulach@1348
   651
 * <p>
jtulach@1348
   652
 * <a name="jcc">
jtulach@1348
   653
 * Categories that behave like the java.lang.Character
jtulach@1348
   654
 * boolean is<i>methodname</i> methods (except for the deprecated ones) are
jtulach@1348
   655
 * available through the same <tt>\p{</tt><i>prop</i><tt>}</tt> syntax where
jtulach@1348
   656
 * the specified property has the name <tt>java<i>methodname</i></tt>.
jtulach@1348
   657
 *
jtulach@1348
   658
 * <h4> Comparison to Perl 5 </h4>
jtulach@1348
   659
 *
jtulach@1348
   660
 * <p>The <code>Pattern</code> engine performs traditional NFA-based matching
jtulach@1348
   661
 * with ordered alternation as occurs in Perl 5.
jtulach@1348
   662
 *
jtulach@1348
   663
 * <p> Perl constructs not supported by this class: </p>
jtulach@1348
   664
 *
jtulach@1348
   665
 * <ul>
jtulach@1348
   666
 *    <li><p> Predefined character classes (Unicode character)
jtulach@1348
   667
 *    <p><tt>\h&nbsp;&nbsp;&nbsp;&nbsp;</tt>A horizontal whitespace
jtulach@1348
   668
 *    <p><tt>\H&nbsp;&nbsp;&nbsp;&nbsp;</tt>A non horizontal whitespace
jtulach@1348
   669
 *    <p><tt>\v&nbsp;&nbsp;&nbsp;&nbsp;</tt>A vertical whitespace
jtulach@1348
   670
 *    <p><tt>\V&nbsp;&nbsp;&nbsp;&nbsp;</tt>A non vertical whitespace
jtulach@1348
   671
 *    <p><tt>\R&nbsp;&nbsp;&nbsp;&nbsp;</tt>Any Unicode linebreak sequence
jtulach@1348
   672
 *    <tt>\u005cu000D\u005cu000A|[\u005cu000A\u005cu000B\u005cu000C\u005cu000D\u005cu0085\u005cu2028\u005cu2029]</tt>
jtulach@1348
   673
 *    <p><tt>\X&nbsp;&nbsp;&nbsp;&nbsp;</tt>Match Unicode
jtulach@1348
   674
 *    <a href="http://www.unicode.org/reports/tr18/#Default_Grapheme_Clusters">
jtulach@1348
   675
 *    <i>extended grapheme cluster</i></a>
jtulach@1348
   676
 *    </p></li>
jtulach@1348
   677
 *
jtulach@1348
   678
 *    <li><p> The backreference constructs, <tt>\g{</tt><i>n</i><tt>}</tt> for
jtulach@1348
   679
 *    the <i>n</i><sup>th</sup><a href="#cg">capturing group</a> and
jtulach@1348
   680
 *    <tt>\g{</tt><i>name</i><tt>}</tt> for
jtulach@1348
   681
 *    <a href="#groupname">named-capturing group</a>.
jtulach@1348
   682
 *    </p></li>
jtulach@1348
   683
 *
jtulach@1348
   684
 *    <li><p> The named character construct, <tt>\N{</tt><i>name</i><tt>}</tt>
jtulach@1348
   685
 *    for a Unicode character by its name.
jtulach@1348
   686
 *    </p></li>
jtulach@1348
   687
 *
jtulach@1348
   688
 *    <li><p> The conditional constructs
jtulach@1348
   689
 *    <tt>(?(</tt><i>condition</i><tt>)</tt><i>X</i><tt>)</tt> and
jtulach@1348
   690
 *    <tt>(?(</tt><i>condition</i><tt>)</tt><i>X</i><tt>|</tt><i>Y</i><tt>)</tt>,
jtulach@1348
   691
 *    </p></li>
jtulach@1348
   692
 *
jtulach@1348
   693
 *    <li><p> The embedded code constructs <tt>(?{</tt><i>code</i><tt>})</tt>
jtulach@1348
   694
 *    and <tt>(??{</tt><i>code</i><tt>})</tt>,</p></li>
jtulach@1348
   695
 *
jtulach@1348
   696
 *    <li><p> The embedded comment syntax <tt>(?#comment)</tt>, and </p></li>
jtulach@1348
   697
 *
jtulach@1348
   698
 *    <li><p> The preprocessing operations <tt>\l</tt> <tt>&#92;u</tt>,
jtulach@1348
   699
 *    <tt>\L</tt>, and <tt>\U</tt>.  </p></li>
jtulach@1348
   700
 *
jtulach@1348
   701
 * </ul>
jtulach@1348
   702
 *
jtulach@1348
   703
 * <p> Constructs supported by this class but not by Perl: </p>
jtulach@1348
   704
 *
jtulach@1348
   705
 * <ul>
jtulach@1348
   706
 *
jtulach@1348
   707
 *    <li><p> Character-class union and intersection as described
jtulach@1348
   708
 *    <a href="#cc">above</a>.</p></li>
jtulach@1348
   709
 *
jtulach@1348
   710
 * </ul>
jtulach@1348
   711
 *
jtulach@1348
   712
 * <p> Notable differences from Perl: </p>
jtulach@1348
   713
 *
jtulach@1348
   714
 * <ul>
jtulach@1348
   715
 *
jtulach@1348
   716
 *    <li><p> In Perl, <tt>\1</tt> through <tt>\9</tt> are always interpreted
jtulach@1348
   717
 *    as back references; a backslash-escaped number greater than <tt>9</tt> is
jtulach@1348
   718
 *    treated as a back reference if at least that many subexpressions exist,
jtulach@1348
   719
 *    otherwise it is interpreted, if possible, as an octal escape.  In this
jtulach@1348
   720
 *    class octal escapes must always begin with a zero. In this class,
jtulach@1348
   721
 *    <tt>\1</tt> through <tt>\9</tt> are always interpreted as back
jtulach@1348
   722
 *    references, and a larger number is accepted as a back reference if at
jtulach@1348
   723
 *    least that many subexpressions exist at that point in the regular
jtulach@1348
   724
 *    expression, otherwise the parser will drop digits until the number is
jtulach@1348
   725
 *    smaller or equal to the existing number of groups or it is one digit.
jtulach@1348
   726
 *    </p></li>
jtulach@1348
   727
 *
jtulach@1348
   728
 *    <li><p> Perl uses the <tt>g</tt> flag to request a match that resumes
jtulach@1348
   729
 *    where the last match left off.  This functionality is provided implicitly
jtulach@1348
   730
 *    by the {@link Matcher} class: Repeated invocations of the {@link
jtulach@1348
   731
 *    Matcher#find find} method will resume where the last match left off,
jtulach@1348
   732
 *    unless the matcher is reset.  </p></li>
jtulach@1348
   733
 *
jtulach@1348
   734
 *    <li><p> In Perl, embedded flags at the top level of an expression affect
jtulach@1348
   735
 *    the whole expression.  In this class, embedded flags always take effect
jtulach@1348
   736
 *    at the point at which they appear, whether they are at the top level or
jtulach@1348
   737
 *    within a group; in the latter case, flags are restored at the end of the
jtulach@1348
   738
 *    group just as in Perl.  </p></li>
jtulach@1348
   739
 *
jtulach@1348
   740
 * </ul>
jtulach@1348
   741
 *
jtulach@1348
   742
 *
jtulach@1348
   743
 * <p> For a more precise description of the behavior of regular expression
jtulach@1348
   744
 * constructs, please see <a href="http://www.oreilly.com/catalog/regex3/">
jtulach@1348
   745
 * <i>Mastering Regular Expressions, 3nd Edition</i>, Jeffrey E. F. Friedl,
jtulach@1348
   746
 * O'Reilly and Associates, 2006.</a>
jtulach@1348
   747
 * </p>
jtulach@1348
   748
 *
jtulach@1348
   749
 * @see java.lang.String#split(String, int)
jtulach@1348
   750
 * @see java.lang.String#split(String)
jtulach@1348
   751
 *
jtulach@1348
   752
 * @author      Mike McCloskey
jtulach@1348
   753
 * @author      Mark Reinhold
jtulach@1348
   754
 * @author      JSR-51 Expert Group
jtulach@1348
   755
 * @since       1.4
jtulach@1348
   756
 * @spec        JSR-51
jtulach@1348
   757
 */
jtulach@1348
   758
jtulach@1348
   759
public final class Pattern
jtulach@1348
   760
    implements java.io.Serializable
jtulach@1348
   761
{
jtulach@1348
   762
jtulach@1348
   763
    /**
jtulach@1348
   764
     * Regular expression modifier values.  Instead of being passed as
jtulach@1348
   765
     * arguments, they can also be passed as inline modifiers.
jtulach@1348
   766
     * For example, the following statements have the same effect.
jtulach@1348
   767
     * <pre>
jtulach@1348
   768
     * RegExp r1 = RegExp.compile("abc", Pattern.I|Pattern.M);
jtulach@1348
   769
     * RegExp r2 = RegExp.compile("(?im)abc", 0);
jtulach@1348
   770
     * </pre>
jtulach@1348
   771
     *
jtulach@1348
   772
     * The flags are duplicated so that the familiar Perl match flag
jtulach@1348
   773
     * names are available.
jtulach@1348
   774
     */
jtulach@1348
   775
jtulach@1348
   776
    /**
jtulach@1348
   777
     * Enables Unix lines mode.
jtulach@1348
   778
     *
jtulach@1348
   779
     * <p> In this mode, only the <tt>'\n'</tt> line terminator is recognized
jtulach@1348
   780
     * in the behavior of <tt>.</tt>, <tt>^</tt>, and <tt>$</tt>.
jtulach@1348
   781
     *
jtulach@1348
   782
     * <p> Unix lines mode can also be enabled via the embedded flag
jtulach@1348
   783
     * expression&nbsp;<tt>(?d)</tt>.
jtulach@1348
   784
     */
jtulach@1348
   785
    public static final int UNIX_LINES = 0x01;
jtulach@1348
   786
jtulach@1348
   787
    /**
jtulach@1348
   788
     * Enables case-insensitive matching.
jtulach@1348
   789
     *
jtulach@1348
   790
     * <p> By default, case-insensitive matching assumes that only characters
jtulach@1348
   791
     * in the US-ASCII charset are being matched.  Unicode-aware
jtulach@1348
   792
     * case-insensitive matching can be enabled by specifying the {@link
jtulach@1348
   793
     * #UNICODE_CASE} flag in conjunction with this flag.
jtulach@1348
   794
     *
jtulach@1348
   795
     * <p> Case-insensitive matching can also be enabled via the embedded flag
jtulach@1348
   796
     * expression&nbsp;<tt>(?i)</tt>.
jtulach@1348
   797
     *
jtulach@1348
   798
     * <p> Specifying this flag may impose a slight performance penalty.  </p>
jtulach@1348
   799
     */
jtulach@1348
   800
    public static final int CASE_INSENSITIVE = 0x02;
jtulach@1348
   801
jtulach@1348
   802
    /**
jtulach@1348
   803
     * Permits whitespace and comments in pattern.
jtulach@1348
   804
     *
jtulach@1348
   805
     * <p> In this mode, whitespace is ignored, and embedded comments starting
jtulach@1348
   806
     * with <tt>#</tt> are ignored until the end of a line.
jtulach@1348
   807
     *
jtulach@1348
   808
     * <p> Comments mode can also be enabled via the embedded flag
jtulach@1348
   809
     * expression&nbsp;<tt>(?x)</tt>.
jtulach@1348
   810
     */
jtulach@1348
   811
    public static final int COMMENTS = 0x04;
jtulach@1348
   812
jtulach@1348
   813
    /**
jtulach@1348
   814
     * Enables multiline mode.
jtulach@1348
   815
     *
jtulach@1348
   816
     * <p> In multiline mode the expressions <tt>^</tt> and <tt>$</tt> match
jtulach@1348
   817
     * just after or just before, respectively, a line terminator or the end of
jtulach@1348
   818
     * the input sequence.  By default these expressions only match at the
jtulach@1348
   819
     * beginning and the end of the entire input sequence.
jtulach@1348
   820
     *
jtulach@1348
   821
     * <p> Multiline mode can also be enabled via the embedded flag
jtulach@1348
   822
     * expression&nbsp;<tt>(?m)</tt>.  </p>
jtulach@1348
   823
     */
jtulach@1348
   824
    public static final int MULTILINE = 0x08;
jtulach@1348
   825
jtulach@1348
   826
    /**
jtulach@1348
   827
     * Enables literal parsing of the pattern.
jtulach@1348
   828
     *
jtulach@1348
   829
     * <p> When this flag is specified then the input string that specifies
jtulach@1348
   830
     * the pattern is treated as a sequence of literal characters.
jtulach@1348
   831
     * Metacharacters or escape sequences in the input sequence will be
jtulach@1348
   832
     * given no special meaning.
jtulach@1348
   833
     *
jtulach@1348
   834
     * <p>The flags CASE_INSENSITIVE and UNICODE_CASE retain their impact on
jtulach@1348
   835
     * matching when used in conjunction with this flag. The other flags
jtulach@1348
   836
     * become superfluous.
jtulach@1348
   837
     *
jtulach@1348
   838
     * <p> There is no embedded flag character for enabling literal parsing.
jtulach@1348
   839
     * @since 1.5
jtulach@1348
   840
     */
jtulach@1348
   841
    public static final int LITERAL = 0x10;
jtulach@1348
   842
jtulach@1348
   843
    /**
jtulach@1348
   844
     * Enables dotall mode.
jtulach@1348
   845
     *
jtulach@1348
   846
     * <p> In dotall mode, the expression <tt>.</tt> matches any character,
jtulach@1348
   847
     * including a line terminator.  By default this expression does not match
jtulach@1348
   848
     * line terminators.
jtulach@1348
   849
     *
jtulach@1348
   850
     * <p> Dotall mode can also be enabled via the embedded flag
jtulach@1348
   851
     * expression&nbsp;<tt>(?s)</tt>.  (The <tt>s</tt> is a mnemonic for
jtulach@1348
   852
     * "single-line" mode, which is what this is called in Perl.)  </p>
jtulach@1348
   853
     */
jtulach@1348
   854
    public static final int DOTALL = 0x20;
jtulach@1348
   855
jtulach@1348
   856
    /**
jtulach@1348
   857
     * Enables Unicode-aware case folding.
jtulach@1348
   858
     *
jtulach@1348
   859
     * <p> When this flag is specified then case-insensitive matching, when
jtulach@1348
   860
     * enabled by the {@link #CASE_INSENSITIVE} flag, is done in a manner
jtulach@1348
   861
     * consistent with the Unicode Standard.  By default, case-insensitive
jtulach@1348
   862
     * matching assumes that only characters in the US-ASCII charset are being
jtulach@1348
   863
     * matched.
jtulach@1348
   864
     *
jtulach@1348
   865
     * <p> Unicode-aware case folding can also be enabled via the embedded flag
jtulach@1348
   866
     * expression&nbsp;<tt>(?u)</tt>.
jtulach@1348
   867
     *
jtulach@1348
   868
     * <p> Specifying this flag may impose a performance penalty.  </p>
jtulach@1348
   869
     */
jtulach@1348
   870
    public static final int UNICODE_CASE = 0x40;
jtulach@1348
   871
jtulach@1348
   872
    /**
jtulach@1348
   873
     * Enables canonical equivalence.
jtulach@1348
   874
     *
jtulach@1348
   875
     * <p> When this flag is specified then two characters will be considered
jtulach@1348
   876
     * to match if, and only if, their full canonical decompositions match.
jtulach@1348
   877
     * The expression <tt>"a&#92;u030A"</tt>, for example, will match the
jtulach@1348
   878
     * string <tt>"&#92;u00E5"</tt> when this flag is specified.  By default,
jtulach@1348
   879
     * matching does not take canonical equivalence into account.
jtulach@1348
   880
     *
jtulach@1348
   881
     * <p> There is no embedded flag character for enabling canonical
jtulach@1348
   882
     * equivalence.
jtulach@1348
   883
     *
jtulach@1348
   884
     * <p> Specifying this flag may impose a performance penalty.  </p>
jtulach@1348
   885
     */
jtulach@1348
   886
    public static final int CANON_EQ = 0x80;
jtulach@1348
   887
jtulach@1348
   888
    /**
jtulach@1348
   889
     * Enables the Unicode version of <i>Predefined character classes</i> and
jtulach@1348
   890
     * <i>POSIX character classes</i>.
jtulach@1348
   891
     *
jtulach@1348
   892
     * <p> When this flag is specified then the (US-ASCII only)
jtulach@1348
   893
     * <i>Predefined character classes</i> and <i>POSIX character classes</i>
jtulach@1348
   894
     * are in conformance with
jtulach@1348
   895
     * <a href="http://www.unicode.org/reports/tr18/"><i>Unicode Technical
jtulach@1348
   896
     * Standard #18: Unicode Regular Expression</i></a>
jtulach@1348
   897
     * <i>Annex C: Compatibility Properties</i>.
jtulach@1348
   898
     * <p>
jtulach@1348
   899
     * The UNICODE_CHARACTER_CLASS mode can also be enabled via the embedded
jtulach@1348
   900
     * flag expression&nbsp;<tt>(?U)</tt>.
jtulach@1348
   901
     * <p>
jtulach@1348
   902
     * The flag implies UNICODE_CASE, that is, it enables Unicode-aware case
jtulach@1348
   903
     * folding.
jtulach@1348
   904
     * <p>
jtulach@1348
   905
     * Specifying this flag may impose a performance penalty.  </p>
jtulach@1348
   906
     * @since 1.7
jtulach@1348
   907
     */
jtulach@1348
   908
    public static final int UNICODE_CHARACTER_CLASS = 0x100;
jtulach@1348
   909
jtulach@1348
   910
    /* Pattern has only two serialized components: The pattern string
jtulach@1348
   911
     * and the flags, which are all that is needed to recompile the pattern
jtulach@1348
   912
     * when it is deserialized.
jtulach@1348
   913
     */
jtulach@1348
   914
jtulach@1348
   915
    /** use serialVersionUID from Merlin b59 for interoperability */
jtulach@1348
   916
    private static final long serialVersionUID = 5073258162644648461L;
jtulach@1348
   917
jtulach@1348
   918
    /**
jtulach@1348
   919
     * The original regular-expression pattern string.
jtulach@1348
   920
     *
jtulach@1348
   921
     * @serial
jtulach@1348
   922
     */
jtulach@1348
   923
    private String pattern;
jtulach@1348
   924
jtulach@1348
   925
    /**
jtulach@1348
   926
     * The original pattern flags.
jtulach@1348
   927
     *
jtulach@1348
   928
     * @serial
jtulach@1348
   929
     */
jtulach@1348
   930
    private int flags;
jtulach@1348
   931
jtulach@1348
   932
    /**
jtulach@1348
   933
     * Boolean indicating this Pattern is compiled; this is necessary in order
jtulach@1348
   934
     * to lazily compile deserialized Patterns.
jtulach@1348
   935
     */
jtulach@1348
   936
    private transient volatile boolean compiled = false;
jtulach@1348
   937
jtulach@1348
   938
    /**
jtulach@1348
   939
     * The normalized pattern string.
jtulach@1348
   940
     */
jtulach@1348
   941
    private transient String normalizedPattern;
jtulach@1348
   942
jtulach@1348
   943
    /**
jtulach@1348
   944
     * The starting point of state machine for the find operation.  This allows
jtulach@1348
   945
     * a match to start anywhere in the input.
jtulach@1348
   946
     */
jtulach@1348
   947
    transient Node root;
jtulach@1348
   948
jtulach@1348
   949
    /**
jtulach@1348
   950
     * The root of object tree for a match operation.  The pattern is matched
jtulach@1348
   951
     * at the beginning.  This may include a find that uses BnM or a First
jtulach@1348
   952
     * node.
jtulach@1348
   953
     */
jtulach@1348
   954
    transient Node matchRoot;
jtulach@1348
   955
jtulach@1348
   956
    /**
jtulach@1348
   957
     * Temporary storage used by parsing pattern slice.
jtulach@1348
   958
     */
jtulach@1348
   959
    transient int[] buffer;
jtulach@1348
   960
jtulach@1348
   961
    /**
jtulach@1348
   962
     * Map the "name" of the "named capturing group" to its group id
jtulach@1348
   963
     * node.
jtulach@1348
   964
     */
jtulach@1348
   965
    transient volatile Map<String, Integer> namedGroups;
jtulach@1348
   966
jtulach@1348
   967
    /**
jtulach@1348
   968
     * Temporary storage used while parsing group references.
jtulach@1348
   969
     */
jtulach@1348
   970
    transient GroupHead[] groupNodes;
jtulach@1348
   971
jtulach@1348
   972
    /**
jtulach@1348
   973
     * Temporary null terminated code point array used by pattern compiling.
jtulach@1348
   974
     */
jtulach@1348
   975
    private transient int[] temp;
jtulach@1348
   976
jtulach@1348
   977
    /**
jtulach@1348
   978
     * The number of capturing groups in this Pattern. Used by matchers to
jtulach@1348
   979
     * allocate storage needed to perform a match.
jtulach@1348
   980
     */
jtulach@1348
   981
    transient int capturingGroupCount;
jtulach@1348
   982
jtulach@1348
   983
    /**
jtulach@1348
   984
     * The local variable count used by parsing tree. Used by matchers to
jtulach@1348
   985
     * allocate storage needed to perform a match.
jtulach@1348
   986
     */
jtulach@1348
   987
    transient int localCount;
jtulach@1348
   988
jtulach@1348
   989
    /**
jtulach@1348
   990
     * Index into the pattern string that keeps track of how much has been
jtulach@1348
   991
     * parsed.
jtulach@1348
   992
     */
jtulach@1348
   993
    private transient int cursor;
jtulach@1348
   994
jtulach@1348
   995
    /**
jtulach@1348
   996
     * Holds the length of the pattern string.
jtulach@1348
   997
     */
jtulach@1348
   998
    private transient int patternLength;
jtulach@1348
   999
jtulach@1348
  1000
    /**
jtulach@1348
  1001
     * If the Start node might possibly match supplementary characters.
jtulach@1348
  1002
     * It is set to true during compiling if
jtulach@1348
  1003
     * (1) There is supplementary char in pattern, or
jtulach@1348
  1004
     * (2) There is complement node of Category or Block
jtulach@1348
  1005
     */
jtulach@1348
  1006
    private transient boolean hasSupplementary;
jtulach@1348
  1007
jtulach@1348
  1008
    /**
jtulach@1348
  1009
     * Compiles the given regular expression into a pattern.  </p>
jtulach@1348
  1010
     *
jtulach@1348
  1011
     * @param  regex
jtulach@1348
  1012
     *         The expression to be compiled
jtulach@1348
  1013
     *
jtulach@1348
  1014
     * @throws  PatternSyntaxException
jtulach@1348
  1015
     *          If the expression's syntax is invalid
jtulach@1348
  1016
     */
jtulach@1348
  1017
    public static Pattern compile(String regex) {
jtulach@1348
  1018
        return new Pattern(regex, 0);
jtulach@1348
  1019
    }
jtulach@1348
  1020
jtulach@1348
  1021
    /**
jtulach@1348
  1022
     * Compiles the given regular expression into a pattern with the given
jtulach@1348
  1023
     * flags.  </p>
jtulach@1348
  1024
     *
jtulach@1348
  1025
     * @param  regex
jtulach@1348
  1026
     *         The expression to be compiled
jtulach@1348
  1027
     *
jtulach@1348
  1028
     * @param  flags
jtulach@1348
  1029
     *         Match flags, a bit mask that may include
jtulach@1348
  1030
     *         {@link #CASE_INSENSITIVE}, {@link #MULTILINE}, {@link #DOTALL},
jtulach@1348
  1031
     *         {@link #UNICODE_CASE}, {@link #CANON_EQ}, {@link #UNIX_LINES},
jtulach@1348
  1032
     *         {@link #LITERAL}, {@link #UNICODE_CHARACTER_CLASS}
jtulach@1348
  1033
     *         and {@link #COMMENTS}
jtulach@1348
  1034
     *
jtulach@1348
  1035
     * @throws  IllegalArgumentException
jtulach@1348
  1036
     *          If bit values other than those corresponding to the defined
jtulach@1348
  1037
     *          match flags are set in <tt>flags</tt>
jtulach@1348
  1038
     *
jtulach@1348
  1039
     * @throws  PatternSyntaxException
jtulach@1348
  1040
     *          If the expression's syntax is invalid
jtulach@1348
  1041
     */
jtulach@1348
  1042
    public static Pattern compile(String regex, int flags) {
jtulach@1348
  1043
        return new Pattern(regex, flags);
jtulach@1348
  1044
    }
jtulach@1348
  1045
jtulach@1348
  1046
    /**
jtulach@1348
  1047
     * Returns the regular expression from which this pattern was compiled.
jtulach@1348
  1048
     * </p>
jtulach@1348
  1049
     *
jtulach@1348
  1050
     * @return  The source of this pattern
jtulach@1348
  1051
     */
jtulach@1348
  1052
    public String pattern() {
jtulach@1348
  1053
        return pattern;
jtulach@1348
  1054
    }
jtulach@1348
  1055
jtulach@1348
  1056
    /**
jtulach@1348
  1057
     * <p>Returns the string representation of this pattern. This
jtulach@1348
  1058
     * is the regular expression from which this pattern was
jtulach@1348
  1059
     * compiled.</p>
jtulach@1348
  1060
     *
jtulach@1348
  1061
     * @return  The string representation of this pattern
jtulach@1348
  1062
     * @since 1.5
jtulach@1348
  1063
     */
jtulach@1348
  1064
    public String toString() {
jtulach@1348
  1065
        return pattern;
jtulach@1348
  1066
    }
jtulach@1348
  1067
jtulach@1348
  1068
    /**
jtulach@1348
  1069
     * Creates a matcher that will match the given input against this pattern.
jtulach@1348
  1070
     * </p>
jtulach@1348
  1071
     *
jtulach@1348
  1072
     * @param  input
jtulach@1348
  1073
     *         The character sequence to be matched
jtulach@1348
  1074
     *
jtulach@1348
  1075
     * @return  A new matcher for this pattern
jtulach@1348
  1076
     */
jtulach@1348
  1077
    public Matcher matcher(CharSequence input) {
jtulach@1348
  1078
        if (!compiled) {
jtulach@1348
  1079
            synchronized(this) {
jtulach@1348
  1080
                if (!compiled)
jtulach@1348
  1081
                    compile();
jtulach@1348
  1082
            }
jtulach@1348
  1083
        }
jtulach@1348
  1084
        Matcher m = new Matcher(this, input);
jtulach@1348
  1085
        return m;
jtulach@1348
  1086
    }
jtulach@1348
  1087
jtulach@1348
  1088
    /**
jtulach@1348
  1089
     * Returns this pattern's match flags.  </p>
jtulach@1348
  1090
     *
jtulach@1348
  1091
     * @return  The match flags specified when this pattern was compiled
jtulach@1348
  1092
     */
jtulach@1348
  1093
    public int flags() {
jtulach@1348
  1094
        return flags;
jtulach@1348
  1095
    }
jtulach@1348
  1096
jtulach@1348
  1097
    /**
jtulach@1348
  1098
     * Compiles the given regular expression and attempts to match the given
jtulach@1348
  1099
     * input against it.
jtulach@1348
  1100
     *
jtulach@1348
  1101
     * <p> An invocation of this convenience method of the form
jtulach@1348
  1102
     *
jtulach@1348
  1103
     * <blockquote><pre>
jtulach@1348
  1104
     * Pattern.matches(regex, input);</pre></blockquote>
jtulach@1348
  1105
     *
jtulach@1348
  1106
     * behaves in exactly the same way as the expression
jtulach@1348
  1107
     *
jtulach@1348
  1108
     * <blockquote><pre>
jtulach@1348
  1109
     * Pattern.compile(regex).matcher(input).matches()</pre></blockquote>
jtulach@1348
  1110
     *
jtulach@1348
  1111
     * <p> If a pattern is to be used multiple times, compiling it once and reusing
jtulach@1348
  1112
     * it will be more efficient than invoking this method each time.  </p>
jtulach@1348
  1113
     *
jtulach@1348
  1114
     * @param  regex
jtulach@1348
  1115
     *         The expression to be compiled
jtulach@1348
  1116
     *
jtulach@1348
  1117
     * @param  input
jtulach@1348
  1118
     *         The character sequence to be matched
jtulach@1348
  1119
     *
jtulach@1348
  1120
     * @throws  PatternSyntaxException
jtulach@1348
  1121
     *          If the expression's syntax is invalid
jtulach@1348
  1122
     */
jtulach@1348
  1123
    public static boolean matches(String regex, CharSequence input) {
jtulach@1348
  1124
        Pattern p = Pattern.compile(regex);
jtulach@1348
  1125
        Matcher m = p.matcher(input);
jtulach@1348
  1126
        return m.matches();
jtulach@1348
  1127
    }
jtulach@1348
  1128
jtulach@1348
  1129
    /**
jtulach@1348
  1130
     * Splits the given input sequence around matches of this pattern.
jtulach@1348
  1131
     *
jtulach@1348
  1132
     * <p> The array returned by this method contains each substring of the
jtulach@1348
  1133
     * input sequence that is terminated by another subsequence that matches
jtulach@1348
  1134
     * this pattern or is terminated by the end of the input sequence.  The
jtulach@1348
  1135
     * substrings in the array are in the order in which they occur in the
jtulach@1348
  1136
     * input.  If this pattern does not match any subsequence of the input then
jtulach@1348
  1137
     * the resulting array has just one element, namely the input sequence in
jtulach@1348
  1138
     * string form.
jtulach@1348
  1139
     *
jtulach@1348
  1140
     * <p> The <tt>limit</tt> parameter controls the number of times the
jtulach@1348
  1141
     * pattern is applied and therefore affects the length of the resulting
jtulach@1348
  1142
     * array.  If the limit <i>n</i> is greater than zero then the pattern
jtulach@1348
  1143
     * will be applied at most <i>n</i>&nbsp;-&nbsp;1 times, the array's
jtulach@1348
  1144
     * length will be no greater than <i>n</i>, and the array's last entry
jtulach@1348
  1145
     * will contain all input beyond the last matched delimiter.  If <i>n</i>
jtulach@1348
  1146
     * is non-positive then the pattern will be applied as many times as
jtulach@1348
  1147
     * possible and the array can have any length.  If <i>n</i> is zero then
jtulach@1348
  1148
     * the pattern will be applied as many times as possible, the array can
jtulach@1348
  1149
     * have any length, and trailing empty strings will be discarded.
jtulach@1348
  1150
     *
jtulach@1348
  1151
     * <p> The input <tt>"boo:and:foo"</tt>, for example, yields the following
jtulach@1348
  1152
     * results with these parameters:
jtulach@1348
  1153
     *
jtulach@1348
  1154
     * <blockquote><table cellpadding=1 cellspacing=0
jtulach@1348
  1155
     *              summary="Split examples showing regex, limit, and result">
jtulach@1348
  1156
     * <tr><th><P align="left"><i>Regex&nbsp;&nbsp;&nbsp;&nbsp;</i></th>
jtulach@1348
  1157
     *     <th><P align="left"><i>Limit&nbsp;&nbsp;&nbsp;&nbsp;</i></th>
jtulach@1348
  1158
     *     <th><P align="left"><i>Result&nbsp;&nbsp;&nbsp;&nbsp;</i></th></tr>
jtulach@1348
  1159
     * <tr><td align=center>:</td>
jtulach@1348
  1160
     *     <td align=center>2</td>
jtulach@1348
  1161
     *     <td><tt>{ "boo", "and:foo" }</tt></td></tr>
jtulach@1348
  1162
     * <tr><td align=center>:</td>
jtulach@1348
  1163
     *     <td align=center>5</td>
jtulach@1348
  1164
     *     <td><tt>{ "boo", "and", "foo" }</tt></td></tr>
jtulach@1348
  1165
     * <tr><td align=center>:</td>
jtulach@1348
  1166
     *     <td align=center>-2</td>
jtulach@1348
  1167
     *     <td><tt>{ "boo", "and", "foo" }</tt></td></tr>
jtulach@1348
  1168
     * <tr><td align=center>o</td>
jtulach@1348
  1169
     *     <td align=center>5</td>
jtulach@1348
  1170
     *     <td><tt>{ "b", "", ":and:f", "", "" }</tt></td></tr>
jtulach@1348
  1171
     * <tr><td align=center>o</td>
jtulach@1348
  1172
     *     <td align=center>-2</td>
jtulach@1348
  1173
     *     <td><tt>{ "b", "", ":and:f", "", "" }</tt></td></tr>
jtulach@1348
  1174
     * <tr><td align=center>o</td>
jtulach@1348
  1175
     *     <td align=center>0</td>
jtulach@1348
  1176
     *     <td><tt>{ "b", "", ":and:f" }</tt></td></tr>
jtulach@1348
  1177
     * </table></blockquote>
jtulach@1348
  1178
     *
jtulach@1348
  1179
     *
jtulach@1348
  1180
     * @param  input
jtulach@1348
  1181
     *         The character sequence to be split
jtulach@1348
  1182
     *
jtulach@1348
  1183
     * @param  limit
jtulach@1348
  1184
     *         The result threshold, as described above
jtulach@1348
  1185
     *
jtulach@1348
  1186
     * @return  The array of strings computed by splitting the input
jtulach@1348
  1187
     *          around matches of this pattern
jtulach@1348
  1188
     */
jtulach@1348
  1189
    public String[] split(CharSequence input, int limit) {
jtulach@1348
  1190
        int index = 0;
jtulach@1348
  1191
        boolean matchLimited = limit > 0;
jtulach@1348
  1192
        ArrayList<String> matchList = new ArrayList<>();
jtulach@1348
  1193
        Matcher m = matcher(input);
jtulach@1348
  1194
jtulach@1348
  1195
        // Add segments before each match found
jtulach@1348
  1196
        while(m.find()) {
jtulach@1348
  1197
            if (!matchLimited || matchList.size() < limit - 1) {
jtulach@1348
  1198
                String match = input.subSequence(index, m.start()).toString();
jtulach@1348
  1199
                matchList.add(match);
jtulach@1348
  1200
                index = m.end();
jtulach@1348
  1201
            } else if (matchList.size() == limit - 1) { // last one
jtulach@1348
  1202
                String match = input.subSequence(index,
jtulach@1348
  1203
                                                 input.length()).toString();
jtulach@1348
  1204
                matchList.add(match);
jtulach@1348
  1205
                index = m.end();
jtulach@1348
  1206
            }
jtulach@1348
  1207
        }
jtulach@1348
  1208
jtulach@1348
  1209
        // If no match was found, return this
jtulach@1348
  1210
        if (index == 0)
jtulach@1348
  1211
            return new String[] {input.toString()};
jtulach@1348
  1212
jtulach@1348
  1213
        // Add remaining segment
jtulach@1348
  1214
        if (!matchLimited || matchList.size() < limit)
jtulach@1348
  1215
            matchList.add(input.subSequence(index, input.length()).toString());
jtulach@1348
  1216
jtulach@1348
  1217
        // Construct result
jtulach@1348
  1218
        int resultSize = matchList.size();
jtulach@1348
  1219
        if (limit == 0)
jtulach@1348
  1220
            while (resultSize > 0 && matchList.get(resultSize-1).equals(""))
jtulach@1348
  1221
                resultSize--;
jtulach@1348
  1222
        String[] result = new String[resultSize];
jtulach@1348
  1223
        return matchList.subList(0, resultSize).toArray(result);
jtulach@1348
  1224
    }
jtulach@1348
  1225
jtulach@1348
  1226
    /**
jtulach@1348
  1227
     * Splits the given input sequence around matches of this pattern.
jtulach@1348
  1228
     *
jtulach@1348
  1229
     * <p> This method works as if by invoking the two-argument {@link
jtulach@1348
  1230
     * #split(java.lang.CharSequence, int) split} method with the given input
jtulach@1348
  1231
     * sequence and a limit argument of zero.  Trailing empty strings are
jtulach@1348
  1232
     * therefore not included in the resulting array. </p>
jtulach@1348
  1233
     *
jtulach@1348
  1234
     * <p> The input <tt>"boo:and:foo"</tt>, for example, yields the following
jtulach@1348
  1235
     * results with these expressions:
jtulach@1348
  1236
     *
jtulach@1348
  1237
     * <blockquote><table cellpadding=1 cellspacing=0
jtulach@1348
  1238
     *              summary="Split examples showing regex and result">
jtulach@1348
  1239
     * <tr><th><P align="left"><i>Regex&nbsp;&nbsp;&nbsp;&nbsp;</i></th>
jtulach@1348
  1240
     *     <th><P align="left"><i>Result</i></th></tr>
jtulach@1348
  1241
     * <tr><td align=center>:</td>
jtulach@1348
  1242
     *     <td><tt>{ "boo", "and", "foo" }</tt></td></tr>
jtulach@1348
  1243
     * <tr><td align=center>o</td>
jtulach@1348
  1244
     *     <td><tt>{ "b", "", ":and:f" }</tt></td></tr>
jtulach@1348
  1245
     * </table></blockquote>
jtulach@1348
  1246
     *
jtulach@1348
  1247
     *
jtulach@1348
  1248
     * @param  input
jtulach@1348
  1249
     *         The character sequence to be split
jtulach@1348
  1250
     *
jtulach@1348
  1251
     * @return  The array of strings computed by splitting the input
jtulach@1348
  1252
     *          around matches of this pattern
jtulach@1348
  1253
     */
jtulach@1348
  1254
    public String[] split(CharSequence input) {
jtulach@1348
  1255
        return split(input, 0);
jtulach@1348
  1256
    }
jtulach@1348
  1257
jtulach@1348
  1258
    /**
jtulach@1348
  1259
     * Returns a literal pattern <code>String</code> for the specified
jtulach@1348
  1260
     * <code>String</code>.
jtulach@1348
  1261
     *
jtulach@1348
  1262
     * <p>This method produces a <code>String</code> that can be used to
jtulach@1348
  1263
     * create a <code>Pattern</code> that would match the string
jtulach@1348
  1264
     * <code>s</code> as if it were a literal pattern.</p> Metacharacters
jtulach@1348
  1265
     * or escape sequences in the input sequence will be given no special
jtulach@1348
  1266
     * meaning.
jtulach@1348
  1267
     *
jtulach@1348
  1268
     * @param  s The string to be literalized
jtulach@1348
  1269
     * @return  A literal string replacement
jtulach@1348
  1270
     * @since 1.5
jtulach@1348
  1271
     */
jtulach@1348
  1272
    public static String quote(String s) {
jtulach@1348
  1273
        int slashEIndex = s.indexOf("\\E");
jtulach@1348
  1274
        if (slashEIndex == -1)
jtulach@1348
  1275
            return "\\Q" + s + "\\E";
jtulach@1348
  1276
jtulach@1348
  1277
        StringBuilder sb = new StringBuilder(s.length() * 2);
jtulach@1348
  1278
        sb.append("\\Q");
jtulach@1348
  1279
        slashEIndex = 0;
jtulach@1348
  1280
        int current = 0;
jtulach@1348
  1281
        while ((slashEIndex = s.indexOf("\\E", current)) != -1) {
jtulach@1348
  1282
            sb.append(s.substring(current, slashEIndex));
jtulach@1348
  1283
            current = slashEIndex + 2;
jtulach@1348
  1284
            sb.append("\\E\\\\E\\Q");
jtulach@1348
  1285
        }
jtulach@1348
  1286
        sb.append(s.substring(current, s.length()));
jtulach@1348
  1287
        sb.append("\\E");
jtulach@1348
  1288
        return sb.toString();
jtulach@1348
  1289
    }
jtulach@1348
  1290
jtulach@1348
  1291
    /**
jtulach@1348
  1292
     * Recompile the Pattern instance from a stream.  The original pattern
jtulach@1348
  1293
     * string is read in and the object tree is recompiled from it.
jtulach@1348
  1294
     */
jtulach@1348
  1295
    private void readObject(java.io.ObjectInputStream s)
jtulach@1348
  1296
        throws java.io.IOException, ClassNotFoundException {
jtulach@1348
  1297
jtulach@1348
  1298
        // Read in all fields
jtulach@1348
  1299
        s.defaultReadObject();
jtulach@1348
  1300
jtulach@1348
  1301
        // Initialize counts
jtulach@1348
  1302
        capturingGroupCount = 1;
jtulach@1348
  1303
        localCount = 0;
jtulach@1348
  1304
jtulach@1348
  1305
        // if length > 0, the Pattern is lazily compiled
jtulach@1348
  1306
        compiled = false;
jtulach@1348
  1307
        if (pattern.length() == 0) {
jtulach@1348
  1308
            root = new Start(lastAccept);
jtulach@1348
  1309
            matchRoot = lastAccept;
jtulach@1348
  1310
            compiled = true;
jtulach@1348
  1311
        }
jtulach@1348
  1312
    }
jtulach@1348
  1313
jtulach@1348
  1314
    /**
jtulach@1348
  1315
     * This private constructor is used to create all Patterns. The pattern
jtulach@1348
  1316
     * string and match flags are all that is needed to completely describe
jtulach@1348
  1317
     * a Pattern. An empty pattern string results in an object tree with
jtulach@1348
  1318
     * only a Start node and a LastNode node.
jtulach@1348
  1319
     */
jtulach@1348
  1320
    private Pattern(String p, int f) {
jtulach@1348
  1321
        pattern = p;
jtulach@1348
  1322
        flags = f;
jtulach@1348
  1323
jtulach@1348
  1324
        // to use UNICODE_CASE if UNICODE_CHARACTER_CLASS present
jtulach@1348
  1325
        if ((flags & UNICODE_CHARACTER_CLASS) != 0)
jtulach@1348
  1326
            flags |= UNICODE_CASE;
jtulach@1348
  1327
jtulach@1348
  1328
        // Reset group index count
jtulach@1348
  1329
        capturingGroupCount = 1;
jtulach@1348
  1330
        localCount = 0;
jtulach@1348
  1331
jtulach@1348
  1332
        if (pattern.length() > 0) {
jtulach@1348
  1333
            compile();
jtulach@1348
  1334
        } else {
jtulach@1348
  1335
            root = new Start(lastAccept);
jtulach@1348
  1336
            matchRoot = lastAccept;
jtulach@1348
  1337
        }
jtulach@1348
  1338
    }
jtulach@1348
  1339
jtulach@1348
  1340
    /**
jtulach@1348
  1341
     * The pattern is converted to normalizedD form and then a pure group
jtulach@1348
  1342
     * is constructed to match canonical equivalences of the characters.
jtulach@1348
  1343
     */
jtulach@1348
  1344
    private void normalize() {
jtulach@1348
  1345
        boolean inCharClass = false;
jtulach@1348
  1346
        int lastCodePoint = -1;
jtulach@1348
  1347
jtulach@1348
  1348
        // Convert pattern into normalizedD form
jtulach@1350
  1349
        normalizedPattern = Normalizer.normalize(pattern, Normalizer.NFD);
jtulach@1348
  1350
        patternLength = normalizedPattern.length();
jtulach@1348
  1351
jtulach@1348
  1352
        // Modify pattern to match canonical equivalences
jtulach@1348
  1353
        StringBuilder newPattern = new StringBuilder(patternLength);
jtulach@1348
  1354
        for(int i=0; i<patternLength; ) {
jtulach@1348
  1355
            int c = normalizedPattern.codePointAt(i);
jtulach@1348
  1356
            StringBuilder sequenceBuffer;
jtulach@1348
  1357
            if ((Character.getType(c) == Character.NON_SPACING_MARK)
jtulach@1348
  1358
                && (lastCodePoint != -1)) {
jtulach@1348
  1359
                sequenceBuffer = new StringBuilder();
jtulach@1348
  1360
                sequenceBuffer.appendCodePoint(lastCodePoint);
jtulach@1348
  1361
                sequenceBuffer.appendCodePoint(c);
jtulach@1348
  1362
                while(Character.getType(c) == Character.NON_SPACING_MARK) {
jtulach@1348
  1363
                    i += Character.charCount(c);
jtulach@1348
  1364
                    if (i >= patternLength)
jtulach@1348
  1365
                        break;
jtulach@1348
  1366
                    c = normalizedPattern.codePointAt(i);
jtulach@1348
  1367
                    sequenceBuffer.appendCodePoint(c);
jtulach@1348
  1368
                }
jtulach@1348
  1369
                String ea = produceEquivalentAlternation(
jtulach@1348
  1370
                                               sequenceBuffer.toString());
jtulach@1348
  1371
                newPattern.setLength(newPattern.length()-Character.charCount(lastCodePoint));
jtulach@1348
  1372
                newPattern.append("(?:").append(ea).append(")");
jtulach@1348
  1373
            } else if (c == '[' && lastCodePoint != '\\') {
jtulach@1348
  1374
                i = normalizeCharClass(newPattern, i);
jtulach@1348
  1375
            } else {
jtulach@1348
  1376
                newPattern.appendCodePoint(c);
jtulach@1348
  1377
            }
jtulach@1348
  1378
            lastCodePoint = c;
jtulach@1348
  1379
            i += Character.charCount(c);
jtulach@1348
  1380
        }
jtulach@1348
  1381
        normalizedPattern = newPattern.toString();
jtulach@1348
  1382
    }
jtulach@1348
  1383
jtulach@1348
  1384
    /**
jtulach@1348
  1385
     * Complete the character class being parsed and add a set
jtulach@1348
  1386
     * of alternations to it that will match the canonical equivalences
jtulach@1348
  1387
     * of the characters within the class.
jtulach@1348
  1388
     */
jtulach@1348
  1389
    private int normalizeCharClass(StringBuilder newPattern, int i) {
jtulach@1348
  1390
        StringBuilder charClass = new StringBuilder();
jtulach@1348
  1391
        StringBuilder eq = null;
jtulach@1348
  1392
        int lastCodePoint = -1;
jtulach@1348
  1393
        String result;
jtulach@1348
  1394
jtulach@1348
  1395
        i++;
jtulach@1348
  1396
        charClass.append("[");
jtulach@1348
  1397
        while(true) {
jtulach@1348
  1398
            int c = normalizedPattern.codePointAt(i);
jtulach@1348
  1399
            StringBuilder sequenceBuffer;
jtulach@1348
  1400
jtulach@1348
  1401
            if (c == ']' && lastCodePoint != '\\') {
jtulach@1348
  1402
                charClass.append((char)c);
jtulach@1348
  1403
                break;
jtulach@1348
  1404
            } else if (Character.getType(c) == Character.NON_SPACING_MARK) {
jtulach@1348
  1405
                sequenceBuffer = new StringBuilder();
jtulach@1348
  1406
                sequenceBuffer.appendCodePoint(lastCodePoint);
jtulach@1348
  1407
                while(Character.getType(c) == Character.NON_SPACING_MARK) {
jtulach@1348
  1408
                    sequenceBuffer.appendCodePoint(c);
jtulach@1348
  1409
                    i += Character.charCount(c);
jtulach@1348
  1410
                    if (i >= normalizedPattern.length())
jtulach@1348
  1411
                        break;
jtulach@1348
  1412
                    c = normalizedPattern.codePointAt(i);
jtulach@1348
  1413
                }
jtulach@1348
  1414
                String ea = produceEquivalentAlternation(
jtulach@1348
  1415
                                                  sequenceBuffer.toString());
jtulach@1348
  1416
jtulach@1348
  1417
                charClass.setLength(charClass.length()-Character.charCount(lastCodePoint));
jtulach@1348
  1418
                if (eq == null)
jtulach@1348
  1419
                    eq = new StringBuilder();
jtulach@1348
  1420
                eq.append('|');
jtulach@1348
  1421
                eq.append(ea);
jtulach@1348
  1422
            } else {
jtulach@1348
  1423
                charClass.appendCodePoint(c);
jtulach@1348
  1424
                i++;
jtulach@1348
  1425
            }
jtulach@1348
  1426
            if (i == normalizedPattern.length())
jtulach@1348
  1427
                throw error("Unclosed character class");
jtulach@1348
  1428
            lastCodePoint = c;
jtulach@1348
  1429
        }
jtulach@1348
  1430
jtulach@1348
  1431
        if (eq != null) {
jtulach@1348
  1432
            result = "(?:"+charClass.toString()+eq.toString()+")";
jtulach@1348
  1433
        } else {
jtulach@1348
  1434
            result = charClass.toString();
jtulach@1348
  1435
        }
jtulach@1348
  1436
jtulach@1348
  1437
        newPattern.append(result);
jtulach@1348
  1438
        return i;
jtulach@1348
  1439
    }
jtulach@1348
  1440
jtulach@1348
  1441
    /**
jtulach@1348
  1442
     * Given a specific sequence composed of a regular character and
jtulach@1348
  1443
     * combining marks that follow it, produce the alternation that will
jtulach@1348
  1444
     * match all canonical equivalences of that sequence.
jtulach@1348
  1445
     */
jtulach@1348
  1446
    private String produceEquivalentAlternation(String source) {
jtulach@1348
  1447
        int len = countChars(source, 0, 1);
jtulach@1348
  1448
        if (source.length() == len)
jtulach@1348
  1449
            // source has one character.
jtulach@1348
  1450
            return source;
jtulach@1348
  1451
jtulach@1348
  1452
        String base = source.substring(0,len);
jtulach@1348
  1453
        String combiningMarks = source.substring(len);
jtulach@1348
  1454
jtulach@1348
  1455
        String[] perms = producePermutations(combiningMarks);
jtulach@1348
  1456
        StringBuilder result = new StringBuilder(source);
jtulach@1348
  1457
jtulach@1348
  1458
        // Add combined permutations
jtulach@1348
  1459
        for(int x=0; x<perms.length; x++) {
jtulach@1348
  1460
            String next = base + perms[x];
jtulach@1348
  1461
            if (x>0)
jtulach@1348
  1462
                result.append("|"+next);
jtulach@1348
  1463
            next = composeOneStep(next);
jtulach@1348
  1464
            if (next != null)
jtulach@1348
  1465
                result.append("|"+produceEquivalentAlternation(next));
jtulach@1348
  1466
        }
jtulach@1348
  1467
        return result.toString();
jtulach@1348
  1468
    }
jtulach@1348
  1469
jtulach@1348
  1470
    /**
jtulach@1348
  1471
     * Returns an array of strings that have all the possible
jtulach@1348
  1472
     * permutations of the characters in the input string.
jtulach@1348
  1473
     * This is used to get a list of all possible orderings
jtulach@1348
  1474
     * of a set of combining marks. Note that some of the permutations
jtulach@1348
  1475
     * are invalid because of combining class collisions, and these
jtulach@1348
  1476
     * possibilities must be removed because they are not canonically
jtulach@1348
  1477
     * equivalent.
jtulach@1348
  1478
     */
jtulach@1348
  1479
    private String[] producePermutations(String input) {
jtulach@1348
  1480
        if (input.length() == countChars(input, 0, 1))
jtulach@1348
  1481
            return new String[] {input};
jtulach@1348
  1482
jtulach@1348
  1483
        if (input.length() == countChars(input, 0, 2)) {
jtulach@1348
  1484
            int c0 = Character.codePointAt(input, 0);
jtulach@1348
  1485
            int c1 = Character.codePointAt(input, Character.charCount(c0));
jtulach@1348
  1486
            if (getClass(c1) == getClass(c0)) {
jtulach@1348
  1487
                return new String[] {input};
jtulach@1348
  1488
            }
jtulach@1348
  1489
            String[] result = new String[2];
jtulach@1348
  1490
            result[0] = input;
jtulach@1348
  1491
            StringBuilder sb = new StringBuilder(2);
jtulach@1348
  1492
            sb.appendCodePoint(c1);
jtulach@1348
  1493
            sb.appendCodePoint(c0);
jtulach@1348
  1494
            result[1] = sb.toString();
jtulach@1348
  1495
            return result;
jtulach@1348
  1496
        }
jtulach@1348
  1497
jtulach@1348
  1498
        int length = 1;
jtulach@1348
  1499
        int nCodePoints = countCodePoints(input);
jtulach@1348
  1500
        for(int x=1; x<nCodePoints; x++)
jtulach@1348
  1501
            length = length * (x+1);
jtulach@1348
  1502
jtulach@1348
  1503
        String[] temp = new String[length];
jtulach@1348
  1504
jtulach@1348
  1505
        int combClass[] = new int[nCodePoints];
jtulach@1348
  1506
        for(int x=0, i=0; x<nCodePoints; x++) {
jtulach@1348
  1507
            int c = Character.codePointAt(input, i);
jtulach@1348
  1508
            combClass[x] = getClass(c);
jtulach@1348
  1509
            i +=  Character.charCount(c);
jtulach@1348
  1510
        }
jtulach@1348
  1511
jtulach@1348
  1512
        // For each char, take it out and add the permutations
jtulach@1348
  1513
        // of the remaining chars
jtulach@1348
  1514
        int index = 0;
jtulach@1348
  1515
        int len;
jtulach@1348
  1516
        // offset maintains the index in code units.
jtulach@1348
  1517
loop:   for(int x=0, offset=0; x<nCodePoints; x++, offset+=len) {
jtulach@1348
  1518
            len = countChars(input, offset, 1);
jtulach@1348
  1519
            boolean skip = false;
jtulach@1348
  1520
            for(int y=x-1; y>=0; y--) {
jtulach@1348
  1521
                if (combClass[y] == combClass[x]) {
jtulach@1348
  1522
                    continue loop;
jtulach@1348
  1523
                }
jtulach@1348
  1524
            }
jtulach@1348
  1525
            StringBuilder sb = new StringBuilder(input);
jtulach@1348
  1526
            String otherChars = sb.delete(offset, offset+len).toString();
jtulach@1348
  1527
            String[] subResult = producePermutations(otherChars);
jtulach@1348
  1528
jtulach@1348
  1529
            String prefix = input.substring(offset, offset+len);
jtulach@1348
  1530
            for(int y=0; y<subResult.length; y++)
jtulach@1348
  1531
                temp[index++] =  prefix + subResult[y];
jtulach@1348
  1532
        }
jtulach@1348
  1533
        String[] result = new String[index];
jtulach@1348
  1534
        for (int x=0; x<index; x++)
jtulach@1348
  1535
            result[x] = temp[x];
jtulach@1348
  1536
        return result;
jtulach@1348
  1537
    }
jtulach@1348
  1538
jtulach@1348
  1539
    private int getClass(int c) {
jtulach@1350
  1540
        return Normalizer.getCombiningClass(c);
jtulach@1348
  1541
    }
jtulach@1348
  1542
jtulach@1348
  1543
    /**
jtulach@1348
  1544
     * Attempts to compose input by combining the first character
jtulach@1348
  1545
     * with the first combining mark following it. Returns a String
jtulach@1348
  1546
     * that is the composition of the leading character with its first
jtulach@1348
  1547
     * combining mark followed by the remaining combining marks. Returns
jtulach@1348
  1548
     * null if the first two characters cannot be further composed.
jtulach@1348
  1549
     */
jtulach@1348
  1550
    private String composeOneStep(String input) {
jtulach@1348
  1551
        int len = countChars(input, 0, 2);
jtulach@1348
  1552
        String firstTwoCharacters = input.substring(0, len);
jtulach@1350
  1553
        String result = Normalizer.normalize(firstTwoCharacters, Normalizer.NFC);
jtulach@1348
  1554
jtulach@1348
  1555
        if (result.equals(firstTwoCharacters))
jtulach@1348
  1556
            return null;
jtulach@1348
  1557
        else {
jtulach@1348
  1558
            String remainder = input.substring(len);
jtulach@1348
  1559
            return result + remainder;
jtulach@1348
  1560
        }
jtulach@1348
  1561
    }
jtulach@1348
  1562
jtulach@1348
  1563
    /**
jtulach@1348
  1564
     * Preprocess any \Q...\E sequences in `temp', meta-quoting them.
jtulach@1348
  1565
     * See the description of `quotemeta' in perlfunc(1).
jtulach@1348
  1566
     */
jtulach@1348
  1567
    private void RemoveQEQuoting() {
jtulach@1348
  1568
        final int pLen = patternLength;
jtulach@1348
  1569
        int i = 0;
jtulach@1348
  1570
        while (i < pLen-1) {
jtulach@1348
  1571
            if (temp[i] != '\\')
jtulach@1348
  1572
                i += 1;
jtulach@1348
  1573
            else if (temp[i + 1] != 'Q')
jtulach@1348
  1574
                i += 2;
jtulach@1348
  1575
            else
jtulach@1348
  1576
                break;
jtulach@1348
  1577
        }
jtulach@1348
  1578
        if (i >= pLen - 1)    // No \Q sequence found
jtulach@1348
  1579
            return;
jtulach@1348
  1580
        int j = i;
jtulach@1348
  1581
        i += 2;
jtulach@1348
  1582
        int[] newtemp = new int[j + 2*(pLen-i) + 2];
jtulach@1348
  1583
        System.arraycopy(temp, 0, newtemp, 0, j);
jtulach@1348
  1584
jtulach@1348
  1585
        boolean inQuote = true;
jtulach@1348
  1586
        while (i < pLen) {
jtulach@1348
  1587
            int c = temp[i++];
jtulach@1348
  1588
            if (! ASCII.isAscii(c) || ASCII.isAlnum(c)) {
jtulach@1348
  1589
                newtemp[j++] = c;
jtulach@1348
  1590
            } else if (c != '\\') {
jtulach@1348
  1591
                if (inQuote) newtemp[j++] = '\\';
jtulach@1348
  1592
                newtemp[j++] = c;
jtulach@1348
  1593
            } else if (inQuote) {
jtulach@1348
  1594
                if (temp[i] == 'E') {
jtulach@1348
  1595
                    i++;
jtulach@1348
  1596
                    inQuote = false;
jtulach@1348
  1597
                } else {
jtulach@1348
  1598
                    newtemp[j++] = '\\';
jtulach@1348
  1599
                    newtemp[j++] = '\\';
jtulach@1348
  1600
                }
jtulach@1348
  1601
            } else {
jtulach@1348
  1602
                if (temp[i] == 'Q') {
jtulach@1348
  1603
                    i++;
jtulach@1348
  1604
                    inQuote = true;
jtulach@1348
  1605
                } else {
jtulach@1348
  1606
                    newtemp[j++] = c;
jtulach@1348
  1607
                    if (i != pLen)
jtulach@1348
  1608
                        newtemp[j++] = temp[i++];
jtulach@1348
  1609
                }
jtulach@1348
  1610
            }
jtulach@1348
  1611
        }
jtulach@1348
  1612
jtulach@1348
  1613
        patternLength = j;
jtulach@1348
  1614
        temp = Arrays.copyOf(newtemp, j + 2); // double zero termination
jtulach@1348
  1615
    }
jtulach@1348
  1616
jtulach@1348
  1617
    /**
jtulach@1348
  1618
     * Copies regular expression to an int array and invokes the parsing
jtulach@1348
  1619
     * of the expression which will create the object tree.
jtulach@1348
  1620
     */
jtulach@1348
  1621
    private void compile() {
jtulach@1348
  1622
        // Handle canonical equivalences
jtulach@1348
  1623
        if (has(CANON_EQ) && !has(LITERAL)) {
jtulach@1348
  1624
            normalize();
jtulach@1348
  1625
        } else {
jtulach@1348
  1626
            normalizedPattern = pattern;
jtulach@1348
  1627
        }
jtulach@1348
  1628
        patternLength = normalizedPattern.length();
jtulach@1348
  1629
jtulach@1348
  1630
        // Copy pattern to int array for convenience
jtulach@1348
  1631
        // Use double zero to terminate pattern
jtulach@1348
  1632
        temp = new int[patternLength + 2];
jtulach@1348
  1633
jtulach@1348
  1634
        hasSupplementary = false;
jtulach@1348
  1635
        int c, count = 0;
jtulach@1348
  1636
        // Convert all chars into code points
jtulach@1348
  1637
        for (int x = 0; x < patternLength; x += Character.charCount(c)) {
jtulach@1348
  1638
            c = normalizedPattern.codePointAt(x);
jtulach@1348
  1639
            if (isSupplementary(c)) {
jtulach@1348
  1640
                hasSupplementary = true;
jtulach@1348
  1641
            }
jtulach@1348
  1642
            temp[count++] = c;
jtulach@1348
  1643
        }
jtulach@1348
  1644
jtulach@1348
  1645
        patternLength = count;   // patternLength now in code points
jtulach@1348
  1646
jtulach@1348
  1647
        if (! has(LITERAL))
jtulach@1348
  1648
            RemoveQEQuoting();
jtulach@1348
  1649
jtulach@1348
  1650
        // Allocate all temporary objects here.
jtulach@1348
  1651
        buffer = new int[32];
jtulach@1348
  1652
        groupNodes = new GroupHead[10];
jtulach@1348
  1653
        namedGroups = null;
jtulach@1348
  1654
jtulach@1348
  1655
        if (has(LITERAL)) {
jtulach@1348
  1656
            // Literal pattern handling
jtulach@1348
  1657
            matchRoot = newSlice(temp, patternLength, hasSupplementary);
jtulach@1348
  1658
            matchRoot.next = lastAccept;
jtulach@1348
  1659
        } else {
jtulach@1348
  1660
            // Start recursive descent parsing
jtulach@1348
  1661
            matchRoot = expr(lastAccept);
jtulach@1348
  1662
            // Check extra pattern characters
jtulach@1348
  1663
            if (patternLength != cursor) {
jtulach@1348
  1664
                if (peek() == ')') {
jtulach@1348
  1665
                    throw error("Unmatched closing ')'");
jtulach@1348
  1666
                } else {
jtulach@1348
  1667
                    throw error("Unexpected internal error");
jtulach@1348
  1668
                }
jtulach@1348
  1669
            }
jtulach@1348
  1670
        }
jtulach@1348
  1671
jtulach@1348
  1672
        // Peephole optimization
jtulach@1348
  1673
        if (matchRoot instanceof Slice) {
jtulach@1348
  1674
            root = BnM.optimize(matchRoot);
jtulach@1348
  1675
            if (root == matchRoot) {
jtulach@1348
  1676
                root = hasSupplementary ? new StartS(matchRoot) : new Start(matchRoot);
jtulach@1348
  1677
            }
jtulach@1348
  1678
        } else if (matchRoot instanceof Begin || matchRoot instanceof First) {
jtulach@1348
  1679
            root = matchRoot;
jtulach@1348
  1680
        } else {
jtulach@1348
  1681
            root = hasSupplementary ? new StartS(matchRoot) : new Start(matchRoot);
jtulach@1348
  1682
        }
jtulach@1348
  1683
jtulach@1348
  1684
        // Release temporary storage
jtulach@1348
  1685
        temp = null;
jtulach@1348
  1686
        buffer = null;
jtulach@1348
  1687
        groupNodes = null;
jtulach@1348
  1688
        patternLength = 0;
jtulach@1348
  1689
        compiled = true;
jtulach@1348
  1690
    }
jtulach@1348
  1691
jtulach@1348
  1692
    Map<String, Integer> namedGroups() {
jtulach@1348
  1693
        if (namedGroups == null)
jtulach@1348
  1694
            namedGroups = new HashMap<>(2);
jtulach@1348
  1695
        return namedGroups;
jtulach@1348
  1696
    }
jtulach@1348
  1697
jtulach@1348
  1698
    /**
jtulach@1348
  1699
     * Used to print out a subtree of the Pattern to help with debugging.
jtulach@1348
  1700
     */
jtulach@1348
  1701
    private static void printObjectTree(Node node) {
jtulach@1348
  1702
        while(node != null) {
jtulach@1348
  1703
            if (node instanceof Prolog) {
jtulach@1348
  1704
                System.out.println(node);
jtulach@1348
  1705
                printObjectTree(((Prolog)node).loop);
jtulach@1348
  1706
                System.out.println("**** end contents prolog loop");
jtulach@1348
  1707
            } else if (node instanceof Loop) {
jtulach@1348
  1708
                System.out.println(node);
jtulach@1348
  1709
                printObjectTree(((Loop)node).body);
jtulach@1348
  1710
                System.out.println("**** end contents Loop body");
jtulach@1348
  1711
            } else if (node instanceof Curly) {
jtulach@1348
  1712
                System.out.println(node);
jtulach@1348
  1713
                printObjectTree(((Curly)node).atom);
jtulach@1348
  1714
                System.out.println("**** end contents Curly body");
jtulach@1348
  1715
            } else if (node instanceof GroupCurly) {
jtulach@1348
  1716
                System.out.println(node);
jtulach@1348
  1717
                printObjectTree(((GroupCurly)node).atom);
jtulach@1348
  1718
                System.out.println("**** end contents GroupCurly body");
jtulach@1348
  1719
            } else if (node instanceof GroupTail) {
jtulach@1348
  1720
                System.out.println(node);
jtulach@1348
  1721
                System.out.println("Tail next is "+node.next);
jtulach@1348
  1722
                return;
jtulach@1348
  1723
            } else {
jtulach@1348
  1724
                System.out.println(node);
jtulach@1348
  1725
            }
jtulach@1348
  1726
            node = node.next;
jtulach@1348
  1727
            if (node != null)
jtulach@1348
  1728
                System.out.println("->next:");
jtulach@1348
  1729
            if (node == Pattern.accept) {
jtulach@1348
  1730
                System.out.println("Accept Node");
jtulach@1348
  1731
                node = null;
jtulach@1348
  1732
            }
jtulach@1348
  1733
       }
jtulach@1348
  1734
    }
jtulach@1348
  1735
jtulach@1348
  1736
    /**
jtulach@1348
  1737
     * Used to accumulate information about a subtree of the object graph
jtulach@1348
  1738
     * so that optimizations can be applied to the subtree.
jtulach@1348
  1739
     */
jtulach@1348
  1740
    static final class TreeInfo {
jtulach@1348
  1741
        int minLength;
jtulach@1348
  1742
        int maxLength;
jtulach@1348
  1743
        boolean maxValid;
jtulach@1348
  1744
        boolean deterministic;
jtulach@1348
  1745
jtulach@1348
  1746
        TreeInfo() {
jtulach@1348
  1747
            reset();
jtulach@1348
  1748
        }
jtulach@1348
  1749
        void reset() {
jtulach@1348
  1750
            minLength = 0;
jtulach@1348
  1751
            maxLength = 0;
jtulach@1348
  1752
            maxValid = true;
jtulach@1348
  1753
            deterministic = true;
jtulach@1348
  1754
        }
jtulach@1348
  1755
    }
jtulach@1348
  1756
jtulach@1348
  1757
    /*
jtulach@1348
  1758
     * The following private methods are mainly used to improve the
jtulach@1348
  1759
     * readability of the code. In order to let the Java compiler easily
jtulach@1348
  1760
     * inline them, we should not put many assertions or error checks in them.
jtulach@1348
  1761
     */
jtulach@1348
  1762
jtulach@1348
  1763
    /**
jtulach@1348
  1764
     * Indicates whether a particular flag is set or not.
jtulach@1348
  1765
     */
jtulach@1348
  1766
    private boolean has(int f) {
jtulach@1348
  1767
        return (flags & f) != 0;
jtulach@1348
  1768
    }
jtulach@1348
  1769
jtulach@1348
  1770
    /**
jtulach@1348
  1771
     * Match next character, signal error if failed.
jtulach@1348
  1772
     */
jtulach@1348
  1773
    private void accept(int ch, String s) {
jtulach@1348
  1774
        int testChar = temp[cursor++];
jtulach@1348
  1775
        if (has(COMMENTS))
jtulach@1348
  1776
            testChar = parsePastWhitespace(testChar);
jtulach@1348
  1777
        if (ch != testChar) {
jtulach@1348
  1778
            throw error(s);
jtulach@1348
  1779
        }
jtulach@1348
  1780
    }
jtulach@1348
  1781
jtulach@1348
  1782
    /**
jtulach@1348
  1783
     * Mark the end of pattern with a specific character.
jtulach@1348
  1784
     */
jtulach@1348
  1785
    private void mark(int c) {
jtulach@1348
  1786
        temp[patternLength] = c;
jtulach@1348
  1787
    }
jtulach@1348
  1788
jtulach@1348
  1789
    /**
jtulach@1348
  1790
     * Peek the next character, and do not advance the cursor.
jtulach@1348
  1791
     */
jtulach@1348
  1792
    private int peek() {
jtulach@1348
  1793
        int ch = temp[cursor];
jtulach@1348
  1794
        if (has(COMMENTS))
jtulach@1348
  1795
            ch = peekPastWhitespace(ch);
jtulach@1348
  1796
        return ch;
jtulach@1348
  1797
    }
jtulach@1348
  1798
jtulach@1348
  1799
    /**
jtulach@1348
  1800
     * Read the next character, and advance the cursor by one.
jtulach@1348
  1801
     */
jtulach@1348
  1802
    private int read() {
jtulach@1348
  1803
        int ch = temp[cursor++];
jtulach@1348
  1804
        if (has(COMMENTS))
jtulach@1348
  1805
            ch = parsePastWhitespace(ch);
jtulach@1348
  1806
        return ch;
jtulach@1348
  1807
    }
jtulach@1348
  1808
jtulach@1348
  1809
    /**
jtulach@1348
  1810
     * Read the next character, and advance the cursor by one,
jtulach@1348
  1811
     * ignoring the COMMENTS setting
jtulach@1348
  1812
     */
jtulach@1348
  1813
    private int readEscaped() {
jtulach@1348
  1814
        int ch = temp[cursor++];
jtulach@1348
  1815
        return ch;
jtulach@1348
  1816
    }
jtulach@1348
  1817
jtulach@1348
  1818
    /**
jtulach@1348
  1819
     * Advance the cursor by one, and peek the next character.
jtulach@1348
  1820
     */
jtulach@1348
  1821
    private int next() {
jtulach@1348
  1822
        int ch = temp[++cursor];
jtulach@1348
  1823
        if (has(COMMENTS))
jtulach@1348
  1824
            ch = peekPastWhitespace(ch);
jtulach@1348
  1825
        return ch;
jtulach@1348
  1826
    }
jtulach@1348
  1827
jtulach@1348
  1828
    /**
jtulach@1348
  1829
     * Advance the cursor by one, and peek the next character,
jtulach@1348
  1830
     * ignoring the COMMENTS setting
jtulach@1348
  1831
     */
jtulach@1348
  1832
    private int nextEscaped() {
jtulach@1348
  1833
        int ch = temp[++cursor];
jtulach@1348
  1834
        return ch;
jtulach@1348
  1835
    }
jtulach@1348
  1836
jtulach@1348
  1837
    /**
jtulach@1348
  1838
     * If in xmode peek past whitespace and comments.
jtulach@1348
  1839
     */
jtulach@1348
  1840
    private int peekPastWhitespace(int ch) {
jtulach@1348
  1841
        while (ASCII.isSpace(ch) || ch == '#') {
jtulach@1348
  1842
            while (ASCII.isSpace(ch))
jtulach@1348
  1843
                ch = temp[++cursor];
jtulach@1348
  1844
            if (ch == '#') {
jtulach@1348
  1845
                ch = peekPastLine();
jtulach@1348
  1846
            }
jtulach@1348
  1847
        }
jtulach@1348
  1848
        return ch;
jtulach@1348
  1849
    }
jtulach@1348
  1850
jtulach@1348
  1851
    /**
jtulach@1348
  1852
     * If in xmode parse past whitespace and comments.
jtulach@1348
  1853
     */
jtulach@1348
  1854
    private int parsePastWhitespace(int ch) {
jtulach@1348
  1855
        while (ASCII.isSpace(ch) || ch == '#') {
jtulach@1348
  1856
            while (ASCII.isSpace(ch))
jtulach@1348
  1857
                ch = temp[cursor++];
jtulach@1348
  1858
            if (ch == '#')
jtulach@1348
  1859
                ch = parsePastLine();
jtulach@1348
  1860
        }
jtulach@1348
  1861
        return ch;
jtulach@1348
  1862
    }
jtulach@1348
  1863
jtulach@1348
  1864
    /**
jtulach@1348
  1865
     * xmode parse past comment to end of line.
jtulach@1348
  1866
     */
jtulach@1348
  1867
    private int parsePastLine() {
jtulach@1348
  1868
        int ch = temp[cursor++];
jtulach@1348
  1869
        while (ch != 0 && !isLineSeparator(ch))
jtulach@1348
  1870
            ch = temp[cursor++];
jtulach@1348
  1871
        return ch;
jtulach@1348
  1872
    }
jtulach@1348
  1873
jtulach@1348
  1874
    /**
jtulach@1348
  1875
     * xmode peek past comment to end of line.
jtulach@1348
  1876
     */
jtulach@1348
  1877
    private int peekPastLine() {
jtulach@1348
  1878
        int ch = temp[++cursor];
jtulach@1348
  1879
        while (ch != 0 && !isLineSeparator(ch))
jtulach@1348
  1880
            ch = temp[++cursor];
jtulach@1348
  1881
        return ch;
jtulach@1348
  1882
    }
jtulach@1348
  1883
jtulach@1348
  1884
    /**
jtulach@1348
  1885
     * Determines if character is a line separator in the current mode
jtulach@1348
  1886
     */
jtulach@1348
  1887
    private boolean isLineSeparator(int ch) {
jtulach@1348
  1888
        if (has(UNIX_LINES)) {
jtulach@1348
  1889
            return ch == '\n';
jtulach@1348
  1890
        } else {
jtulach@1348
  1891
            return (ch == '\n' ||
jtulach@1348
  1892
                    ch == '\r' ||
jtulach@1348
  1893
                    (ch|1) == '\u2029' ||
jtulach@1348
  1894
                    ch == '\u0085');
jtulach@1348
  1895
        }
jtulach@1348
  1896
    }
jtulach@1348
  1897
jtulach@1348
  1898
    /**
jtulach@1348
  1899
     * Read the character after the next one, and advance the cursor by two.
jtulach@1348
  1900
     */
jtulach@1348
  1901
    private int skip() {
jtulach@1348
  1902
        int i = cursor;
jtulach@1348
  1903
        int ch = temp[i+1];
jtulach@1348
  1904
        cursor = i + 2;
jtulach@1348
  1905
        return ch;
jtulach@1348
  1906
    }
jtulach@1348
  1907
jtulach@1348
  1908
    /**
jtulach@1348
  1909
     * Unread one next character, and retreat cursor by one.
jtulach@1348
  1910
     */
jtulach@1348
  1911
    private void unread() {
jtulach@1348
  1912
        cursor--;
jtulach@1348
  1913
    }
jtulach@1348
  1914
jtulach@1348
  1915
    /**
jtulach@1348
  1916
     * Internal method used for handling all syntax errors. The pattern is
jtulach@1348
  1917
     * displayed with a pointer to aid in locating the syntax error.
jtulach@1348
  1918
     */
jtulach@1348
  1919
    private PatternSyntaxException error(String s) {
jtulach@1348
  1920
        return new PatternSyntaxException(s, normalizedPattern,  cursor - 1);
jtulach@1348
  1921
    }
jtulach@1348
  1922
jtulach@1348
  1923
    /**
jtulach@1348
  1924
     * Determines if there is any supplementary character or unpaired
jtulach@1348
  1925
     * surrogate in the specified range.
jtulach@1348
  1926
     */
jtulach@1348
  1927
    private boolean findSupplementary(int start, int end) {
jtulach@1348
  1928
        for (int i = start; i < end; i++) {
jtulach@1348
  1929
            if (isSupplementary(temp[i]))
jtulach@1348
  1930
                return true;
jtulach@1348
  1931
        }
jtulach@1348
  1932
        return false;
jtulach@1348
  1933
    }
jtulach@1348
  1934
jtulach@1348
  1935
    /**
jtulach@1348
  1936
     * Determines if the specified code point is a supplementary
jtulach@1348
  1937
     * character or unpaired surrogate.
jtulach@1348
  1938
     */
jtulach@1348
  1939
    private static final boolean isSupplementary(int ch) {
jtulach@1348
  1940
        return ch >= Character.MIN_SUPPLEMENTARY_CODE_POINT ||
jtulach@1348
  1941
               Character.isSurrogate((char)ch);
jtulach@1348
  1942
    }
jtulach@1348
  1943
jtulach@1348
  1944
    /**
jtulach@1348
  1945
     *  The following methods handle the main parsing. They are sorted
jtulach@1348
  1946
     *  according to their precedence order, the lowest one first.
jtulach@1348
  1947
     */
jtulach@1348
  1948
jtulach@1348
  1949
    /**
jtulach@1348
  1950
     * The expression is parsed with branch nodes added for alternations.
jtulach@1348
  1951
     * This may be called recursively to parse sub expressions that may
jtulach@1348
  1952
     * contain alternations.
jtulach@1348
  1953
     */
jtulach@1348
  1954
    private Node expr(Node end) {
jtulach@1348
  1955
        Node prev = null;
jtulach@1348
  1956
        Node firstTail = null;
jtulach@1348
  1957
        Node branchConn = null;
jtulach@1348
  1958
jtulach@1348
  1959
        for (;;) {
jtulach@1348
  1960
            Node node = sequence(end);
jtulach@1348
  1961
            Node nodeTail = root;      //double return
jtulach@1348
  1962
            if (prev == null) {
jtulach@1348
  1963
                prev = node;
jtulach@1348
  1964
                firstTail = nodeTail;
jtulach@1348
  1965
            } else {
jtulach@1348
  1966
                // Branch
jtulach@1348
  1967
                if (branchConn == null) {
jtulach@1348
  1968
                    branchConn = new BranchConn();
jtulach@1348
  1969
                    branchConn.next = end;
jtulach@1348
  1970
                }
jtulach@1348
  1971
                if (node == end) {
jtulach@1348
  1972
                    // if the node returned from sequence() is "end"
jtulach@1348
  1973
                    // we have an empty expr, set a null atom into
jtulach@1348
  1974
                    // the branch to indicate to go "next" directly.
jtulach@1348
  1975
                    node = null;
jtulach@1348
  1976
                } else {
jtulach@1348
  1977
                    // the "tail.next" of each atom goes to branchConn
jtulach@1348
  1978
                    nodeTail.next = branchConn;
jtulach@1348
  1979
                }
jtulach@1348
  1980
                if (prev instanceof Branch) {
jtulach@1348
  1981
                    ((Branch)prev).add(node);
jtulach@1348
  1982
                } else {
jtulach@1348
  1983
                    if (prev == end) {
jtulach@1348
  1984
                        prev = null;
jtulach@1348
  1985
                    } else {
jtulach@1348
  1986
                        // replace the "end" with "branchConn" at its tail.next
jtulach@1348
  1987
                        // when put the "prev" into the branch as the first atom.
jtulach@1348
  1988
                        firstTail.next = branchConn;
jtulach@1348
  1989
                    }
jtulach@1348
  1990
                    prev = new Branch(prev, node, branchConn);
jtulach@1348
  1991
                }
jtulach@1348
  1992
            }
jtulach@1348
  1993
            if (peek() != '|') {
jtulach@1348
  1994
                return prev;
jtulach@1348
  1995
            }
jtulach@1348
  1996
            next();
jtulach@1348
  1997
        }
jtulach@1348
  1998
    }
jtulach@1348
  1999
jtulach@1348
  2000
    /**
jtulach@1348
  2001
     * Parsing of sequences between alternations.
jtulach@1348
  2002
     */
jtulach@1348
  2003
    private Node sequence(Node end) {
jtulach@1348
  2004
        Node head = null;
jtulach@1348
  2005
        Node tail = null;
jtulach@1348
  2006
        Node node = null;
jtulach@1348
  2007
    LOOP:
jtulach@1348
  2008
        for (;;) {
jtulach@1348
  2009
            int ch = peek();
jtulach@1348
  2010
            switch (ch) {
jtulach@1348
  2011
            case '(':
jtulach@1348
  2012
                // Because group handles its own closure,
jtulach@1348
  2013
                // we need to treat it differently
jtulach@1348
  2014
                node = group0();
jtulach@1348
  2015
                // Check for comment or flag group
jtulach@1348
  2016
                if (node == null)
jtulach@1348
  2017
                    continue;
jtulach@1348
  2018
                if (head == null)
jtulach@1348
  2019
                    head = node;
jtulach@1348
  2020
                else
jtulach@1348
  2021
                    tail.next = node;
jtulach@1348
  2022
                // Double return: Tail was returned in root
jtulach@1348
  2023
                tail = root;
jtulach@1348
  2024
                continue;
jtulach@1348
  2025
            case '[':
jtulach@1348
  2026
                node = clazz(true);
jtulach@1348
  2027
                break;
jtulach@1348
  2028
            case '\\':
jtulach@1348
  2029
                ch = nextEscaped();
jtulach@1348
  2030
                if (ch == 'p' || ch == 'P') {
jtulach@1348
  2031
                    boolean oneLetter = true;
jtulach@1348
  2032
                    boolean comp = (ch == 'P');
jtulach@1348
  2033
                    ch = next(); // Consume { if present
jtulach@1348
  2034
                    if (ch != '{') {
jtulach@1348
  2035
                        unread();
jtulach@1348
  2036
                    } else {
jtulach@1348
  2037
                        oneLetter = false;
jtulach@1348
  2038
                    }
jtulach@1348
  2039
                    node = family(oneLetter, comp);
jtulach@1348
  2040
                } else {
jtulach@1348
  2041
                    unread();
jtulach@1348
  2042
                    node = atom();
jtulach@1348
  2043
                }
jtulach@1348
  2044
                break;
jtulach@1348
  2045
            case '^':
jtulach@1348
  2046
                next();
jtulach@1348
  2047
                if (has(MULTILINE)) {
jtulach@1348
  2048
                    if (has(UNIX_LINES))
jtulach@1348
  2049
                        node = new UnixCaret();
jtulach@1348
  2050
                    else
jtulach@1348
  2051
                        node = new Caret();
jtulach@1348
  2052
                } else {
jtulach@1348
  2053
                    node = new Begin();
jtulach@1348
  2054
                }
jtulach@1348
  2055
                break;
jtulach@1348
  2056
            case '$':
jtulach@1348
  2057
                next();
jtulach@1348
  2058
                if (has(UNIX_LINES))
jtulach@1348
  2059
                    node = new UnixDollar(has(MULTILINE));
jtulach@1348
  2060
                else
jtulach@1348
  2061
                    node = new Dollar(has(MULTILINE));
jtulach@1348
  2062
                break;
jtulach@1348
  2063
            case '.':
jtulach@1348
  2064
                next();
jtulach@1348
  2065
                if (has(DOTALL)) {
jtulach@1348
  2066
                    node = new All();
jtulach@1348
  2067
                } else {
jtulach@1348
  2068
                    if (has(UNIX_LINES))
jtulach@1348
  2069
                        node = new UnixDot();
jtulach@1348
  2070
                    else {
jtulach@1348
  2071
                        node = new Dot();
jtulach@1348
  2072
                    }
jtulach@1348
  2073
                }
jtulach@1348
  2074
                break;
jtulach@1348
  2075
            case '|':
jtulach@1348
  2076
            case ')':
jtulach@1348
  2077
                break LOOP;
jtulach@1348
  2078
            case ']': // Now interpreting dangling ] and } as literals
jtulach@1348
  2079
            case '}':
jtulach@1348
  2080
                node = atom();
jtulach@1348
  2081
                break;
jtulach@1348
  2082
            case '?':
jtulach@1348
  2083
            case '*':
jtulach@1348
  2084
            case '+':
jtulach@1348
  2085
                next();
jtulach@1348
  2086
                throw error("Dangling meta character '" + ((char)ch) + "'");
jtulach@1348
  2087
            case 0:
jtulach@1348
  2088
                if (cursor >= patternLength) {
jtulach@1348
  2089
                    break LOOP;
jtulach@1348
  2090
                }
jtulach@1348
  2091
                // Fall through
jtulach@1348
  2092
            default:
jtulach@1348
  2093
                node = atom();
jtulach@1348
  2094
                break;
jtulach@1348
  2095
            }
jtulach@1348
  2096
jtulach@1348
  2097
            node = closure(node);
jtulach@1348
  2098
jtulach@1348
  2099
            if (head == null) {
jtulach@1348
  2100
                head = tail = node;
jtulach@1348
  2101
            } else {
jtulach@1348
  2102
                tail.next = node;
jtulach@1348
  2103
                tail = node;
jtulach@1348
  2104
            }
jtulach@1348
  2105
        }
jtulach@1348
  2106
        if (head == null) {
jtulach@1348
  2107
            return end;
jtulach@1348
  2108
        }
jtulach@1348
  2109
        tail.next = end;
jtulach@1348
  2110
        root = tail;      //double return
jtulach@1348
  2111
        return head;
jtulach@1348
  2112
    }
jtulach@1348
  2113
jtulach@1348
  2114
    /**
jtulach@1348
  2115
     * Parse and add a new Single or Slice.
jtulach@1348
  2116
     */
jtulach@1348
  2117
    private Node atom() {
jtulach@1348
  2118
        int first = 0;
jtulach@1348
  2119
        int prev = -1;
jtulach@1348
  2120
        boolean hasSupplementary = false;
jtulach@1348
  2121
        int ch = peek();
jtulach@1348
  2122
        for (;;) {
jtulach@1348
  2123
            switch (ch) {
jtulach@1348
  2124
            case '*':
jtulach@1348
  2125
            case '+':
jtulach@1348
  2126
            case '?':
jtulach@1348
  2127
            case '{':
jtulach@1348
  2128
                if (first > 1) {
jtulach@1348
  2129
                    cursor = prev;    // Unwind one character
jtulach@1348
  2130
                    first--;
jtulach@1348
  2131
                }
jtulach@1348
  2132
                break;
jtulach@1348
  2133
            case '$':
jtulach@1348
  2134
            case '.':
jtulach@1348
  2135
            case '^':
jtulach@1348
  2136
            case '(':
jtulach@1348
  2137
            case '[':
jtulach@1348
  2138
            case '|':
jtulach@1348
  2139
            case ')':
jtulach@1348
  2140
                break;
jtulach@1348
  2141
            case '\\':
jtulach@1348
  2142
                ch = nextEscaped();
jtulach@1348
  2143
                if (ch == 'p' || ch == 'P') { // Property
jtulach@1348
  2144
                    if (first > 0) { // Slice is waiting; handle it first
jtulach@1348
  2145
                        unread();
jtulach@1348
  2146
                        break;
jtulach@1348
  2147
                    } else { // No slice; just return the family node
jtulach@1348
  2148
                        boolean comp = (ch == 'P');
jtulach@1348
  2149
                        boolean oneLetter = true;
jtulach@1348
  2150
                        ch = next(); // Consume { if present
jtulach@1348
  2151
                        if (ch != '{')
jtulach@1348
  2152
                            unread();
jtulach@1348
  2153
                        else
jtulach@1348
  2154
                            oneLetter = false;
jtulach@1348
  2155
                        return family(oneLetter, comp);
jtulach@1348
  2156
                    }
jtulach@1348
  2157
                }
jtulach@1348
  2158
                unread();
jtulach@1348
  2159
                prev = cursor;
jtulach@1348
  2160
                ch = escape(false, first == 0);
jtulach@1348
  2161
                if (ch >= 0) {
jtulach@1348
  2162
                    append(ch, first);
jtulach@1348
  2163
                    first++;
jtulach@1348
  2164
                    if (isSupplementary(ch)) {
jtulach@1348
  2165
                        hasSupplementary = true;
jtulach@1348
  2166
                    }
jtulach@1348
  2167
                    ch = peek();
jtulach@1348
  2168
                    continue;
jtulach@1348
  2169
                } else if (first == 0) {
jtulach@1348
  2170
                    return root;
jtulach@1348
  2171
                }
jtulach@1348
  2172
                // Unwind meta escape sequence
jtulach@1348
  2173
                cursor = prev;
jtulach@1348
  2174
                break;
jtulach@1348
  2175
            case 0:
jtulach@1348
  2176
                if (cursor >= patternLength) {
jtulach@1348
  2177
                    break;
jtulach@1348
  2178
                }
jtulach@1348
  2179
                // Fall through
jtulach@1348
  2180
            default:
jtulach@1348
  2181
                prev = cursor;
jtulach@1348
  2182
                append(ch, first);
jtulach@1348
  2183
                first++;
jtulach@1348
  2184
                if (isSupplementary(ch)) {
jtulach@1348
  2185
                    hasSupplementary = true;
jtulach@1348
  2186
                }
jtulach@1348
  2187
                ch = next();
jtulach@1348
  2188
                continue;
jtulach@1348
  2189
            }
jtulach@1348
  2190
            break;
jtulach@1348
  2191
        }
jtulach@1348
  2192
        if (first == 1) {
jtulach@1348
  2193
            return newSingle(buffer[0]);
jtulach@1348
  2194
        } else {
jtulach@1348
  2195
            return newSlice(buffer, first, hasSupplementary);
jtulach@1348
  2196
        }
jtulach@1348
  2197
    }
jtulach@1348
  2198
jtulach@1348
  2199
    private void append(int ch, int len) {
jtulach@1348
  2200
        if (len >= buffer.length) {
jtulach@1348
  2201
            int[] tmp = new int[len+len];
jtulach@1348
  2202
            System.arraycopy(buffer, 0, tmp, 0, len);
jtulach@1348
  2203
            buffer = tmp;
jtulach@1348
  2204
        }
jtulach@1348
  2205
        buffer[len] = ch;
jtulach@1348
  2206
    }
jtulach@1348
  2207
jtulach@1348
  2208
    /**
jtulach@1348
  2209
     * Parses a backref greedily, taking as many numbers as it
jtulach@1348
  2210
     * can. The first digit is always treated as a backref, but
jtulach@1348
  2211
     * multi digit numbers are only treated as a backref if at
jtulach@1348
  2212
     * least that many backrefs exist at this point in the regex.
jtulach@1348
  2213
     */
jtulach@1348
  2214
    private Node ref(int refNum) {
jtulach@1348
  2215
        boolean done = false;
jtulach@1348
  2216
        while(!done) {
jtulach@1348
  2217
            int ch = peek();
jtulach@1348
  2218
            switch(ch) {
jtulach@1348
  2219
            case '0':
jtulach@1348
  2220
            case '1':
jtulach@1348
  2221
            case '2':
jtulach@1348
  2222
            case '3':
jtulach@1348
  2223
            case '4':
jtulach@1348
  2224
            case '5':
jtulach@1348
  2225
            case '6':
jtulach@1348
  2226
            case '7':
jtulach@1348
  2227
            case '8':
jtulach@1348
  2228
            case '9':
jtulach@1348
  2229
                int newRefNum = (refNum * 10) + (ch - '0');
jtulach@1348
  2230
                // Add another number if it doesn't make a group
jtulach@1348
  2231
                // that doesn't exist
jtulach@1348
  2232
                if (capturingGroupCount - 1 < newRefNum) {
jtulach@1348
  2233
                    done = true;
jtulach@1348
  2234
                    break;
jtulach@1348
  2235
                }
jtulach@1348
  2236
                refNum = newRefNum;
jtulach@1348
  2237
                read();
jtulach@1348
  2238
                break;
jtulach@1348
  2239
            default:
jtulach@1348
  2240
                done = true;
jtulach@1348
  2241
                break;
jtulach@1348
  2242
            }
jtulach@1348
  2243
        }
jtulach@1348
  2244
        if (has(CASE_INSENSITIVE))
jtulach@1348
  2245
            return new CIBackRef(refNum, has(UNICODE_CASE));
jtulach@1348
  2246
        else
jtulach@1348
  2247
            return new BackRef(refNum);
jtulach@1348
  2248
    }
jtulach@1348
  2249
jtulach@1348
  2250
    /**
jtulach@1348
  2251
     * Parses an escape sequence to determine the actual value that needs
jtulach@1348
  2252
     * to be matched.
jtulach@1348
  2253
     * If -1 is returned and create was true a new object was added to the tree
jtulach@1348
  2254
     * to handle the escape sequence.
jtulach@1348
  2255
     * If the returned value is greater than zero, it is the value that
jtulach@1348
  2256
     * matches the escape sequence.
jtulach@1348
  2257
     */
jtulach@1348
  2258
    private int escape(boolean inclass, boolean create) {
jtulach@1348
  2259
        int ch = skip();
jtulach@1348
  2260
        switch (ch) {
jtulach@1348
  2261
        case '0':
jtulach@1348
  2262
            return o();
jtulach@1348
  2263
        case '1':
jtulach@1348
  2264
        case '2':
jtulach@1348
  2265
        case '3':
jtulach@1348
  2266
        case '4':
jtulach@1348
  2267
        case '5':
jtulach@1348
  2268
        case '6':
jtulach@1348
  2269
        case '7':
jtulach@1348
  2270
        case '8':
jtulach@1348
  2271
        case '9':
jtulach@1348
  2272
            if (inclass) break;
jtulach@1348
  2273
            if (create) {
jtulach@1348
  2274
                root = ref((ch - '0'));
jtulach@1348
  2275
            }
jtulach@1348
  2276
            return -1;
jtulach@1348
  2277
        case 'A':
jtulach@1348
  2278
            if (inclass) break;
jtulach@1348
  2279
            if (create) root = new Begin();
jtulach@1348
  2280
            return -1;
jtulach@1348
  2281
        case 'B':
jtulach@1348
  2282
            if (inclass) break;
jtulach@1348
  2283
            if (create) root = new Bound(Bound.NONE, has(UNICODE_CHARACTER_CLASS));
jtulach@1348
  2284
            return -1;
jtulach@1348
  2285
        case 'C':
jtulach@1348
  2286
            break;
jtulach@1348
  2287
        case 'D':
jtulach@1348
  2288
            if (create) root = has(UNICODE_CHARACTER_CLASS)
jtulach@1348
  2289
                               ? new Utype(UnicodeProp.DIGIT).complement()
jtulach@1348
  2290
                               : new Ctype(ASCII.DIGIT).complement();
jtulach@1348
  2291
            return -1;
jtulach@1348
  2292
        case 'E':
jtulach@1348
  2293
        case 'F':
jtulach@1348
  2294
            break;
jtulach@1348
  2295
        case 'G':
jtulach@1348
  2296
            if (inclass) break;
jtulach@1348
  2297
            if (create) root = new LastMatch();
jtulach@1348
  2298
            return -1;
jtulach@1348
  2299
        case 'H':
jtulach@1348
  2300
        case 'I':
jtulach@1348
  2301
        case 'J':
jtulach@1348
  2302
        case 'K':
jtulach@1348
  2303
        case 'L':
jtulach@1348
  2304
        case 'M':
jtulach@1348
  2305
        case 'N':
jtulach@1348
  2306
        case 'O':
jtulach@1348
  2307
        case 'P':
jtulach@1348
  2308
        case 'Q':
jtulach@1348
  2309
        case 'R':
jtulach@1348
  2310
            break;
jtulach@1348
  2311
        case 'S':
jtulach@1348
  2312
            if (create) root = has(UNICODE_CHARACTER_CLASS)
jtulach@1348
  2313
                               ? new Utype(UnicodeProp.WHITE_SPACE).complement()
jtulach@1348
  2314
                               : new Ctype(ASCII.SPACE).complement();
jtulach@1348
  2315
            return -1;
jtulach@1348
  2316
        case 'T':
jtulach@1348
  2317
        case 'U':
jtulach@1348
  2318
        case 'V':
jtulach@1348
  2319
            break;
jtulach@1348
  2320
        case 'W':
jtulach@1348
  2321
            if (create) root = has(UNICODE_CHARACTER_CLASS)
jtulach@1348
  2322
                               ? new Utype(UnicodeProp.WORD).complement()
jtulach@1348
  2323
                               : new Ctype(ASCII.WORD).complement();
jtulach@1348
  2324
            return -1;
jtulach@1348
  2325
        case 'X':
jtulach@1348
  2326
        case 'Y':
jtulach@1348
  2327
            break;
jtulach@1348
  2328
        case 'Z':
jtulach@1348
  2329
            if (inclass) break;
jtulach@1348
  2330
            if (create) {
jtulach@1348
  2331
                if (has(UNIX_LINES))
jtulach@1348
  2332
                    root = new UnixDollar(false);
jtulach@1348
  2333
                else
jtulach@1348
  2334
                    root = new Dollar(false);
jtulach@1348
  2335
            }
jtulach@1348
  2336
            return -1;
jtulach@1348
  2337
        case 'a':
jtulach@1348
  2338
            return '\007';
jtulach@1348
  2339
        case 'b':
jtulach@1348
  2340
            if (inclass) break;
jtulach@1348
  2341
            if (create) root = new Bound(Bound.BOTH, has(UNICODE_CHARACTER_CLASS));
jtulach@1348
  2342
            return -1;
jtulach@1348
  2343
        case 'c':
jtulach@1348
  2344
            return c();
jtulach@1348
  2345
        case 'd':
jtulach@1348
  2346
            if (create) root = has(UNICODE_CHARACTER_CLASS)
jtulach@1348
  2347
                               ? new Utype(UnicodeProp.DIGIT)
jtulach@1348
  2348
                               : new Ctype(ASCII.DIGIT);
jtulach@1348
  2349
            return -1;
jtulach@1348
  2350
        case 'e':
jtulach@1348
  2351
            return '\033';
jtulach@1348
  2352
        case 'f':
jtulach@1348
  2353
            return '\f';
jtulach@1348
  2354
        case 'g':
jtulach@1348
  2355
        case 'h':
jtulach@1348
  2356
        case 'i':
jtulach@1348
  2357
        case 'j':
jtulach@1348
  2358
            break;
jtulach@1348
  2359
        case 'k':
jtulach@1348
  2360
            if (inclass)
jtulach@1348
  2361
                break;
jtulach@1348
  2362
            if (read() != '<')
jtulach@1348
  2363
                throw error("\\k is not followed by '<' for named capturing group");
jtulach@1348
  2364
            String name = groupname(read());
jtulach@1348
  2365
            if (!namedGroups().containsKey(name))
jtulach@1348
  2366
                throw error("(named capturing group <"+ name+"> does not exit");
jtulach@1348
  2367
            if (create) {
jtulach@1348
  2368
                if (has(CASE_INSENSITIVE))
jtulach@1348
  2369
                    root = new CIBackRef(namedGroups().get(name), has(UNICODE_CASE));
jtulach@1348
  2370
                else
jtulach@1348
  2371
                    root = new BackRef(namedGroups().get(name));
jtulach@1348
  2372
            }
jtulach@1348
  2373
            return -1;
jtulach@1348
  2374
        case 'l':
jtulach@1348
  2375
        case 'm':
jtulach@1348
  2376
            break;
jtulach@1348
  2377
        case 'n':
jtulach@1348
  2378
            return '\n';
jtulach@1348
  2379
        case 'o':
jtulach@1348
  2380
        case 'p':
jtulach@1348
  2381
        case 'q':
jtulach@1348
  2382
            break;
jtulach@1348
  2383
        case 'r':
jtulach@1348
  2384
            return '\r';
jtulach@1348
  2385
        case 's':
jtulach@1348
  2386
            if (create) root = has(UNICODE_CHARACTER_CLASS)
jtulach@1348
  2387
                               ? new Utype(UnicodeProp.WHITE_SPACE)
jtulach@1348
  2388
                               : new Ctype(ASCII.SPACE);
jtulach@1348
  2389
            return -1;
jtulach@1348
  2390
        case 't':
jtulach@1348
  2391
            return '\t';
jtulach@1348
  2392
        case 'u':
jtulach@1348
  2393
            return u();
jtulach@1348
  2394
        case 'v':
jtulach@1348
  2395
            return '\013';
jtulach@1348
  2396
        case 'w':
jtulach@1348
  2397
            if (create) root = has(UNICODE_CHARACTER_CLASS)
jtulach@1348
  2398
                               ? new Utype(UnicodeProp.WORD)
jtulach@1348
  2399
                               : new Ctype(ASCII.WORD);
jtulach@1348
  2400
            return -1;
jtulach@1348
  2401
        case 'x':
jtulach@1348
  2402
            return x();
jtulach@1348
  2403
        case 'y':
jtulach@1348
  2404
            break;
jtulach@1348
  2405
        case 'z':
jtulach@1348
  2406
            if (inclass) break;
jtulach@1348
  2407
            if (create) root = new End();
jtulach@1348
  2408
            return -1;
jtulach@1348
  2409
        default:
jtulach@1348
  2410
            return ch;
jtulach@1348
  2411
        }
jtulach@1348
  2412
        throw error("Illegal/unsupported escape sequence");
jtulach@1348
  2413
    }
jtulach@1348
  2414
jtulach@1348
  2415
    /**
jtulach@1348
  2416
     * Parse a character class, and return the node that matches it.
jtulach@1348
  2417
     *
jtulach@1348
  2418
     * Consumes a ] on the way out if consume is true. Usually consume
jtulach@1348
  2419
     * is true except for the case of [abc&&def] where def is a separate
jtulach@1348
  2420
     * right hand node with "understood" brackets.
jtulach@1348
  2421
     */
jtulach@1348
  2422
    private CharProperty clazz(boolean consume) {
jtulach@1348
  2423
        CharProperty prev = null;
jtulach@1348
  2424
        CharProperty node = null;
jtulach@1348
  2425
        BitClass bits = new BitClass();
jtulach@1348
  2426
        boolean include = true;
jtulach@1348
  2427
        boolean firstInClass = true;
jtulach@1348
  2428
        int ch = next();
jtulach@1348
  2429
        for (;;) {
jtulach@1348
  2430
            switch (ch) {
jtulach@1348
  2431
                case '^':
jtulach@1348
  2432
                    // Negates if first char in a class, otherwise literal
jtulach@1348
  2433
                    if (firstInClass) {
jtulach@1348
  2434
                        if (temp[cursor-1] != '[')
jtulach@1348
  2435
                            break;
jtulach@1348
  2436
                        ch = next();
jtulach@1348
  2437
                        include = !include;
jtulach@1348
  2438
                        continue;
jtulach@1348
  2439
                    } else {
jtulach@1348
  2440
                        // ^ not first in class, treat as literal
jtulach@1348
  2441
                        break;
jtulach@1348
  2442
                    }
jtulach@1348
  2443
                case '[':
jtulach@1348
  2444
                    firstInClass = false;
jtulach@1348
  2445
                    node = clazz(true);
jtulach@1348
  2446
                    if (prev == null)
jtulach@1348
  2447
                        prev = node;
jtulach@1348
  2448
                    else
jtulach@1348
  2449
                        prev = union(prev, node);
jtulach@1348
  2450
                    ch = peek();
jtulach@1348
  2451
                    continue;
jtulach@1348
  2452
                case '&':
jtulach@1348
  2453
                    firstInClass = false;
jtulach@1348
  2454
                    ch = next();
jtulach@1348
  2455
                    if (ch == '&') {
jtulach@1348
  2456
                        ch = next();
jtulach@1348
  2457
                        CharProperty rightNode = null;
jtulach@1348
  2458
                        while (ch != ']' && ch != '&') {
jtulach@1348
  2459
                            if (ch == '[') {
jtulach@1348
  2460
                                if (rightNode == null)
jtulach@1348
  2461
                                    rightNode = clazz(true);
jtulach@1348
  2462
                                else
jtulach@1348
  2463
                                    rightNode = union(rightNode, clazz(true));
jtulach@1348
  2464
                            } else { // abc&&def
jtulach@1348
  2465
                                unread();
jtulach@1348
  2466
                                rightNode = clazz(false);
jtulach@1348
  2467
                            }
jtulach@1348
  2468
                            ch = peek();
jtulach@1348
  2469
                        }
jtulach@1348
  2470
                        if (rightNode != null)
jtulach@1348
  2471
                            node = rightNode;
jtulach@1348
  2472
                        if (prev == null) {
jtulach@1348
  2473
                            if (rightNode == null)
jtulach@1348
  2474
                                throw error("Bad class syntax");
jtulach@1348
  2475
                            else
jtulach@1348
  2476
                                prev = rightNode;
jtulach@1348
  2477
                        } else {
jtulach@1348
  2478
                            prev = intersection(prev, node);
jtulach@1348
  2479
                        }
jtulach@1348
  2480
                    } else {
jtulach@1348
  2481
                        // treat as a literal &
jtulach@1348
  2482
                        unread();
jtulach@1348
  2483
                        break;
jtulach@1348
  2484
                    }
jtulach@1348
  2485
                    continue;
jtulach@1348
  2486
                case 0:
jtulach@1348
  2487
                    firstInClass = false;
jtulach@1348
  2488
                    if (cursor >= patternLength)
jtulach@1348
  2489
                        throw error("Unclosed character class");
jtulach@1348
  2490
                    break;
jtulach@1348
  2491
                case ']':
jtulach@1348
  2492
                    firstInClass = false;
jtulach@1348
  2493
                    if (prev != null) {
jtulach@1348
  2494
                        if (consume)
jtulach@1348
  2495
                            next();
jtulach@1348
  2496
                        return prev;
jtulach@1348
  2497
                    }
jtulach@1348
  2498
                    break;
jtulach@1348
  2499
                default:
jtulach@1348
  2500
                    firstInClass = false;
jtulach@1348
  2501
                    break;
jtulach@1348
  2502
            }
jtulach@1348
  2503
            node = range(bits);
jtulach@1348
  2504
            if (include) {
jtulach@1348
  2505
                if (prev == null) {
jtulach@1348
  2506
                    prev = node;
jtulach@1348
  2507
                } else {
jtulach@1348
  2508
                    if (prev != node)
jtulach@1348
  2509
                        prev = union(prev, node);
jtulach@1348
  2510
                }
jtulach@1348
  2511
            } else {
jtulach@1348
  2512
                if (prev == null) {
jtulach@1348
  2513
                    prev = node.complement();
jtulach@1348
  2514
                } else {
jtulach@1348
  2515
                    if (prev != node)
jtulach@1348
  2516
                        prev = setDifference(prev, node);
jtulach@1348
  2517
                }
jtulach@1348
  2518
            }
jtulach@1348
  2519
            ch = peek();
jtulach@1348
  2520
        }
jtulach@1348
  2521
    }
jtulach@1348
  2522
jtulach@1348
  2523
    private CharProperty bitsOrSingle(BitClass bits, int ch) {
jtulach@1348
  2524
        /* Bits can only handle codepoints in [u+0000-u+00ff] range.
jtulach@1348
  2525
           Use "single" node instead of bits when dealing with unicode
jtulach@1348
  2526
           case folding for codepoints listed below.
jtulach@1348
  2527
           (1)Uppercase out of range: u+00ff, u+00b5
jtulach@1348
  2528
              toUpperCase(u+00ff) -> u+0178
jtulach@1348
  2529
              toUpperCase(u+00b5) -> u+039c
jtulach@1348
  2530
           (2)LatinSmallLetterLongS u+17f
jtulach@1348
  2531
              toUpperCase(u+017f) -> u+0053
jtulach@1348
  2532
           (3)LatinSmallLetterDotlessI u+131
jtulach@1348
  2533
              toUpperCase(u+0131) -> u+0049
jtulach@1348
  2534
           (4)LatinCapitalLetterIWithDotAbove u+0130
jtulach@1348
  2535
              toLowerCase(u+0130) -> u+0069
jtulach@1348
  2536
           (5)KelvinSign u+212a
jtulach@1348
  2537
              toLowerCase(u+212a) ==> u+006B
jtulach@1348
  2538
           (6)AngstromSign u+212b
jtulach@1348
  2539
              toLowerCase(u+212b) ==> u+00e5
jtulach@1348
  2540
        */
jtulach@1348
  2541
        int d;
jtulach@1348
  2542
        if (ch < 256 &&
jtulach@1348
  2543
            !(has(CASE_INSENSITIVE) && has(UNICODE_CASE) &&
jtulach@1348
  2544
              (ch == 0xff || ch == 0xb5 ||
jtulach@1348
  2545
               ch == 0x49 || ch == 0x69 ||  //I and i
jtulach@1348
  2546
               ch == 0x53 || ch == 0x73 ||  //S and s
jtulach@1348
  2547
               ch == 0x4b || ch == 0x6b ||  //K and k
jtulach@1348
  2548
               ch == 0xc5 || ch == 0xe5)))  //A+ring
jtulach@1348
  2549
            return bits.add(ch, flags());
jtulach@1348
  2550
        return newSingle(ch);
jtulach@1348
  2551
    }
jtulach@1348
  2552
jtulach@1348
  2553
    /**
jtulach@1348
  2554
     * Parse a single character or a character range in a character class
jtulach@1348
  2555
     * and return its representative node.
jtulach@1348
  2556
     */
jtulach@1348
  2557
    private CharProperty range(BitClass bits) {
jtulach@1348
  2558
        int ch = peek();
jtulach@1348
  2559
        if (ch == '\\') {
jtulach@1348
  2560
            ch = nextEscaped();
jtulach@1348
  2561
            if (ch == 'p' || ch == 'P') { // A property
jtulach@1348
  2562
                boolean comp = (ch == 'P');
jtulach@1348
  2563
                boolean oneLetter = true;
jtulach@1348
  2564
                // Consume { if present
jtulach@1348
  2565
                ch = next();
jtulach@1348
  2566
                if (ch != '{')
jtulach@1348
  2567
                    unread();
jtulach@1348
  2568
                else
jtulach@1348
  2569
                    oneLetter = false;
jtulach@1348
  2570
                return family(oneLetter, comp);
jtulach@1348
  2571
            } else { // ordinary escape
jtulach@1348
  2572
                unread();
jtulach@1348
  2573
                ch = escape(true, true);
jtulach@1348
  2574
                if (ch == -1)
jtulach@1348
  2575
                    return (CharProperty) root;
jtulach@1348
  2576
            }
jtulach@1348
  2577
        } else {
jtulach@1348
  2578
            ch = single();
jtulach@1348
  2579
        }
jtulach@1348
  2580
        if (ch >= 0) {
jtulach@1348
  2581
            if (peek() == '-') {
jtulach@1348
  2582
                int endRange = temp[cursor+1];
jtulach@1348
  2583
                if (endRange == '[') {
jtulach@1348
  2584
                    return bitsOrSingle(bits, ch);
jtulach@1348
  2585
                }
jtulach@1348
  2586
                if (endRange != ']') {
jtulach@1348
  2587
                    next();
jtulach@1348
  2588
                    int m = single();
jtulach@1348
  2589
                    if (m < ch)
jtulach@1348
  2590
                        throw error("Illegal character range");
jtulach@1348
  2591
                    if (has(CASE_INSENSITIVE))
jtulach@1348
  2592
                        return caseInsensitiveRangeFor(ch, m);
jtulach@1348
  2593
                    else
jtulach@1348
  2594
                        return rangeFor(ch, m);
jtulach@1348
  2595
                }
jtulach@1348
  2596
            }
jtulach@1348
  2597
            return bitsOrSingle(bits, ch);
jtulach@1348
  2598
        }
jtulach@1348
  2599
        throw error("Unexpected character '"+((char)ch)+"'");
jtulach@1348
  2600
    }
jtulach@1348
  2601
jtulach@1348
  2602
    private int single() {
jtulach@1348
  2603
        int ch = peek();
jtulach@1348
  2604
        switch (ch) {
jtulach@1348
  2605
        case '\\':
jtulach@1348
  2606
            return escape(true, false);
jtulach@1348
  2607
        default:
jtulach@1348
  2608
            next();
jtulach@1348
  2609
            return ch;
jtulach@1348
  2610
        }
jtulach@1348
  2611
    }
jtulach@1348
  2612
jtulach@1348
  2613
    /**
jtulach@1348
  2614
     * Parses a Unicode character family and returns its representative node.
jtulach@1348
  2615
     */
jtulach@1348
  2616
    private CharProperty family(boolean singleLetter,
jtulach@1348
  2617
                                boolean maybeComplement)
jtulach@1348
  2618
    {
jtulach@1348
  2619
        next();
jtulach@1348
  2620
        String name;
jtulach@1348
  2621
        CharProperty node = null;
jtulach@1348
  2622
jtulach@1348
  2623
        if (singleLetter) {
jtulach@1348
  2624
            int c = temp[cursor];
jtulach@1348
  2625
            if (!Character.isSupplementaryCodePoint(c)) {
jtulach@1348
  2626
                name = String.valueOf((char)c);
jtulach@1348
  2627
            } else {
jtulach@1348
  2628
                name = new String(temp, cursor, 1);
jtulach@1348
  2629
            }
jtulach@1348
  2630
            read();
jtulach@1348
  2631
        } else {
jtulach@1348
  2632
            int i = cursor;
jtulach@1348
  2633
            mark('}');
jtulach@1348
  2634
            while(read() != '}') {
jtulach@1348
  2635
            }
jtulach@1348
  2636
            mark('\000');
jtulach@1348
  2637
            int j = cursor;
jtulach@1348
  2638
            if (j > patternLength)
jtulach@1348
  2639
                throw error("Unclosed character family");
jtulach@1348
  2640
            if (i + 1 >= j)
jtulach@1348
  2641
                throw error("Empty character family");
jtulach@1348
  2642
            name = new String(temp, i, j-i-1);
jtulach@1348
  2643
        }
jtulach@1348
  2644
jtulach@1348
  2645
        int i = name.indexOf('=');
jtulach@1348
  2646
        if (i != -1) {
jtulach@1348
  2647
            // property construct \p{name=value}
jtulach@1348
  2648
            String value = name.substring(i + 1);
jtulach@1348
  2649
            name = name.substring(0, i).toLowerCase(Locale.ENGLISH);
jtulach@1350
  2650
    /*        if ("sc".equals(name) || "script".equals(name)) {
jtulach@1348
  2651
                node = unicodeScriptPropertyFor(value);
jtulach@1348
  2652
            } else if ("blk".equals(name) || "block".equals(name)) {
jtulach@1348
  2653
                node = unicodeBlockPropertyFor(value);
jtulach@1350
  2654
            } else*/ if ("gc".equals(name) || "general_category".equals(name)) {
jtulach@1348
  2655
                node = charPropertyNodeFor(value);
jtulach@1348
  2656
            } else {
jtulach@1348
  2657
                throw error("Unknown Unicode property {name=<" + name + ">, "
jtulach@1348
  2658
                             + "value=<" + value + ">}");
jtulach@1348
  2659
            }
jtulach@1348
  2660
        } else {
jtulach@1350
  2661
            /*if (name.startsWith("In")) {
jtulach@1348
  2662
                // \p{inBlockName}
jtulach@1348
  2663
                node = unicodeBlockPropertyFor(name.substring(2));
jtulach@1348
  2664
            } else if (name.startsWith("Is")) {
jtulach@1348
  2665
                // \p{isGeneralCategory} and \p{isScriptName}
jtulach@1348
  2666
                name = name.substring(2);
jtulach@1348
  2667
                UnicodeProp uprop = UnicodeProp.forName(name);
jtulach@1348
  2668
                if (uprop != null)
jtulach@1348
  2669
                    node = new Utype(uprop);
jtulach@1348
  2670
                if (node == null)
jtulach@1348
  2671
                    node = CharPropertyNames.charPropertyFor(name);
jtulach@1348
  2672
                if (node == null)
jtulach@1348
  2673
                    node = unicodeScriptPropertyFor(name);
jtulach@1350
  2674
            } else*/ {
jtulach@1348
  2675
                if (has(UNICODE_CHARACTER_CLASS)) {
jtulach@1348
  2676
                    UnicodeProp uprop = UnicodeProp.forPOSIXName(name);
jtulach@1348
  2677
                    if (uprop != null)
jtulach@1348
  2678
                        node = new Utype(uprop);
jtulach@1348
  2679
                }
jtulach@1348
  2680
                if (node == null)
jtulach@1348
  2681
                    node = charPropertyNodeFor(name);
jtulach@1348
  2682
            }
jtulach@1348
  2683
        }
jtulach@1348
  2684
        if (maybeComplement) {
jtulach@1350
  2685
            if (node instanceof Category /*|| node instanceof Block*/)
jtulach@1348
  2686
                hasSupplementary = true;
jtulach@1348
  2687
            node = node.complement();
jtulach@1348
  2688
        }
jtulach@1348
  2689
        return node;
jtulach@1348
  2690
    }
jtulach@1348
  2691
jtulach@1348
  2692
jtulach@1348
  2693
    /**
jtulach@1348
  2694
     * Returns a CharProperty matching all characters belong to
jtulach@1348
  2695
     * a UnicodeScript.
jtulach@1350
  2696
     *
jtulach@1348
  2697
    private CharProperty unicodeScriptPropertyFor(String name) {
jtulach@1348
  2698
        final Character.UnicodeScript script;
jtulach@1348
  2699
        try {
jtulach@1348
  2700
            script = Character.UnicodeScript.forName(name);
jtulach@1348
  2701
        } catch (IllegalArgumentException iae) {
jtulach@1348
  2702
            throw error("Unknown character script name {" + name + "}");
jtulach@1348
  2703
        }
jtulach@1348
  2704
        return new Script(script);
jtulach@1348
  2705
    }
jtulach@1348
  2706
jtulach@1348
  2707
    /**
jtulach@1348
  2708
     * Returns a CharProperty matching all characters in a UnicodeBlock.
jtulach@1350
  2709
     *
jtulach@1348
  2710
    private CharProperty unicodeBlockPropertyFor(String name) {
jtulach@1348
  2711
        final Character.UnicodeBlock block;
jtulach@1348
  2712
        try {
jtulach@1348
  2713
            block = Character.UnicodeBlock.forName(name);
jtulach@1348
  2714
        } catch (IllegalArgumentException iae) {
jtulach@1348
  2715
            throw error("Unknown character block name {" + name + "}");
jtulach@1348
  2716
        }
jtulach@1348
  2717
        return new Block(block);
jtulach@1348
  2718
    }
jtulach@1348
  2719
jtulach@1348
  2720
    /**
jtulach@1348
  2721
     * Returns a CharProperty matching all characters in a named property.
jtulach@1348
  2722
     */
jtulach@1348
  2723
    private CharProperty charPropertyNodeFor(String name) {
jtulach@1348
  2724
        CharProperty p = CharPropertyNames.charPropertyFor(name);
jtulach@1348
  2725
        if (p == null)
jtulach@1348
  2726
            throw error("Unknown character property name {" + name + "}");
jtulach@1348
  2727
        return p;
jtulach@1348
  2728
    }
jtulach@1348
  2729
jtulach@1348
  2730
    /**
jtulach@1348
  2731
     * Parses and returns the name of a "named capturing group", the trailing
jtulach@1348
  2732
     * ">" is consumed after parsing.
jtulach@1348
  2733
     */
jtulach@1348
  2734
    private String groupname(int ch) {
jtulach@1348
  2735
        StringBuilder sb = new StringBuilder();
jtulach@1348
  2736
        sb.append(Character.toChars(ch));
jtulach@1348
  2737
        while (ASCII.isLower(ch=read()) || ASCII.isUpper(ch) ||
jtulach@1348
  2738
               ASCII.isDigit(ch)) {
jtulach@1348
  2739
            sb.append(Character.toChars(ch));
jtulach@1348
  2740
        }
jtulach@1348
  2741
        if (sb.length() == 0)
jtulach@1348
  2742
            throw error("named capturing group has 0 length name");
jtulach@1348
  2743
        if (ch != '>')
jtulach@1348
  2744
            throw error("named capturing group is missing trailing '>'");
jtulach@1348
  2745
        return sb.toString();
jtulach@1348
  2746
    }
jtulach@1348
  2747
jtulach@1348
  2748
    /**
jtulach@1348
  2749
     * Parses a group and returns the head node of a set of nodes that process
jtulach@1348
  2750
     * the group. Sometimes a double return system is used where the tail is
jtulach@1348
  2751
     * returned in root.
jtulach@1348
  2752
     */
jtulach@1348
  2753
    private Node group0() {
jtulach@1348
  2754
        boolean capturingGroup = false;
jtulach@1348
  2755
        Node head = null;
jtulach@1348
  2756
        Node tail = null;
jtulach@1348
  2757
        int save = flags;
jtulach@1348
  2758
        root = null;
jtulach@1348
  2759
        int ch = next();
jtulach@1348
  2760
        if (ch == '?') {
jtulach@1348
  2761
            ch = skip();
jtulach@1348
  2762
            switch (ch) {
jtulach@1348
  2763
            case ':':   //  (?:xxx) pure group
jtulach@1348
  2764
                head = createGroup(true);
jtulach@1348
  2765
                tail = root;
jtulach@1348
  2766
                head.next = expr(tail);
jtulach@1348
  2767
                break;
jtulach@1348
  2768
            case '=':   // (?=xxx) and (?!xxx) lookahead
jtulach@1348
  2769
            case '!':
jtulach@1348
  2770
                head = createGroup(true);
jtulach@1348
  2771
                tail = root;
jtulach@1348
  2772
                head.next = expr(tail);
jtulach@1348
  2773
                if (ch == '=') {
jtulach@1348
  2774
                    head = tail = new Pos(head);
jtulach@1348
  2775
                } else {
jtulach@1348
  2776
                    head = tail = new Neg(head);
jtulach@1348
  2777
                }
jtulach@1348
  2778
                break;
jtulach@1348
  2779
            case '>':   // (?>xxx)  independent group
jtulach@1348
  2780
                head = createGroup(true);
jtulach@1348
  2781
                tail = root;
jtulach@1348
  2782
                head.next = expr(tail);
jtulach@1348
  2783
                head = tail = new Ques(head, INDEPENDENT);
jtulach@1348
  2784
                break;
jtulach@1348
  2785
            case '<':   // (?<xxx)  look behind
jtulach@1348
  2786
                ch = read();
jtulach@1348
  2787
                if (ASCII.isLower(ch) || ASCII.isUpper(ch)) {
jtulach@1348
  2788
                    // named captured group
jtulach@1348
  2789
                    String name = groupname(ch);
jtulach@1348
  2790
                    if (namedGroups().containsKey(name))
jtulach@1348
  2791
                        throw error("Named capturing group <" + name
jtulach@1348
  2792
                                    + "> is already defined");
jtulach@1348
  2793
                    capturingGroup = true;
jtulach@1348
  2794
                    head = createGroup(false);
jtulach@1348
  2795
                    tail = root;
jtulach@1348
  2796
                    namedGroups().put(name, capturingGroupCount-1);
jtulach@1348
  2797
                    head.next = expr(tail);
jtulach@1348
  2798
                    break;
jtulach@1348
  2799
                }
jtulach@1348
  2800
                int start = cursor;
jtulach@1348
  2801
                head = createGroup(true);
jtulach@1348
  2802
                tail = root;
jtulach@1348
  2803
                head.next = expr(tail);
jtulach@1348
  2804
                tail.next = lookbehindEnd;
jtulach@1348
  2805
                TreeInfo info = new TreeInfo();
jtulach@1348
  2806
                head.study(info);
jtulach@1348
  2807
                if (info.maxValid == false) {
jtulach@1348
  2808
                    throw error("Look-behind group does not have "
jtulach@1348
  2809
                                + "an obvious maximum length");
jtulach@1348
  2810
                }
jtulach@1348
  2811
                boolean hasSupplementary = findSupplementary(start, patternLength);
jtulach@1348
  2812
                if (ch == '=') {
jtulach@1348
  2813
                    head = tail = (hasSupplementary ?
jtulach@1348
  2814
                                   new BehindS(head, info.maxLength,
jtulach@1348
  2815
                                               info.minLength) :
jtulach@1348
  2816
                                   new Behind(head, info.maxLength,
jtulach@1348
  2817
                                              info.minLength));
jtulach@1348
  2818
                } else if (ch == '!') {
jtulach@1348
  2819
                    head = tail = (hasSupplementary ?
jtulach@1348
  2820
                                   new NotBehindS(head, info.maxLength,
jtulach@1348
  2821
                                                  info.minLength) :
jtulach@1348
  2822
                                   new NotBehind(head, info.maxLength,
jtulach@1348
  2823
                                                 info.minLength));
jtulach@1348
  2824
                } else {
jtulach@1348
  2825
                    throw error("Unknown look-behind group");
jtulach@1348
  2826
                }
jtulach@1348
  2827
                break;
jtulach@1348
  2828
            case '$':
jtulach@1348
  2829
            case '@':
jtulach@1348
  2830
                throw error("Unknown group type");
jtulach@1348
  2831
            default:    // (?xxx:) inlined match flags
jtulach@1348
  2832
                unread();
jtulach@1348
  2833
                addFlag();
jtulach@1348
  2834
                ch = read();
jtulach@1348
  2835
                if (ch == ')') {
jtulach@1348
  2836
                    return null;    // Inline modifier only
jtulach@1348
  2837
                }
jtulach@1348
  2838
                if (ch != ':') {
jtulach@1348
  2839
                    throw error("Unknown inline modifier");
jtulach@1348
  2840
                }
jtulach@1348
  2841
                head = createGroup(true);
jtulach@1348
  2842
                tail = root;
jtulach@1348
  2843
                head.next = expr(tail);
jtulach@1348
  2844
                break;
jtulach@1348
  2845
            }
jtulach@1348
  2846
        } else { // (xxx) a regular group
jtulach@1348
  2847
            capturingGroup = true;
jtulach@1348
  2848
            head = createGroup(false);
jtulach@1348
  2849
            tail = root;
jtulach@1348
  2850
            head.next = expr(tail);
jtulach@1348
  2851
        }
jtulach@1348
  2852
jtulach@1348
  2853
        accept(')', "Unclosed group");
jtulach@1348
  2854
        flags = save;
jtulach@1348
  2855
jtulach@1348
  2856
        // Check for quantifiers
jtulach@1348
  2857
        Node node = closure(head);
jtulach@1348
  2858
        if (node == head) { // No closure
jtulach@1348
  2859
            root = tail;
jtulach@1348
  2860
            return node;    // Dual return
jtulach@1348
  2861
        }
jtulach@1348
  2862
        if (head == tail) { // Zero length assertion
jtulach@1348
  2863
            root = node;
jtulach@1348
  2864
            return node;    // Dual return
jtulach@1348
  2865
        }
jtulach@1348
  2866
jtulach@1348
  2867
        if (node instanceof Ques) {
jtulach@1348
  2868
            Ques ques = (Ques) node;
jtulach@1348
  2869
            if (ques.type == POSSESSIVE) {
jtulach@1348
  2870
                root = node;
jtulach@1348
  2871
                return node;
jtulach@1348
  2872
            }
jtulach@1348
  2873
            tail.next = new BranchConn();
jtulach@1348
  2874
            tail = tail.next;
jtulach@1348
  2875
            if (ques.type == GREEDY) {
jtulach@1348
  2876
                head = new Branch(head, null, tail);
jtulach@1348
  2877
            } else { // Reluctant quantifier
jtulach@1348
  2878
                head = new Branch(null, head, tail);
jtulach@1348
  2879
            }
jtulach@1348
  2880
            root = tail;
jtulach@1348
  2881
            return head;
jtulach@1348
  2882
        } else if (node instanceof Curly) {
jtulach@1348
  2883
            Curly curly = (Curly) node;
jtulach@1348
  2884
            if (curly.type == POSSESSIVE) {
jtulach@1348
  2885
                root = node;
jtulach@1348
  2886
                return node;
jtulach@1348
  2887
            }
jtulach@1348
  2888
            // Discover if the group is deterministic
jtulach@1348
  2889
            TreeInfo info = new TreeInfo();
jtulach@1348
  2890
            if (head.study(info)) { // Deterministic
jtulach@1348
  2891
                GroupTail temp = (GroupTail) tail;
jtulach@1348
  2892
                head = root = new GroupCurly(head.next, curly.cmin,
jtulach@1348
  2893
                                   curly.cmax, curly.type,
jtulach@1348
  2894
                                   ((GroupTail)tail).localIndex,
jtulach@1348
  2895
                                   ((GroupTail)tail).groupIndex,
jtulach@1348
  2896
                                             capturingGroup);
jtulach@1348
  2897
                return head;
jtulach@1348
  2898
            } else { // Non-deterministic
jtulach@1348
  2899
                int temp = ((GroupHead) head).localIndex;
jtulach@1348
  2900
                Loop loop;
jtulach@1348
  2901
                if (curly.type == GREEDY)
jtulach@1348
  2902
                    loop = new Loop(this.localCount, temp);
jtulach@1348
  2903
                else  // Reluctant Curly
jtulach@1348
  2904
                    loop = new LazyLoop(this.localCount, temp);
jtulach@1348
  2905
                Prolog prolog = new Prolog(loop);
jtulach@1348
  2906
                this.localCount += 1;
jtulach@1348
  2907
                loop.cmin = curly.cmin;
jtulach@1348
  2908
                loop.cmax = curly.cmax;
jtulach@1348
  2909
                loop.body = head;
jtulach@1348
  2910
                tail.next = loop;
jtulach@1348
  2911
                root = loop;
jtulach@1348
  2912
                return prolog; // Dual return
jtulach@1348
  2913
            }
jtulach@1348
  2914
        }
jtulach@1348
  2915
        throw error("Internal logic error");
jtulach@1348
  2916
    }
jtulach@1348
  2917
jtulach@1348
  2918
    /**
jtulach@1348
  2919
     * Create group head and tail nodes using double return. If the group is
jtulach@1348
  2920
     * created with anonymous true then it is a pure group and should not
jtulach@1348
  2921
     * affect group counting.
jtulach@1348
  2922
     */
jtulach@1348
  2923
    private Node createGroup(boolean anonymous) {
jtulach@1348
  2924
        int localIndex = localCount++;
jtulach@1348
  2925
        int groupIndex = 0;
jtulach@1348
  2926
        if (!anonymous)
jtulach@1348
  2927
            groupIndex = capturingGroupCount++;
jtulach@1348
  2928
        GroupHead head = new GroupHead(localIndex);
jtulach@1348
  2929
        root = new GroupTail(localIndex, groupIndex);
jtulach@1348
  2930
        if (!anonymous && groupIndex < 10)
jtulach@1348
  2931
            groupNodes[groupIndex] = head;
jtulach@1348
  2932
        return head;
jtulach@1348
  2933
    }
jtulach@1348
  2934
jtulach@1348
  2935
    /**
jtulach@1348
  2936
     * Parses inlined match flags and set them appropriately.
jtulach@1348
  2937
     */
jtulach@1348
  2938
    private void addFlag() {
jtulach@1348
  2939
        int ch = peek();
jtulach@1348
  2940
        for (;;) {
jtulach@1348
  2941
            switch (ch) {
jtulach@1348
  2942
            case 'i':
jtulach@1348
  2943
                flags |= CASE_INSENSITIVE;
jtulach@1348
  2944
                break;
jtulach@1348
  2945
            case 'm':
jtulach@1348
  2946
                flags |= MULTILINE;
jtulach@1348
  2947
                break;
jtulach@1348
  2948
            case 's':
jtulach@1348
  2949
                flags |= DOTALL;
jtulach@1348
  2950
                break;
jtulach@1348
  2951
            case 'd':
jtulach@1348
  2952
                flags |= UNIX_LINES;
jtulach@1348
  2953
                break;
jtulach@1348
  2954
            case 'u':
jtulach@1348
  2955
                flags |= UNICODE_CASE;
jtulach@1348
  2956
                break;
jtulach@1348
  2957
            case 'c':
jtulach@1348
  2958
                flags |= CANON_EQ;
jtulach@1348
  2959
                break;
jtulach@1348
  2960
            case 'x':
jtulach@1348
  2961
                flags |= COMMENTS;
jtulach@1348
  2962
                break;
jtulach@1348
  2963
            case 'U':
jtulach@1348
  2964
                flags |= (UNICODE_CHARACTER_CLASS | UNICODE_CASE);
jtulach@1348
  2965
                break;
jtulach@1348
  2966
            case '-': // subFlag then fall through
jtulach@1348
  2967
                ch = next();
jtulach@1348
  2968
                subFlag();
jtulach@1348
  2969
            default:
jtulach@1348
  2970
                return;
jtulach@1348
  2971
            }
jtulach@1348
  2972
            ch = next();
jtulach@1348
  2973
        }
jtulach@1348
  2974
    }
jtulach@1348
  2975
jtulach@1348
  2976
    /**
jtulach@1348
  2977
     * Parses the second part of inlined match flags and turns off
jtulach@1348
  2978
     * flags appropriately.
jtulach@1348
  2979
     */
jtulach@1348
  2980
    private void subFlag() {
jtulach@1348
  2981
        int ch = peek();
jtulach@1348
  2982
        for (;;) {
jtulach@1348
  2983
            switch (ch) {
jtulach@1348
  2984
            case 'i':
jtulach@1348
  2985
                flags &= ~CASE_INSENSITIVE;
jtulach@1348
  2986
                break;
jtulach@1348
  2987
            case 'm':
jtulach@1348
  2988
                flags &= ~MULTILINE;
jtulach@1348
  2989
                break;
jtulach@1348
  2990
            case 's':
jtulach@1348
  2991
                flags &= ~DOTALL;
jtulach@1348
  2992
                break;
jtulach@1348
  2993
            case 'd':
jtulach@1348
  2994
                flags &= ~UNIX_LINES;
jtulach@1348
  2995
                break;
jtulach@1348
  2996
            case 'u':
jtulach@1348
  2997
                flags &= ~UNICODE_CASE;
jtulach@1348
  2998
                break;
jtulach@1348
  2999
            case 'c':
jtulach@1348
  3000
                flags &= ~CANON_EQ;
jtulach@1348
  3001
                break;
jtulach@1348
  3002
            case 'x':
jtulach@1348
  3003
                flags &= ~COMMENTS;
jtulach@1348
  3004
                break;
jtulach@1348
  3005
            case 'U':
jtulach@1348
  3006
                flags &= ~(UNICODE_CHARACTER_CLASS | UNICODE_CASE);
jtulach@1348
  3007
            default:
jtulach@1348
  3008
                return;
jtulach@1348
  3009
            }
jtulach@1348
  3010
            ch = next();
jtulach@1348
  3011
        }
jtulach@1348
  3012
    }
jtulach@1348
  3013
jtulach@1348
  3014
    static final int MAX_REPS   = 0x7FFFFFFF;
jtulach@1348
  3015
jtulach@1348
  3016
    static final int GREEDY     = 0;
jtulach@1348
  3017
jtulach@1348
  3018
    static final int LAZY       = 1;
jtulach@1348
  3019
jtulach@1348
  3020
    static final int POSSESSIVE = 2;
jtulach@1348
  3021
jtulach@1348
  3022
    static final int INDEPENDENT = 3;
jtulach@1348
  3023
jtulach@1348
  3024
    /**
jtulach@1348
  3025
     * Processes repetition. If the next character peeked is a quantifier
jtulach@1348
  3026
     * then new nodes must be appended to handle the repetition.
jtulach@1348
  3027
     * Prev could be a single or a group, so it could be a chain of nodes.
jtulach@1348
  3028
     */
jtulach@1348
  3029
    private Node closure(Node prev) {
jtulach@1348
  3030
        Node atom;
jtulach@1348
  3031
        int ch = peek();
jtulach@1348
  3032
        switch (ch) {
jtulach@1348
  3033
        case '?':
jtulach@1348
  3034
            ch = next();
jtulach@1348
  3035
            if (ch == '?') {
jtulach@1348
  3036
                next();
jtulach@1348
  3037
                return new Ques(prev, LAZY);
jtulach@1348
  3038
            } else if (ch == '+') {
jtulach@1348
  3039
                next();
jtulach@1348
  3040
                return new Ques(prev, POSSESSIVE);
jtulach@1348
  3041
            }
jtulach@1348
  3042
            return new Ques(prev, GREEDY);
jtulach@1348
  3043
        case '*':
jtulach@1348
  3044
            ch = next();
jtulach@1348
  3045
            if (ch == '?') {
jtulach@1348
  3046
                next();
jtulach@1348
  3047
                return new Curly(prev, 0, MAX_REPS, LAZY);
jtulach@1348
  3048
            } else if (ch == '+') {
jtulach@1348
  3049
                next();
jtulach@1348
  3050
                return new Curly(prev, 0, MAX_REPS, POSSESSIVE);
jtulach@1348
  3051
            }
jtulach@1348
  3052
            return new Curly(prev, 0, MAX_REPS, GREEDY);
jtulach@1348
  3053
        case '+':
jtulach@1348
  3054
            ch = next();
jtulach@1348
  3055
            if (ch == '?') {
jtulach@1348
  3056
                next();
jtulach@1348
  3057
                return new Curly(prev, 1, MAX_REPS, LAZY);
jtulach@1348
  3058
            } else if (ch == '+') {
jtulach@1348
  3059
                next();
jtulach@1348
  3060
                return new Curly(prev, 1, MAX_REPS, POSSESSIVE);
jtulach@1348
  3061
            }
jtulach@1348
  3062
            return new Curly(prev, 1, MAX_REPS, GREEDY);
jtulach@1348
  3063
        case '{':
jtulach@1348
  3064
            ch = temp[cursor+1];
jtulach@1348
  3065
            if (ASCII.isDigit(ch)) {
jtulach@1348
  3066
                skip();
jtulach@1348
  3067
                int cmin = 0;
jtulach@1348
  3068
                do {
jtulach@1348
  3069
                    cmin = cmin * 10 + (ch - '0');
jtulach@1348
  3070
                } while (ASCII.isDigit(ch = read()));
jtulach@1348
  3071
                int cmax = cmin;
jtulach@1348
  3072
                if (ch == ',') {
jtulach@1348
  3073
                    ch = read();
jtulach@1348
  3074
                    cmax = MAX_REPS;
jtulach@1348
  3075
                    if (ch != '}') {
jtulach@1348
  3076
                        cmax = 0;
jtulach@1348
  3077
                        while (ASCII.isDigit(ch)) {
jtulach@1348
  3078
                            cmax = cmax * 10 + (ch - '0');
jtulach@1348
  3079
                            ch = read();
jtulach@1348
  3080
                        }
jtulach@1348
  3081
                    }
jtulach@1348
  3082
                }
jtulach@1348
  3083
                if (ch != '}')
jtulach@1348
  3084
                    throw error("Unclosed counted closure");
jtulach@1348
  3085
                if (((cmin) | (cmax) | (cmax - cmin)) < 0)
jtulach@1348
  3086
                    throw error("Illegal repetition range");
jtulach@1348
  3087
                Curly curly;
jtulach@1348
  3088
                ch = peek();
jtulach@1348
  3089
                if (ch == '?') {
jtulach@1348
  3090
                    next();
jtulach@1348
  3091
                    curly = new Curly(prev, cmin, cmax, LAZY);
jtulach@1348
  3092
                } else if (ch == '+') {
jtulach@1348
  3093
                    next();
jtulach@1348
  3094
                    curly = new Curly(prev, cmin, cmax, POSSESSIVE);
jtulach@1348
  3095
                } else {
jtulach@1348
  3096
                    curly = new Curly(prev, cmin, cmax, GREEDY);
jtulach@1348
  3097
                }
jtulach@1348
  3098
                return curly;
jtulach@1348
  3099
            } else {
jtulach@1348
  3100
                throw error("Illegal repetition");
jtulach@1348
  3101
            }
jtulach@1348
  3102
        default:
jtulach@1348
  3103
            return prev;
jtulach@1348
  3104
        }
jtulach@1348
  3105
    }
jtulach@1348
  3106
jtulach@1348
  3107
    /**
jtulach@1348
  3108
     *  Utility method for parsing control escape sequences.
jtulach@1348
  3109
     */
jtulach@1348
  3110
    private int c() {
jtulach@1348
  3111
        if (cursor < patternLength) {
jtulach@1348
  3112
            return read() ^ 64;
jtulach@1348
  3113
        }
jtulach@1348
  3114
        throw error("Illegal control escape sequence");
jtulach@1348
  3115
    }
jtulach@1348
  3116
jtulach@1348
  3117
    /**
jtulach@1348
  3118
     *  Utility method for parsing octal escape sequences.
jtulach@1348
  3119
     */
jtulach@1348
  3120
    private int o() {
jtulach@1348
  3121
        int n = read();
jtulach@1348
  3122
        if (((n-'0')|('7'-n)) >= 0) {
jtulach@1348
  3123
            int m = read();
jtulach@1348
  3124
            if (((m-'0')|('7'-m)) >= 0) {
jtulach@1348
  3125
                int o = read();
jtulach@1348
  3126
                if ((((o-'0')|('7'-o)) >= 0) && (((n-'0')|('3'-n)) >= 0)) {
jtulach@1348
  3127
                    return (n - '0') * 64 + (m - '0') * 8 + (o - '0');
jtulach@1348
  3128
                }
jtulach@1348
  3129
                unread();
jtulach@1348
  3130
                return (n - '0') * 8 + (m - '0');
jtulach@1348
  3131
            }
jtulach@1348
  3132
            unread();
jtulach@1348
  3133
            return (n - '0');
jtulach@1348
  3134
        }
jtulach@1348
  3135
        throw error("Illegal octal escape sequence");
jtulach@1348
  3136
    }
jtulach@1348
  3137
jtulach@1348
  3138
    /**
jtulach@1348
  3139
     *  Utility method for parsing hexadecimal escape sequences.
jtulach@1348
  3140
     */
jtulach@1348
  3141
    private int x() {
jtulach@1348
  3142
        int n = read();
jtulach@1348
  3143
        if (ASCII.isHexDigit(n)) {
jtulach@1348
  3144
            int m = read();
jtulach@1348
  3145
            if (ASCII.isHexDigit(m)) {
jtulach@1348
  3146
                return ASCII.toDigit(n) * 16 + ASCII.toDigit(m);
jtulach@1348
  3147
            }
jtulach@1348
  3148
        } else if (n == '{' && ASCII.isHexDigit(peek())) {
jtulach@1348
  3149
            int ch = 0;
jtulach@1348
  3150
            while (ASCII.isHexDigit(n = read())) {
jtulach@1348
  3151
                ch = (ch << 4) + ASCII.toDigit(n);
jtulach@1348
  3152
                if (ch > Character.MAX_CODE_POINT)
jtulach@1348
  3153
                    throw error("Hexadecimal codepoint is too big");
jtulach@1348
  3154
            }
jtulach@1348
  3155
            if (n != '}')
jtulach@1348
  3156
                throw error("Unclosed hexadecimal escape sequence");
jtulach@1348
  3157
            return ch;
jtulach@1348
  3158
        }
jtulach@1348
  3159
        throw error("Illegal hexadecimal escape sequence");
jtulach@1348
  3160
    }
jtulach@1348
  3161
jtulach@1348
  3162
    /**
jtulach@1348
  3163
     *  Utility method for parsing unicode escape sequences.
jtulach@1348
  3164
     */
jtulach@1348
  3165
    private int cursor() {
jtulach@1348
  3166
        return cursor;
jtulach@1348
  3167
    }
jtulach@1348
  3168
jtulach@1348
  3169
    private void setcursor(int pos) {
jtulach@1348
  3170
        cursor = pos;
jtulach@1348
  3171
    }
jtulach@1348
  3172
jtulach@1348
  3173
    private int uxxxx() {
jtulach@1348
  3174
        int n = 0;
jtulach@1348
  3175
        for (int i = 0; i < 4; i++) {
jtulach@1348
  3176
            int ch = read();
jtulach@1348
  3177
            if (!ASCII.isHexDigit(ch)) {
jtulach@1348
  3178
                throw error("Illegal Unicode escape sequence");
jtulach@1348
  3179
            }
jtulach@1348
  3180
            n = n * 16 + ASCII.toDigit(ch);
jtulach@1348
  3181
        }
jtulach@1348
  3182
        return n;
jtulach@1348
  3183
    }
jtulach@1348
  3184
jtulach@1348
  3185
    private int u() {
jtulach@1348
  3186
        int n = uxxxx();
jtulach@1348
  3187
        if (Character.isHighSurrogate((char)n)) {
jtulach@1348
  3188
            int cur = cursor();
jtulach@1348
  3189
            if (read() == '\\' && read() == 'u') {
jtulach@1348
  3190
                int n2 = uxxxx();
jtulach@1348
  3191
                if (Character.isLowSurrogate((char)n2))
jtulach@1348
  3192
                    return Character.toCodePoint((char)n, (char)n2);
jtulach@1348
  3193
            }
jtulach@1348
  3194
            setcursor(cur);
jtulach@1348
  3195
        }
jtulach@1348
  3196
        return n;
jtulach@1348
  3197
    }
jtulach@1348
  3198
jtulach@1348
  3199
    //
jtulach@1348
  3200
    // Utility methods for code point support
jtulach@1348
  3201
    //
jtulach@1348
  3202
jtulach@1348
  3203
    private static final int countChars(CharSequence seq, int index,
jtulach@1348
  3204
                                        int lengthInCodePoints) {
jtulach@1348
  3205
        // optimization
jtulach@1348
  3206
        if (lengthInCodePoints == 1 && !Character.isHighSurrogate(seq.charAt(index))) {
jtulach@1348
  3207
            assert (index >= 0 && index < seq.length());
jtulach@1348
  3208
            return 1;
jtulach@1348
  3209
        }
jtulach@1348
  3210
        int length = seq.length();
jtulach@1348
  3211
        int x = index;
jtulach@1348
  3212
        if (lengthInCodePoints >= 0) {
jtulach@1348
  3213
            assert (index >= 0 && index < length);
jtulach@1348
  3214
            for (int i = 0; x < length && i < lengthInCodePoints; i++) {
jtulach@1348
  3215
                if (Character.isHighSurrogate(seq.charAt(x++))) {
jtulach@1348
  3216
                    if (x < length && Character.isLowSurrogate(seq.charAt(x))) {
jtulach@1348
  3217
                        x++;
jtulach@1348
  3218
                    }
jtulach@1348
  3219
                }
jtulach@1348
  3220
            }
jtulach@1348
  3221
            return x - index;
jtulach@1348
  3222
        }
jtulach@1348
  3223
jtulach@1348
  3224
        assert (index >= 0 && index <= length);
jtulach@1348
  3225
        if (index == 0) {
jtulach@1348
  3226
            return 0;
jtulach@1348
  3227
        }
jtulach@1348
  3228
        int len = -lengthInCodePoints;
jtulach@1348
  3229
        for (int i = 0; x > 0 && i < len; i++) {
jtulach@1348
  3230
            if (Character.isLowSurrogate(seq.charAt(--x))) {
jtulach@1348
  3231
                if (x > 0 && Character.isHighSurrogate(seq.charAt(x-1))) {
jtulach@1348
  3232
                    x--;
jtulach@1348
  3233
                }
jtulach@1348
  3234
            }
jtulach@1348
  3235
        }
jtulach@1348
  3236
        return index - x;
jtulach@1348
  3237
    }
jtulach@1348
  3238
jtulach@1348
  3239
    private static final int countCodePoints(CharSequence seq) {
jtulach@1348
  3240
        int length = seq.length();
jtulach@1348
  3241
        int n = 0;
jtulach@1348
  3242
        for (int i = 0; i < length; ) {
jtulach@1348
  3243
            n++;
jtulach@1348
  3244
            if (Character.isHighSurrogate(seq.charAt(i++))) {
jtulach@1348
  3245
                if (i < length && Character.isLowSurrogate(seq.charAt(i))) {
jtulach@1348
  3246
                    i++;
jtulach@1348
  3247
                }
jtulach@1348
  3248
            }
jtulach@1348
  3249
        }
jtulach@1348
  3250
        return n;
jtulach@1348
  3251
    }
jtulach@1348
  3252
jtulach@1348
  3253
    /**
jtulach@1348
  3254
     *  Creates a bit vector for matching Latin-1 values. A normal BitClass
jtulach@1348
  3255
     *  never matches values above Latin-1, and a complemented BitClass always
jtulach@1348
  3256
     *  matches values above Latin-1.
jtulach@1348
  3257
     */
jtulach@1348
  3258
    private static final class BitClass extends BmpCharProperty {
jtulach@1348
  3259
        final boolean[] bits;
jtulach@1348
  3260
        BitClass() { bits = new boolean[256]; }
jtulach@1348
  3261
        private BitClass(boolean[] bits) { this.bits = bits; }
jtulach@1348
  3262
        BitClass add(int c, int flags) {
jtulach@1348
  3263
            assert c >= 0 && c <= 255;
jtulach@1348
  3264
            if ((flags & CASE_INSENSITIVE) != 0) {
jtulach@1348
  3265
                if (ASCII.isAscii(c)) {
jtulach@1348
  3266
                    bits[ASCII.toUpper(c)] = true;
jtulach@1348
  3267
                    bits[ASCII.toLower(c)] = true;
jtulach@1348
  3268
                } else if ((flags & UNICODE_CASE) != 0) {
jtulach@1348
  3269
                    bits[Character.toLowerCase(c)] = true;
jtulach@1348
  3270
                    bits[Character.toUpperCase(c)] = true;
jtulach@1348
  3271
                }
jtulach@1348
  3272
            }
jtulach@1348
  3273
            bits[c] = true;
jtulach@1348
  3274
            return this;
jtulach@1348
  3275
        }
jtulach@1348
  3276
        boolean isSatisfiedBy(int ch) {
jtulach@1348
  3277
            return ch < 256 && bits[ch];
jtulach@1348
  3278
        }
jtulach@1348
  3279
    }
jtulach@1348
  3280
jtulach@1348
  3281
    /**
jtulach@1348
  3282
     *  Returns a suitably optimized, single character matcher.
jtulach@1348
  3283
     */
jtulach@1348
  3284
    private CharProperty newSingle(final int ch) {
jtulach@1348
  3285
        if (has(CASE_INSENSITIVE)) {
jtulach@1348
  3286
            int lower, upper;
jtulach@1348
  3287
            if (has(UNICODE_CASE)) {
jtulach@1348
  3288
                upper = Character.toUpperCase(ch);
jtulach@1348
  3289
                lower = Character.toLowerCase(upper);
jtulach@1348
  3290
                if (upper != lower)
jtulach@1348
  3291
                    return new SingleU(lower);
jtulach@1348
  3292
            } else if (ASCII.isAscii(ch)) {
jtulach@1348
  3293
                lower = ASCII.toLower(ch);
jtulach@1348
  3294
                upper = ASCII.toUpper(ch);
jtulach@1348
  3295
                if (lower != upper)
jtulach@1348
  3296
                    return new SingleI(lower, upper);
jtulach@1348
  3297
            }
jtulach@1348
  3298
        }
jtulach@1348
  3299
        if (isSupplementary(ch))
jtulach@1348
  3300
            return new SingleS(ch);    // Match a given Unicode character
jtulach@1348
  3301
        return new Single(ch);         // Match a given BMP character
jtulach@1348
  3302
    }
jtulach@1348
  3303
jtulach@1348
  3304
    /**
jtulach@1348
  3305
     *  Utility method for creating a string slice matcher.
jtulach@1348
  3306
     */
jtulach@1348
  3307
    private Node newSlice(int[] buf, int count, boolean hasSupplementary) {
jtulach@1348
  3308
        int[] tmp = new int[count];
jtulach@1348
  3309
        if (has(CASE_INSENSITIVE)) {
jtulach@1348
  3310
            if (has(UNICODE_CASE)) {
jtulach@1348
  3311
                for (int i = 0; i < count; i++) {
jtulach@1348
  3312
                    tmp[i] = Character.toLowerCase(
jtulach@1348
  3313
                                 Character.toUpperCase(buf[i]));
jtulach@1348
  3314
                }
jtulach@1348
  3315
                return hasSupplementary? new SliceUS(tmp) : new SliceU(tmp);
jtulach@1348
  3316
            }
jtulach@1348
  3317
            for (int i = 0; i < count; i++) {
jtulach@1348
  3318
                tmp[i] = ASCII.toLower(buf[i]);
jtulach@1348
  3319
            }
jtulach@1348
  3320
            return hasSupplementary? new SliceIS(tmp) : new SliceI(tmp);
jtulach@1348
  3321
        }
jtulach@1348
  3322
        for (int i = 0; i < count; i++) {
jtulach@1348
  3323
            tmp[i] = buf[i];
jtulach@1348
  3324
        }
jtulach@1348
  3325
        return hasSupplementary ? new SliceS(tmp) : new Slice(tmp);
jtulach@1348
  3326
    }
jtulach@1348
  3327
jtulach@1348
  3328
    /**
jtulach@1348
  3329
     * The following classes are the building components of the object
jtulach@1348
  3330
     * tree that represents a compiled regular expression. The object tree
jtulach@1348
  3331
     * is made of individual elements that handle constructs in the Pattern.
jtulach@1348
  3332
     * Each type of object knows how to match its equivalent construct with
jtulach@1348
  3333
     * the match() method.
jtulach@1348
  3334
     */
jtulach@1348
  3335
jtulach@1348
  3336
    /**
jtulach@1348
  3337
     * Base class for all node classes. Subclasses should override the match()
jtulach@1348
  3338
     * method as appropriate. This class is an accepting node, so its match()
jtulach@1348
  3339
     * always returns true.
jtulach@1348
  3340
     */
jtulach@1348
  3341
    static class Node extends Object {
jtulach@1348
  3342
        Node next;
jtulach@1348
  3343
        Node() {
jtulach@1348
  3344
            next = Pattern.accept;
jtulach@1348
  3345
        }
jtulach@1348
  3346
        /**
jtulach@1348
  3347
         * This method implements the classic accept node.
jtulach@1348
  3348
         */
jtulach@1348
  3349
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  3350
            matcher.last = i;
jtulach@1348
  3351
            matcher.groups[0] = matcher.first;
jtulach@1348
  3352
            matcher.groups[1] = matcher.last;
jtulach@1348
  3353
            return true;
jtulach@1348
  3354
        }
jtulach@1348
  3355
        /**
jtulach@1348
  3356
         * This method is good for all zero length assertions.
jtulach@1348
  3357
         */
jtulach@1348
  3358
        boolean study(TreeInfo info) {
jtulach@1348
  3359
            if (next != null) {
jtulach@1348
  3360
                return next.study(info);
jtulach@1348
  3361
            } else {
jtulach@1348
  3362
                return info.deterministic;
jtulach@1348
  3363
            }
jtulach@1348
  3364
        }
jtulach@1348
  3365
    }
jtulach@1348
  3366
jtulach@1348
  3367
    static class LastNode extends Node {
jtulach@1348
  3368
        /**
jtulach@1348
  3369
         * This method implements the classic accept node with
jtulach@1348
  3370
         * the addition of a check to see if the match occurred
jtulach@1348
  3371
         * using all of the input.
jtulach@1348
  3372
         */
jtulach@1348
  3373
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  3374
            if (matcher.acceptMode == Matcher.ENDANCHOR && i != matcher.to)
jtulach@1348
  3375
                return false;
jtulach@1348
  3376
            matcher.last = i;
jtulach@1348
  3377
            matcher.groups[0] = matcher.first;
jtulach@1348
  3378
            matcher.groups[1] = matcher.last;
jtulach@1348
  3379
            return true;
jtulach@1348
  3380
        }
jtulach@1348
  3381
    }
jtulach@1348
  3382
jtulach@1348
  3383
    /**
jtulach@1348
  3384
     * Used for REs that can start anywhere within the input string.
jtulach@1348
  3385
     * This basically tries to match repeatedly at each spot in the
jtulach@1348
  3386
     * input string, moving forward after each try. An anchored search
jtulach@1348
  3387
     * or a BnM will bypass this node completely.
jtulach@1348
  3388
     */
jtulach@1348
  3389
    static class Start extends Node {
jtulach@1348
  3390
        int minLength;
jtulach@1348
  3391
        Start(Node node) {
jtulach@1348
  3392
            this.next = node;
jtulach@1348
  3393
            TreeInfo info = new TreeInfo();
jtulach@1348
  3394
            next.study(info);
jtulach@1348
  3395
            minLength = info.minLength;
jtulach@1348
  3396
        }
jtulach@1348
  3397
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  3398
            if (i > matcher.to - minLength) {
jtulach@1348
  3399
                matcher.hitEnd = true;
jtulach@1348
  3400
                return false;
jtulach@1348
  3401
            }
jtulach@1348
  3402
            int guard = matcher.to - minLength;
jtulach@1348
  3403
            for (; i <= guard; i++) {
jtulach@1348
  3404
                if (next.match(matcher, i, seq)) {
jtulach@1348
  3405
                    matcher.first = i;
jtulach@1348
  3406
                    matcher.groups[0] = matcher.first;
jtulach@1348
  3407
                    matcher.groups[1] = matcher.last;
jtulach@1348
  3408
                    return true;
jtulach@1348
  3409
                }
jtulach@1348
  3410
            }
jtulach@1348
  3411
            matcher.hitEnd = true;
jtulach@1348
  3412
            return false;
jtulach@1348
  3413
        }
jtulach@1348
  3414
        boolean study(TreeInfo info) {
jtulach@1348
  3415
            next.study(info);
jtulach@1348
  3416
            info.maxValid = false;
jtulach@1348
  3417
            info.deterministic = false;
jtulach@1348
  3418
            return false;
jtulach@1348
  3419
        }
jtulach@1348
  3420
    }
jtulach@1348
  3421
jtulach@1348
  3422
    /*
jtulach@1348
  3423
     * StartS supports supplementary characters, including unpaired surrogates.
jtulach@1348
  3424
     */
jtulach@1348
  3425
    static final class StartS extends Start {
jtulach@1348
  3426
        StartS(Node node) {
jtulach@1348
  3427
            super(node);
jtulach@1348
  3428
        }
jtulach@1348
  3429
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  3430
            if (i > matcher.to - minLength) {
jtulach@1348
  3431
                matcher.hitEnd = true;
jtulach@1348
  3432
                return false;
jtulach@1348
  3433
            }
jtulach@1348
  3434
            int guard = matcher.to - minLength;
jtulach@1348
  3435
            while (i <= guard) {
jtulach@1348
  3436
                //if ((ret = next.match(matcher, i, seq)) || i == guard)
jtulach@1348
  3437
                if (next.match(matcher, i, seq)) {
jtulach@1348
  3438
                    matcher.first = i;
jtulach@1348
  3439
                    matcher.groups[0] = matcher.first;
jtulach@1348
  3440
                    matcher.groups[1] = matcher.last;
jtulach@1348
  3441
                    return true;
jtulach@1348
  3442
                }
jtulach@1348
  3443
                if (i == guard)
jtulach@1348
  3444
                    break;
jtulach@1348
  3445
                // Optimization to move to the next character. This is
jtulach@1348
  3446
                // faster than countChars(seq, i, 1).
jtulach@1348
  3447
                if (Character.isHighSurrogate(seq.charAt(i++))) {
jtulach@1348
  3448
                    if (i < seq.length() &&
jtulach@1348
  3449
                        Character.isLowSurrogate(seq.charAt(i))) {
jtulach@1348
  3450
                        i++;
jtulach@1348
  3451
                    }
jtulach@1348
  3452
                }
jtulach@1348
  3453
            }
jtulach@1348
  3454
            matcher.hitEnd = true;
jtulach@1348
  3455
            return false;
jtulach@1348
  3456
        }
jtulach@1348
  3457
    }
jtulach@1348
  3458
jtulach@1348
  3459
    /**
jtulach@1348
  3460
     * Node to anchor at the beginning of input. This object implements the
jtulach@1348
  3461
     * match for a \A sequence, and the caret anchor will use this if not in
jtulach@1348
  3462
     * multiline mode.
jtulach@1348
  3463
     */
jtulach@1348
  3464
    static final class Begin extends Node {
jtulach@1348
  3465
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  3466
            int fromIndex = (matcher.anchoringBounds) ?
jtulach@1348
  3467
                matcher.from : 0;
jtulach@1348
  3468
            if (i == fromIndex && next.match(matcher, i, seq)) {
jtulach@1348
  3469
                matcher.first = i;
jtulach@1348
  3470
                matcher.groups[0] = i;
jtulach@1348
  3471
                matcher.groups[1] = matcher.last;
jtulach@1348
  3472
                return true;
jtulach@1348
  3473
            } else {
jtulach@1348
  3474
                return false;
jtulach@1348
  3475
            }
jtulach@1348
  3476
        }
jtulach@1348
  3477
    }
jtulach@1348
  3478
jtulach@1348
  3479
    /**
jtulach@1348
  3480
     * Node to anchor at the end of input. This is the absolute end, so this
jtulach@1348
  3481
     * should not match at the last newline before the end as $ will.
jtulach@1348
  3482
     */
jtulach@1348
  3483
    static final class End extends Node {
jtulach@1348
  3484
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  3485
            int endIndex = (matcher.anchoringBounds) ?
jtulach@1348
  3486
                matcher.to : matcher.getTextLength();
jtulach@1348
  3487
            if (i == endIndex) {
jtulach@1348
  3488
                matcher.hitEnd = true;
jtulach@1348
  3489
                return next.match(matcher, i, seq);
jtulach@1348
  3490
            }
jtulach@1348
  3491
            return false;
jtulach@1348
  3492
        }
jtulach@1348
  3493
    }
jtulach@1348
  3494
jtulach@1348
  3495
    /**
jtulach@1348
  3496
     * Node to anchor at the beginning of a line. This is essentially the
jtulach@1348
  3497
     * object to match for the multiline ^.
jtulach@1348
  3498
     */
jtulach@1348
  3499
    static final class Caret extends Node {
jtulach@1348
  3500
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  3501
            int startIndex = matcher.from;
jtulach@1348
  3502
            int endIndex = matcher.to;
jtulach@1348
  3503
            if (!matcher.anchoringBounds) {
jtulach@1348
  3504
                startIndex = 0;
jtulach@1348
  3505
                endIndex = matcher.getTextLength();
jtulach@1348
  3506
            }
jtulach@1348
  3507
            // Perl does not match ^ at end of input even after newline
jtulach@1348
  3508
            if (i == endIndex) {
jtulach@1348
  3509
                matcher.hitEnd = true;
jtulach@1348
  3510
                return false;
jtulach@1348
  3511
            }
jtulach@1348
  3512
            if (i > startIndex) {
jtulach@1348
  3513
                char ch = seq.charAt(i-1);
jtulach@1348
  3514
                if (ch != '\n' && ch != '\r'
jtulach@1348
  3515
                    && (ch|1) != '\u2029'
jtulach@1348
  3516
                    && ch != '\u0085' ) {
jtulach@1348
  3517
                    return false;
jtulach@1348
  3518
                }
jtulach@1348
  3519
                // Should treat /r/n as one newline
jtulach@1348
  3520
                if (ch == '\r' && seq.charAt(i) == '\n')
jtulach@1348
  3521
                    return false;
jtulach@1348
  3522
            }
jtulach@1348
  3523
            return next.match(matcher, i, seq);
jtulach@1348
  3524
        }
jtulach@1348
  3525
    }
jtulach@1348
  3526
jtulach@1348
  3527
    /**
jtulach@1348
  3528
     * Node to anchor at the beginning of a line when in unixdot mode.
jtulach@1348
  3529
     */
jtulach@1348
  3530
    static final class UnixCaret extends Node {
jtulach@1348
  3531
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  3532
            int startIndex = matcher.from;
jtulach@1348
  3533
            int endIndex = matcher.to;
jtulach@1348
  3534
            if (!matcher.anchoringBounds) {
jtulach@1348
  3535
                startIndex = 0;
jtulach@1348
  3536
                endIndex = matcher.getTextLength();
jtulach@1348
  3537
            }
jtulach@1348
  3538
            // Perl does not match ^ at end of input even after newline
jtulach@1348
  3539
            if (i == endIndex) {
jtulach@1348
  3540
                matcher.hitEnd = true;
jtulach@1348
  3541
                return false;
jtulach@1348
  3542
            }
jtulach@1348
  3543
            if (i > startIndex) {
jtulach@1348
  3544
                char ch = seq.charAt(i-1);
jtulach@1348
  3545
                if (ch != '\n') {
jtulach@1348
  3546
                    return false;
jtulach@1348
  3547
                }
jtulach@1348
  3548
            }
jtulach@1348
  3549
            return next.match(matcher, i, seq);
jtulach@1348
  3550
        }
jtulach@1348
  3551
    }
jtulach@1348
  3552
jtulach@1348
  3553
    /**
jtulach@1348
  3554
     * Node to match the location where the last match ended.
jtulach@1348
  3555
     * This is used for the \G construct.
jtulach@1348
  3556
     */
jtulach@1348
  3557
    static final class LastMatch extends Node {
jtulach@1348
  3558
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  3559
            if (i != matcher.oldLast)
jtulach@1348
  3560
                return false;
jtulach@1348
  3561
            return next.match(matcher, i, seq);
jtulach@1348
  3562
        }
jtulach@1348
  3563
    }
jtulach@1348
  3564
jtulach@1348
  3565
    /**
jtulach@1348
  3566
     * Node to anchor at the end of a line or the end of input based on the
jtulach@1348
  3567
     * multiline mode.
jtulach@1348
  3568
     *
jtulach@1348
  3569
     * When not in multiline mode, the $ can only match at the very end
jtulach@1348
  3570
     * of the input, unless the input ends in a line terminator in which
jtulach@1348
  3571
     * it matches right before the last line terminator.
jtulach@1348
  3572
     *
jtulach@1348
  3573
     * Note that \r\n is considered an atomic line terminator.
jtulach@1348
  3574
     *
jtulach@1348
  3575
     * Like ^ the $ operator matches at a position, it does not match the
jtulach@1348
  3576
     * line terminators themselves.
jtulach@1348
  3577
     */
jtulach@1348
  3578
    static final class Dollar extends Node {
jtulach@1348
  3579
        boolean multiline;
jtulach@1348
  3580
        Dollar(boolean mul) {
jtulach@1348
  3581
            multiline = mul;
jtulach@1348
  3582
        }
jtulach@1348
  3583
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  3584
            int endIndex = (matcher.anchoringBounds) ?
jtulach@1348
  3585
                matcher.to : matcher.getTextLength();
jtulach@1348
  3586
            if (!multiline) {
jtulach@1348
  3587
                if (i < endIndex - 2)
jtulach@1348
  3588
                    return false;
jtulach@1348
  3589
                if (i == endIndex - 2) {
jtulach@1348
  3590
                    char ch = seq.charAt(i);
jtulach@1348
  3591
                    if (ch != '\r')
jtulach@1348
  3592
                        return false;
jtulach@1348
  3593
                    ch = seq.charAt(i + 1);
jtulach@1348
  3594
                    if (ch != '\n')
jtulach@1348
  3595
                        return false;
jtulach@1348
  3596
                }
jtulach@1348
  3597
            }
jtulach@1348
  3598
            // Matches before any line terminator; also matches at the
jtulach@1348
  3599
            // end of input
jtulach@1348
  3600
            // Before line terminator:
jtulach@1348
  3601
            // If multiline, we match here no matter what
jtulach@1348
  3602
            // If not multiline, fall through so that the end
jtulach@1348
  3603
            // is marked as hit; this must be a /r/n or a /n
jtulach@1348
  3604
            // at the very end so the end was hit; more input
jtulach@1348
  3605
            // could make this not match here
jtulach@1348
  3606
            if (i < endIndex) {
jtulach@1348
  3607
                char ch = seq.charAt(i);
jtulach@1348
  3608
                 if (ch == '\n') {
jtulach@1348
  3609
                     // No match between \r\n
jtulach@1348
  3610
                     if (i > 0 && seq.charAt(i-1) == '\r')
jtulach@1348
  3611
                         return false;
jtulach@1348
  3612
                     if (multiline)
jtulach@1348
  3613
                         return next.match(matcher, i, seq);
jtulach@1348
  3614
                 } else if (ch == '\r' || ch == '\u0085' ||
jtulach@1348
  3615
                            (ch|1) == '\u2029') {
jtulach@1348
  3616
                     if (multiline)
jtulach@1348
  3617
                         return next.match(matcher, i, seq);
jtulach@1348
  3618
                 } else { // No line terminator, no match
jtulach@1348
  3619
                     return false;
jtulach@1348
  3620
                 }
jtulach@1348
  3621
            }
jtulach@1348
  3622
            // Matched at current end so hit end
jtulach@1348
  3623
            matcher.hitEnd = true;
jtulach@1348
  3624
            // If a $ matches because of end of input, then more input
jtulach@1348
  3625
            // could cause it to fail!
jtulach@1348
  3626
            matcher.requireEnd = true;
jtulach@1348
  3627
            return next.match(matcher, i, seq);
jtulach@1348
  3628
        }
jtulach@1348
  3629
        boolean study(TreeInfo info) {
jtulach@1348
  3630
            next.study(info);
jtulach@1348
  3631
            return info.deterministic;
jtulach@1348
  3632
        }
jtulach@1348
  3633
    }
jtulach@1348
  3634
jtulach@1348
  3635
    /**
jtulach@1348
  3636
     * Node to anchor at the end of a line or the end of input based on the
jtulach@1348
  3637
     * multiline mode when in unix lines mode.
jtulach@1348
  3638
     */
jtulach@1348
  3639
    static final class UnixDollar extends Node {
jtulach@1348
  3640
        boolean multiline;
jtulach@1348
  3641
        UnixDollar(boolean mul) {
jtulach@1348
  3642
            multiline = mul;
jtulach@1348
  3643
        }
jtulach@1348
  3644
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  3645
            int endIndex = (matcher.anchoringBounds) ?
jtulach@1348
  3646
                matcher.to : matcher.getTextLength();
jtulach@1348
  3647
            if (i < endIndex) {
jtulach@1348
  3648
                char ch = seq.charAt(i);
jtulach@1348
  3649
                if (ch == '\n') {
jtulach@1348
  3650
                    // If not multiline, then only possible to
jtulach@1348
  3651
                    // match at very end or one before end
jtulach@1348
  3652
                    if (multiline == false && i != endIndex - 1)
jtulach@1348
  3653
                        return false;
jtulach@1348
  3654
                    // If multiline return next.match without setting
jtulach@1348
  3655
                    // matcher.hitEnd
jtulach@1348
  3656
                    if (multiline)
jtulach@1348
  3657
                        return next.match(matcher, i, seq);
jtulach@1348
  3658
                } else {
jtulach@1348
  3659
                    return false;
jtulach@1348
  3660
                }
jtulach@1348
  3661
            }
jtulach@1348
  3662
            // Matching because at the end or 1 before the end;
jtulach@1348
  3663
            // more input could change this so set hitEnd
jtulach@1348
  3664
            matcher.hitEnd = true;
jtulach@1348
  3665
            // If a $ matches because of end of input, then more input
jtulach@1348
  3666
            // could cause it to fail!
jtulach@1348
  3667
            matcher.requireEnd = true;
jtulach@1348
  3668
            return next.match(matcher, i, seq);
jtulach@1348
  3669
        }
jtulach@1348
  3670
        boolean study(TreeInfo info) {
jtulach@1348
  3671
            next.study(info);
jtulach@1348
  3672
            return info.deterministic;
jtulach@1348
  3673
        }
jtulach@1348
  3674
    }
jtulach@1348
  3675
jtulach@1348
  3676
    /**
jtulach@1348
  3677
     * Abstract node class to match one character satisfying some
jtulach@1348
  3678
     * boolean property.
jtulach@1348
  3679
     */
jtulach@1348
  3680
    private static abstract class CharProperty extends Node {
jtulach@1348
  3681
        abstract boolean isSatisfiedBy(int ch);
jtulach@1348
  3682
        CharProperty complement() {
jtulach@1348
  3683
            return new CharProperty() {
jtulach@1348
  3684
                    boolean isSatisfiedBy(int ch) {
jtulach@1348
  3685
                        return ! CharProperty.this.isSatisfiedBy(ch);}};
jtulach@1348
  3686
        }
jtulach@1348
  3687
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  3688
            if (i < matcher.to) {
jtulach@1348
  3689
                int ch = Character.codePointAt(seq, i);
jtulach@1348
  3690
                return isSatisfiedBy(ch)
jtulach@1348
  3691
                    && next.match(matcher, i+Character.charCount(ch), seq);
jtulach@1348
  3692
            } else {
jtulach@1348
  3693
                matcher.hitEnd = true;
jtulach@1348
  3694
                return false;
jtulach@1348
  3695
            }
jtulach@1348
  3696
        }
jtulach@1348
  3697
        boolean study(TreeInfo info) {
jtulach@1348
  3698
            info.minLength++;
jtulach@1348
  3699
            info.maxLength++;
jtulach@1348
  3700
            return next.study(info);
jtulach@1348
  3701
        }
jtulach@1348
  3702
    }
jtulach@1348
  3703
jtulach@1348
  3704
    /**
jtulach@1348
  3705
     * Optimized version of CharProperty that works only for
jtulach@1348
  3706
     * properties never satisfied by Supplementary characters.
jtulach@1348
  3707
     */
jtulach@1348
  3708
    private static abstract class BmpCharProperty extends CharProperty {
jtulach@1348
  3709
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  3710
            if (i < matcher.to) {
jtulach@1348
  3711
                return isSatisfiedBy(seq.charAt(i))
jtulach@1348
  3712
                    && next.match(matcher, i+1, seq);
jtulach@1348
  3713
            } else {
jtulach@1348
  3714
                matcher.hitEnd = true;
jtulach@1348
  3715
                return false;
jtulach@1348
  3716
            }
jtulach@1348
  3717
        }
jtulach@1348
  3718
    }
jtulach@1348
  3719
jtulach@1348
  3720
    /**
jtulach@1348
  3721
     * Node class that matches a Supplementary Unicode character
jtulach@1348
  3722
     */
jtulach@1348
  3723
    static final class SingleS extends CharProperty {
jtulach@1348
  3724
        final int c;
jtulach@1348
  3725
        SingleS(int c) { this.c = c; }
jtulach@1348
  3726
        boolean isSatisfiedBy(int ch) {
jtulach@1348
  3727
            return ch == c;
jtulach@1348
  3728
        }
jtulach@1348
  3729
    }
jtulach@1348
  3730
jtulach@1348
  3731
    /**
jtulach@1348
  3732
     * Optimization -- matches a given BMP character
jtulach@1348
  3733
     */
jtulach@1348
  3734
    static final class Single extends BmpCharProperty {
jtulach@1348
  3735
        final int c;
jtulach@1348
  3736
        Single(int c) { this.c = c; }
jtulach@1348
  3737
        boolean isSatisfiedBy(int ch) {
jtulach@1348
  3738
            return ch == c;
jtulach@1348
  3739
        }
jtulach@1348
  3740
    }
jtulach@1348
  3741
jtulach@1348
  3742
    /**
jtulach@1348
  3743
     * Case insensitive matches a given BMP character
jtulach@1348
  3744
     */
jtulach@1348
  3745
    static final class SingleI extends BmpCharProperty {
jtulach@1348
  3746
        final int lower;
jtulach@1348
  3747
        final int upper;
jtulach@1348
  3748
        SingleI(int lower, int upper) {
jtulach@1348
  3749
            this.lower = lower;
jtulach@1348
  3750
            this.upper = upper;
jtulach@1348
  3751
        }
jtulach@1348
  3752
        boolean isSatisfiedBy(int ch) {
jtulach@1348
  3753
            return ch == lower || ch == upper;
jtulach@1348
  3754
        }
jtulach@1348
  3755
    }
jtulach@1348
  3756
jtulach@1348
  3757
    /**
jtulach@1348
  3758
     * Unicode case insensitive matches a given Unicode character
jtulach@1348
  3759
     */
jtulach@1348
  3760
    static final class SingleU extends CharProperty {
jtulach@1348
  3761
        final int lower;
jtulach@1348
  3762
        SingleU(int lower) {
jtulach@1348
  3763
            this.lower = lower;
jtulach@1348
  3764
        }
jtulach@1348
  3765
        boolean isSatisfiedBy(int ch) {
jtulach@1348
  3766
            return lower == ch ||
jtulach@1348
  3767
                lower == Character.toLowerCase(Character.toUpperCase(ch));
jtulach@1348
  3768
        }
jtulach@1348
  3769
    }
jtulach@1348
  3770
jtulach@1348
  3771
jtulach@1348
  3772
    /**
jtulach@1348
  3773
     * Node class that matches a Unicode block.
jtulach@1350
  3774
     *
jtulach@1348
  3775
    static final class Block extends CharProperty {
jtulach@1348
  3776
        final Character.UnicodeBlock block;
jtulach@1348
  3777
        Block(Character.UnicodeBlock block) {
jtulach@1348
  3778
            this.block = block;
jtulach@1348
  3779
        }
jtulach@1348
  3780
        boolean isSatisfiedBy(int ch) {
jtulach@1348
  3781
            return block == Character.UnicodeBlock.of(ch);
jtulach@1348
  3782
        }
jtulach@1348
  3783
    }
jtulach@1348
  3784
jtulach@1348
  3785
    /**
jtulach@1348
  3786
     * Node class that matches a Unicode script
jtulach@1350
  3787
     *
jtulach@1348
  3788
    static final class Script extends CharProperty {
jtulach@1348
  3789
        final Character.UnicodeScript script;
jtulach@1348
  3790
        Script(Character.UnicodeScript script) {
jtulach@1348
  3791
            this.script = script;
jtulach@1348
  3792
        }
jtulach@1348
  3793
        boolean isSatisfiedBy(int ch) {
jtulach@1348
  3794
            return script == Character.UnicodeScript.of(ch);
jtulach@1348
  3795
        }
jtulach@1348
  3796
    }
jtulach@1348
  3797
jtulach@1348
  3798
    /**
jtulach@1348
  3799
     * Node class that matches a Unicode category.
jtulach@1348
  3800
     */
jtulach@1348
  3801
    static final class Category extends CharProperty {
jtulach@1348
  3802
        final int typeMask;
jtulach@1348
  3803
        Category(int typeMask) { this.typeMask = typeMask; }
jtulach@1348
  3804
        boolean isSatisfiedBy(int ch) {
jtulach@1348
  3805
            return (typeMask & (1 << Character.getType(ch))) != 0;
jtulach@1348
  3806
        }
jtulach@1348
  3807
    }
jtulach@1348
  3808
jtulach@1348
  3809
    /**
jtulach@1348
  3810
     * Node class that matches a Unicode "type"
jtulach@1348
  3811
     */
jtulach@1348
  3812
    static final class Utype extends CharProperty {
jtulach@1348
  3813
        final UnicodeProp uprop;
jtulach@1348
  3814
        Utype(UnicodeProp uprop) { this.uprop = uprop; }
jtulach@1348
  3815
        boolean isSatisfiedBy(int ch) {
jtulach@1348
  3816
            return uprop.is(ch);
jtulach@1348
  3817
        }
jtulach@1348
  3818
    }
jtulach@1348
  3819
jtulach@1348
  3820
jtulach@1348
  3821
    /**
jtulach@1348
  3822
     * Node class that matches a POSIX type.
jtulach@1348
  3823
     */
jtulach@1348
  3824
    static final class Ctype extends BmpCharProperty {
jtulach@1348
  3825
        final int ctype;
jtulach@1348
  3826
        Ctype(int ctype) { this.ctype = ctype; }
jtulach@1348
  3827
        boolean isSatisfiedBy(int ch) {
jtulach@1348
  3828
            return ch < 128 && ASCII.isType(ch, ctype);
jtulach@1348
  3829
        }
jtulach@1348
  3830
    }
jtulach@1348
  3831
jtulach@1348
  3832
    /**
jtulach@1348
  3833
     * Base class for all Slice nodes
jtulach@1348
  3834
     */
jtulach@1348
  3835
    static class SliceNode extends Node {
jtulach@1348
  3836
        int[] buffer;
jtulach@1348
  3837
        SliceNode(int[] buf) {
jtulach@1348
  3838
            buffer = buf;
jtulach@1348
  3839
        }
jtulach@1348
  3840
        boolean study(TreeInfo info) {
jtulach@1348
  3841
            info.minLength += buffer.length;
jtulach@1348
  3842
            info.maxLength += buffer.length;
jtulach@1348
  3843
            return next.study(info);
jtulach@1348
  3844
        }
jtulach@1348
  3845
    }
jtulach@1348
  3846
jtulach@1348
  3847
    /**
jtulach@1348
  3848
     * Node class for a case sensitive/BMP-only sequence of literal
jtulach@1348
  3849
     * characters.
jtulach@1348
  3850
     */
jtulach@1348
  3851
    static final class Slice extends SliceNode {
jtulach@1348
  3852
        Slice(int[] buf) {
jtulach@1348
  3853
            super(buf);
jtulach@1348
  3854
        }
jtulach@1348
  3855
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  3856
            int[] buf = buffer;
jtulach@1348
  3857
            int len = buf.length;
jtulach@1348
  3858
            for (int j=0; j<len; j++) {
jtulach@1348
  3859
                if ((i+j) >= matcher.to) {
jtulach@1348
  3860
                    matcher.hitEnd = true;
jtulach@1348
  3861
                    return false;
jtulach@1348
  3862
                }
jtulach@1348
  3863
                if (buf[j] != seq.charAt(i+j))
jtulach@1348
  3864
                    return false;
jtulach@1348
  3865
            }
jtulach@1348
  3866
            return next.match(matcher, i+len, seq);
jtulach@1348
  3867
        }
jtulach@1348
  3868
    }
jtulach@1348
  3869
jtulach@1348
  3870
    /**
jtulach@1348
  3871
     * Node class for a case_insensitive/BMP-only sequence of literal
jtulach@1348
  3872
     * characters.
jtulach@1348
  3873
     */
jtulach@1348
  3874
    static class SliceI extends SliceNode {
jtulach@1348
  3875
        SliceI(int[] buf) {
jtulach@1348
  3876
            super(buf);
jtulach@1348
  3877
        }
jtulach@1348
  3878
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  3879
            int[] buf = buffer;
jtulach@1348
  3880
            int len = buf.length;
jtulach@1348
  3881
            for (int j=0; j<len; j++) {
jtulach@1348
  3882
                if ((i+j) >= matcher.to) {
jtulach@1348
  3883
                    matcher.hitEnd = true;
jtulach@1348
  3884
                    return false;
jtulach@1348
  3885
                }
jtulach@1348
  3886
                int c = seq.charAt(i+j);
jtulach@1348
  3887
                if (buf[j] != c &&
jtulach@1348
  3888
                    buf[j] != ASCII.toLower(c))
jtulach@1348
  3889
                    return false;
jtulach@1348
  3890
            }
jtulach@1348
  3891
            return next.match(matcher, i+len, seq);
jtulach@1348
  3892
        }
jtulach@1348
  3893
    }
jtulach@1348
  3894
jtulach@1348
  3895
    /**
jtulach@1348
  3896
     * Node class for a unicode_case_insensitive/BMP-only sequence of
jtulach@1348
  3897
     * literal characters. Uses unicode case folding.
jtulach@1348
  3898
     */
jtulach@1348
  3899
    static final class SliceU extends SliceNode {
jtulach@1348
  3900
        SliceU(int[] buf) {
jtulach@1348
  3901
            super(buf);
jtulach@1348
  3902
        }
jtulach@1348
  3903
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  3904
            int[] buf = buffer;
jtulach@1348
  3905
            int len = buf.length;
jtulach@1348
  3906
            for (int j=0; j<len; j++) {
jtulach@1348
  3907
                if ((i+j) >= matcher.to) {
jtulach@1348
  3908
                    matcher.hitEnd = true;
jtulach@1348
  3909
                    return false;
jtulach@1348
  3910
                }
jtulach@1348
  3911
                int c = seq.charAt(i+j);
jtulach@1348
  3912
                if (buf[j] != c &&
jtulach@1348
  3913
                    buf[j] != Character.toLowerCase(Character.toUpperCase(c)))
jtulach@1348
  3914
                    return false;
jtulach@1348
  3915
            }
jtulach@1348
  3916
            return next.match(matcher, i+len, seq);
jtulach@1348
  3917
        }
jtulach@1348
  3918
    }
jtulach@1348
  3919
jtulach@1348
  3920
    /**
jtulach@1348
  3921
     * Node class for a case sensitive sequence of literal characters
jtulach@1348
  3922
     * including supplementary characters.
jtulach@1348
  3923
     */
jtulach@1348
  3924
    static final class SliceS extends SliceNode {
jtulach@1348
  3925
        SliceS(int[] buf) {
jtulach@1348
  3926
            super(buf);
jtulach@1348
  3927
        }
jtulach@1348
  3928
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  3929
            int[] buf = buffer;
jtulach@1348
  3930
            int x = i;
jtulach@1348
  3931
            for (int j = 0; j < buf.length; j++) {
jtulach@1348
  3932
                if (x >= matcher.to) {
jtulach@1348
  3933
                    matcher.hitEnd = true;
jtulach@1348
  3934
                    return false;
jtulach@1348
  3935
                }
jtulach@1348
  3936
                int c = Character.codePointAt(seq, x);
jtulach@1348
  3937
                if (buf[j] != c)
jtulach@1348
  3938
                    return false;
jtulach@1348
  3939
                x += Character.charCount(c);
jtulach@1348
  3940
                if (x > matcher.to) {
jtulach@1348
  3941
                    matcher.hitEnd = true;
jtulach@1348
  3942
                    return false;
jtulach@1348
  3943
                }
jtulach@1348
  3944
            }
jtulach@1348
  3945
            return next.match(matcher, x, seq);
jtulach@1348
  3946
        }
jtulach@1348
  3947
    }
jtulach@1348
  3948
jtulach@1348
  3949
    /**
jtulach@1348
  3950
     * Node class for a case insensitive sequence of literal characters
jtulach@1348
  3951
     * including supplementary characters.
jtulach@1348
  3952
     */
jtulach@1348
  3953
    static class SliceIS extends SliceNode {
jtulach@1348
  3954
        SliceIS(int[] buf) {
jtulach@1348
  3955
            super(buf);
jtulach@1348
  3956
        }
jtulach@1348
  3957
        int toLower(int c) {
jtulach@1348
  3958
            return ASCII.toLower(c);
jtulach@1348
  3959
        }
jtulach@1348
  3960
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  3961
            int[] buf = buffer;
jtulach@1348
  3962
            int x = i;
jtulach@1348
  3963
            for (int j = 0; j < buf.length; j++) {
jtulach@1348
  3964
                if (x >= matcher.to) {
jtulach@1348
  3965
                    matcher.hitEnd = true;
jtulach@1348
  3966
                    return false;
jtulach@1348
  3967
                }
jtulach@1348
  3968
                int c = Character.codePointAt(seq, x);
jtulach@1348
  3969
                if (buf[j] != c && buf[j] != toLower(c))
jtulach@1348
  3970
                    return false;
jtulach@1348
  3971
                x += Character.charCount(c);
jtulach@1348
  3972
                if (x > matcher.to) {
jtulach@1348
  3973
                    matcher.hitEnd = true;
jtulach@1348
  3974
                    return false;
jtulach@1348
  3975
                }
jtulach@1348
  3976
            }
jtulach@1348
  3977
            return next.match(matcher, x, seq);
jtulach@1348
  3978
        }
jtulach@1348
  3979
    }
jtulach@1348
  3980
jtulach@1348
  3981
    /**
jtulach@1348
  3982
     * Node class for a case insensitive sequence of literal characters.
jtulach@1348
  3983
     * Uses unicode case folding.
jtulach@1348
  3984
     */
jtulach@1348
  3985
    static final class SliceUS extends SliceIS {
jtulach@1348
  3986
        SliceUS(int[] buf) {
jtulach@1348
  3987
            super(buf);
jtulach@1348
  3988
        }
jtulach@1348
  3989
        int toLower(int c) {
jtulach@1348
  3990
            return Character.toLowerCase(Character.toUpperCase(c));
jtulach@1348
  3991
        }
jtulach@1348
  3992
    }
jtulach@1348
  3993
jtulach@1348
  3994
    private static boolean inRange(int lower, int ch, int upper) {
jtulach@1348
  3995
        return lower <= ch && ch <= upper;
jtulach@1348
  3996
    }
jtulach@1348
  3997
jtulach@1348
  3998
    /**
jtulach@1348
  3999
     * Returns node for matching characters within an explicit value range.
jtulach@1348
  4000
     */
jtulach@1348
  4001
    private static CharProperty rangeFor(final int lower,
jtulach@1348
  4002
                                         final int upper) {
jtulach@1348
  4003
        return new CharProperty() {
jtulach@1348
  4004
                boolean isSatisfiedBy(int ch) {
jtulach@1348
  4005
                    return inRange(lower, ch, upper);}};
jtulach@1348
  4006
    }
jtulach@1348
  4007
jtulach@1348
  4008
    /**
jtulach@1348
  4009
     * Returns node for matching characters within an explicit value
jtulach@1348
  4010
     * range in a case insensitive manner.
jtulach@1348
  4011
     */
jtulach@1348
  4012
    private CharProperty caseInsensitiveRangeFor(final int lower,
jtulach@1348
  4013
                                                 final int upper) {
jtulach@1348
  4014
        if (has(UNICODE_CASE))
jtulach@1348
  4015
            return new CharProperty() {
jtulach@1348
  4016
                boolean isSatisfiedBy(int ch) {
jtulach@1348
  4017
                    if (inRange(lower, ch, upper))
jtulach@1348
  4018
                        return true;
jtulach@1348
  4019
                    int up = Character.toUpperCase(ch);
jtulach@1348
  4020
                    return inRange(lower, up, upper) ||
jtulach@1348
  4021
                           inRange(lower, Character.toLowerCase(up), upper);}};
jtulach@1348
  4022
        return new CharProperty() {
jtulach@1348
  4023
            boolean isSatisfiedBy(int ch) {
jtulach@1348
  4024
                return inRange(lower, ch, upper) ||
jtulach@1348
  4025
                    ASCII.isAscii(ch) &&
jtulach@1348
  4026
                        (inRange(lower, ASCII.toUpper(ch), upper) ||
jtulach@1348
  4027
                         inRange(lower, ASCII.toLower(ch), upper));
jtulach@1348
  4028
            }};
jtulach@1348
  4029
    }
jtulach@1348
  4030
jtulach@1348
  4031
    /**
jtulach@1348
  4032
     * Implements the Unicode category ALL and the dot metacharacter when
jtulach@1348
  4033
     * in dotall mode.
jtulach@1348
  4034
     */
jtulach@1348
  4035
    static final class All extends CharProperty {
jtulach@1348
  4036
        boolean isSatisfiedBy(int ch) {
jtulach@1348
  4037
            return true;
jtulach@1348
  4038
        }
jtulach@1348
  4039
    }
jtulach@1348
  4040
jtulach@1348
  4041
    /**
jtulach@1348
  4042
     * Node class for the dot metacharacter when dotall is not enabled.
jtulach@1348
  4043
     */
jtulach@1348
  4044
    static final class Dot extends CharProperty {
jtulach@1348
  4045
        boolean isSatisfiedBy(int ch) {
jtulach@1348
  4046
            return (ch != '\n' && ch != '\r'
jtulach@1348
  4047
                    && (ch|1) != '\u2029'
jtulach@1348
  4048
                    && ch != '\u0085');
jtulach@1348
  4049
        }
jtulach@1348
  4050
    }
jtulach@1348
  4051
jtulach@1348
  4052
    /**
jtulach@1348
  4053
     * Node class for the dot metacharacter when dotall is not enabled
jtulach@1348
  4054
     * but UNIX_LINES is enabled.
jtulach@1348
  4055
     */
jtulach@1348
  4056
    static final class UnixDot extends CharProperty {
jtulach@1348
  4057
        boolean isSatisfiedBy(int ch) {
jtulach@1348
  4058
            return ch != '\n';
jtulach@1348
  4059
        }
jtulach@1348
  4060
    }
jtulach@1348
  4061
jtulach@1348
  4062
    /**
jtulach@1348
  4063
     * The 0 or 1 quantifier. This one class implements all three types.
jtulach@1348
  4064
     */
jtulach@1348
  4065
    static final class Ques extends Node {
jtulach@1348
  4066
        Node atom;
jtulach@1348
  4067
        int type;
jtulach@1348
  4068
        Ques(Node node, int type) {
jtulach@1348
  4069
            this.atom = node;
jtulach@1348
  4070
            this.type = type;
jtulach@1348
  4071
        }
jtulach@1348
  4072
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  4073
            switch (type) {
jtulach@1348
  4074
            case GREEDY:
jtulach@1348
  4075
                return (atom.match(matcher, i, seq) && next.match(matcher, matcher.last, seq))
jtulach@1348
  4076
                    || next.match(matcher, i, seq);
jtulach@1348
  4077
            case LAZY:
jtulach@1348
  4078
                return next.match(matcher, i, seq)
jtulach@1348
  4079
                    || (atom.match(matcher, i, seq) && next.match(matcher, matcher.last, seq));
jtulach@1348
  4080
            case POSSESSIVE:
jtulach@1348
  4081
                if (atom.match(matcher, i, seq)) i = matcher.last;
jtulach@1348
  4082
                return next.match(matcher, i, seq);
jtulach@1348
  4083
            default:
jtulach@1348
  4084
                return atom.match(matcher, i, seq) && next.match(matcher, matcher.last, seq);
jtulach@1348
  4085
            }
jtulach@1348
  4086
        }
jtulach@1348
  4087
        boolean study(TreeInfo info) {
jtulach@1348
  4088
            if (type != INDEPENDENT) {
jtulach@1348
  4089
                int minL = info.minLength;
jtulach@1348
  4090
                atom.study(info);
jtulach@1348
  4091
                info.minLength = minL;
jtulach@1348
  4092
                info.deterministic = false;
jtulach@1348
  4093
                return next.study(info);
jtulach@1348
  4094
            } else {
jtulach@1348
  4095
                atom.study(info);
jtulach@1348
  4096
                return next.study(info);
jtulach@1348
  4097
            }
jtulach@1348
  4098
        }
jtulach@1348
  4099
    }
jtulach@1348
  4100
jtulach@1348
  4101
    /**
jtulach@1348
  4102
     * Handles the curly-brace style repetition with a specified minimum and
jtulach@1348
  4103
     * maximum occurrences. The * quantifier is handled as a special case.
jtulach@1348
  4104
     * This class handles the three types.
jtulach@1348
  4105
     */
jtulach@1348
  4106
    static final class Curly extends Node {
jtulach@1348
  4107
        Node atom;
jtulach@1348
  4108
        int type;
jtulach@1348
  4109
        int cmin;
jtulach@1348
  4110
        int cmax;
jtulach@1348
  4111
jtulach@1348
  4112
        Curly(Node node, int cmin, int cmax, int type) {
jtulach@1348
  4113
            this.atom = node;
jtulach@1348
  4114
            this.type = type;
jtulach@1348
  4115
            this.cmin = cmin;
jtulach@1348
  4116
            this.cmax = cmax;
jtulach@1348
  4117
        }
jtulach@1348
  4118
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  4119
            int j;
jtulach@1348
  4120
            for (j = 0; j < cmin; j++) {
jtulach@1348
  4121
                if (atom.match(matcher, i, seq)) {
jtulach@1348
  4122
                    i = matcher.last;
jtulach@1348
  4123
                    continue;
jtulach@1348
  4124
                }
jtulach@1348
  4125
                return false;
jtulach@1348
  4126
            }
jtulach@1348
  4127
            if (type == GREEDY)
jtulach@1348
  4128
                return match0(matcher, i, j, seq);
jtulach@1348
  4129
            else if (type == LAZY)
jtulach@1348
  4130
                return match1(matcher, i, j, seq);
jtulach@1348
  4131
            else
jtulach@1348
  4132
                return match2(matcher, i, j, seq);
jtulach@1348
  4133
        }
jtulach@1348
  4134
        // Greedy match.
jtulach@1348
  4135
        // i is the index to start matching at
jtulach@1348
  4136
        // j is the number of atoms that have matched
jtulach@1348
  4137
        boolean match0(Matcher matcher, int i, int j, CharSequence seq) {
jtulach@1348
  4138
            if (j >= cmax) {
jtulach@1348
  4139
                // We have matched the maximum... continue with the rest of
jtulach@1348
  4140
                // the regular expression
jtulach@1348
  4141
                return next.match(matcher, i, seq);
jtulach@1348
  4142
            }
jtulach@1348
  4143
            int backLimit = j;
jtulach@1348
  4144
            while (atom.match(matcher, i, seq)) {
jtulach@1348
  4145
                // k is the length of this match
jtulach@1348
  4146
                int k = matcher.last - i;
jtulach@1348
  4147
                if (k == 0) // Zero length match
jtulach@1348
  4148
                    break;
jtulach@1348
  4149
                // Move up index and number matched
jtulach@1348
  4150
                i = matcher.last;
jtulach@1348
  4151
                j++;
jtulach@1348
  4152
                // We are greedy so match as many as we can
jtulach@1348
  4153
                while (j < cmax) {
jtulach@1348
  4154
                    if (!atom.match(matcher, i, seq))
jtulach@1348
  4155
                        break;
jtulach@1348
  4156
                    if (i + k != matcher.last) {
jtulach@1348
  4157
                        if (match0(matcher, matcher.last, j+1, seq))
jtulach@1348
  4158
                            return true;
jtulach@1348
  4159
                        break;
jtulach@1348
  4160
                    }
jtulach@1348
  4161
                    i += k;
jtulach@1348
  4162
                    j++;
jtulach@1348
  4163
                }
jtulach@1348
  4164
                // Handle backing off if match fails
jtulach@1348
  4165
                while (j >= backLimit) {
jtulach@1348
  4166
                   if (next.match(matcher, i, seq))
jtulach@1348
  4167
                        return true;
jtulach@1348
  4168
                    i -= k;
jtulach@1348
  4169
                    j--;
jtulach@1348
  4170
                }
jtulach@1348
  4171
                return false;
jtulach@1348
  4172
            }
jtulach@1348
  4173
            return next.match(matcher, i, seq);
jtulach@1348
  4174
        }
jtulach@1348
  4175
        // Reluctant match. At this point, the minimum has been satisfied.
jtulach@1348
  4176
        // i is the index to start matching at
jtulach@1348
  4177
        // j is the number of atoms that have matched
jtulach@1348
  4178
        boolean match1(Matcher matcher, int i, int j, CharSequence seq) {
jtulach@1348
  4179
            for (;;) {
jtulach@1348
  4180
                // Try finishing match without consuming any more
jtulach@1348
  4181
                if (next.match(matcher, i, seq))
jtulach@1348
  4182
                    return true;
jtulach@1348
  4183
                // At the maximum, no match found
jtulach@1348
  4184
                if (j >= cmax)
jtulach@1348
  4185
                    return false;
jtulach@1348
  4186
                // Okay, must try one more atom
jtulach@1348
  4187
                if (!atom.match(matcher, i, seq))
jtulach@1348
  4188
                    return false;
jtulach@1348
  4189
                // If we haven't moved forward then must break out
jtulach@1348
  4190
                if (i == matcher.last)
jtulach@1348
  4191
                    return false;
jtulach@1348
  4192
                // Move up index and number matched
jtulach@1348
  4193
                i = matcher.last;
jtulach@1348
  4194
                j++;
jtulach@1348
  4195
            }
jtulach@1348
  4196
        }
jtulach@1348
  4197
        boolean match2(Matcher matcher, int i, int j, CharSequence seq) {
jtulach@1348
  4198
            for (; j < cmax; j++) {
jtulach@1348
  4199
                if (!atom.match(matcher, i, seq))
jtulach@1348
  4200
                    break;
jtulach@1348
  4201
                if (i == matcher.last)
jtulach@1348
  4202
                    break;
jtulach@1348
  4203
                i = matcher.last;
jtulach@1348
  4204
            }
jtulach@1348
  4205
            return next.match(matcher, i, seq);
jtulach@1348
  4206
        }
jtulach@1348
  4207
        boolean study(TreeInfo info) {
jtulach@1348
  4208
            // Save original info
jtulach@1348
  4209
            int minL = info.minLength;
jtulach@1348
  4210
            int maxL = info.maxLength;
jtulach@1348
  4211
            boolean maxV = info.maxValid;
jtulach@1348
  4212
            boolean detm = info.deterministic;
jtulach@1348
  4213
            info.reset();
jtulach@1348
  4214
jtulach@1348
  4215
            atom.study(info);
jtulach@1348
  4216
jtulach@1348
  4217
            int temp = info.minLength * cmin + minL;
jtulach@1348
  4218
            if (temp < minL) {
jtulach@1348
  4219
                temp = 0xFFFFFFF; // arbitrary large number
jtulach@1348
  4220
            }
jtulach@1348
  4221
            info.minLength = temp;
jtulach@1348
  4222
jtulach@1348
  4223
            if (maxV & info.maxValid) {
jtulach@1348
  4224
                temp = info.maxLength * cmax + maxL;
jtulach@1348
  4225
                info.maxLength = temp;
jtulach@1348
  4226
                if (temp < maxL) {
jtulach@1348
  4227
                    info.maxValid = false;
jtulach@1348
  4228
                }
jtulach@1348
  4229
            } else {
jtulach@1348
  4230
                info.maxValid = false;
jtulach@1348
  4231
            }
jtulach@1348
  4232
jtulach@1348
  4233
            if (info.deterministic && cmin == cmax)
jtulach@1348
  4234
                info.deterministic = detm;
jtulach@1348
  4235
            else
jtulach@1348
  4236
                info.deterministic = false;
jtulach@1348
  4237
jtulach@1348
  4238
            return next.study(info);
jtulach@1348
  4239
        }
jtulach@1348
  4240
    }
jtulach@1348
  4241
jtulach@1348
  4242
    /**
jtulach@1348
  4243
     * Handles the curly-brace style repetition with a specified minimum and
jtulach@1348
  4244
     * maximum occurrences in deterministic cases. This is an iterative
jtulach@1348
  4245
     * optimization over the Prolog and Loop system which would handle this
jtulach@1348
  4246
     * in a recursive way. The * quantifier is handled as a special case.
jtulach@1348
  4247
     * If capture is true then this class saves group settings and ensures
jtulach@1348
  4248
     * that groups are unset when backing off of a group match.
jtulach@1348
  4249
     */
jtulach@1348
  4250
    static final class GroupCurly extends Node {
jtulach@1348
  4251
        Node atom;
jtulach@1348
  4252
        int type;
jtulach@1348
  4253
        int cmin;
jtulach@1348
  4254
        int cmax;
jtulach@1348
  4255
        int localIndex;
jtulach@1348
  4256
        int groupIndex;
jtulach@1348
  4257
        boolean capture;
jtulach@1348
  4258
jtulach@1348
  4259
        GroupCurly(Node node, int cmin, int cmax, int type, int local,
jtulach@1348
  4260
                   int group, boolean capture) {
jtulach@1348
  4261
            this.atom = node;
jtulach@1348
  4262
            this.type = type;
jtulach@1348
  4263
            this.cmin = cmin;
jtulach@1348
  4264
            this.cmax = cmax;
jtulach@1348
  4265
            this.localIndex = local;
jtulach@1348
  4266
            this.groupIndex = group;
jtulach@1348
  4267
            this.capture = capture;
jtulach@1348
  4268
        }
jtulach@1348
  4269
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  4270
            int[] groups = matcher.groups;
jtulach@1348
  4271
            int[] locals = matcher.locals;
jtulach@1348
  4272
            int save0 = locals[localIndex];
jtulach@1348
  4273
            int save1 = 0;
jtulach@1348
  4274
            int save2 = 0;
jtulach@1348
  4275
jtulach@1348
  4276
            if (capture) {
jtulach@1348
  4277
                save1 = groups[groupIndex];
jtulach@1348
  4278
                save2 = groups[groupIndex+1];
jtulach@1348
  4279
            }
jtulach@1348
  4280
jtulach@1348
  4281
            // Notify GroupTail there is no need to setup group info
jtulach@1348
  4282
            // because it will be set here
jtulach@1348
  4283
            locals[localIndex] = -1;
jtulach@1348
  4284
jtulach@1348
  4285
            boolean ret = true;
jtulach@1348
  4286
            for (int j = 0; j < cmin; j++) {
jtulach@1348
  4287
                if (atom.match(matcher, i, seq)) {
jtulach@1348
  4288
                    if (capture) {
jtulach@1348
  4289
                        groups[groupIndex] = i;
jtulach@1348
  4290
                        groups[groupIndex+1] = matcher.last;
jtulach@1348
  4291
                    }
jtulach@1348
  4292
                    i = matcher.last;
jtulach@1348
  4293
                } else {
jtulach@1348
  4294
                    ret = false;
jtulach@1348
  4295
                    break;
jtulach@1348
  4296
                }
jtulach@1348
  4297
            }
jtulach@1348
  4298
            if (ret) {
jtulach@1348
  4299
                if (type == GREEDY) {
jtulach@1348
  4300
                    ret = match0(matcher, i, cmin, seq);
jtulach@1348
  4301
                } else if (type == LAZY) {
jtulach@1348
  4302
                    ret = match1(matcher, i, cmin, seq);
jtulach@1348
  4303
                } else {
jtulach@1348
  4304
                    ret = match2(matcher, i, cmin, seq);
jtulach@1348
  4305
                }
jtulach@1348
  4306
            }
jtulach@1348
  4307
            if (!ret) {
jtulach@1348
  4308
                locals[localIndex] = save0;
jtulach@1348
  4309
                if (capture) {
jtulach@1348
  4310
                    groups[groupIndex] = save1;
jtulach@1348
  4311
                    groups[groupIndex+1] = save2;
jtulach@1348
  4312
                }
jtulach@1348
  4313
            }
jtulach@1348
  4314
            return ret;
jtulach@1348
  4315
        }
jtulach@1348
  4316
        // Aggressive group match
jtulach@1348
  4317
        boolean match0(Matcher matcher, int i, int j, CharSequence seq) {
jtulach@1348
  4318
            int[] groups = matcher.groups;
jtulach@1348
  4319
            int save0 = 0;
jtulach@1348
  4320
            int save1 = 0;
jtulach@1348
  4321
            if (capture) {
jtulach@1348
  4322
                save0 = groups[groupIndex];
jtulach@1348
  4323
                save1 = groups[groupIndex+1];
jtulach@1348
  4324
            }
jtulach@1348
  4325
            for (;;) {
jtulach@1348
  4326
                if (j >= cmax)
jtulach@1348
  4327
                    break;
jtulach@1348
  4328
                if (!atom.match(matcher, i, seq))
jtulach@1348
  4329
                    break;
jtulach@1348
  4330
                int k = matcher.last - i;
jtulach@1348
  4331
                if (k <= 0) {
jtulach@1348
  4332
                    if (capture) {
jtulach@1348
  4333
                        groups[groupIndex] = i;
jtulach@1348
  4334
                        groups[groupIndex+1] = i + k;
jtulach@1348
  4335
                    }
jtulach@1348
  4336
                    i = i + k;
jtulach@1348
  4337
                    break;
jtulach@1348
  4338
                }
jtulach@1348
  4339
                for (;;) {
jtulach@1348
  4340
                    if (capture) {
jtulach@1348
  4341
                        groups[groupIndex] = i;
jtulach@1348
  4342
                        groups[groupIndex+1] = i + k;
jtulach@1348
  4343
                    }
jtulach@1348
  4344
                    i = i + k;
jtulach@1348
  4345
                    if (++j >= cmax)
jtulach@1348
  4346
                        break;
jtulach@1348
  4347
                    if (!atom.match(matcher, i, seq))
jtulach@1348
  4348
                        break;
jtulach@1348
  4349
                    if (i + k != matcher.last) {
jtulach@1348
  4350
                        if (match0(matcher, i, j, seq))
jtulach@1348
  4351
                            return true;
jtulach@1348
  4352
                        break;
jtulach@1348
  4353
                    }
jtulach@1348
  4354
                }
jtulach@1348
  4355
                while (j > cmin) {
jtulach@1348
  4356
                    if (next.match(matcher, i, seq)) {
jtulach@1348
  4357
                        if (capture) {
jtulach@1348
  4358
                            groups[groupIndex+1] = i;
jtulach@1348
  4359
                            groups[groupIndex] = i - k;
jtulach@1348
  4360
                        }
jtulach@1348
  4361
                        i = i - k;
jtulach@1348
  4362
                        return true;
jtulach@1348
  4363
                    }
jtulach@1348
  4364
                    // backing off
jtulach@1348
  4365
                    if (capture) {
jtulach@1348
  4366
                        groups[groupIndex+1] = i;
jtulach@1348
  4367
                        groups[groupIndex] = i - k;
jtulach@1348
  4368
                    }
jtulach@1348
  4369
                    i = i - k;
jtulach@1348
  4370
                    j--;
jtulach@1348
  4371
                }
jtulach@1348
  4372
                break;
jtulach@1348
  4373
            }
jtulach@1348
  4374
            if (capture) {
jtulach@1348
  4375
                groups[groupIndex] = save0;
jtulach@1348
  4376
                groups[groupIndex+1] = save1;
jtulach@1348
  4377
            }
jtulach@1348
  4378
            return next.match(matcher, i, seq);
jtulach@1348
  4379
        }
jtulach@1348
  4380
        // Reluctant matching
jtulach@1348
  4381
        boolean match1(Matcher matcher, int i, int j, CharSequence seq) {
jtulach@1348
  4382
            for (;;) {
jtulach@1348
  4383
                if (next.match(matcher, i, seq))
jtulach@1348
  4384
                    return true;
jtulach@1348
  4385
                if (j >= cmax)
jtulach@1348
  4386
                    return false;
jtulach@1348
  4387
                if (!atom.match(matcher, i, seq))
jtulach@1348
  4388
                    return false;
jtulach@1348
  4389
                if (i == matcher.last)
jtulach@1348
  4390
                    return false;
jtulach@1348
  4391
                if (capture) {
jtulach@1348
  4392
                    matcher.groups[groupIndex] = i;
jtulach@1348
  4393
                    matcher.groups[groupIndex+1] = matcher.last;
jtulach@1348
  4394
                }
jtulach@1348
  4395
                i = matcher.last;
jtulach@1348
  4396
                j++;
jtulach@1348
  4397
            }
jtulach@1348
  4398
        }
jtulach@1348
  4399
        // Possessive matching
jtulach@1348
  4400
        boolean match2(Matcher matcher, int i, int j, CharSequence seq) {
jtulach@1348
  4401
            for (; j < cmax; j++) {
jtulach@1348
  4402
                if (!atom.match(matcher, i, seq)) {
jtulach@1348
  4403
                    break;
jtulach@1348
  4404
                }
jtulach@1348
  4405
                if (capture) {
jtulach@1348
  4406
                    matcher.groups[groupIndex] = i;
jtulach@1348
  4407
                    matcher.groups[groupIndex+1] = matcher.last;
jtulach@1348
  4408
                }
jtulach@1348
  4409
                if (i == matcher.last) {
jtulach@1348
  4410
                    break;
jtulach@1348
  4411
                }
jtulach@1348
  4412
                i = matcher.last;
jtulach@1348
  4413
            }
jtulach@1348
  4414
            return next.match(matcher, i, seq);
jtulach@1348
  4415
        }
jtulach@1348
  4416
        boolean study(TreeInfo info) {
jtulach@1348
  4417
            // Save original info
jtulach@1348
  4418
            int minL = info.minLength;
jtulach@1348
  4419
            int maxL = info.maxLength;
jtulach@1348
  4420
            boolean maxV = info.maxValid;
jtulach@1348
  4421
            boolean detm = info.deterministic;
jtulach@1348
  4422
            info.reset();
jtulach@1348
  4423
jtulach@1348
  4424
            atom.study(info);
jtulach@1348
  4425
jtulach@1348
  4426
            int temp = info.minLength * cmin + minL;
jtulach@1348
  4427
            if (temp < minL) {
jtulach@1348
  4428
                temp = 0xFFFFFFF; // Arbitrary large number
jtulach@1348
  4429
            }
jtulach@1348
  4430
            info.minLength = temp;
jtulach@1348
  4431
jtulach@1348
  4432
            if (maxV & info.maxValid) {
jtulach@1348
  4433
                temp = info.maxLength * cmax + maxL;
jtulach@1348
  4434
                info.maxLength = temp;
jtulach@1348
  4435
                if (temp < maxL) {
jtulach@1348
  4436
                    info.maxValid = false;
jtulach@1348
  4437
                }
jtulach@1348
  4438
            } else {
jtulach@1348
  4439
                info.maxValid = false;
jtulach@1348
  4440
            }
jtulach@1348
  4441
jtulach@1348
  4442
            if (info.deterministic && cmin == cmax) {
jtulach@1348
  4443
                info.deterministic = detm;
jtulach@1348
  4444
            } else {
jtulach@1348
  4445
                info.deterministic = false;
jtulach@1348
  4446
            }
jtulach@1348
  4447
jtulach@1348
  4448
            return next.study(info);
jtulach@1348
  4449
        }
jtulach@1348
  4450
    }
jtulach@1348
  4451
jtulach@1348
  4452
    /**
jtulach@1348
  4453
     * A Guard node at the end of each atom node in a Branch. It
jtulach@1348
  4454
     * serves the purpose of chaining the "match" operation to
jtulach@1348
  4455
     * "next" but not the "study", so we can collect the TreeInfo
jtulach@1348
  4456
     * of each atom node without including the TreeInfo of the
jtulach@1348
  4457
     * "next".
jtulach@1348
  4458
     */
jtulach@1348
  4459
    static final class BranchConn extends Node {
jtulach@1348
  4460
        BranchConn() {};
jtulach@1348
  4461
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  4462
            return next.match(matcher, i, seq);
jtulach@1348
  4463
        }
jtulach@1348
  4464
        boolean study(TreeInfo info) {
jtulach@1348
  4465
            return info.deterministic;
jtulach@1348
  4466
        }
jtulach@1348
  4467
    }
jtulach@1348
  4468
jtulach@1348
  4469
    /**
jtulach@1348
  4470
     * Handles the branching of alternations. Note this is also used for
jtulach@1348
  4471
     * the ? quantifier to branch between the case where it matches once
jtulach@1348
  4472
     * and where it does not occur.
jtulach@1348
  4473
     */
jtulach@1348
  4474
    static final class Branch extends Node {
jtulach@1348
  4475
        Node[] atoms = new Node[2];
jtulach@1348
  4476
        int size = 2;
jtulach@1348
  4477
        Node conn;
jtulach@1348
  4478
        Branch(Node first, Node second, Node branchConn) {
jtulach@1348
  4479
            conn = branchConn;
jtulach@1348
  4480
            atoms[0] = first;
jtulach@1348
  4481
            atoms[1] = second;
jtulach@1348
  4482
        }
jtulach@1348
  4483
jtulach@1348
  4484
        void add(Node node) {
jtulach@1348
  4485
            if (size >= atoms.length) {
jtulach@1348
  4486
                Node[] tmp = new Node[atoms.length*2];
jtulach@1348
  4487
                System.arraycopy(atoms, 0, tmp, 0, atoms.length);
jtulach@1348
  4488
                atoms = tmp;
jtulach@1348
  4489
            }
jtulach@1348
  4490
            atoms[size++] = node;
jtulach@1348
  4491
        }
jtulach@1348
  4492
jtulach@1348
  4493
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  4494
            for (int n = 0; n < size; n++) {
jtulach@1348
  4495
                if (atoms[n] == null) {
jtulach@1348
  4496
                    if (conn.next.match(matcher, i, seq))
jtulach@1348
  4497
                        return true;
jtulach@1348
  4498
                } else if (atoms[n].match(matcher, i, seq)) {
jtulach@1348
  4499
                    return true;
jtulach@1348
  4500
                }
jtulach@1348
  4501
            }
jtulach@1348
  4502
            return false;
jtulach@1348
  4503
        }
jtulach@1348
  4504
jtulach@1348
  4505
        boolean study(TreeInfo info) {
jtulach@1348
  4506
            int minL = info.minLength;
jtulach@1348
  4507
            int maxL = info.maxLength;
jtulach@1348
  4508
            boolean maxV = info.maxValid;
jtulach@1348
  4509
jtulach@1348
  4510
            int minL2 = Integer.MAX_VALUE; //arbitrary large enough num
jtulach@1348
  4511
            int maxL2 = -1;
jtulach@1348
  4512
            for (int n = 0; n < size; n++) {
jtulach@1348
  4513
                info.reset();
jtulach@1348
  4514
                if (atoms[n] != null)
jtulach@1348
  4515
                    atoms[n].study(info);
jtulach@1348
  4516
                minL2 = Math.min(minL2, info.minLength);
jtulach@1348
  4517
                maxL2 = Math.max(maxL2, info.maxLength);
jtulach@1348
  4518
                maxV = (maxV & info.maxValid);
jtulach@1348
  4519
            }
jtulach@1348
  4520
jtulach@1348
  4521
            minL += minL2;
jtulach@1348
  4522
            maxL += maxL2;
jtulach@1348
  4523
jtulach@1348
  4524
            info.reset();
jtulach@1348
  4525
            conn.next.study(info);
jtulach@1348
  4526
jtulach@1348
  4527
            info.minLength += minL;
jtulach@1348
  4528
            info.maxLength += maxL;
jtulach@1348
  4529
            info.maxValid &= maxV;
jtulach@1348
  4530
            info.deterministic = false;
jtulach@1348
  4531
            return false;
jtulach@1348
  4532
        }
jtulach@1348
  4533
    }
jtulach@1348
  4534
jtulach@1348
  4535
    /**
jtulach@1348
  4536
     * The GroupHead saves the location where the group begins in the locals
jtulach@1348
  4537
     * and restores them when the match is done.
jtulach@1348
  4538
     *
jtulach@1348
  4539
     * The matchRef is used when a reference to this group is accessed later
jtulach@1348
  4540
     * in the expression. The locals will have a negative value in them to
jtulach@1348
  4541
     * indicate that we do not want to unset the group if the reference
jtulach@1348
  4542
     * doesn't match.
jtulach@1348
  4543
     */
jtulach@1348
  4544
    static final class GroupHead extends Node {
jtulach@1348
  4545
        int localIndex;
jtulach@1348
  4546
        GroupHead(int localCount) {
jtulach@1348
  4547
            localIndex = localCount;
jtulach@1348
  4548
        }
jtulach@1348
  4549
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  4550
            int save = matcher.locals[localIndex];
jtulach@1348
  4551
            matcher.locals[localIndex] = i;
jtulach@1348
  4552
            boolean ret = next.match(matcher, i, seq);
jtulach@1348
  4553
            matcher.locals[localIndex] = save;
jtulach@1348
  4554
            return ret;
jtulach@1348
  4555
        }
jtulach@1348
  4556
        boolean matchRef(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  4557
            int save = matcher.locals[localIndex];
jtulach@1348
  4558
            matcher.locals[localIndex] = ~i; // HACK
jtulach@1348
  4559
            boolean ret = next.match(matcher, i, seq);
jtulach@1348
  4560
            matcher.locals[localIndex] = save;
jtulach@1348
  4561
            return ret;
jtulach@1348
  4562
        }
jtulach@1348
  4563
    }
jtulach@1348
  4564
jtulach@1348
  4565
    /**
jtulach@1348
  4566
     * Recursive reference to a group in the regular expression. It calls
jtulach@1348
  4567
     * matchRef because if the reference fails to match we would not unset
jtulach@1348
  4568
     * the group.
jtulach@1348
  4569
     */
jtulach@1348
  4570
    static final class GroupRef extends Node {
jtulach@1348
  4571
        GroupHead head;
jtulach@1348
  4572
        GroupRef(GroupHead head) {
jtulach@1348
  4573
            this.head = head;
jtulach@1348
  4574
        }
jtulach@1348
  4575
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  4576
            return head.matchRef(matcher, i, seq)
jtulach@1348
  4577
                && next.match(matcher, matcher.last, seq);
jtulach@1348
  4578
        }
jtulach@1348
  4579
        boolean study(TreeInfo info) {
jtulach@1348
  4580
            info.maxValid = false;
jtulach@1348
  4581
            info.deterministic = false;
jtulach@1348
  4582
            return next.study(info);
jtulach@1348
  4583
        }
jtulach@1348
  4584
    }
jtulach@1348
  4585
jtulach@1348
  4586
    /**
jtulach@1348
  4587
     * The GroupTail handles the setting of group beginning and ending
jtulach@1348
  4588
     * locations when groups are successfully matched. It must also be able to
jtulach@1348
  4589
     * unset groups that have to be backed off of.
jtulach@1348
  4590
     *
jtulach@1348
  4591
     * The GroupTail node is also used when a previous group is referenced,
jtulach@1348
  4592
     * and in that case no group information needs to be set.
jtulach@1348
  4593
     */
jtulach@1348
  4594
    static final class GroupTail extends Node {
jtulach@1348
  4595
        int localIndex;
jtulach@1348
  4596
        int groupIndex;
jtulach@1348
  4597
        GroupTail(int localCount, int groupCount) {
jtulach@1348
  4598
            localIndex = localCount;
jtulach@1348
  4599
            groupIndex = groupCount + groupCount;
jtulach@1348
  4600
        }
jtulach@1348
  4601
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  4602
            int tmp = matcher.locals[localIndex];
jtulach@1348
  4603
            if (tmp >= 0) { // This is the normal group case.
jtulach@1348
  4604
                // Save the group so we can unset it if it
jtulach@1348
  4605
                // backs off of a match.
jtulach@1348
  4606
                int groupStart = matcher.groups[groupIndex];
jtulach@1348
  4607
                int groupEnd = matcher.groups[groupIndex+1];
jtulach@1348
  4608
jtulach@1348
  4609
                matcher.groups[groupIndex] = tmp;
jtulach@1348
  4610
                matcher.groups[groupIndex+1] = i;
jtulach@1348
  4611
                if (next.match(matcher, i, seq)) {
jtulach@1348
  4612
                    return true;
jtulach@1348
  4613
                }
jtulach@1348
  4614
                matcher.groups[groupIndex] = groupStart;
jtulach@1348
  4615
                matcher.groups[groupIndex+1] = groupEnd;
jtulach@1348
  4616
                return false;
jtulach@1348
  4617
            } else {
jtulach@1348
  4618
                // This is a group reference case. We don't need to save any
jtulach@1348
  4619
                // group info because it isn't really a group.
jtulach@1348
  4620
                matcher.last = i;
jtulach@1348
  4621
                return true;
jtulach@1348
  4622
            }
jtulach@1348
  4623
        }
jtulach@1348
  4624
    }
jtulach@1348
  4625
jtulach@1348
  4626
    /**
jtulach@1348
  4627
     * This sets up a loop to handle a recursive quantifier structure.
jtulach@1348
  4628
     */
jtulach@1348
  4629
    static final class Prolog extends Node {
jtulach@1348
  4630
        Loop loop;
jtulach@1348
  4631
        Prolog(Loop loop) {
jtulach@1348
  4632
            this.loop = loop;
jtulach@1348
  4633
        }
jtulach@1348
  4634
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  4635
            return loop.matchInit(matcher, i, seq);
jtulach@1348
  4636
        }
jtulach@1348
  4637
        boolean study(TreeInfo info) {
jtulach@1348
  4638
            return loop.study(info);
jtulach@1348
  4639
        }
jtulach@1348
  4640
    }
jtulach@1348
  4641
jtulach@1348
  4642
    /**
jtulach@1348
  4643
     * Handles the repetition count for a greedy Curly. The matchInit
jtulach@1348
  4644
     * is called from the Prolog to save the index of where the group
jtulach@1348
  4645
     * beginning is stored. A zero length group check occurs in the
jtulach@1348
  4646
     * normal match but is skipped in the matchInit.
jtulach@1348
  4647
     */
jtulach@1348
  4648
    static class Loop extends Node {
jtulach@1348
  4649
        Node body;
jtulach@1348
  4650
        int countIndex; // local count index in matcher locals
jtulach@1348
  4651
        int beginIndex; // group beginning index
jtulach@1348
  4652
        int cmin, cmax;
jtulach@1348
  4653
        Loop(int countIndex, int beginIndex) {
jtulach@1348
  4654
            this.countIndex = countIndex;
jtulach@1348
  4655
            this.beginIndex = beginIndex;
jtulach@1348
  4656
        }
jtulach@1348
  4657
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  4658
            // Avoid infinite loop in zero-length case.
jtulach@1348
  4659
            if (i > matcher.locals[beginIndex]) {
jtulach@1348
  4660
                int count = matcher.locals[countIndex];
jtulach@1348
  4661
jtulach@1348
  4662
                // This block is for before we reach the minimum
jtulach@1348
  4663
                // iterations required for the loop to match
jtulach@1348
  4664
                if (count < cmin) {
jtulach@1348
  4665
                    matcher.locals[countIndex] = count + 1;
jtulach@1348
  4666
                    boolean b = body.match(matcher, i, seq);
jtulach@1348
  4667
                    // If match failed we must backtrack, so
jtulach@1348
  4668
                    // the loop count should NOT be incremented
jtulach@1348
  4669
                    if (!b)
jtulach@1348
  4670
                        matcher.locals[countIndex] = count;
jtulach@1348
  4671
                    // Return success or failure since we are under
jtulach@1348
  4672
                    // minimum
jtulach@1348
  4673
                    return b;
jtulach@1348
  4674
                }
jtulach@1348
  4675
                // This block is for after we have the minimum
jtulach@1348
  4676
                // iterations required for the loop to match
jtulach@1348
  4677
                if (count < cmax) {
jtulach@1348
  4678
                    matcher.locals[countIndex] = count + 1;
jtulach@1348
  4679
                    boolean b = body.match(matcher, i, seq);
jtulach@1348
  4680
                    // If match failed we must backtrack, so
jtulach@1348
  4681
                    // the loop count should NOT be incremented
jtulach@1348
  4682
                    if (!b)
jtulach@1348
  4683
                        matcher.locals[countIndex] = count;
jtulach@1348
  4684
                    else
jtulach@1348
  4685
                        return true;
jtulach@1348
  4686
                }
jtulach@1348
  4687
            }
jtulach@1348
  4688
            return next.match(matcher, i, seq);
jtulach@1348
  4689
        }
jtulach@1348
  4690
        boolean matchInit(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  4691
            int save = matcher.locals[countIndex];
jtulach@1348
  4692
            boolean ret = false;
jtulach@1348
  4693
            if (0 < cmin) {
jtulach@1348
  4694
                matcher.locals[countIndex] = 1;
jtulach@1348
  4695
                ret = body.match(matcher, i, seq);
jtulach@1348
  4696
            } else if (0 < cmax) {
jtulach@1348
  4697
                matcher.locals[countIndex] = 1;
jtulach@1348
  4698
                ret = body.match(matcher, i, seq);
jtulach@1348
  4699
                if (ret == false)
jtulach@1348
  4700
                    ret = next.match(matcher, i, seq);
jtulach@1348
  4701
            } else {
jtulach@1348
  4702
                ret = next.match(matcher, i, seq);
jtulach@1348
  4703
            }
jtulach@1348
  4704
            matcher.locals[countIndex] = save;
jtulach@1348
  4705
            return ret;
jtulach@1348
  4706
        }
jtulach@1348
  4707
        boolean study(TreeInfo info) {
jtulach@1348
  4708
            info.maxValid = false;
jtulach@1348
  4709
            info.deterministic = false;
jtulach@1348
  4710
            return false;
jtulach@1348
  4711
        }
jtulach@1348
  4712
    }
jtulach@1348
  4713
jtulach@1348
  4714
    /**
jtulach@1348
  4715
     * Handles the repetition count for a reluctant Curly. The matchInit
jtulach@1348
  4716
     * is called from the Prolog to save the index of where the group
jtulach@1348
  4717
     * beginning is stored. A zero length group check occurs in the
jtulach@1348
  4718
     * normal match but is skipped in the matchInit.
jtulach@1348
  4719
     */
jtulach@1348
  4720
    static final class LazyLoop extends Loop {
jtulach@1348
  4721
        LazyLoop(int countIndex, int beginIndex) {
jtulach@1348
  4722
            super(countIndex, beginIndex);
jtulach@1348
  4723
        }
jtulach@1348
  4724
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  4725
            // Check for zero length group
jtulach@1348
  4726
            if (i > matcher.locals[beginIndex]) {
jtulach@1348
  4727
                int count = matcher.locals[countIndex];
jtulach@1348
  4728
                if (count < cmin) {
jtulach@1348
  4729
                    matcher.locals[countIndex] = count + 1;
jtulach@1348
  4730
                    boolean result = body.match(matcher, i, seq);
jtulach@1348
  4731
                    // If match failed we must backtrack, so
jtulach@1348
  4732
                    // the loop count should NOT be incremented
jtulach@1348
  4733
                    if (!result)
jtulach@1348
  4734
                        matcher.locals[countIndex] = count;
jtulach@1348
  4735
                    return result;
jtulach@1348
  4736
                }
jtulach@1348
  4737
                if (next.match(matcher, i, seq))
jtulach@1348
  4738
                    return true;
jtulach@1348
  4739
                if (count < cmax) {
jtulach@1348
  4740
                    matcher.locals[countIndex] = count + 1;
jtulach@1348
  4741
                    boolean result = body.match(matcher, i, seq);
jtulach@1348
  4742
                    // If match failed we must backtrack, so
jtulach@1348
  4743
                    // the loop count should NOT be incremented
jtulach@1348
  4744
                    if (!result)
jtulach@1348
  4745
                        matcher.locals[countIndex] = count;
jtulach@1348
  4746
                    return result;
jtulach@1348
  4747
                }
jtulach@1348
  4748
                return false;
jtulach@1348
  4749
            }
jtulach@1348
  4750
            return next.match(matcher, i, seq);
jtulach@1348
  4751
        }
jtulach@1348
  4752
        boolean matchInit(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  4753
            int save = matcher.locals[countIndex];
jtulach@1348
  4754
            boolean ret = false;
jtulach@1348
  4755
            if (0 < cmin) {
jtulach@1348
  4756
                matcher.locals[countIndex] = 1;
jtulach@1348
  4757
                ret = body.match(matcher, i, seq);
jtulach@1348
  4758
            } else if (next.match(matcher, i, seq)) {
jtulach@1348
  4759
                ret = true;
jtulach@1348
  4760
            } else if (0 < cmax) {
jtulach@1348
  4761
                matcher.locals[countIndex] = 1;
jtulach@1348
  4762
                ret = body.match(matcher, i, seq);
jtulach@1348
  4763
            }
jtulach@1348
  4764
            matcher.locals[countIndex] = save;
jtulach@1348
  4765
            return ret;
jtulach@1348
  4766
        }
jtulach@1348
  4767
        boolean study(TreeInfo info) {
jtulach@1348
  4768
            info.maxValid = false;
jtulach@1348
  4769
            info.deterministic = false;
jtulach@1348
  4770
            return false;
jtulach@1348
  4771
        }
jtulach@1348
  4772
    }
jtulach@1348
  4773
jtulach@1348
  4774
    /**
jtulach@1348
  4775
     * Refers to a group in the regular expression. Attempts to match
jtulach@1348
  4776
     * whatever the group referred to last matched.
jtulach@1348
  4777
     */
jtulach@1348
  4778
    static class BackRef extends Node {
jtulach@1348
  4779
        int groupIndex;
jtulach@1348
  4780
        BackRef(int groupCount) {
jtulach@1348
  4781
            super();
jtulach@1348
  4782
            groupIndex = groupCount + groupCount;
jtulach@1348
  4783
        }
jtulach@1348
  4784
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  4785
            int j = matcher.groups[groupIndex];
jtulach@1348
  4786
            int k = matcher.groups[groupIndex+1];
jtulach@1348
  4787
jtulach@1348
  4788
            int groupSize = k - j;
jtulach@1348
  4789
jtulach@1348
  4790
            // If the referenced group didn't match, neither can this
jtulach@1348
  4791
            if (j < 0)
jtulach@1348
  4792
                return false;
jtulach@1348
  4793
jtulach@1348
  4794
            // If there isn't enough input left no match
jtulach@1348
  4795
            if (i + groupSize > matcher.to) {
jtulach@1348
  4796
                matcher.hitEnd = true;
jtulach@1348
  4797
                return false;
jtulach@1348
  4798
            }
jtulach@1348
  4799
jtulach@1348
  4800
            // Check each new char to make sure it matches what the group
jtulach@1348
  4801
            // referenced matched last time around
jtulach@1348
  4802
            for (int index=0; index<groupSize; index++)
jtulach@1348
  4803
                if (seq.charAt(i+index) != seq.charAt(j+index))
jtulach@1348
  4804
                    return false;
jtulach@1348
  4805
jtulach@1348
  4806
            return next.match(matcher, i+groupSize, seq);
jtulach@1348
  4807
        }
jtulach@1348
  4808
        boolean study(TreeInfo info) {
jtulach@1348
  4809
            info.maxValid = false;
jtulach@1348
  4810
            return next.study(info);
jtulach@1348
  4811
        }
jtulach@1348
  4812
    }
jtulach@1348
  4813
jtulach@1348
  4814
    static class CIBackRef extends Node {
jtulach@1348
  4815
        int groupIndex;
jtulach@1348
  4816
        boolean doUnicodeCase;
jtulach@1348
  4817
        CIBackRef(int groupCount, boolean doUnicodeCase) {
jtulach@1348
  4818
            super();
jtulach@1348
  4819
            groupIndex = groupCount + groupCount;
jtulach@1348
  4820
            this.doUnicodeCase = doUnicodeCase;
jtulach@1348
  4821
        }
jtulach@1348
  4822
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  4823
            int j = matcher.groups[groupIndex];
jtulach@1348
  4824
            int k = matcher.groups[groupIndex+1];
jtulach@1348
  4825
jtulach@1348
  4826
            int groupSize = k - j;
jtulach@1348
  4827
jtulach@1348
  4828
            // If the referenced group didn't match, neither can this
jtulach@1348
  4829
            if (j < 0)
jtulach@1348
  4830
                return false;
jtulach@1348
  4831
jtulach@1348
  4832
            // If there isn't enough input left no match
jtulach@1348
  4833
            if (i + groupSize > matcher.to) {
jtulach@1348
  4834
                matcher.hitEnd = true;
jtulach@1348
  4835
                return false;
jtulach@1348
  4836
            }
jtulach@1348
  4837
jtulach@1348
  4838
            // Check each new char to make sure it matches what the group
jtulach@1348
  4839
            // referenced matched last time around
jtulach@1348
  4840
            int x = i;
jtulach@1348
  4841
            for (int index=0; index<groupSize; index++) {
jtulach@1348
  4842
                int c1 = Character.codePointAt(seq, x);
jtulach@1348
  4843
                int c2 = Character.codePointAt(seq, j);
jtulach@1348
  4844
                if (c1 != c2) {
jtulach@1348
  4845
                    if (doUnicodeCase) {
jtulach@1348
  4846
                        int cc1 = Character.toUpperCase(c1);
jtulach@1348
  4847
                        int cc2 = Character.toUpperCase(c2);
jtulach@1348
  4848
                        if (cc1 != cc2 &&
jtulach@1348
  4849
                            Character.toLowerCase(cc1) !=
jtulach@1348
  4850
                            Character.toLowerCase(cc2))
jtulach@1348
  4851
                            return false;
jtulach@1348
  4852
                    } else {
jtulach@1348
  4853
                        if (ASCII.toLower(c1) != ASCII.toLower(c2))
jtulach@1348
  4854
                            return false;
jtulach@1348
  4855
                    }
jtulach@1348
  4856
                }
jtulach@1348
  4857
                x += Character.charCount(c1);
jtulach@1348
  4858
                j += Character.charCount(c2);
jtulach@1348
  4859
            }
jtulach@1348
  4860
jtulach@1348
  4861
            return next.match(matcher, i+groupSize, seq);
jtulach@1348
  4862
        }
jtulach@1348
  4863
        boolean study(TreeInfo info) {
jtulach@1348
  4864
            info.maxValid = false;
jtulach@1348
  4865
            return next.study(info);
jtulach@1348
  4866
        }
jtulach@1348
  4867
    }
jtulach@1348
  4868
jtulach@1348
  4869
    /**
jtulach@1348
  4870
     * Searches until the next instance of its atom. This is useful for
jtulach@1348
  4871
     * finding the atom efficiently without passing an instance of it
jtulach@1348
  4872
     * (greedy problem) and without a lot of wasted search time (reluctant
jtulach@1348
  4873
     * problem).
jtulach@1348
  4874
     */
jtulach@1348
  4875
    static final class First extends Node {
jtulach@1348
  4876
        Node atom;
jtulach@1348
  4877
        First(Node node) {
jtulach@1348
  4878
            this.atom = BnM.optimize(node);
jtulach@1348
  4879
        }
jtulach@1348
  4880
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  4881
            if (atom instanceof BnM) {
jtulach@1348
  4882
                return atom.match(matcher, i, seq)
jtulach@1348
  4883
                    && next.match(matcher, matcher.last, seq);
jtulach@1348
  4884
            }
jtulach@1348
  4885
            for (;;) {
jtulach@1348
  4886
                if (i > matcher.to) {
jtulach@1348
  4887
                    matcher.hitEnd = true;
jtulach@1348
  4888
                    return false;
jtulach@1348
  4889
                }
jtulach@1348
  4890
                if (atom.match(matcher, i, seq)) {
jtulach@1348
  4891
                    return next.match(matcher, matcher.last, seq);
jtulach@1348
  4892
                }
jtulach@1348
  4893
                i += countChars(seq, i, 1);
jtulach@1348
  4894
                matcher.first++;
jtulach@1348
  4895
            }
jtulach@1348
  4896
        }
jtulach@1348
  4897
        boolean study(TreeInfo info) {
jtulach@1348
  4898
            atom.study(info);
jtulach@1348
  4899
            info.maxValid = false;
jtulach@1348
  4900
            info.deterministic = false;
jtulach@1348
  4901
            return next.study(info);
jtulach@1348
  4902
        }
jtulach@1348
  4903
    }
jtulach@1348
  4904
jtulach@1348
  4905
    static final class Conditional extends Node {
jtulach@1348
  4906
        Node cond, yes, not;
jtulach@1348
  4907
        Conditional(Node cond, Node yes, Node not) {
jtulach@1348
  4908
            this.cond = cond;
jtulach@1348
  4909
            this.yes = yes;
jtulach@1348
  4910
            this.not = not;
jtulach@1348
  4911
        }
jtulach@1348
  4912
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  4913
            if (cond.match(matcher, i, seq)) {
jtulach@1348
  4914
                return yes.match(matcher, i, seq);
jtulach@1348
  4915
            } else {
jtulach@1348
  4916
                return not.match(matcher, i, seq);
jtulach@1348
  4917
            }
jtulach@1348
  4918
        }
jtulach@1348
  4919
        boolean study(TreeInfo info) {
jtulach@1348
  4920
            int minL = info.minLength;
jtulach@1348
  4921
            int maxL = info.maxLength;
jtulach@1348
  4922
            boolean maxV = info.maxValid;
jtulach@1348
  4923
            info.reset();
jtulach@1348
  4924
            yes.study(info);
jtulach@1348
  4925
jtulach@1348
  4926
            int minL2 = info.minLength;
jtulach@1348
  4927
            int maxL2 = info.maxLength;
jtulach@1348
  4928
            boolean maxV2 = info.maxValid;
jtulach@1348
  4929
            info.reset();
jtulach@1348
  4930
            not.study(info);
jtulach@1348
  4931
jtulach@1348
  4932
            info.minLength = minL + Math.min(minL2, info.minLength);
jtulach@1348
  4933
            info.maxLength = maxL + Math.max(maxL2, info.maxLength);
jtulach@1348
  4934
            info.maxValid = (maxV & maxV2 & info.maxValid);
jtulach@1348
  4935
            info.deterministic = false;
jtulach@1348
  4936
            return next.study(info);
jtulach@1348
  4937
        }
jtulach@1348
  4938
    }
jtulach@1348
  4939
jtulach@1348
  4940
    /**
jtulach@1348
  4941
     * Zero width positive lookahead.
jtulach@1348
  4942
     */
jtulach@1348
  4943
    static final class Pos extends Node {
jtulach@1348
  4944
        Node cond;
jtulach@1348
  4945
        Pos(Node cond) {
jtulach@1348
  4946
            this.cond = cond;
jtulach@1348
  4947
        }
jtulach@1348
  4948
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  4949
            int savedTo = matcher.to;
jtulach@1348
  4950
            boolean conditionMatched = false;
jtulach@1348
  4951
jtulach@1348
  4952
            // Relax transparent region boundaries for lookahead
jtulach@1348
  4953
            if (matcher.transparentBounds)
jtulach@1348
  4954
                matcher.to = matcher.getTextLength();
jtulach@1348
  4955
            try {
jtulach@1348
  4956
                conditionMatched = cond.match(matcher, i, seq);
jtulach@1348
  4957
            } finally {
jtulach@1348
  4958
                // Reinstate region boundaries
jtulach@1348
  4959
                matcher.to = savedTo;
jtulach@1348
  4960
            }
jtulach@1348
  4961
            return conditionMatched && next.match(matcher, i, seq);
jtulach@1348
  4962
        }
jtulach@1348
  4963
    }
jtulach@1348
  4964
jtulach@1348
  4965
    /**
jtulach@1348
  4966
     * Zero width negative lookahead.
jtulach@1348
  4967
     */
jtulach@1348
  4968
    static final class Neg extends Node {
jtulach@1348
  4969
        Node cond;
jtulach@1348
  4970
        Neg(Node cond) {
jtulach@1348
  4971
            this.cond = cond;
jtulach@1348
  4972
        }
jtulach@1348
  4973
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  4974
            int savedTo = matcher.to;
jtulach@1348
  4975
            boolean conditionMatched = false;
jtulach@1348
  4976
jtulach@1348
  4977
            // Relax transparent region boundaries for lookahead
jtulach@1348
  4978
            if (matcher.transparentBounds)
jtulach@1348
  4979
                matcher.to = matcher.getTextLength();
jtulach@1348
  4980
            try {
jtulach@1348
  4981
                if (i < matcher.to) {
jtulach@1348
  4982
                    conditionMatched = !cond.match(matcher, i, seq);
jtulach@1348
  4983
                } else {
jtulach@1348
  4984
                    // If a negative lookahead succeeds then more input
jtulach@1348
  4985
                    // could cause it to fail!
jtulach@1348
  4986
                    matcher.requireEnd = true;
jtulach@1348
  4987
                    conditionMatched = !cond.match(matcher, i, seq);
jtulach@1348
  4988
                }
jtulach@1348
  4989
            } finally {
jtulach@1348
  4990
                // Reinstate region boundaries
jtulach@1348
  4991
                matcher.to = savedTo;
jtulach@1348
  4992
            }
jtulach@1348
  4993
            return conditionMatched && next.match(matcher, i, seq);
jtulach@1348
  4994
        }
jtulach@1348
  4995
    }
jtulach@1348
  4996
jtulach@1348
  4997
    /**
jtulach@1348
  4998
     * For use with lookbehinds; matches the position where the lookbehind
jtulach@1348
  4999
     * was encountered.
jtulach@1348
  5000
     */
jtulach@1348
  5001
    static Node lookbehindEnd = new Node() {
jtulach@1348
  5002
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  5003
            return i == matcher.lookbehindTo;
jtulach@1348
  5004
        }
jtulach@1348
  5005
    };
jtulach@1348
  5006
jtulach@1348
  5007
    /**
jtulach@1348
  5008
     * Zero width positive lookbehind.
jtulach@1348
  5009
     */
jtulach@1348
  5010
    static class Behind extends Node {
jtulach@1348
  5011
        Node cond;
jtulach@1348
  5012
        int rmax, rmin;
jtulach@1348
  5013
        Behind(Node cond, int rmax, int rmin) {
jtulach@1348
  5014
            this.cond = cond;
jtulach@1348
  5015
            this.rmax = rmax;
jtulach@1348
  5016
            this.rmin = rmin;
jtulach@1348
  5017
        }
jtulach@1348
  5018
jtulach@1348
  5019
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  5020
            int savedFrom = matcher.from;
jtulach@1348
  5021
            boolean conditionMatched = false;
jtulach@1348
  5022
            int startIndex = (!matcher.transparentBounds) ?
jtulach@1348
  5023
                             matcher.from : 0;
jtulach@1348
  5024
            int from = Math.max(i - rmax, startIndex);
jtulach@1348
  5025
            // Set end boundary
jtulach@1348
  5026
            int savedLBT = matcher.lookbehindTo;
jtulach@1348
  5027
            matcher.lookbehindTo = i;
jtulach@1348
  5028
            // Relax transparent region boundaries for lookbehind
jtulach@1348
  5029
            if (matcher.transparentBounds)
jtulach@1348
  5030
                matcher.from = 0;
jtulach@1348
  5031
            for (int j = i - rmin; !conditionMatched && j >= from; j--) {
jtulach@1348
  5032
                conditionMatched = cond.match(matcher, j, seq);
jtulach@1348
  5033
            }
jtulach@1348
  5034
            matcher.from = savedFrom;
jtulach@1348
  5035
            matcher.lookbehindTo = savedLBT;
jtulach@1348
  5036
            return conditionMatched && next.match(matcher, i, seq);
jtulach@1348
  5037
        }
jtulach@1348
  5038
    }
jtulach@1348
  5039
jtulach@1348
  5040
    /**
jtulach@1348
  5041
     * Zero width positive lookbehind, including supplementary
jtulach@1348
  5042
     * characters or unpaired surrogates.
jtulach@1348
  5043
     */
jtulach@1348
  5044
    static final class BehindS extends Behind {
jtulach@1348
  5045
        BehindS(Node cond, int rmax, int rmin) {
jtulach@1348
  5046
            super(cond, rmax, rmin);
jtulach@1348
  5047
        }
jtulach@1348
  5048
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  5049
            int rmaxChars = countChars(seq, i, -rmax);
jtulach@1348
  5050
            int rminChars = countChars(seq, i, -rmin);
jtulach@1348
  5051
            int savedFrom = matcher.from;
jtulach@1348
  5052
            int startIndex = (!matcher.transparentBounds) ?
jtulach@1348
  5053
                             matcher.from : 0;
jtulach@1348
  5054
            boolean conditionMatched = false;
jtulach@1348
  5055
            int from = Math.max(i - rmaxChars, startIndex);
jtulach@1348
  5056
            // Set end boundary
jtulach@1348
  5057
            int savedLBT = matcher.lookbehindTo;
jtulach@1348
  5058
            matcher.lookbehindTo = i;
jtulach@1348
  5059
            // Relax transparent region boundaries for lookbehind
jtulach@1348
  5060
            if (matcher.transparentBounds)
jtulach@1348
  5061
                matcher.from = 0;
jtulach@1348
  5062
jtulach@1348
  5063
            for (int j = i - rminChars;
jtulach@1348
  5064
                 !conditionMatched && j >= from;
jtulach@1348
  5065
                 j -= j>from ? countChars(seq, j, -1) : 1) {
jtulach@1348
  5066
                conditionMatched = cond.match(matcher, j, seq);
jtulach@1348
  5067
            }
jtulach@1348
  5068
            matcher.from = savedFrom;
jtulach@1348
  5069
            matcher.lookbehindTo = savedLBT;
jtulach@1348
  5070
            return conditionMatched && next.match(matcher, i, seq);
jtulach@1348
  5071
        }
jtulach@1348
  5072
    }
jtulach@1348
  5073
jtulach@1348
  5074
    /**
jtulach@1348
  5075
     * Zero width negative lookbehind.
jtulach@1348
  5076
     */
jtulach@1348
  5077
    static class NotBehind extends Node {
jtulach@1348
  5078
        Node cond;
jtulach@1348
  5079
        int rmax, rmin;
jtulach@1348
  5080
        NotBehind(Node cond, int rmax, int rmin) {
jtulach@1348
  5081
            this.cond = cond;
jtulach@1348
  5082
            this.rmax = rmax;
jtulach@1348
  5083
            this.rmin = rmin;
jtulach@1348
  5084
        }
jtulach@1348
  5085
jtulach@1348
  5086
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  5087
            int savedLBT = matcher.lookbehindTo;
jtulach@1348
  5088
            int savedFrom = matcher.from;
jtulach@1348
  5089
            boolean conditionMatched = false;
jtulach@1348
  5090
            int startIndex = (!matcher.transparentBounds) ?
jtulach@1348
  5091
                             matcher.from : 0;
jtulach@1348
  5092
            int from = Math.max(i - rmax, startIndex);
jtulach@1348
  5093
            matcher.lookbehindTo = i;
jtulach@1348
  5094
            // Relax transparent region boundaries for lookbehind
jtulach@1348
  5095
            if (matcher.transparentBounds)
jtulach@1348
  5096
                matcher.from = 0;
jtulach@1348
  5097
            for (int j = i - rmin; !conditionMatched && j >= from; j--) {
jtulach@1348
  5098
                conditionMatched = cond.match(matcher, j, seq);
jtulach@1348
  5099
            }
jtulach@1348
  5100
            // Reinstate region boundaries
jtulach@1348
  5101
            matcher.from = savedFrom;
jtulach@1348
  5102
            matcher.lookbehindTo = savedLBT;
jtulach@1348
  5103
            return !conditionMatched && next.match(matcher, i, seq);
jtulach@1348
  5104
        }
jtulach@1348
  5105
    }
jtulach@1348
  5106
jtulach@1348
  5107
    /**
jtulach@1348
  5108
     * Zero width negative lookbehind, including supplementary
jtulach@1348
  5109
     * characters or unpaired surrogates.
jtulach@1348
  5110
     */
jtulach@1348
  5111
    static final class NotBehindS extends NotBehind {
jtulach@1348
  5112
        NotBehindS(Node cond, int rmax, int rmin) {
jtulach@1348
  5113
            super(cond, rmax, rmin);
jtulach@1348
  5114
        }
jtulach@1348
  5115
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  5116
            int rmaxChars = countChars(seq, i, -rmax);
jtulach@1348
  5117
            int rminChars = countChars(seq, i, -rmin);
jtulach@1348
  5118
            int savedFrom = matcher.from;
jtulach@1348
  5119
            int savedLBT = matcher.lookbehindTo;
jtulach@1348
  5120
            boolean conditionMatched = false;
jtulach@1348
  5121
            int startIndex = (!matcher.transparentBounds) ?
jtulach@1348
  5122
                             matcher.from : 0;
jtulach@1348
  5123
            int from = Math.max(i - rmaxChars, startIndex);
jtulach@1348
  5124
            matcher.lookbehindTo = i;
jtulach@1348
  5125
            // Relax transparent region boundaries for lookbehind
jtulach@1348
  5126
            if (matcher.transparentBounds)
jtulach@1348
  5127
                matcher.from = 0;
jtulach@1348
  5128
            for (int j = i - rminChars;
jtulach@1348
  5129
                 !conditionMatched && j >= from;
jtulach@1348
  5130
                 j -= j>from ? countChars(seq, j, -1) : 1) {
jtulach@1348
  5131
                conditionMatched = cond.match(matcher, j, seq);
jtulach@1348
  5132
            }
jtulach@1348
  5133
            //Reinstate region boundaries
jtulach@1348
  5134
            matcher.from = savedFrom;
jtulach@1348
  5135
            matcher.lookbehindTo = savedLBT;
jtulach@1348
  5136
            return !conditionMatched && next.match(matcher, i, seq);
jtulach@1348
  5137
        }
jtulach@1348
  5138
    }
jtulach@1348
  5139
jtulach@1348
  5140
    /**
jtulach@1348
  5141
     * Returns the set union of two CharProperty nodes.
jtulach@1348
  5142
     */
jtulach@1348
  5143
    private static CharProperty union(final CharProperty lhs,
jtulach@1348
  5144
                                      final CharProperty rhs) {
jtulach@1348
  5145
        return new CharProperty() {
jtulach@1348
  5146
                boolean isSatisfiedBy(int ch) {
jtulach@1348
  5147
                    return lhs.isSatisfiedBy(ch) || rhs.isSatisfiedBy(ch);}};
jtulach@1348
  5148
    }
jtulach@1348
  5149
jtulach@1348
  5150
    /**
jtulach@1348
  5151
     * Returns the set intersection of two CharProperty nodes.
jtulach@1348
  5152
     */
jtulach@1348
  5153
    private static CharProperty intersection(final CharProperty lhs,
jtulach@1348
  5154
                                             final CharProperty rhs) {
jtulach@1348
  5155
        return new CharProperty() {
jtulach@1348
  5156
                boolean isSatisfiedBy(int ch) {
jtulach@1348
  5157
                    return lhs.isSatisfiedBy(ch) && rhs.isSatisfiedBy(ch);}};
jtulach@1348
  5158
    }
jtulach@1348
  5159
jtulach@1348
  5160
    /**
jtulach@1348
  5161
     * Returns the set difference of two CharProperty nodes.
jtulach@1348
  5162
     */
jtulach@1348
  5163
    private static CharProperty setDifference(final CharProperty lhs,
jtulach@1348
  5164
                                              final CharProperty rhs) {
jtulach@1348
  5165
        return new CharProperty() {
jtulach@1348
  5166
                boolean isSatisfiedBy(int ch) {
jtulach@1348
  5167
                    return ! rhs.isSatisfiedBy(ch) && lhs.isSatisfiedBy(ch);}};
jtulach@1348
  5168
    }
jtulach@1348
  5169
jtulach@1348
  5170
    /**
jtulach@1348
  5171
     * Handles word boundaries. Includes a field to allow this one class to
jtulach@1348
  5172
     * deal with the different types of word boundaries we can match. The word
jtulach@1348
  5173
     * characters include underscores, letters, and digits. Non spacing marks
jtulach@1348
  5174
     * can are also part of a word if they have a base character, otherwise
jtulach@1348
  5175
     * they are ignored for purposes of finding word boundaries.
jtulach@1348
  5176
     */
jtulach@1348
  5177
    static final class Bound extends Node {
jtulach@1348
  5178
        static int LEFT = 0x1;
jtulach@1348
  5179
        static int RIGHT= 0x2;
jtulach@1348
  5180
        static int BOTH = 0x3;
jtulach@1348
  5181
        static int NONE = 0x4;
jtulach@1348
  5182
        int type;
jtulach@1348
  5183
        boolean useUWORD;
jtulach@1348
  5184
        Bound(int n, boolean useUWORD) {
jtulach@1348
  5185
            type = n;
jtulach@1348
  5186
            this.useUWORD = useUWORD;
jtulach@1348
  5187
        }
jtulach@1348
  5188
jtulach@1348
  5189
        boolean isWord(int ch) {
jtulach@1348
  5190
            return useUWORD ? UnicodeProp.WORD.is(ch)
jtulach@1348
  5191
                            : (ch == '_' || Character.isLetterOrDigit(ch));
jtulach@1348
  5192
        }
jtulach@1348
  5193
jtulach@1348
  5194
        int check(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  5195
            int ch;
jtulach@1348
  5196
            boolean left = false;
jtulach@1348
  5197
            int startIndex = matcher.from;
jtulach@1348
  5198
            int endIndex = matcher.to;
jtulach@1348
  5199
            if (matcher.transparentBounds) {
jtulach@1348
  5200
                startIndex = 0;
jtulach@1348
  5201
                endIndex = matcher.getTextLength();
jtulach@1348
  5202
            }
jtulach@1348
  5203
            if (i > startIndex) {
jtulach@1348
  5204
                ch = Character.codePointBefore(seq, i);
jtulach@1348
  5205
                left = (isWord(ch) ||
jtulach@1348
  5206
                    ((Character.getType(ch) == Character.NON_SPACING_MARK)
jtulach@1348
  5207
                     && hasBaseCharacter(matcher, i-1, seq)));
jtulach@1348
  5208
            }
jtulach@1348
  5209
            boolean right = false;
jtulach@1348
  5210
            if (i < endIndex) {
jtulach@1348
  5211
                ch = Character.codePointAt(seq, i);
jtulach@1348
  5212
                right = (isWord(ch) ||
jtulach@1348
  5213
                    ((Character.getType(ch) == Character.NON_SPACING_MARK)
jtulach@1348
  5214
                     && hasBaseCharacter(matcher, i, seq)));
jtulach@1348
  5215
            } else {
jtulach@1348
  5216
                // Tried to access char past the end
jtulach@1348
  5217
                matcher.hitEnd = true;
jtulach@1348
  5218
                // The addition of another char could wreck a boundary
jtulach@1348
  5219
                matcher.requireEnd = true;
jtulach@1348
  5220
            }
jtulach@1348
  5221
            return ((left ^ right) ? (right ? LEFT : RIGHT) : NONE);
jtulach@1348
  5222
        }
jtulach@1348
  5223
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  5224
            return (check(matcher, i, seq) & type) > 0
jtulach@1348
  5225
                && next.match(matcher, i, seq);
jtulach@1348
  5226
        }
jtulach@1348
  5227
    }
jtulach@1348
  5228
jtulach@1348
  5229
    /**
jtulach@1348
  5230
     * Non spacing marks only count as word characters in bounds calculations
jtulach@1348
  5231
     * if they have a base character.
jtulach@1348
  5232
     */
jtulach@1348
  5233
    private static boolean hasBaseCharacter(Matcher matcher, int i,
jtulach@1348
  5234
                                            CharSequence seq)
jtulach@1348
  5235
    {
jtulach@1348
  5236
        int start = (!matcher.transparentBounds) ?
jtulach@1348
  5237
            matcher.from : 0;
jtulach@1348
  5238
        for (int x=i; x >= start; x--) {
jtulach@1348
  5239
            int ch = Character.codePointAt(seq, x);
jtulach@1348
  5240
            if (Character.isLetterOrDigit(ch))
jtulach@1348
  5241
                return true;
jtulach@1348
  5242
            if (Character.getType(ch) == Character.NON_SPACING_MARK)
jtulach@1348
  5243
                continue;
jtulach@1348
  5244
            return false;
jtulach@1348
  5245
        }
jtulach@1348
  5246
        return false;
jtulach@1348
  5247
    }
jtulach@1348
  5248
jtulach@1348
  5249
    /**
jtulach@1348
  5250
     * Attempts to match a slice in the input using the Boyer-Moore string
jtulach@1348
  5251
     * matching algorithm. The algorithm is based on the idea that the
jtulach@1348
  5252
     * pattern can be shifted farther ahead in the search text if it is
jtulach@1348
  5253
     * matched right to left.
jtulach@1348
  5254
     * <p>
jtulach@1348
  5255
     * The pattern is compared to the input one character at a time, from
jtulach@1348
  5256
     * the rightmost character in the pattern to the left. If the characters
jtulach@1348
  5257
     * all match the pattern has been found. If a character does not match,
jtulach@1348
  5258
     * the pattern is shifted right a distance that is the maximum of two
jtulach@1348
  5259
     * functions, the bad character shift and the good suffix shift. This
jtulach@1348
  5260
     * shift moves the attempted match position through the input more
jtulach@1348
  5261
     * quickly than a naive one position at a time check.
jtulach@1348
  5262
     * <p>
jtulach@1348
  5263
     * The bad character shift is based on the character from the text that
jtulach@1348
  5264
     * did not match. If the character does not appear in the pattern, the
jtulach@1348
  5265
     * pattern can be shifted completely beyond the bad character. If the
jtulach@1348
  5266
     * character does occur in the pattern, the pattern can be shifted to
jtulach@1348
  5267
     * line the pattern up with the next occurrence of that character.
jtulach@1348
  5268
     * <p>
jtulach@1348
  5269
     * The good suffix shift is based on the idea that some subset on the right
jtulach@1348
  5270
     * side of the pattern has matched. When a bad character is found, the
jtulach@1348
  5271
     * pattern can be shifted right by the pattern length if the subset does
jtulach@1348
  5272
     * not occur again in pattern, or by the amount of distance to the
jtulach@1348
  5273
     * next occurrence of the subset in the pattern.
jtulach@1348
  5274
     *
jtulach@1348
  5275
     * Boyer-Moore search methods adapted from code by Amy Yu.
jtulach@1348
  5276
     */
jtulach@1348
  5277
    static class BnM extends Node {
jtulach@1348
  5278
        int[] buffer;
jtulach@1348
  5279
        int[] lastOcc;
jtulach@1348
  5280
        int[] optoSft;
jtulach@1348
  5281
jtulach@1348
  5282
        /**
jtulach@1348
  5283
         * Pre calculates arrays needed to generate the bad character
jtulach@1348
  5284
         * shift and the good suffix shift. Only the last seven bits
jtulach@1348
  5285
         * are used to see if chars match; This keeps the tables small
jtulach@1348
  5286
         * and covers the heavily used ASCII range, but occasionally
jtulach@1348
  5287
         * results in an aliased match for the bad character shift.
jtulach@1348
  5288
         */
jtulach@1348
  5289
        static Node optimize(Node node) {
jtulach@1348
  5290
            if (!(node instanceof Slice)) {
jtulach@1348
  5291
                return node;
jtulach@1348
  5292
            }
jtulach@1348
  5293
jtulach@1348
  5294
            int[] src = ((Slice) node).buffer;
jtulach@1348
  5295
            int patternLength = src.length;
jtulach@1348
  5296
            // The BM algorithm requires a bit of overhead;
jtulach@1348
  5297
            // If the pattern is short don't use it, since
jtulach@1348
  5298
            // a shift larger than the pattern length cannot
jtulach@1348
  5299
            // be used anyway.
jtulach@1348
  5300
            if (patternLength < 4) {
jtulach@1348
  5301
                return node;
jtulach@1348
  5302
            }
jtulach@1348
  5303
            int i, j, k;
jtulach@1348
  5304
            int[] lastOcc = new int[128];
jtulach@1348
  5305
            int[] optoSft = new int[patternLength];
jtulach@1348
  5306
            // Precalculate part of the bad character shift
jtulach@1348
  5307
            // It is a table for where in the pattern each
jtulach@1348
  5308
            // lower 7-bit value occurs
jtulach@1348
  5309
            for (i = 0; i < patternLength; i++) {
jtulach@1348
  5310
                lastOcc[src[i]&0x7F] = i + 1;
jtulach@1348
  5311
            }
jtulach@1348
  5312
            // Precalculate the good suffix shift
jtulach@1348
  5313
            // i is the shift amount being considered
jtulach@1348
  5314
NEXT:       for (i = patternLength; i > 0; i--) {
jtulach@1348
  5315
                // j is the beginning index of suffix being considered
jtulach@1348
  5316
                for (j = patternLength - 1; j >= i; j--) {
jtulach@1348
  5317
                    // Testing for good suffix
jtulach@1348
  5318
                    if (src[j] == src[j-i]) {
jtulach@1348
  5319
                        // src[j..len] is a good suffix
jtulach@1348
  5320
                        optoSft[j-1] = i;
jtulach@1348
  5321
                    } else {
jtulach@1348
  5322
                        // No match. The array has already been
jtulach@1348
  5323
                        // filled up with correct values before.
jtulach@1348
  5324
                        continue NEXT;
jtulach@1348
  5325
                    }
jtulach@1348
  5326
                }
jtulach@1348
  5327
                // This fills up the remaining of optoSft
jtulach@1348
  5328
                // any suffix can not have larger shift amount
jtulach@1348
  5329
                // then its sub-suffix. Why???
jtulach@1348
  5330
                while (j > 0) {
jtulach@1348
  5331
                    optoSft[--j] = i;
jtulach@1348
  5332
                }
jtulach@1348
  5333
            }
jtulach@1348
  5334
            // Set the guard value because of unicode compression
jtulach@1348
  5335
            optoSft[patternLength-1] = 1;
jtulach@1348
  5336
            if (node instanceof SliceS)
jtulach@1348
  5337
                return new BnMS(src, lastOcc, optoSft, node.next);
jtulach@1348
  5338
            return new BnM(src, lastOcc, optoSft, node.next);
jtulach@1348
  5339
        }
jtulach@1348
  5340
        BnM(int[] src, int[] lastOcc, int[] optoSft, Node next) {
jtulach@1348
  5341
            this.buffer = src;
jtulach@1348
  5342
            this.lastOcc = lastOcc;
jtulach@1348
  5343
            this.optoSft = optoSft;
jtulach@1348
  5344
            this.next = next;
jtulach@1348
  5345
        }
jtulach@1348
  5346
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  5347
            int[] src = buffer;
jtulach@1348
  5348
            int patternLength = src.length;
jtulach@1348
  5349
            int last = matcher.to - patternLength;
jtulach@1348
  5350
jtulach@1348
  5351
            // Loop over all possible match positions in text
jtulach@1348
  5352
NEXT:       while (i <= last) {
jtulach@1348
  5353
                // Loop over pattern from right to left
jtulach@1348
  5354
                for (int j = patternLength - 1; j >= 0; j--) {
jtulach@1348
  5355
                    int ch = seq.charAt(i+j);
jtulach@1348
  5356
                    if (ch != src[j]) {
jtulach@1348
  5357
                        // Shift search to the right by the maximum of the
jtulach@1348
  5358
                        // bad character shift and the good suffix shift
jtulach@1348
  5359
                        i += Math.max(j + 1 - lastOcc[ch&0x7F], optoSft[j]);
jtulach@1348
  5360
                        continue NEXT;
jtulach@1348
  5361
                    }
jtulach@1348
  5362
                }
jtulach@1348
  5363
                // Entire pattern matched starting at i
jtulach@1348
  5364
                matcher.first = i;
jtulach@1348
  5365
                boolean ret = next.match(matcher, i + patternLength, seq);
jtulach@1348
  5366
                if (ret) {
jtulach@1348
  5367
                    matcher.first = i;
jtulach@1348
  5368
                    matcher.groups[0] = matcher.first;
jtulach@1348
  5369
                    matcher.groups[1] = matcher.last;
jtulach@1348
  5370
                    return true;
jtulach@1348
  5371
                }
jtulach@1348
  5372
                i++;
jtulach@1348
  5373
            }
jtulach@1348
  5374
            // BnM is only used as the leading node in the unanchored case,
jtulach@1348
  5375
            // and it replaced its Start() which always searches to the end
jtulach@1348
  5376
            // if it doesn't find what it's looking for, so hitEnd is true.
jtulach@1348
  5377
            matcher.hitEnd = true;
jtulach@1348
  5378
            return false;
jtulach@1348
  5379
        }
jtulach@1348
  5380
        boolean study(TreeInfo info) {
jtulach@1348
  5381
            info.minLength += buffer.length;
jtulach@1348
  5382
            info.maxValid = false;
jtulach@1348
  5383
            return next.study(info);
jtulach@1348
  5384
        }
jtulach@1348
  5385
    }
jtulach@1348
  5386
jtulach@1348
  5387
    /**
jtulach@1348
  5388
     * Supplementary support version of BnM(). Unpaired surrogates are
jtulach@1348
  5389
     * also handled by this class.
jtulach@1348
  5390
     */
jtulach@1348
  5391
    static final class BnMS extends BnM {
jtulach@1348
  5392
        int lengthInChars;
jtulach@1348
  5393
jtulach@1348
  5394
        BnMS(int[] src, int[] lastOcc, int[] optoSft, Node next) {
jtulach@1348
  5395
            super(src, lastOcc, optoSft, next);
jtulach@1348
  5396
            for (int x = 0; x < buffer.length; x++) {
jtulach@1348
  5397
                lengthInChars += Character.charCount(buffer[x]);
jtulach@1348
  5398
            }
jtulach@1348
  5399
        }
jtulach@1348
  5400
        boolean match(Matcher matcher, int i, CharSequence seq) {
jtulach@1348
  5401
            int[] src = buffer;
jtulach@1348
  5402
            int patternLength = src.length;
jtulach@1348
  5403
            int last = matcher.to - lengthInChars;
jtulach@1348
  5404
jtulach@1348
  5405
            // Loop over all possible match positions in text
jtulach@1348
  5406
NEXT:       while (i <= last) {
jtulach@1348
  5407
                // Loop over pattern from right to left
jtulach@1348
  5408
                int ch;
jtulach@1348
  5409
                for (int j = countChars(seq, i, patternLength), x = patternLength - 1;
jtulach@1348
  5410
                     j > 0; j -= Character.charCount(ch), x--) {
jtulach@1348
  5411
                    ch = Character.codePointBefore(seq, i+j);
jtulach@1348
  5412
                    if (ch != src[x]) {
jtulach@1348
  5413
                        // Shift search to the right by the maximum of the
jtulach@1348
  5414
                        // bad character shift and the good suffix shift
jtulach@1348
  5415
                        int n = Math.max(x + 1 - lastOcc[ch&0x7F], optoSft[x]);
jtulach@1348
  5416
                        i += countChars(seq, i, n);
jtulach@1348
  5417
                        continue NEXT;
jtulach@1348
  5418
                    }
jtulach@1348
  5419
                }
jtulach@1348
  5420
                // Entire pattern matched starting at i
jtulach@1348
  5421
                matcher.first = i;
jtulach@1348
  5422
                boolean ret = next.match(matcher, i + lengthInChars, seq);
jtulach@1348
  5423
                if (ret) {
jtulach@1348
  5424
                    matcher.first = i;
jtulach@1348
  5425
                    matcher.groups[0] = matcher.first;
jtulach@1348
  5426
                    matcher.groups[1] = matcher.last;
jtulach@1348
  5427
                    return true;
jtulach@1348
  5428
                }
jtulach@1348
  5429
                i += countChars(seq, i, 1);
jtulach@1348
  5430
            }
jtulach@1348
  5431
            matcher.hitEnd = true;
jtulach@1348
  5432
            return false;
jtulach@1348
  5433
        }
jtulach@1348
  5434
    }
jtulach@1348
  5435
jtulach@1348
  5436
///////////////////////////////////////////////////////////////////////////////
jtulach@1348
  5437
///////////////////////////////////////////////////////////////////////////////
jtulach@1348
  5438
jtulach@1348
  5439
    /**
jtulach@1348
  5440
     *  This must be the very first initializer.
jtulach@1348
  5441
     */
jtulach@1348
  5442
    static Node accept = new Node();
jtulach@1348
  5443
jtulach@1348
  5444
    static Node lastAccept = new LastNode();
jtulach@1348
  5445
jtulach@1348
  5446
    private static class CharPropertyNames {
jtulach@1348
  5447
jtulach@1348
  5448
        static CharProperty charPropertyFor(String name) {
jtulach@1348
  5449
            CharPropertyFactory m = map.get(name);
jtulach@1348
  5450
            return m == null ? null : m.make();
jtulach@1348
  5451
        }
jtulach@1348
  5452
jtulach@1348
  5453
        private static abstract class CharPropertyFactory {
jtulach@1348
  5454
            abstract CharProperty make();
jtulach@1348
  5455
        }
jtulach@1348
  5456
jtulach@1348
  5457
        private static void defCategory(String name,
jtulach@1348
  5458
                                        final int typeMask) {
jtulach@1348
  5459
            map.put(name, new CharPropertyFactory() {
jtulach@1348
  5460
                    CharProperty make() { return new Category(typeMask);}});
jtulach@1348
  5461
        }
jtulach@1348
  5462
jtulach@1348
  5463
        private static void defRange(String name,
jtulach@1348
  5464
                                     final int lower, final int upper) {
jtulach@1348
  5465
            map.put(name, new CharPropertyFactory() {
jtulach@1348
  5466
                    CharProperty make() { return rangeFor(lower, upper);}});
jtulach@1348
  5467
        }
jtulach@1348
  5468
jtulach@1348
  5469
        private static void defCtype(String name,
jtulach@1348
  5470
                                     final int ctype) {
jtulach@1348
  5471
            map.put(name, new CharPropertyFactory() {
jtulach@1348
  5472
                    CharProperty make() { return new Ctype(ctype);}});
jtulach@1348
  5473
        }
jtulach@1348
  5474
jtulach@1348
  5475
        private static abstract class CloneableProperty
jtulach@1348
  5476
            extends CharProperty implements Cloneable
jtulach@1348
  5477
        {
jtulach@1348
  5478
            public CloneableProperty clone() {
jtulach@1348
  5479
                try {
jtulach@1348
  5480
                    return (CloneableProperty) super.clone();
jtulach@1348
  5481
                } catch (CloneNotSupportedException e) {
jtulach@1348
  5482
                    throw new AssertionError(e);
jtulach@1348
  5483
                }
jtulach@1348
  5484
            }
jtulach@1348
  5485
        }
jtulach@1348
  5486
jtulach@1348
  5487
        private static void defClone(String name,
jtulach@1348
  5488
                                     final CloneableProperty p) {
jtulach@1348
  5489
            map.put(name, new CharPropertyFactory() {
jtulach@1348
  5490
                    CharProperty make() { return p.clone();}});
jtulach@1348
  5491
        }
jtulach@1348
  5492
jtulach@1348
  5493
        private static final HashMap<String, CharPropertyFactory> map
jtulach@1348
  5494
            = new HashMap<>();
jtulach@1348
  5495
jtulach@1348
  5496
        static {
jtulach@1348
  5497
            // Unicode character property aliases, defined in
jtulach@1348
  5498
            // http://www.unicode.org/Public/UNIDATA/PropertyValueAliases.txt
jtulach@1348
  5499
            defCategory("Cn", 1<<Character.UNASSIGNED);
jtulach@1348
  5500
            defCategory("Lu", 1<<Character.UPPERCASE_LETTER);
jtulach@1348
  5501
            defCategory("Ll", 1<<Character.LOWERCASE_LETTER);
jtulach@1348
  5502
            defCategory("Lt", 1<<Character.TITLECASE_LETTER);
jtulach@1348
  5503
            defCategory("Lm", 1<<Character.MODIFIER_LETTER);
jtulach@1348
  5504
            defCategory("Lo", 1<<Character.OTHER_LETTER);
jtulach@1348
  5505
            defCategory("Mn", 1<<Character.NON_SPACING_MARK);
jtulach@1348
  5506
            defCategory("Me", 1<<Character.ENCLOSING_MARK);
jtulach@1348
  5507
            defCategory("Mc", 1<<Character.COMBINING_SPACING_MARK);
jtulach@1348
  5508
            defCategory("Nd", 1<<Character.DECIMAL_DIGIT_NUMBER);
jtulach@1348
  5509
            defCategory("Nl", 1<<Character.LETTER_NUMBER);
jtulach@1348
  5510
            defCategory("No", 1<<Character.OTHER_NUMBER);
jtulach@1348
  5511
            defCategory("Zs", 1<<Character.SPACE_SEPARATOR);
jtulach@1348
  5512
            defCategory("Zl", 1<<Character.LINE_SEPARATOR);
jtulach@1348
  5513
            defCategory("Zp", 1<<Character.PARAGRAPH_SEPARATOR);
jtulach@1348
  5514
            defCategory("Cc", 1<<Character.CONTROL);
jtulach@1348
  5515
            defCategory("Cf", 1<<Character.FORMAT);
jtulach@1348
  5516
            defCategory("Co", 1<<Character.PRIVATE_USE);
jtulach@1348
  5517
            defCategory("Cs", 1<<Character.SURROGATE);
jtulach@1348
  5518
            defCategory("Pd", 1<<Character.DASH_PUNCTUATION);
jtulach@1348
  5519
            defCategory("Ps", 1<<Character.START_PUNCTUATION);
jtulach@1348
  5520
            defCategory("Pe", 1<<Character.END_PUNCTUATION);
jtulach@1348
  5521
            defCategory("Pc", 1<<Character.CONNECTOR_PUNCTUATION);
jtulach@1348
  5522
            defCategory("Po", 1<<Character.OTHER_PUNCTUATION);
jtulach@1348
  5523
            defCategory("Sm", 1<<Character.MATH_SYMBOL);
jtulach@1348
  5524
            defCategory("Sc", 1<<Character.CURRENCY_SYMBOL);
jtulach@1348
  5525
            defCategory("Sk", 1<<Character.MODIFIER_SYMBOL);
jtulach@1348
  5526
            defCategory("So", 1<<Character.OTHER_SYMBOL);
jtulach@1348
  5527
            defCategory("Pi", 1<<Character.INITIAL_QUOTE_PUNCTUATION);
jtulach@1348
  5528
            defCategory("Pf", 1<<Character.FINAL_QUOTE_PUNCTUATION);
jtulach@1348
  5529
            defCategory("L", ((1<<Character.UPPERCASE_LETTER) |
jtulach@1348
  5530
                              (1<<Character.LOWERCASE_LETTER) |
jtulach@1348
  5531
                              (1<<Character.TITLECASE_LETTER) |
jtulach@1348
  5532
                              (1<<Character.MODIFIER_LETTER)  |
jtulach@1348
  5533
                              (1<<Character.OTHER_LETTER)));
jtulach@1348
  5534
            defCategory("M", ((1<<Character.NON_SPACING_MARK) |
jtulach@1348
  5535
                              (1<<Character.ENCLOSING_MARK)   |
jtulach@1348
  5536
                              (1<<Character.COMBINING_SPACING_MARK)));
jtulach@1348
  5537
            defCategory("N", ((1<<Character.DECIMAL_DIGIT_NUMBER) |
jtulach@1348
  5538
                              (1<<Character.LETTER_NUMBER)        |
jtulach@1348
  5539
                              (1<<Character.OTHER_NUMBER)));
jtulach@1348
  5540
            defCategory("Z", ((1<<Character.SPACE_SEPARATOR) |
jtulach@1348
  5541
                              (1<<Character.LINE_SEPARATOR)  |
jtulach@1348
  5542
                              (1<<Character.PARAGRAPH_SEPARATOR)));
jtulach@1348
  5543
            defCategory("C", ((1<<Character.CONTROL)     |
jtulach@1348
  5544
                              (1<<Character.FORMAT)      |
jtulach@1348
  5545
                              (1<<Character.PRIVATE_USE) |
jtulach@1348
  5546
                              (1<<Character.SURROGATE))); // Other
jtulach@1348
  5547
            defCategory("P", ((1<<Character.DASH_PUNCTUATION)      |
jtulach@1348
  5548
                              (1<<Character.START_PUNCTUATION)     |
jtulach@1348
  5549
                              (1<<Character.END_PUNCTUATION)       |
jtulach@1348
  5550
                              (1<<Character.CONNECTOR_PUNCTUATION) |
jtulach@1348
  5551
                              (1<<Character.OTHER_PUNCTUATION)     |
jtulach@1348
  5552
                              (1<<Character.INITIAL_QUOTE_PUNCTUATION) |
jtulach@1348
  5553
                              (1<<Character.FINAL_QUOTE_PUNCTUATION)));
jtulach@1348
  5554
            defCategory("S", ((1<<Character.MATH_SYMBOL)     |
jtulach@1348
  5555
                              (1<<Character.CURRENCY_SYMBOL) |
jtulach@1348
  5556
                              (1<<Character.MODIFIER_SYMBOL) |
jtulach@1348
  5557
                              (1<<Character.OTHER_SYMBOL)));
jtulach@1348
  5558
            defCategory("LC", ((1<<Character.UPPERCASE_LETTER) |
jtulach@1348
  5559
                               (1<<Character.LOWERCASE_LETTER) |
jtulach@1348
  5560
                               (1<<Character.TITLECASE_LETTER)));
jtulach@1348
  5561
            defCategory("LD", ((1<<Character.UPPERCASE_LETTER) |
jtulach@1348
  5562
                               (1<<Character.LOWERCASE_LETTER) |
jtulach@1348
  5563
                               (1<<Character.TITLECASE_LETTER) |
jtulach@1348
  5564
                               (1<<Character.MODIFIER_LETTER)  |
jtulach@1348
  5565
                               (1<<Character.OTHER_LETTER)     |
jtulach@1348
  5566
                               (1<<Character.DECIMAL_DIGIT_NUMBER)));
jtulach@1348
  5567
            defRange("L1", 0x00, 0xFF); // Latin-1
jtulach@1348
  5568
            map.put("all", new CharPropertyFactory() {
jtulach@1348
  5569
                    CharProperty make() { return new All(); }});
jtulach@1348
  5570
jtulach@1348
  5571
            // Posix regular expression character classes, defined in
jtulach@1348
  5572
            // http://www.unix.org/onlinepubs/009695399/basedefs/xbd_chap09.html
jtulach@1348
  5573
            defRange("ASCII", 0x00, 0x7F);   // ASCII
jtulach@1348
  5574
            defCtype("Alnum", ASCII.ALNUM);  // Alphanumeric characters
jtulach@1348
  5575
            defCtype("Alpha", ASCII.ALPHA);  // Alphabetic characters
jtulach@1348
  5576
            defCtype("Blank", ASCII.BLANK);  // Space and tab characters
jtulach@1348
  5577
            defCtype("Cntrl", ASCII.CNTRL);  // Control characters
jtulach@1348
  5578
            defRange("Digit", '0', '9');     // Numeric characters
jtulach@1348
  5579
            defCtype("Graph", ASCII.GRAPH);  // printable and visible
jtulach@1348
  5580
            defRange("Lower", 'a', 'z');     // Lower-case alphabetic
jtulach@1348
  5581
            defRange("Print", 0x20, 0x7E);   // Printable characters
jtulach@1348
  5582
            defCtype("Punct", ASCII.PUNCT);  // Punctuation characters
jtulach@1348
  5583
            defCtype("Space", ASCII.SPACE);  // Space characters
jtulach@1348
  5584
            defRange("Upper", 'A', 'Z');     // Upper-case alphabetic
jtulach@1348
  5585
            defCtype("XDigit",ASCII.XDIGIT); // hexadecimal digits
jtulach@1348
  5586
jtulach@1348
  5587
            // Java character properties, defined by methods in Character.java
jtulach@1348
  5588
            defClone("javaLowerCase", new CloneableProperty() {
jtulach@1348
  5589
                boolean isSatisfiedBy(int ch) {
jtulach@1348
  5590
                    return Character.isLowerCase(ch);}});
jtulach@1348
  5591
            defClone("javaUpperCase", new CloneableProperty() {
jtulach@1348
  5592
                boolean isSatisfiedBy(int ch) {
jtulach@1348
  5593
                    return Character.isUpperCase(ch);}});
jtulach@1348
  5594
            defClone("javaAlphabetic", new CloneableProperty() {
jtulach@1348
  5595
                boolean isSatisfiedBy(int ch) {
jtulach@1348
  5596
                    return Character.isAlphabetic(ch);}});
jtulach@1348
  5597
            defClone("javaIdeographic", new CloneableProperty() {
jtulach@1348
  5598
                boolean isSatisfiedBy(int ch) {
jtulach@1348
  5599
                    return Character.isIdeographic(ch);}});
jtulach@1348
  5600
            defClone("javaTitleCase", new CloneableProperty() {
jtulach@1348
  5601
                boolean isSatisfiedBy(int ch) {
jtulach@1348
  5602
                    return Character.isTitleCase(ch);}});
jtulach@1348
  5603
            defClone("javaDigit", new CloneableProperty() {
jtulach@1348
  5604
                boolean isSatisfiedBy(int ch) {
jtulach@1348
  5605
                    return Character.isDigit(ch);}});
jtulach@1348
  5606
            defClone("javaDefined", new CloneableProperty() {
jtulach@1348
  5607
                boolean isSatisfiedBy(int ch) {
jtulach@1348
  5608
                    return Character.isDefined(ch);}});
jtulach@1348
  5609
            defClone("javaLetter", new CloneableProperty() {
jtulach@1348
  5610
                boolean isSatisfiedBy(int ch) {
jtulach@1348
  5611
                    return Character.isLetter(ch);}});
jtulach@1348
  5612
            defClone("javaLetterOrDigit", new CloneableProperty() {
jtulach@1348
  5613
                boolean isSatisfiedBy(int ch) {
jtulach@1348
  5614
                    return Character.isLetterOrDigit(ch);}});
jtulach@1348
  5615
            defClone("javaJavaIdentifierStart", new CloneableProperty() {
jtulach@1348
  5616
                boolean isSatisfiedBy(int ch) {
jtulach@1348
  5617
                    return Character.isJavaIdentifierStart(ch);}});
jtulach@1348
  5618
            defClone("javaJavaIdentifierPart", new CloneableProperty() {
jtulach@1348
  5619
                boolean isSatisfiedBy(int ch) {
jtulach@1348
  5620
                    return Character.isJavaIdentifierPart(ch);}});
jtulach@1348
  5621
            defClone("javaUnicodeIdentifierStart", new CloneableProperty() {
jtulach@1348
  5622
                boolean isSatisfiedBy(int ch) {
jtulach@1348
  5623
                    return Character.isUnicodeIdentifierStart(ch);}});
jtulach@1348
  5624
            defClone("javaUnicodeIdentifierPart", new CloneableProperty() {
jtulach@1348
  5625
                boolean isSatisfiedBy(int ch) {
jtulach@1348
  5626
                    return Character.isUnicodeIdentifierPart(ch);}});
jtulach@1348
  5627
            defClone("javaIdentifierIgnorable", new CloneableProperty() {
jtulach@1348
  5628
                boolean isSatisfiedBy(int ch) {
jtulach@1348
  5629
                    return Character.isIdentifierIgnorable(ch);}});
jtulach@1348
  5630
            defClone("javaSpaceChar", new CloneableProperty() {
jtulach@1348
  5631
                boolean isSatisfiedBy(int ch) {
jtulach@1348
  5632
                    return Character.isSpaceChar(ch);}});
jtulach@1348
  5633
            defClone("javaWhitespace", new CloneableProperty() {
jtulach@1348
  5634
                boolean isSatisfiedBy(int ch) {
jtulach@1348
  5635
                    return Character.isWhitespace(ch);}});
jtulach@1348
  5636
            defClone("javaISOControl", new CloneableProperty() {
jtulach@1348
  5637
                boolean isSatisfiedBy(int ch) {
jtulach@1348
  5638
                    return Character.isISOControl(ch);}});
jtulach@1348
  5639
            defClone("javaMirrored", new CloneableProperty() {
jtulach@1348
  5640
                boolean isSatisfiedBy(int ch) {
jtulach@1348
  5641
                    return Character.isMirrored(ch);}});
jtulach@1348
  5642
        }
jtulach@1348
  5643
    }
jtulach@1350
  5644
    
jtulach@1350
  5645
    private static final class Normalizer {
jtulach@1350
  5646
        public static final int NFD = 1;
jtulach@1350
  5647
        public static final int NFC = 2;
jtulach@1350
  5648
jtulach@1350
  5649
        static String normalize(String pattern, int NFD) {
jtulach@1350
  5650
            return pattern;
jtulach@1350
  5651
        }
jtulach@1350
  5652
jtulach@1350
  5653
        private static int getCombiningClass(int c) {
jtulach@1350
  5654
            return 1;
jtulach@1350
  5655
        }
jtulach@1350
  5656
    }
jtulach@1348
  5657
}