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