39
39
import org .truffleruby .core .hash .HashLiteralNode ;
40
40
import org .truffleruby .core .module .ModuleNodes ;
41
41
import org .truffleruby .core .numeric .RubyBignum ;
42
+ import org .truffleruby .core .regexp .InterpolatedRegexpNode ;
42
43
import org .truffleruby .core .regexp .RegexpOptions ;
43
44
import org .truffleruby .core .regexp .RubyRegexp ;
44
45
import org .truffleruby .core .rescue .AssignRescueVariableNode ;
77
78
import org .truffleruby .language .control .LocalReturnNode ;
78
79
import org .truffleruby .language .control .NextNode ;
79
80
import org .truffleruby .language .control .NotNodeGen ;
81
+ import org .truffleruby .language .control .OnceNode ;
80
82
import org .truffleruby .language .control .OrNodeGen ;
81
83
import org .truffleruby .language .control .RaiseException ;
82
84
import org .truffleruby .language .control .RedoNode ;
@@ -1514,19 +1516,26 @@ public RubyNode visitInterpolatedMatchLastLineNode(Nodes.InterpolatedMatchLastLi
1514
1516
1515
1517
@ Override
1516
1518
public RubyNode visitInterpolatedRegularExpressionNode (Nodes .InterpolatedRegularExpressionNode node ) {
1517
- return defaultVisit (node );
1518
- }
1519
+ final ToSNode [] children = translateInterpolatedParts (node .parts );
1519
1520
1520
- @ Override
1521
- public RubyNode visitInterpolatedStringNode (Nodes .InterpolatedStringNode node ) {
1522
- final ToSNode [] children = new ToSNode [node .parts .length ];
1521
+ var encodingAndOptions = getRegexpEncodingAndOptions (new Nodes .RegularExpressionFlags (node .flags ));
1523
1522
1524
- for ( int i = 0 ; i < node . parts . length ; i ++) {
1525
- var part = node . parts [ i ] ;
1523
+ var rubyNode = new InterpolatedRegexpNode ( children , encodingAndOptions . options );
1524
+ assignNodePositionInSource ( node , rubyNode ) ;
1526
1525
1527
- children [i ] = ToSNodeGen .create (part .accept (this ));
1526
+ if (node .isOnce ()) {
1527
+ final RubyNode ret = new OnceNode (rubyNode );
1528
+ assignNodePositionOnly (node , ret );
1529
+ return ret ;
1528
1530
}
1529
1531
1532
+ return rubyNode ;
1533
+ }
1534
+
1535
+ @ Override
1536
+ public RubyNode visitInterpolatedStringNode (Nodes .InterpolatedStringNode node ) {
1537
+ final ToSNode [] children = translateInterpolatedParts (node .parts );
1538
+
1530
1539
final RubyNode rubyNode = new InterpolatedStringNode (children , sourceEncoding .jcoding );
1531
1540
assignNodePositionInSource (node , rubyNode );
1532
1541
@@ -1535,12 +1544,7 @@ public RubyNode visitInterpolatedStringNode(Nodes.InterpolatedStringNode node) {
1535
1544
1536
1545
@ Override
1537
1546
public RubyNode visitInterpolatedSymbolNode (Nodes .InterpolatedSymbolNode node ) {
1538
- final ToSNode [] children = new ToSNode [node .parts .length ];
1539
-
1540
- for (int i = 0 ; i < node .parts .length ; i ++) {
1541
- final RubyNode expression = node .parts [i ].accept (this );
1542
- children [i ] = ToSNodeGen .create (expression );
1543
- }
1547
+ final ToSNode [] children = translateInterpolatedParts (node .parts );
1544
1548
1545
1549
final RubyNode stringNode = new InterpolatedStringNode (children , sourceEncoding .jcoding );
1546
1550
final RubyNode rubyNode = StringToSymbolNodeGen .create (stringNode );
@@ -1559,6 +1563,16 @@ public RubyNode visitInterpolatedXStringNode(Nodes.InterpolatedXStringNode node)
1559
1563
return rubyNode ;
1560
1564
}
1561
1565
1566
+ private ToSNode [] translateInterpolatedParts (Nodes .Node [] parts ) {
1567
+ final ToSNode [] children = new ToSNode [parts .length ];
1568
+
1569
+ for (int i = 0 ; i < parts .length ; i ++) {
1570
+ RubyNode expression = parts [i ].accept (this );
1571
+ children [i ] = ToSNodeGen .create (expression );
1572
+ }
1573
+ return children ;
1574
+ }
1575
+
1562
1576
@ Override
1563
1577
public RubyNode visitKeywordHashNode (Nodes .KeywordHashNode node ) {
1564
1578
// translate it like a HashNode, whether it is keywords or not is checked in getKeywordArgumentsDescriptor()
@@ -1832,24 +1846,39 @@ public RubyNode visitRedoNode(Nodes.RedoNode node) {
1832
1846
@ Override
1833
1847
public RubyNode visitRegularExpressionNode (Nodes .RegularExpressionNode node ) {
1834
1848
var source = toTString (node .unescaped );
1849
+ var encodingAndOptions = getRegexpEncodingAndOptions (new Nodes .RegularExpressionFlags (node .flags ));
1850
+ try {
1851
+ final RubyRegexp regexp = RubyRegexp .create (language , source , encodingAndOptions .encoding ,
1852
+ encodingAndOptions .options , currentNode );
1853
+ final ObjectLiteralNode literalNode = new ObjectLiteralNode (regexp );
1854
+ assignNodePositionInSource (node , literalNode );
1855
+ return literalNode ;
1856
+ } catch (DeferredRaiseException dre ) {
1857
+ throw dre .getException (RubyLanguage .getCurrentContext ());
1858
+ }
1859
+ }
1835
1860
1861
+ private record RegexpEncodingAndOptions (RubyEncoding encoding , RegexpOptions options ) {
1862
+ }
1863
+
1864
+ private RegexpEncodingAndOptions getRegexpEncodingAndOptions (Nodes .RegularExpressionFlags flags ) {
1836
1865
final KCode kcode ;
1837
1866
final RubyEncoding regexpEncoding ;
1838
1867
final boolean fixed ;
1839
1868
boolean explicitEncoding = true ;
1840
- if (node .isAscii8bit ()) {
1869
+ if (flags .isAscii8bit ()) {
1841
1870
fixed = false ;
1842
1871
kcode = KCode .NONE ;
1843
1872
regexpEncoding = Encodings .BINARY ;
1844
- } else if (node .isUtf8 ()) {
1873
+ } else if (flags .isUtf8 ()) {
1845
1874
fixed = true ;
1846
1875
kcode = KCode .UTF8 ;
1847
1876
regexpEncoding = Encodings .UTF_8 ;
1848
- } else if (node .isEucJp ()) {
1877
+ } else if (flags .isEucJp ()) {
1849
1878
fixed = true ;
1850
1879
kcode = KCode .EUC ;
1851
1880
regexpEncoding = Encodings .getBuiltInEncoding (EUCJPEncoding .INSTANCE );
1852
- } else if (node .isWindows31j ()) {
1881
+ } else if (flags .isWindows31j ()) {
1853
1882
fixed = true ;
1854
1883
kcode = KCode .SJIS ;
1855
1884
regexpEncoding = Encodings .getBuiltInEncoding (Windows_31JEncoding .INSTANCE );
@@ -1860,16 +1889,9 @@ public RubyNode visitRegularExpressionNode(Nodes.RegularExpressionNode node) {
1860
1889
explicitEncoding = false ;
1861
1890
}
1862
1891
1863
- final RegexpOptions options = new RegexpOptions (kcode , fixed , node .isOnce (), node .isExtended (),
1864
- node .isMultiLine (), node .isIgnoreCase (), node .isAscii8bit (), !explicitEncoding , true );
1865
- try {
1866
- final RubyRegexp regexp = RubyRegexp .create (language , source , regexpEncoding , options , currentNode );
1867
- final ObjectLiteralNode literalNode = new ObjectLiteralNode (regexp );
1868
- assignNodePositionInSource (node , literalNode );
1869
- return literalNode ;
1870
- } catch (DeferredRaiseException dre ) {
1871
- throw dre .getException (RubyLanguage .getCurrentContext ());
1872
- }
1892
+ final RegexpOptions options = new RegexpOptions (kcode , fixed , flags .isOnce (), flags .isExtended (),
1893
+ flags .isMultiLine (), flags .isIgnoreCase (), flags .isAscii8bit (), !explicitEncoding , true );
1894
+ return new RegexpEncodingAndOptions (regexpEncoding , options );
1873
1895
}
1874
1896
1875
1897
@ Override
0 commit comments