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

Print this page

        

*** 23,51 **** * 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; /** * 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; /** Global search flag for this regexp.*/ private boolean global; /** Case insensitive flag for this regexp */ --- 23,52 ---- * questions. */ package jdk.nashorn.internal.parser; 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 source; /** Global search flag for this regexp.*/ private boolean global; /** Case insensitive flag for this regexp */
*** 53,78 **** /** 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; /** 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 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; final HashSet<Character> usedFlags = new HashSet<>(); ! int flags = 0; for (final char ch : flagString.toCharArray()) { if (usedFlags.contains(ch)) { throwParserException("repeated.flag", Character.toString(ch)); } --- 54,79 ---- /** Multi-line flag for this regexp */ private boolean multiline; /** Java regexp pattern to use for match. We compile to one of these */ ! private Regex regex; /** BitVector that keeps track of groups in negative lookahead */ private BitVector groupsInNegativeLookahead; /** * Creates RegExpLiteral object from given input and flagString. * ! * @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 source, final String flagString) throws ParserException { ! this.source = source; final HashSet<Character> usedFlags = new HashSet<>(); ! int option = Option.SINGLELINE; for (final char ch : flagString.toCharArray()) { if (usedFlags.contains(ch)) { throwParserException("repeated.flag", Character.toString(ch)); }
*** 82,97 **** this.global = true; usedFlags.add(ch); break; case 'i': this.ignoreCase = true; ! flags |= CASE_INSENSITIVE | UNICODE_CASE; usedFlags.add(ch); break; case 'm': this.multiline = true; ! flags |= MULTILINE; usedFlags.add(ch); break; default: throwParserException("unsupported.flag", Character.toString(ch)); } --- 83,99 ---- this.global = true; usedFlags.add(ch); break; case 'i': this.ignoreCase = true; ! option |= Option.IGNORECASE; usedFlags.add(ch); break; case 'm': this.multiline = true; ! option &= ~Option.SINGLELINE; ! option |= Option.NEGATE_SINGLELINE; usedFlags.add(ch); break; default: throwParserException("unsupported.flag", Character.toString(ch)); }
*** 99,131 **** try { RegExpScanner parsed; try { ! parsed = RegExpScanner.scan(input); } catch (final PatternSyntaxException e) { // refine the exception with a better syntax error, if this // passes, just rethrow what we have ! Pattern.compile(input, flags); throw e; } if (parsed != null) { ! this.pattern = Pattern.compile(parsed.getJavaPattern(), flags); this.groupsInNegativeLookahead = parsed.getGroupsInNegativeLookahead(); } } catch (final PatternSyntaxException e2) { throwParserException("syntax", e2.getMessage()); } } /** ! * @return the input */ ! public String getInput() { ! return input; } /** * @return the global */ --- 101,136 ---- try { RegExpScanner parsed; try { ! 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(source, 0); throw e; } if (parsed != null) { ! 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 source string */ ! public String getSource() { ! return source; } /** * @return the global */
*** 148,159 **** } /** * @return the pattern */ ! public Pattern getPattern() { ! return pattern; } /** * @return the groupsInNegativeLookahead */ --- 153,164 ---- } /** * @return the pattern */ ! public Regex getRegex() { ! return regex; } /** * @return the groupsInNegativeLookahead */