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