20
20
import org .truffleruby .core .cast .StringToSymbolNodeGen ;
21
21
import org .truffleruby .core .cast .ToSNode ;
22
22
import org .truffleruby .core .cast .ToSNodeGen ;
23
+ import org .truffleruby .core .encoding .EncodingManager ;
23
24
import org .truffleruby .core .encoding .Encodings ;
24
25
import org .truffleruby .core .encoding .RubyEncoding ;
25
26
import org .truffleruby .core .encoding .TStringUtils ;
26
27
import org .truffleruby .core .module .ModuleNodes ;
27
28
import org .truffleruby .core .string .InterpolatedStringNode ;
28
- import org .truffleruby .core .string .TStringConstants ;
29
29
import org .truffleruby .core .symbol .RubySymbol ;
30
30
import org .truffleruby .language .RubyContextSourceNode ;
31
31
import org .truffleruby .language .RubyNode ;
63
63
import org .yarp .AbstractNodeVisitor ;
64
64
import org .yarp .Nodes ;
65
65
66
- import java .nio .charset .StandardCharsets ;
67
66
import java .util .Arrays ;
68
67
import java .util .List ;
69
68
@@ -79,6 +78,7 @@ public final class YARPTranslator extends AbstractNodeVisitor<RubyNode> {
79
78
private final ParserContext parserContext ;
80
79
private final Node currentNode ;
81
80
private final RubyDeferredWarnings rubyWarnings ;
81
+ private final RubyEncoding sourceEncoding ;
82
82
83
83
public YARPTranslator (
84
84
RubyLanguage language ,
@@ -97,6 +97,7 @@ public YARPTranslator(
97
97
this .parserContext = parserContext ;
98
98
this .currentNode = currentNode ;
99
99
this .rubyWarnings = rubyWarnings ;
100
+ this .sourceEncoding = Encodings .UTF_8 ; // TODO
100
101
}
101
102
102
103
public RubyRootNode translate (Nodes .Node node ) {
@@ -200,7 +201,7 @@ public RubyNode visitBreakNode(Nodes.BreakNode node) {
200
201
}
201
202
202
203
public RubyNode visitCallNode (Nodes .CallNode node ) {
203
- var methodName = new String (node .name , StandardCharsets . UTF_8 );
204
+ var methodName = new String (node .name , EncodingManager . charsetForEncoding ( sourceEncoding . jcoding ) );
204
205
var receiver = node .receiver == null ? new SelfNode () : node .receiver .accept (this );
205
206
var arguments = node .arguments .arguments ;
206
207
@@ -343,7 +344,7 @@ public RubyNode visitEmbeddedStatementsNode(Nodes.EmbeddedStatementsNode node) {
343
344
// empty interpolation expression, e.g. in "a #{} b"
344
345
if (node .statements == null ) {
345
346
RubyNode rubyNode = new ObjectLiteralNode (
346
- language .getFrozenStringLiteral (TStringConstants . EMPTY_BINARY , Encodings . BINARY ));
347
+ language .getFrozenStringLiteral (sourceEncoding . tencoding . getEmpty (), sourceEncoding ));
347
348
assignNodePositionInSource (node , rubyNode );
348
349
return rubyNode ;
349
350
}
@@ -516,9 +517,9 @@ public RubyNode visitInterpolatedStringNode(Nodes.InterpolatedStringNode node) {
516
517
517
518
// a special case for `:"abc"` literal - convert to Symbol ourselves
518
519
if (node .parts .length == 1 && node .parts [0 ] instanceof Nodes .StringNode s ) {
519
- final TruffleString tstring = TStringUtils .fromByteArray (s .unescaped , Encodings . UTF_8 );
520
- final TruffleString cachedTString = language .tstringCache .getTString (tstring , Encodings . UTF_8 );
521
- final RubyNode rubyNode = new StringLiteralNode (cachedTString , Encodings . UTF_8 );
520
+ final TruffleString tstring = TStringUtils .fromByteArray (s .unescaped , sourceEncoding );
521
+ final TruffleString cachedTString = language .tstringCache .getTString (tstring , sourceEncoding );
522
+ final RubyNode rubyNode = new StringLiteralNode (cachedTString , sourceEncoding );
522
523
523
524
assignNodePositionInSource (node , rubyNode );
524
525
copyNewlineFlag (s , rubyNode );
@@ -535,7 +536,7 @@ public RubyNode visitInterpolatedStringNode(Nodes.InterpolatedStringNode node) {
535
536
children [i ] = ToSNodeGen .create (part .accept (this ));
536
537
}
537
538
538
- final RubyNode rubyNode = new InterpolatedStringNode (children , Encodings . UTF_8 .jcoding );
539
+ final RubyNode rubyNode = new InterpolatedStringNode (children , sourceEncoding .jcoding );
539
540
assignNodePositionInSource (node , rubyNode );
540
541
541
542
return rubyNode ;
@@ -564,7 +565,7 @@ public RubyNode visitInterpolatedSymbolNode(Nodes.InterpolatedSymbolNode node) {
564
565
children [i ] = ToSNodeGen .create (part .accept (this ));
565
566
}
566
567
567
- final RubyNode stringNode = new InterpolatedStringNode (children , Encodings . UTF_8 .jcoding );
568
+ final RubyNode stringNode = new InterpolatedStringNode (children , sourceEncoding .jcoding );
568
569
final RubyNode rubyNode = StringToSymbolNodeGen .create (stringNode );
569
570
assignNodePositionInSource (node , rubyNode );
570
571
@@ -800,7 +801,7 @@ public RubyNode visitStringConcatNode(Nodes.StringConcatNode node) {
800
801
}
801
802
802
803
public RubyNode visitStringNode (Nodes .StringNode node ) {
803
- final RubyEncoding encoding = Encodings . UTF_8 ;
804
+ final RubyEncoding encoding = sourceEncoding ;
804
805
final TruffleString tstring = TStringUtils .fromByteArray (node .unescaped , encoding );
805
806
final TruffleString cachedTString = language .tstringCache .getTString (tstring , encoding );
806
807
final RubyNode rubyNode = new StringLiteralNode (cachedTString , encoding );
@@ -917,11 +918,13 @@ protected RubyContextSourceNode createCallNode(RubyNode receiver, String method,
917
918
}
918
919
919
920
private String toString (Nodes .Location location ) {
920
- return new String (sourceBytes , location .startOffset , location .length , StandardCharsets .US_ASCII );
921
+ return new String (sourceBytes , location .startOffset , location .length ,
922
+ EncodingManager .charsetForEncoding (sourceEncoding .jcoding ));
921
923
}
922
924
923
925
private String toString (Nodes .Node node ) {
924
- return new String (sourceBytes , node .startOffset , node .length , StandardCharsets .US_ASCII );
926
+ return new String (sourceBytes , node .startOffset , node .length ,
927
+ EncodingManager .charsetForEncoding (sourceEncoding .jcoding ));
925
928
}
926
929
927
930
private String toString (Nodes .SymbolNode node ) {
0 commit comments