1.1 --- a/src/jdk/nashorn/internal/runtime/RegExp.java Fri Feb 22 10:39:00 2013 -0400 1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 @@ -1,177 +0,0 @@ 1.4 -/* 1.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 - * 1.8 - * This code is free software; you can redistribute it and/or modify it 1.9 - * under the terms of the GNU General Public License version 2 only, as 1.10 - * published by the Free Software Foundation. Oracle designates this 1.11 - * particular file as subject to the "Classpath" exception as provided 1.12 - * by Oracle in the LICENSE file that accompanied this code. 1.13 - * 1.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 - * version 2 for more details (a copy is included in the LICENSE file that 1.18 - * accompanied this code). 1.19 - * 1.20 - * You should have received a copy of the GNU General Public License version 1.21 - * 2 along with this work; if not, write to the Free Software Foundation, 1.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 - * 1.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 - * or visit www.oracle.com if you need additional information or have any 1.26 - * questions. 1.27 - */ 1.28 - 1.29 -package jdk.nashorn.internal.runtime; 1.30 - 1.31 -import static java.util.regex.Pattern.CASE_INSENSITIVE; 1.32 -import static java.util.regex.Pattern.MULTILINE; 1.33 -import static java.util.regex.Pattern.UNICODE_CASE; 1.34 - 1.35 -import java.util.HashSet; 1.36 -import java.util.regex.Pattern; 1.37 -import java.util.regex.PatternSyntaxException; 1.38 - 1.39 -/** 1.40 - * This class is used to represent a parsed regular expression. Accepts input 1.41 - * pattern string and flagString. This is used by AbstractParser to validate 1.42 - * RegExp literals as well as by NativeRegExp to parse RegExp constructor arguments. 1.43 - */ 1.44 -public final class RegExp { 1.45 - /** Pattern string. */ 1.46 - private final String input; 1.47 - 1.48 - /** Global search flag for this regexp.*/ 1.49 - private boolean global; 1.50 - 1.51 - /** Case insensitive flag for this regexp */ 1.52 - private boolean ignoreCase; 1.53 - 1.54 - /** Multi-line flag for this regexp */ 1.55 - private boolean multiline; 1.56 - 1.57 - /** Java regexp pattern to use for match. We compile to one of these */ 1.58 - private Pattern pattern; 1.59 - 1.60 - /** BitVector that keeps track of groups in negative lookahead */ 1.61 - private BitVector groupsInNegativeLookahead; 1.62 - 1.63 - /** 1.64 - * Creates RegExpLiteral object from given input and flagString. 1.65 - * 1.66 - * @param input RegExp pattern string 1.67 - * @param flagString RegExp flags 1.68 - * @throws ParserException if flagString is invalid or input string has syntax error. 1.69 - */ 1.70 - public RegExp(final String input, final String flagString) throws ParserException { 1.71 - this.input = input; 1.72 - final HashSet<Character> usedFlags = new HashSet<>(); 1.73 - int flags = 0; 1.74 - 1.75 - for (final char ch : flagString.toCharArray()) { 1.76 - if (usedFlags.contains(ch)) { 1.77 - throwParserException("repeated.flag", Character.toString(ch)); 1.78 - } 1.79 - 1.80 - switch (ch) { 1.81 - case 'g': 1.82 - this.global = true; 1.83 - usedFlags.add(ch); 1.84 - break; 1.85 - case 'i': 1.86 - this.ignoreCase = true; 1.87 - flags |= CASE_INSENSITIVE | UNICODE_CASE; 1.88 - usedFlags.add(ch); 1.89 - break; 1.90 - case 'm': 1.91 - this.multiline = true; 1.92 - flags |= MULTILINE; 1.93 - usedFlags.add(ch); 1.94 - break; 1.95 - default: 1.96 - throwParserException("unsupported.flag", Character.toString(ch)); 1.97 - } 1.98 - } 1.99 - 1.100 - try { 1.101 - RegExpScanner parsed; 1.102 - 1.103 - try { 1.104 - parsed = RegExpScanner.scan(input); 1.105 - } catch (final PatternSyntaxException e) { 1.106 - // refine the exception with a better syntax error, if this 1.107 - // passes, just rethrow what we have 1.108 - Pattern.compile(input, flags); 1.109 - throw e; 1.110 - } 1.111 - 1.112 - if (parsed != null) { 1.113 - this.pattern = Pattern.compile(parsed.getJavaPattern(), flags); 1.114 - this.groupsInNegativeLookahead = parsed.getGroupsInNegativeLookahead(); 1.115 - } 1.116 - } catch (final PatternSyntaxException e2) { 1.117 - throwParserException("syntax", e2.getMessage()); 1.118 - } 1.119 - 1.120 - } 1.121 - 1.122 - /** 1.123 - * @return the input 1.124 - */ 1.125 - public String getInput() { 1.126 - return input; 1.127 - } 1.128 - 1.129 - /** 1.130 - * @return the global 1.131 - */ 1.132 - public boolean isGlobal() { 1.133 - return global; 1.134 - } 1.135 - 1.136 - /** 1.137 - * @return the ignoreCase 1.138 - */ 1.139 - public boolean isIgnoreCase() { 1.140 - return ignoreCase; 1.141 - } 1.142 - 1.143 - /** 1.144 - * @return the multiline 1.145 - */ 1.146 - public boolean isMultiline() { 1.147 - return multiline; 1.148 - } 1.149 - 1.150 - /** 1.151 - * @return the pattern 1.152 - */ 1.153 - public Pattern getPattern() { 1.154 - return pattern; 1.155 - } 1.156 - 1.157 - /** 1.158 - * @return the groupsInNegativeLookahead 1.159 - */ 1.160 - public BitVector getGroupsInNegativeLookahead() { 1.161 - return groupsInNegativeLookahead; 1.162 - } 1.163 - 1.164 - /** 1.165 - * Validation method for RegExp input and flagString - we don't care about the RegExp object 1.166 - * 1.167 - * @param input regexp input 1.168 - * @param flagString flag string 1.169 - * 1.170 - * @throws ParserException if invalid regexp and flags 1.171 - */ 1.172 - @SuppressWarnings({"unused", "ResultOfObjectAllocationIgnored"}) 1.173 - public static void validate(final String input, final String flagString) throws ParserException { 1.174 - new RegExp(input, flagString); 1.175 - } 1.176 - 1.177 - private static void throwParserException(final String key, final String str) throws ParserException { 1.178 - throw new ParserException(ECMAErrors.getMessage("parser.error.regex." + key, str)); 1.179 - } 1.180 -}