Skip to content

Commit 2af3567

Browse files
committed
TRegex: reorganize flavor implementations.
1 parent b1fea44 commit 2af3567

File tree

73 files changed

+825
-575
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+825
-575
lines changed

regex/mx.regex/suite.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
# SOFTWARE.
4040
#
4141
suite = {
42-
"mxversion": "7.55.2",
42+
"mxversion": "7.58.0",
4343

4444
"name" : "regex",
4545

regex/src/com.oracle.truffle.regex.test/src/com/oracle/truffle/regex/tregex/parser/flavors/PythonFlagsTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444

4545
import org.junit.Test;
4646

47+
import com.oracle.truffle.regex.flavor.python.PythonFlags;
48+
4749
public class PythonFlagsTest {
4850

4951
private static PythonFlags parse(String flags) {

regex/src/com.oracle.truffle.regex.test/src/com/oracle/truffle/regex/tregex/test/JavaUtilPatternTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
import com.oracle.truffle.regex.RegexSyntaxException.ErrorCode;
6262
import com.oracle.truffle.regex.charset.Range;
6363
import com.oracle.truffle.regex.tregex.parser.CaseFoldData;
64-
import com.oracle.truffle.regex.tregex.parser.flavors.java.JavaFlags;
64+
import com.oracle.truffle.regex.flavor.java.JavaFlags;
6565
import com.oracle.truffle.regex.tregex.string.Encodings;
6666
import com.oracle.truffle.regex.util.EmptyArrays;
6767

regex/src/com.oracle.truffle.regex.test/src/com/oracle/truffle/regex/tregex/test/PythonTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
import org.junit.Assert;
4949
import org.junit.Test;
5050

51-
import com.oracle.truffle.regex.errors.PyErrorMessages;
51+
import com.oracle.truffle.regex.flavor.python.PyErrorMessages;
5252
import com.oracle.truffle.regex.tregex.TRegexOptions;
5353
import com.oracle.truffle.regex.tregex.string.Encodings;
5454

regex/src/com.oracle.truffle.regex.test/src/com/oracle/truffle/regex/tregex/test/RegexOptionsTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@
5656
import com.oracle.truffle.regex.RegexLanguage;
5757
import com.oracle.truffle.regex.RegexOptions;
5858
import com.oracle.truffle.regex.RegexSyntaxException;
59+
import com.oracle.truffle.regex.flavor.js.ECMAScriptFlavor;
60+
import com.oracle.truffle.regex.flavor.python.PythonFlavor;
61+
import com.oracle.truffle.regex.flavor.ruby.RubyFlavor;
5962
import com.oracle.truffle.regex.test.dummylang.TRegexTestDummyLanguage;
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.PythonFlavor;
63-
import com.oracle.truffle.regex.tregex.parser.flavors.RubyFlavor;
63+
import com.oracle.truffle.regex.tregex.parser.MatchingMode;
6464
import com.oracle.truffle.regex.tregex.string.Encodings;
6565

6666
public class RegexOptionsTest extends RegexTestBase {

regex/src/com.oracle.truffle.regex.test/src/com/oracle/truffle/regex/tregex/test/RubyTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
import org.junit.Assert;
4646
import org.junit.Test;
4747

48-
import com.oracle.truffle.regex.errors.RbErrorMessages;
48+
import com.oracle.truffle.regex.flavor.ruby.RbErrorMessages;
4949
import com.oracle.truffle.regex.tregex.string.Encodings;
5050

5151
public class RubyTests extends RegexTestBase {

regex/src/com.oracle.truffle.regex/src/com/oracle/truffle/regex/AbstractRegexObject.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
*/
4141
package com.oracle.truffle.regex;
4242

43+
import java.util.LinkedHashMap;
44+
import java.util.List;
45+
import java.util.Map;
46+
4347
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4448
import com.oracle.truffle.api.TruffleLanguage;
4549
import com.oracle.truffle.api.interop.InteropLibrary;
@@ -51,10 +55,6 @@
5155
import com.oracle.truffle.regex.util.TruffleReadOnlyMap;
5256
import com.oracle.truffle.regex.util.TruffleSmallReadOnlyStringToIntMap;
5357

54-
import java.util.LinkedHashMap;
55-
import java.util.List;
56-
import java.util.Map;
57-
5858
@ExportLibrary(InteropLibrary.class)
5959
public abstract class AbstractRegexObject implements TruffleObject {
6060

regex/src/com.oracle.truffle.regex/src/com/oracle/truffle/regex/RegexLanguage.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,10 @@
5757
import com.oracle.truffle.regex.analysis.InputStringGenerator;
5858
import com.oracle.truffle.regex.tregex.TRegexCompiler;
5959
import com.oracle.truffle.regex.tregex.buffer.CompilationBuffer;
60+
import com.oracle.truffle.regex.tregex.parser.RegexFlavor;
6061
import com.oracle.truffle.regex.tregex.parser.RegexParser;
61-
import com.oracle.truffle.regex.tregex.parser.RegexParserGlobals;
6262
import com.oracle.truffle.regex.tregex.parser.RegexValidator;
6363
import com.oracle.truffle.regex.tregex.parser.ast.GroupBoundaries;
64-
import com.oracle.truffle.regex.tregex.parser.flavors.ECMAScriptFlavor;
65-
import com.oracle.truffle.regex.tregex.parser.flavors.RegexFlavor;
6664
import com.oracle.truffle.regex.tregex.string.Encodings;
6765
import com.oracle.truffle.regex.util.TruffleNull;
6866

@@ -146,11 +144,9 @@ public final class RegexLanguage extends TruffleLanguage<RegexLanguage.RegexCont
146144
public static final String MIME_TYPE = "application/tregex";
147145

148146
private final GroupBoundaries[] cachedGroupBoundaries;
149-
public final RegexParserGlobals parserGlobals;
150147

151148
public RegexLanguage() {
152149
this.cachedGroupBoundaries = GroupBoundaries.createCachedGroupBoundaries();
153-
this.parserGlobals = new RegexParserGlobals(this);
154150
}
155151

156152
public GroupBoundaries[] getCachedGroupBoundaries() {
@@ -184,7 +180,7 @@ private static RegexSource createRegexSource(ParsingRequest parsingRequest) {
184180
String pattern = srcStr.substring(firstSlash + 1, lastSlash);
185181
String flags = srcStr.substring(lastSlash + 1);
186182
// ECMAScript-specific: the 'u' and 'v' flags change the encoding
187-
if (optBuilder.getFlavor() == ECMAScriptFlavor.INSTANCE) {
183+
if (optBuilder.getFlavor().getName().equals(RegexOptions.FLAVOR_ECMASCRIPT)) {
188184
if (flags.indexOf('u') >= 0 || flags.indexOf('v') >= 0) {
189185
if (!optBuilder.isUtf16ExplodeAstralSymbols() && optBuilder.getEncoding() == Encodings.UTF_16_RAW) {
190186
optBuilder.encoding(Encodings.UTF_16);

regex/src/com.oracle.truffle.regex/src/com/oracle/truffle/regex/RegexObject.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,6 @@
6868
import com.oracle.truffle.regex.literal.LiteralRegexExecNode;
6969
import com.oracle.truffle.regex.result.RegexResult;
7070
import com.oracle.truffle.regex.tregex.TRegexCompilationRequest;
71-
import com.oracle.truffle.regex.tregex.parser.flavors.OracleDBFlags;
72-
import com.oracle.truffle.regex.tregex.parser.flavors.PythonFlags;
73-
import com.oracle.truffle.regex.tregex.parser.flavors.RubyFlags;
74-
import com.oracle.truffle.regex.tregex.parser.flavors.java.JavaFlags;
7571
import com.oracle.truffle.regex.util.TruffleReadOnlyKeysArray;
7672

7773
/**
@@ -84,10 +80,10 @@
8480
* compiler. The type differs based on the flavor of regular expressions used:
8581
* <ul>
8682
* <li>{@link RegexFlags} if the flavor was {@code ECMAScript}</li>
87-
* <li>{@link JavaFlags} if the flavor was {@code JavaUtilPattern}</li>
88-
* <li>{@link OracleDBFlags} if the flavor was {@code OracleDB}</li>
89-
* <li>{@link PythonFlags} if the flavor was {@code Python}</li>
90-
* <li>{@link RubyFlags} if the flavor was {@code Ruby}</li>
83+
* <li>{@code JavaFlags} if the flavor was {@code JavaUtilPattern}</li>
84+
* <li>{@code OracleDBFlags} if the flavor was {@code OracleDB}</li>
85+
* <li>{@code PythonFlags} if the flavor was {@code Python}</li>
86+
* <li>{@code RubyFlags} if the flavor was {@code Ruby}</li>
9187
* </ul>
9288
* </li>
9389
* <li>{@code int groupCount}: number of capture groups present in the regular expression, including

regex/src/com.oracle.truffle.regex/src/com/oracle/truffle/regex/RegexOptions.java

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@
5555
import com.oracle.truffle.api.Option;
5656
import com.oracle.truffle.api.TruffleLanguage;
5757
import 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;
5863
import com.oracle.truffle.regex.result.RegexResult;
5964
import 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;
6767
import 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

Comments
 (0)