author | hannesw |
Thu, 22 Aug 2013 17:23:50 +0200 | |
changeset 19621 | 1b2a79d8924c |
parent 16525 | 1409942e618e |
child 24778 | 2ff5d7041566 |
permissions | -rw-r--r-- |
16147 | 1 |
/* |
16151 | 2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
16147 | 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. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
16258 | 26 |
package jdk.nashorn.internal.runtime.regexp; |
16147 | 27 |
|
16258 | 28 |
import jdk.nashorn.internal.runtime.BitVector; |
29 |
import jdk.nashorn.internal.runtime.ECMAErrors; |
|
30 |
import jdk.nashorn.internal.runtime.ParserException; |
|
16147 | 31 |
|
16258 | 32 |
import java.util.regex.MatchResult; |
16147 | 33 |
|
34 |
/** |
|
16258 | 35 |
* This is the base class for representing a parsed regular expression. |
36 |
* |
|
37 |
* Instances of this class are created by a {@link RegExpFactory}. |
|
16147 | 38 |
*/ |
16258 | 39 |
public abstract class RegExp { |
40 |
||
16147 | 41 |
/** Pattern string. */ |
16258 | 42 |
private final String source; |
16147 | 43 |
|
44 |
/** Global search flag for this regexp.*/ |
|
45 |
private boolean global; |
|
46 |
||
47 |
/** Case insensitive flag for this regexp */ |
|
48 |
private boolean ignoreCase; |
|
49 |
||
50 |
/** Multi-line flag for this regexp */ |
|
51 |
private boolean multiline; |
|
52 |
||
53 |
/** BitVector that keeps track of groups in negative lookahead */ |
|
16258 | 54 |
protected BitVector groupsInNegativeLookahead; |
16147 | 55 |
|
56 |
/** |
|
16258 | 57 |
* Constructor. |
16147 | 58 |
* |
16258 | 59 |
* @param source the source string |
60 |
* @param flags the flags string |
|
16147 | 61 |
*/ |
16258 | 62 |
protected RegExp(final String source, final String flags) { |
19621
1b2a79d8924c
8023531: new RegExp('').toString() should return '/(?:)/'
hannesw
parents:
16525
diff
changeset
|
63 |
this.source = source.length() == 0 ? "(?:)" : source; |
16258 | 64 |
for (int i = 0; i < flags.length(); i++) { |
65 |
final char ch = flags.charAt(i); |
|
16147 | 66 |
switch (ch) { |
67 |
case 'g': |
|
16258 | 68 |
if (this.global) { |
69 |
throwParserException("repeated.flag", "g"); |
|
70 |
} |
|
16147 | 71 |
this.global = true; |
72 |
break; |
|
73 |
case 'i': |
|
16258 | 74 |
if (this.ignoreCase) { |
75 |
throwParserException("repeated.flag", "i"); |
|
76 |
} |
|
16147 | 77 |
this.ignoreCase = true; |
78 |
break; |
|
79 |
case 'm': |
|
16258 | 80 |
if (this.multiline) { |
81 |
throwParserException("repeated.flag", "m"); |
|
82 |
} |
|
16147 | 83 |
this.multiline = true; |
84 |
break; |
|
85 |
default: |
|
86 |
throwParserException("unsupported.flag", Character.toString(ch)); |
|
87 |
} |
|
88 |
} |
|
16258 | 89 |
} |
16147 | 90 |
|
16258 | 91 |
/** |
92 |
* Get the source pattern of this regular expression. |
|
93 |
* |
|
94 |
* @return the source string |
|
95 |
*/ |
|
96 |
public String getSource() { |
|
97 |
return source; |
|
16147 | 98 |
} |
99 |
||
100 |
/** |
|
16258 | 101 |
* Set the global flag of this regular expression to {@code global}. |
102 |
* |
|
103 |
* @param global the new global flag |
|
16147 | 104 |
*/ |
16258 | 105 |
public void setGlobal(final boolean global) { |
106 |
this.global = global; |
|
16147 | 107 |
} |
108 |
||
109 |
/** |
|
16258 | 110 |
* Get the global flag of this regular expression. |
111 |
* |
|
112 |
* @return the global flag |
|
16147 | 113 |
*/ |
114 |
public boolean isGlobal() { |
|
115 |
return global; |
|
116 |
} |
|
117 |
||
118 |
/** |
|
16258 | 119 |
* Get the ignore-case flag of this regular expression. |
120 |
* |
|
121 |
* @return the ignore-case flag |
|
16147 | 122 |
*/ |
123 |
public boolean isIgnoreCase() { |
|
124 |
return ignoreCase; |
|
125 |
} |
|
126 |
||
127 |
/** |
|
16258 | 128 |
* Get the multiline flag of this regular expression. |
129 |
* |
|
130 |
* @return the multiline flag |
|
16147 | 131 |
*/ |
132 |
public boolean isMultiline() { |
|
133 |
return multiline; |
|
134 |
} |
|
135 |
||
136 |
/** |
|
16258 | 137 |
* Get a bitset indicating which of the groups in this regular expression are inside a negative lookahead. |
138 |
* |
|
139 |
* @return the groups-in-negative-lookahead bitset |
|
16147 | 140 |
*/ |
141 |
public BitVector getGroupsInNegativeLookahead() { |
|
142 |
return groupsInNegativeLookahead; |
|
143 |
} |
|
144 |
||
145 |
/** |
|
16258 | 146 |
* Match this regular expression against {@code str}, starting at index {@code start} |
147 |
* and return a {@link MatchResult} with the result. |
|
16147 | 148 |
* |
16258 | 149 |
* @param str the string |
150 |
* @return the matcher |
|
16147 | 151 |
*/ |
16258 | 152 |
public abstract RegExpMatcher match(String str); |
16147 | 153 |
|
16258 | 154 |
/** |
155 |
* Throw a regexp parser exception. |
|
156 |
* |
|
157 |
* @param key the message key |
|
158 |
* @param str string argument |
|
16525
1409942e618e
8009982: Lazy execution bugfix. Added lazy sunspider unit test. Added mandreel to compile-octane test. Fixed warnings
lagergren
parents:
16258
diff
changeset
|
159 |
* @throws jdk.nashorn.internal.runtime.ParserException unconditionally |
16258 | 160 |
*/ |
161 |
protected static void throwParserException(final String key, final String str) throws ParserException { |
|
16147 | 162 |
throw new ParserException(ECMAErrors.getMessage("parser.error.regex." + key, str)); |
163 |
} |
|
164 |
} |