Skip to content

Commit 5f51922

Browse files
committed
Remove duplicate preprocess for static Regexp literals
1 parent 94aa446 commit 5f51922

File tree

2 files changed

+10
-22
lines changed

2 files changed

+10
-22
lines changed

src/main/java/org/truffleruby/core/regexp/ClassicRegexp.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -951,22 +951,8 @@ public static void appendRegexpString(TStringBuilder to, TStringWithEncoding ful
951951
// Code that used to be in ParserSupport but copied here as ParserSupport is coupled with the JRuby lexer & parser.
952952
// Needed until https://github.com/ruby/prism/issues/1997 is fixed.
953953

954-
// From ParserSupport#newRegexpNode
955-
public static TStringWithEncoding findEncodingForRegexpLiteral(TStringWithEncoding regexp, RegexpOptions options,
956-
RubyEncoding lexerEncoding, Node currentNode) throws DeferredRaiseException {
957-
return regexpFragmentCheck(regexp, options, lexerEncoding, currentNode);
958-
}
959-
960-
// MRI: reg_fragment_check
961-
public static TStringWithEncoding regexpFragmentCheck(TStringWithEncoding value, RegexpOptions options,
962-
RubyEncoding lexerEncoding, Node currentNode) throws DeferredRaiseException {
963-
final TStringWithEncoding strEnc = setRegexpEncoding(value, options, lexerEncoding, currentNode);
964-
ClassicRegexp.preprocessCheck(strEnc);
965-
return strEnc;
966-
}
967-
968954
// MRI: reg_fragment_setenc_gen
969-
private static TStringWithEncoding setRegexpEncoding(TStringWithEncoding value, RegexpOptions options,
955+
public static TStringWithEncoding setRegexpEncoding(TStringWithEncoding value, RegexpOptions options,
970956
RubyEncoding lexerEncoding, Node currentNode) throws DeferredRaiseException {
971957
options = options.setup();
972958
final RubyEncoding optionsEncoding = options.getEncoding() == null

src/main/java/org/truffleruby/parser/YARPTranslator.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2325,10 +2325,11 @@ public RubyNode visitInterpolatedMatchLastLineNode(Nodes.InterpolatedMatchLastLi
23252325
@Override
23262326
public RubyNode visitInterpolatedRegularExpressionNode(Nodes.InterpolatedRegularExpressionNode node) {
23272327
var encodingAndOptions = getRegexpEncodingAndOptions(new Nodes.RegularExpressionFlags(node.flags));
2328+
var options = encodingAndOptions.options;
23282329
final ToSNode[] children = translateInterpolatedParts(node.parts);
23292330

23302331
final RubyEncoding encoding;
2331-
if (!encodingAndOptions.options.isKcodeDefault()) { // explicit encoding
2332+
if (!options.isKcodeDefault()) { // explicit encoding
23322333
encoding = encodingAndOptions.encoding;
23332334
} else {
23342335
// use BINARY explicitly probably because forcing encoding isn't implemented yet in Prism
@@ -2341,15 +2342,16 @@ public RubyNode visitInterpolatedRegularExpressionNode(Nodes.InterpolatedRegular
23412342
if (child.getValueNode() instanceof StringLiteralNode stringLiteralNode) {
23422343
var fragment = new TStringWithEncoding(stringLiteralNode.getTString(), stringLiteralNode.getEncoding());
23432344
try {
2344-
ClassicRegexp.regexpFragmentCheck(fragment, encodingAndOptions.options, sourceEncoding,
2345-
currentNode);
2345+
// MRI: reg_fragment_check
2346+
var strEnc = ClassicRegexp.setRegexpEncoding(fragment, options, sourceEncoding, currentNode);
2347+
ClassicRegexp.preprocessCheck(strEnc);
23462348
} catch (DeferredRaiseException dre) {
23472349
throw regexpErrorToSyntaxError(dre, node);
23482350
}
23492351
}
23502352
}
23512353

2352-
RubyNode rubyNode = new InterpolatedRegexpNode(children, encoding, encodingAndOptions.options);
2354+
RubyNode rubyNode = new InterpolatedRegexpNode(children, encoding, options);
23532355

23542356
if (node.isOnce()) {
23552357
rubyNode = new OnceNode(rubyNode);
@@ -2892,17 +2894,17 @@ public RubyNode visitRedoNode(Nodes.RedoNode node) {
28922894
public RubyNode visitRegularExpressionNode(Nodes.RegularExpressionNode node) {
28932895
var encodingAndOptions = getRegexpEncodingAndOptions(new Nodes.RegularExpressionFlags(node.flags));
28942896
var encoding = encodingAndOptions.encoding;
2897+
var options = encodingAndOptions.options;
28952898
var source = TruffleString.fromByteArrayUncached(node.unescaped, encoding.tencoding, false);
28962899
var sourceWithEnc = new TStringWithEncoding(source, encoding);
28972900

28982901
final RubyRegexp regexp;
28992902
try {
29002903
// Needed until https://github.com/ruby/prism/issues/1997 is fixed
2901-
sourceWithEnc = ClassicRegexp.findEncodingForRegexpLiteral(sourceWithEnc, encodingAndOptions.options,
2902-
sourceEncoding, currentNode);
2904+
sourceWithEnc = ClassicRegexp.setRegexpEncoding(sourceWithEnc, options, sourceEncoding, currentNode);
29032905

29042906
regexp = RubyRegexp.create(language, sourceWithEnc.tstring, sourceWithEnc.encoding,
2905-
encodingAndOptions.options, currentNode);
2907+
options, currentNode);
29062908
} catch (DeferredRaiseException dre) {
29072909
throw regexpErrorToSyntaxError(dre, node);
29082910
}

0 commit comments

Comments
 (0)