1 /* |
|
2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
|
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 |
|
26 package jdk.nashorn.internal.runtime.regexp; |
|
27 |
|
28 import jdk.nashorn.internal.runtime.ParserException; |
|
29 |
|
30 import static java.util.regex.Pattern.CASE_INSENSITIVE; |
|
31 import static java.util.regex.Pattern.MULTILINE; |
|
32 import static java.util.regex.Pattern.UNICODE_CASE; |
|
33 |
|
34 import java.util.regex.Matcher; |
|
35 import java.util.regex.Pattern; |
|
36 import java.util.regex.PatternSyntaxException; |
|
37 |
|
38 /** |
|
39 * Default regular expression implementation based on java.util.regex package. |
|
40 * |
|
41 * Note that this class is not thread-safe as it stores the current match result |
|
42 * and the string being matched in instance fields. |
|
43 */ |
|
44 public class DefaultRegExp extends RegExp { |
|
45 |
|
46 /** Java regexp pattern to use for match. We compile to one of these */ |
|
47 private Pattern pattern; |
|
48 |
|
49 /** The matcher */ |
|
50 private RegExpMatcher matcher; |
|
51 |
|
52 /** |
|
53 * Construct a Regular expression from the given {@code source} and {@code flags} strings. |
|
54 * |
|
55 * @param source RegExp source string |
|
56 * @param flags RegExp flag string |
|
57 * @throws ParserException if flags is invalid or source string has syntax error. |
|
58 */ |
|
59 public DefaultRegExp(final String source, final String flags) throws ParserException { |
|
60 super(source, flags); |
|
61 |
|
62 int intFlags = 0; |
|
63 |
|
64 if (isIgnoreCase()) { |
|
65 intFlags |= CASE_INSENSITIVE | UNICODE_CASE; |
|
66 } |
|
67 if (isMultiline()) { |
|
68 intFlags |= MULTILINE; |
|
69 } |
|
70 |
|
71 try { |
|
72 RegExpScanner parsed; |
|
73 |
|
74 try { |
|
75 parsed = RegExpScanner.scan(source); |
|
76 } catch (final PatternSyntaxException e) { |
|
77 // refine the exception with a better syntax error, if this |
|
78 // passes, just rethrow what we have |
|
79 Pattern.compile(source, intFlags); |
|
80 throw e; |
|
81 } |
|
82 |
|
83 if (parsed != null) { |
|
84 this.pattern = Pattern.compile(parsed.getJavaPattern(), intFlags); |
|
85 this.groupsInNegativeLookahead = parsed.getGroupsInNegativeLookahead(); |
|
86 } |
|
87 } catch (final PatternSyntaxException e2) { |
|
88 throwParserException("syntax", e2.getMessage()); |
|
89 } |
|
90 } |
|
91 |
|
92 @Override |
|
93 public RegExpMatcher match(final String str) { |
|
94 if (pattern == null) { |
|
95 return null; // never matches or similar, e.g. a[] |
|
96 } |
|
97 |
|
98 RegExpMatcher currentMatcher = this.matcher; |
|
99 |
|
100 if (currentMatcher == null || matcher.getInput() != str) { |
|
101 currentMatcher = new DefaultMatcher(str); |
|
102 this.matcher = currentMatcher; |
|
103 } |
|
104 |
|
105 return currentMatcher; |
|
106 } |
|
107 |
|
108 class DefaultMatcher implements RegExpMatcher { |
|
109 final String input; |
|
110 final Matcher defaultMatcher; |
|
111 |
|
112 DefaultMatcher(final String input) { |
|
113 this.input = input; |
|
114 this.defaultMatcher = pattern.matcher(input); |
|
115 } |
|
116 |
|
117 @Override |
|
118 public boolean search(final int start) { |
|
119 return defaultMatcher.find(start); |
|
120 } |
|
121 |
|
122 @Override |
|
123 public String getInput() { |
|
124 return input; |
|
125 } |
|
126 |
|
127 @Override |
|
128 public int start() { |
|
129 return defaultMatcher.start(); |
|
130 } |
|
131 |
|
132 @Override |
|
133 public int start(final int group) { |
|
134 return defaultMatcher.start(group); |
|
135 } |
|
136 |
|
137 @Override |
|
138 public int end() { |
|
139 return defaultMatcher.end(); |
|
140 } |
|
141 |
|
142 @Override |
|
143 public int end(final int group) { |
|
144 return defaultMatcher.end(group); |
|
145 } |
|
146 |
|
147 @Override |
|
148 public String group() { |
|
149 return defaultMatcher.group(); |
|
150 } |
|
151 |
|
152 @Override |
|
153 public String group(final int group) { |
|
154 return defaultMatcher.group(group); |
|
155 } |
|
156 |
|
157 @Override |
|
158 public int groupCount() { |
|
159 return defaultMatcher.groupCount(); |
|
160 } |
|
161 } |
|
162 |
|
163 } |
|