Skip to content

Commit 6c791ef

Browse files
committed
Avoid SourceIndexLength in YARPTranslator and fix newline flag for sequence()
1 parent f887a30 commit 6c791ef

File tree

3 files changed

+38
-34
lines changed

3 files changed

+38
-34
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public static SourceIndexLength enclosing(SourceIndexLength base, RubyNode... se
8585
return new SourceIndexLength(start, end - start);
8686
}
8787

88-
private static List<RubyNode> flatten(List<RubyNode> sequence, boolean allowTrailingNil) {
88+
static List<RubyNode> flatten(List<RubyNode> sequence, boolean allowTrailingNil) {
8989
return flattenFromN(sequence, allowTrailingNil, 0);
9090
}
9191

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

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import com.oracle.truffle.api.nodes.Node;
1313
import com.oracle.truffle.api.source.Source;
14+
import com.oracle.truffle.api.source.SourceSection;
1415
import com.oracle.truffle.api.strings.TruffleString;
1516
import org.truffleruby.RubyLanguage;
1617
import org.truffleruby.annotations.Split;
@@ -30,14 +31,14 @@
3031
import org.truffleruby.language.RubyNode;
3132
import org.truffleruby.language.RubyRootNode;
3233
import org.truffleruby.language.RubyTopLevelRootNode;
33-
import org.truffleruby.language.SourceIndexLength;
3434
import org.truffleruby.language.arguments.EmptyArgumentsDescriptor;
3535
import org.truffleruby.language.constants.ReadConstantNode;
3636
import org.truffleruby.language.control.AndNodeGen;
3737
import org.truffleruby.language.control.IfElseNodeGen;
3838
import org.truffleruby.language.control.IfNodeGen;
3939
import org.truffleruby.language.control.OrNodeGen;
4040
import org.truffleruby.language.control.RetryNode;
41+
import org.truffleruby.language.control.SequenceNode;
4142
import org.truffleruby.language.control.UnlessNodeGen;
4243
import org.truffleruby.language.defined.DefinedNode;
4344
import org.truffleruby.language.dispatch.RubyCallNode;
@@ -64,7 +65,10 @@
6465

6566
import java.nio.charset.StandardCharsets;
6667
import java.util.Arrays;
68+
import java.util.List;
6769

70+
// NOTE: we should avoid SourceIndexLength in YARPTranslator, instead pass a Nodes.Node as location,
71+
// because that's inefficient and there is typically no need for such an object since YARP location info is correct.
6872
public final class YARPTranslator extends AbstractNodeVisitor<RubyNode> {
6973

7074
private final RubyLanguage language;
@@ -251,21 +255,19 @@ public RubyNode visitClassVariableOperatorWriteNode(Nodes.ClassVariableOperatorW
251255
}
252256

253257
public RubyNode visitClassVariableReadNode(Nodes.ClassVariableReadNode node) {
254-
final SourceIndexLength sourceSection = new SourceIndexLength(node.startOffset, node.length);
255258
final RubyNode rubyNode = new ReadClassVariableNode(
256-
getLexicalScopeNode("class variable lookup", sourceSection),
259+
getLexicalScopeNode("class variable lookup", node),
257260
toString(node));
258261

259262
assignNodePositionInSource(node, rubyNode);
260263
return rubyNode;
261264
}
262265

263266
public RubyNode visitClassVariableWriteNode(Nodes.ClassVariableWriteNode node) {
264-
final SourceIndexLength sourceSection = new SourceIndexLength(node.startOffset, node.length);
265267
final RubyNode rhs = node.value.accept(this);
266268

267269
final RubyNode rubyNode = new WriteClassVariableNode(
268-
getLexicalScopeNode("set dynamic class variable", sourceSection),
270+
getLexicalScopeNode("set dynamic class variable", node),
269271
toString(node.name_loc),
270272
rhs);
271273

@@ -451,8 +453,7 @@ public RubyNode visitIfNode(Nodes.IfNode node) {
451453
} else {
452454
// if (condition)
453455
// end
454-
final SourceIndexLength sourceSection = new SourceIndexLength(node.startOffset, node.length);
455-
rubyNode = Translator.sequence(sourceSection, Arrays.asList(conditionNode, new NilLiteralNode(true)));
456+
rubyNode = sequence(node, Arrays.asList(conditionNode, new NilLiteralNode(true)));
456457
}
457458

458459
return rubyNode;
@@ -786,14 +787,12 @@ public RubyNode visitSplatNode(Nodes.SplatNode node) {
786787
}
787788

788789
public RubyNode visitStatementsNode(Nodes.StatementsNode node) {
789-
var location = new SourceIndexLength(node.startOffset, node.length);
790-
791790
var body = node.body;
792791
var translated = new RubyNode[body.length];
793792
for (int i = 0; i < body.length; i++) {
794793
translated[i] = body[i].accept(this);
795794
}
796-
return Translator.sequence(location, Arrays.asList(translated));
795+
return sequence(node, Arrays.asList(translated));
797796
}
798797

799798
public RubyNode visitStringConcatNode(Nodes.StringConcatNode node) {
@@ -859,8 +858,7 @@ public RubyNode visitUnlessNode(Nodes.UnlessNode node) {
859858
} else {
860859
// unless (condition)
861860
// end
862-
final SourceIndexLength sourceSection = new SourceIndexLength(node.startOffset, node.length);
863-
rubyNode = Translator.sequence(sourceSection, Arrays.asList(conditionNode, new NilLiteralNode(true)));
861+
rubyNode = sequence(node, Arrays.asList(conditionNode, new NilLiteralNode(true)));
864862
}
865863

866864
return rubyNode;
@@ -891,27 +889,11 @@ protected RubyNode defaultVisit(Nodes.Node node) {
891889
throw new Error("Unknown node: " + node);
892890
}
893891

894-
protected RubyNode translateNodeOrNil(SourceIndexLength sourceSection, Nodes.Node node) {
895-
final RubyNode rubyNode;
896-
if (node == null) {
897-
rubyNode = nilNode(sourceSection);
898-
} else {
899-
rubyNode = node.accept(this);
900-
}
901-
return rubyNode;
902-
}
903-
904-
protected RubyNode nilNode(SourceIndexLength sourceSection) {
905-
final RubyNode literal = new NilLiteralNode(false);
906-
literal.unsafeSetSourceSection(sourceSection);
907-
return literal;
908-
}
909-
910-
private RubyNode getLexicalScopeNode(String kind, SourceIndexLength sourceSection) {
892+
private RubyNode getLexicalScopeNode(String kind, Nodes.Node yarpNode) {
911893
if (environment.isDynamicConstantLookup()) {
912894
if (language.options.LOG_DYNAMIC_CONSTANT_LOOKUP) {
913895
RubyLanguage.LOGGER.info(() -> kind + " at " +
914-
RubyLanguage.getCurrentContext().fileLine(sourceSection.toSourceSection(source)));
896+
RubyLanguage.getCurrentContext().fileLine(getSourceSection(yarpNode)));
915897
}
916898
return new GetDynamicLexicalScopeNode();
917899
} else {
@@ -946,15 +928,37 @@ private String toString(Nodes.SymbolNode node) {
946928
return new String(node.unescaped);
947929
}
948930

949-
private void assignNodePositionInSource(Nodes.Node yarpNode, RubyNode rubyNode) {
931+
private SourceSection getSourceSection(Nodes.Node yarpNode) {
932+
return source.createSection(yarpNode.startOffset, yarpNode.length);
933+
}
934+
935+
private static void assignNodePositionInSource(Nodes.Node yarpNode, RubyNode rubyNode) {
950936
rubyNode.unsafeSetSourceSection(yarpNode.startOffset, yarpNode.length);
951937
copyNewlineFlag(yarpNode, rubyNode);
952938
}
953939

954-
private void copyNewlineFlag(Nodes.Node yarpNode, RubyNode rubyNode) {
940+
private static void copyNewlineFlag(Nodes.Node yarpNode, RubyNode rubyNode) {
955941
if (yarpNode.hasNewLineFlag()) {
956942
rubyNode.unsafeSetIsNewLine();
957943
}
958944
}
959945

946+
private static RubyNode sequence(Nodes.Node yarpNode, List<RubyNode> sequence) {
947+
assert !yarpNode.hasNewLineFlag() : "Expected node passed to sequence() to not have a newline flag";
948+
final List<RubyNode> flattened = Translator.flatten(sequence, true);
949+
950+
if (flattened.isEmpty()) {
951+
final RubyNode nilNode = new NilLiteralNode(true);
952+
assignNodePositionInSource(yarpNode, nilNode);
953+
return nilNode;
954+
} else if (flattened.size() == 1) {
955+
return flattened.get(0);
956+
} else {
957+
final RubyNode[] flatSequence = flattened.toArray(RubyNode.EMPTY_ARRAY);
958+
var sequenceNode = new SequenceNode(flatSequence);
959+
assignNodePositionInSource(yarpNode, sequenceNode);
960+
return sequenceNode;
961+
}
962+
}
963+
960964
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ public RootCallTarget parse(RubySource rubySource, ParserContext parserContext,
255255
// beginNodeMemo.set(translator.translateNodeOrNil(sourceIndexLength, node.getBeginNode()));
256256
// }
257257
// return translator.translateNodeOrNil(sourceIndexLength, node.getBodyNode());
258-
return translator.translateNodeOrNil(sourceIndexLength, node);
258+
return node.accept(translator);
259259
});
260260
} finally {
261261
printParseTranslateExecuteMetric("after-translate", context, source);

0 commit comments

Comments
 (0)