5555import com .oracle .truffle .api .Option ;
5656import com .oracle .truffle .api .TruffleLanguage ;
5757import com .oracle .truffle .api .source .Source ;
58+ import com .oracle .truffle .regex .flavor .java .JavaFlavorProvider ;
59+ import com .oracle .truffle .regex .flavor .js .JSFlavorProvider ;
60+ import com .oracle .truffle .regex .flavor .oracledb .OracleDBFlavorProvider ;
61+ import com .oracle .truffle .regex .flavor .python .PythonFlavorProvider ;
62+ import com .oracle .truffle .regex .flavor .ruby .RubyFlavorProvider ;
5863import com .oracle .truffle .regex .result .RegexResult ;
5964import com .oracle .truffle .regex .tregex .TRegexOptions ;
60- import com .oracle .truffle .regex .tregex .parser .flavors .ECMAScriptFlavor ;
61- import com .oracle .truffle .regex .tregex .parser .flavors .MatchingMode ;
62- import com .oracle .truffle .regex .tregex .parser .flavors .OracleDBFlavor ;
63- import com .oracle .truffle .regex .tregex .parser .flavors .PythonFlavor ;
64- import com .oracle .truffle .regex .tregex .parser .flavors .RegexFlavor ;
65- import com .oracle .truffle .regex .tregex .parser .flavors .RubyFlavor ;
66- import com .oracle .truffle .regex .tregex .parser .flavors .java .JavaFlavor ;
65+ import com .oracle .truffle .regex .tregex .parser .MatchingMode ;
66+ import com .oracle .truffle .regex .tregex .parser .RegexFlavor ;
6767import com .oracle .truffle .regex .tregex .string .Encodings ;
6868
6969/**
@@ -201,8 +201,18 @@ public final class RegexOptions {
201201 public static final String FLAVOR_ORACLE_DB = "OracleDB" ;
202202 public static final String FLAVOR_ECMASCRIPT = "ECMAScript" ;
203203 public static final String FLAVOR_JAVA = "JavaUtilPattern" ;
204+
205+ private static final RegexFlavor [] FLAVOR_CACHE = new RegexFlavor [FlavorOption .values ().length ];
204206 private static final String [] FLAVOR_OPTIONS = {FLAVOR_PYTHON , FLAVOR_RUBY , FLAVOR_ORACLE_DB , FLAVOR_ECMASCRIPT , FLAVOR_JAVA };
205207
208+ static {
209+ FLAVOR_CACHE [FlavorOption .ECMAScript .ordinal ()] = new JSFlavorProvider ().get ();
210+ FLAVOR_CACHE [FlavorOption .Python .ordinal ()] = new PythonFlavorProvider ().get ();
211+ FLAVOR_CACHE [FlavorOption .Ruby .ordinal ()] = new RubyFlavorProvider ().get ();
212+ FLAVOR_CACHE [FlavorOption .OracleDB .ordinal ()] = new OracleDBFlavorProvider ().get ();
213+ FLAVOR_CACHE [FlavorOption .JavaUtilPattern .ordinal ()] = new JavaFlavorProvider ().get ();
214+ }
215+
206216 public enum FlavorOption {
207217 ECMAScript ,
208218 Python ,
@@ -211,16 +221,14 @@ public enum FlavorOption {
211221 JavaUtilPattern ;
212222
213223 RegexFlavor get () {
214- return switch (this ) {
215- case ECMAScript -> ECMAScriptFlavor .INSTANCE ;
216- case Python -> PythonFlavor .INSTANCE ;
217- case Ruby -> RubyFlavor .INSTANCE ;
218- case OracleDB -> OracleDBFlavor .INSTANCE ;
219- case JavaUtilPattern -> JavaFlavor .INSTANCE ;
220- };
224+ return FLAVOR_CACHE [ordinal ()];
221225 }
222226 }
223227
228+ private static RegexFlavor getDefaultFlavor () {
229+ return FlavorOption .ECMAScript .get ();
230+ }
231+
224232 @ Option (category = OptionCategory .USER , stability = OptionStability .STABLE , help = "Regex flavor to use." , usageSyntax = "ECMAScript|JavaUtilPattern|OracleDB|Python|Ruby" ) //
225233 public static final OptionKey <FlavorOption > Flavor = new OptionKey <>(FlavorOption .ECMAScript );
226234
@@ -246,7 +254,7 @@ RegexFlavor get() {
246254 "'search': Default. Search for a match anywhere in the input string. " +
247255 "'match': Anchor match at starting index. " +
248256 "'fullmatch': Anchor match at starting and end index." , usageSyntax = "search|match|fullmatch" ) //
249- public static final OptionKey <MatchingMode > MatchingMode = new OptionKey <>(com .oracle .truffle .regex .tregex .parser .flavors . MatchingMode .search );
257+ public static final OptionKey <MatchingMode > MatchingMode = new OptionKey <>(com .oracle .truffle .regex .tregex .parser .MatchingMode .search );
250258
251259 public static final String PYTHON_LOCALE_NAME = "PythonLocale" ;
252260
@@ -286,7 +294,7 @@ public static OptionDescriptors getDescriptors() {
286294 public static final RegexOptions DEFAULT = new RegexOptions (0 ,
287295 (short ) TRegexOptions .TRegexMaxDFATransitions ,
288296 (short ) TRegexOptions .TRegexMaxBackTrackerMergeExplodeSize ,
289- ECMAScriptFlavor . INSTANCE ,
297+ getDefaultFlavor () ,
290298 Encodings .UTF_16_RAW , null , null , JAVA_JDK_VERSION_DEFAULT ,
291299 (short ) TRegexOptions .TRegexQuantifierUnrollLimitSingleCC ,
292300 (short ) TRegexOptions .TRegexQuantifierUnrollLimitGroup );
@@ -370,7 +378,7 @@ public boolean isDumpAutomata() {
370378 }
371379
372380 public boolean isDumpAutomataWithSourceSections () {
373- return isDumpAutomata () && (getFlavor () == ECMAScriptFlavor . INSTANCE || getFlavor () == OracleDBFlavor . INSTANCE );
381+ return isDumpAutomata () && (getFlavor (). getName (). equals ( FLAVOR_ECMASCRIPT ) || getFlavor (). getName (). equals ( FLAVOR_ORACLE_DB ) );
374382 }
375383
376384 /**
@@ -465,7 +473,7 @@ public String getPythonLocale() {
465473 }
466474
467475 /**
468- * JDK compatibility version for {@link JavaFlavor}.
476+ * JDK compatibility version for {@code JavaFlavor}.
469477 */
470478 public int getJavaJDKVersion () {
471479 return javaJDKVersion ;
@@ -560,15 +568,7 @@ public String toString() {
560568 if (isMustAdvance ()) {
561569 sb .append (MUST_ADVANCE_NAME + "=true," );
562570 }
563- if (flavor == PythonFlavor .INSTANCE ) {
564- sb .append (FLAVOR_NAME + "=" + FLAVOR_PYTHON + "," );
565- } else if (flavor == RubyFlavor .INSTANCE ) {
566- sb .append (FLAVOR_NAME + "=" + FLAVOR_RUBY + "," );
567- } else if (flavor == OracleDBFlavor .INSTANCE ) {
568- sb .append (FLAVOR_NAME + "=" + FLAVOR_ORACLE_DB + "," );
569- } else if (flavor == JavaFlavor .INSTANCE ) {
570- sb .append (FLAVOR_NAME + "=" + FLAVOR_JAVA + "," );
571- }
571+ sb .append (FLAVOR_NAME ).append ('=' ).append (flavor .getName ()).append (',' );
572572 sb .append (ENCODING_NAME + "=" ).append (encoding .getName ()).append ("," );
573573 if (matchingMode != null ) {
574574 sb .append (MATCHING_MODE_NAME ).append ('=' ).append (matchingMode ).append (',' );
@@ -611,7 +611,7 @@ private Builder(Source source, String sourceString, OptionValues optionValues) {
611611 this .src = sourceString ;
612612 this .optionValues = optionValues ;
613613 this .flags = 0 ;
614- this .flavor = ECMAScriptFlavor . INSTANCE ;
614+ this .flavor = getDefaultFlavor () ;
615615 quantifierUnrollThresholdSingleCC = DEFAULT .quantifierUnrollLimitSingleCC ;
616616 quantifierUnrollThresholdGroup = DEFAULT .quantifierUnrollLimitGroup ;
617617 }
@@ -841,15 +841,15 @@ private RegexFlavor parseFlavor() throws RegexSyntaxException {
841841 }
842842 switch (src .charAt (i )) {
843843 case 'E' :
844- return expectValue (ECMAScriptFlavor . INSTANCE , FLAVOR_ECMASCRIPT , FLAVOR_OPTIONS );
844+ return expectValue (FlavorOption . ECMAScript , FLAVOR_ECMASCRIPT , FLAVOR_OPTIONS ). get ( );
845845 case 'J' :
846- return expectValue (JavaFlavor . INSTANCE , FLAVOR_JAVA , FLAVOR_OPTIONS );
846+ return expectValue (FlavorOption . JavaUtilPattern , FLAVOR_JAVA , FLAVOR_OPTIONS ). get ( );
847847 case 'R' :
848- return expectValue (RubyFlavor . INSTANCE , FLAVOR_RUBY , FLAVOR_OPTIONS );
848+ return expectValue (FlavorOption . Ruby , FLAVOR_RUBY , FLAVOR_OPTIONS ). get ( );
849849 case 'O' :
850- return expectValue (OracleDBFlavor . INSTANCE , FLAVOR_ORACLE_DB , FLAVOR_OPTIONS );
850+ return expectValue (FlavorOption . OracleDB , FLAVOR_ORACLE_DB , FLAVOR_OPTIONS ). get ( );
851851 case 'P' :
852- return expectValue (PythonFlavor . INSTANCE , FLAVOR_PYTHON , FLAVOR_OPTIONS );
852+ return expectValue (FlavorOption . Python , FLAVOR_PYTHON , FLAVOR_OPTIONS ). get ( );
853853 default :
854854 throw optionsSyntaxErrorUnexpectedValue (FLAVOR_OPTIONS );
855855 }
@@ -894,11 +894,11 @@ private MatchingMode parseMatchingMode(String optionName) throws RegexSyntaxExce
894894 }
895895 switch (src .charAt (i )) {
896896 case 's' :
897- return expectValue (com .oracle .truffle .regex .tregex .parser .flavors . MatchingMode .search , MATCHING_MODE_SEARCH , MATCHING_MODE_OPTIONS );
897+ return expectValue (com .oracle .truffle .regex .tregex .parser .MatchingMode .search , MATCHING_MODE_SEARCH , MATCHING_MODE_OPTIONS );
898898 case 'm' :
899- return expectValue (com .oracle .truffle .regex .tregex .parser .flavors . MatchingMode .match , MATCHING_MODE_MATCH , MATCHING_MODE_OPTIONS );
899+ return expectValue (com .oracle .truffle .regex .tregex .parser .MatchingMode .match , MATCHING_MODE_MATCH , MATCHING_MODE_OPTIONS );
900900 case 'f' :
901- return expectValue (com .oracle .truffle .regex .tregex .parser .flavors . MatchingMode .fullmatch , MATCHING_MODE_FULLMATCH , MATCHING_MODE_OPTIONS );
901+ return expectValue (com .oracle .truffle .regex .tregex .parser .MatchingMode .fullmatch , MATCHING_MODE_FULLMATCH , MATCHING_MODE_OPTIONS );
902902 default :
903903 throw optionsSyntaxErrorUnexpectedValue (MATCHING_MODE_OPTIONS );
904904 }
0 commit comments