src/jdk/nashorn/internal/parser/RegExp.java

Print this page

        

@@ -23,29 +23,30 @@
  * questions.
  */
 
 package jdk.nashorn.internal.parser;
 
-import static java.util.regex.Pattern.CASE_INSENSITIVE;
-import static java.util.regex.Pattern.MULTILINE;
-import static java.util.regex.Pattern.UNICODE_CASE;
-
 import java.util.HashSet;
 import java.util.regex.Pattern;
 import java.util.regex.PatternSyntaxException;
 import jdk.nashorn.internal.runtime.BitVector;
 import jdk.nashorn.internal.runtime.ECMAErrors;
 import jdk.nashorn.internal.runtime.ParserException;
 
+import jdk.nashorn.internal.joni.Option;
+import jdk.nashorn.internal.joni.Regex;
+import jdk.nashorn.internal.joni.Syntax;
+import jdk.nashorn.internal.joni.exception.JOniException;
+
 /**
  * This class is used to represent a parsed regular expression. Accepts input
  * pattern string and flagString. This is used by AbstractParser to validate
  * RegExp literals as well as by NativeRegExp to parse RegExp constructor arguments.
  */
 public final class RegExp {
     /** Pattern string. */
-    private final String input;
+    private final String source;
 
     /** Global search flag for this regexp.*/
     private boolean global;
 
     /** Case insensitive flag for this regexp */

@@ -53,26 +54,26 @@
 
     /** Multi-line flag for this regexp */
     private boolean multiline;
 
     /** Java regexp pattern to use for match. We compile to one of these */
-    private Pattern pattern;
+    private Regex regex;
 
     /** BitVector that keeps track of groups in negative lookahead */
     private BitVector groupsInNegativeLookahead;
 
     /**
      * Creates RegExpLiteral object from given input and flagString.
      *
-     * @param input RegExp pattern string
+     * @param source RegExp pattern string
      * @param flagString RegExp flags
      * @throws ParserException if flagString is invalid or input string has syntax error.
      */
-    public RegExp(final String input, final String flagString) throws ParserException {
-        this.input = input;
+    public RegExp(final String source, final String flagString) throws ParserException {
+        this.source = source;
         final HashSet<Character> usedFlags = new HashSet<>();
-        int flags = 0;
+        int option = Option.SINGLELINE;
 
         for (final char ch : flagString.toCharArray()) {
             if (usedFlags.contains(ch)) {
                 throwParserException("repeated.flag", Character.toString(ch));
             }

@@ -82,16 +83,17 @@
                 this.global = true;
                 usedFlags.add(ch);
                 break;
             case 'i':
                 this.ignoreCase = true;
-                flags |= CASE_INSENSITIVE | UNICODE_CASE;
+                option |= Option.IGNORECASE;
                 usedFlags.add(ch);
                 break;
             case 'm':
                 this.multiline = true;
-                flags |= MULTILINE;
+                option &= ~Option.SINGLELINE;
+                option |= Option.NEGATE_SINGLELINE;
                 usedFlags.add(ch);
                 break;
             default:
                 throwParserException("unsupported.flag", Character.toString(ch));
             }

@@ -99,33 +101,36 @@
 
         try {
             RegExpScanner parsed;
 
             try {
-                parsed = RegExpScanner.scan(input);
+                parsed = RegExpScanner.scan(source);
             } catch (final PatternSyntaxException e) {
                 // refine the exception with a better syntax error, if this
                 // passes, just rethrow what we have
-                Pattern.compile(input, flags);
+                Pattern.compile(source, 0);
                 throw e;
             }
 
             if (parsed != null) {
-                this.pattern = Pattern.compile(parsed.getJavaPattern(), flags);
+                char[] javaPattern = parsed.getJavaPattern().toCharArray();
+                this.regex = new Regex(javaPattern, 0, javaPattern.length, option, Syntax.JAVASCRIPT);
                 this.groupsInNegativeLookahead = parsed.getGroupsInNegativeLookahead();
             }
         } catch (final PatternSyntaxException e2) {
             throwParserException("syntax", e2.getMessage());
+        } catch (JOniException e2) {
+            throwParserException("syntax", e2.getMessage());
         }
 
     }
 
     /**
-     * @return the input
+     * @return the source string
      */
-    public String getInput() {
-        return input;
+    public String getSource() {
+        return source;
     }
 
     /**
      * @return the global
      */

@@ -148,12 +153,12 @@
     }
 
     /**
      * @return the pattern
      */
-    public Pattern getPattern() {
-        return pattern;
+    public Regex getRegex() {
+        return regex;
     }
 
     /**
      * @return the groupsInNegativeLookahead
      */