Skip to content

Commit 6e08974

Browse files
committed
[GR-20334] Store BEGIN nodes separately in RootParseNode.
PullRequest: truffleruby/1215
2 parents c82a6c3 + 3af3f69 commit 6e08974

File tree

3 files changed

+35
-19
lines changed

3 files changed

+35
-19
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,10 @@ public RubyRootNode parse(RubySource rubySource, ParserContext parserContext, St
240240
topLevel);
241241

242242
printParseTranslateExecuteMetric("before-translate", context, source);
243+
RubyNode beginNode = null;
244+
if (node.getBeginNode() != null) {
245+
beginNode = translator.translateNodeOrNil(sourceIndexLength, node.getBeginNode());
246+
}
243247
RubyNode truffleNode = translator.translateNodeOrNil(sourceIndexLength, node.getBodyNode());
244248
printParseTranslateExecuteMetric("after-translate", context, source);
245249

@@ -271,6 +275,12 @@ public RubyRootNode parse(RubySource rubySource, ParserContext parserContext, St
271275
Arrays.asList(translator.initFlipFlopStates(sourceIndexLength), truffleNode));
272276
}
273277

278+
if (beginNode != null) {
279+
truffleNode = Translator.sequence(
280+
sourceIndexLength,
281+
Arrays.asList(beginNode, truffleNode));
282+
}
283+
274284
// Catch next
275285

276286
truffleNode = new CatchNextNode(truffleNode);

src/main/java/org/truffleruby/parser/ast/RootParseNode.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,24 @@
4242
* of the code.
4343
*
4444
*/
45-
// TODO: Store BEGIN and END information into this node
4645
public class RootParseNode extends ParseNode {
4746

47+
private final ParseNode beginNode;
4848
private final Source source;
4949
private final ParseNode bodyNode;
5050
private final String file;
5151
private final int endPosition;
5252

53-
public RootParseNode(Source source, SourceIndexLength position, ParseNode bodyNode, String file, int endPosition) {
53+
public RootParseNode(
54+
Source source,
55+
SourceIndexLength position,
56+
ParseNode beginNode,
57+
ParseNode bodyNode,
58+
String file,
59+
int endPosition) {
5460
super(position);
55-
5661
this.source = source;
62+
this.beginNode = beginNode;
5763
this.bodyNode = bodyNode;
5864
this.file = file;
5965
this.endPosition = endPosition;
@@ -72,6 +78,10 @@ public String getFile() {
7278
return file;
7379
}
7480

81+
public ParseNode getBeginNode() {
82+
return beginNode;
83+
}
84+
7585
/**
7686
* First real AST node to be interpreted
7787
*

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

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -288,28 +288,24 @@ public ParseNode addRootNode(ParseNode topOfAST) {
288288
final int endPosition = lexer.getEndPosition();
289289

290290
SourceIndexLength position;
291-
if (result.getBeginNodes().isEmpty()) {
292-
if (topOfAST == null) {
293-
topOfAST = NilImplicitParseNode.NIL;
294-
position = lexer.getPosition();
295-
} else {
296-
position = topOfAST.getPosition();
297-
}
291+
292+
if (topOfAST == null) {
293+
topOfAST = NilImplicitParseNode.NIL;
294+
position = lexer.getPosition();
298295
} else {
296+
position = topOfAST.getPosition();
297+
}
298+
299+
BlockParseNode beginAST = null;
300+
if (!result.getBeginNodes().isEmpty()) {
299301
position = topOfAST != null ? topOfAST.getPosition() : result.getBeginNodes().get(0).getPosition();
300-
BlockParseNode newTopOfAST = new BlockParseNode(position);
302+
beginAST = new BlockParseNode(position);
301303
for (ParseNode beginNode : result.getBeginNodes()) {
302-
appendToBlock(newTopOfAST, beginNode);
303-
}
304-
305-
// Add real top to new top (unless this top is empty [only begin/end nodes or truly empty])
306-
if (topOfAST != null) {
307-
newTopOfAST.add(topOfAST);
304+
appendToBlock(beginAST, beginNode);
308305
}
309-
topOfAST = newTopOfAST;
310306
}
311307

312-
return new RootParseNode(lexer.getSource(), position, topOfAST, lexer.getFile(), endPosition);
308+
return new RootParseNode(lexer.getSource(), position, beginAST, topOfAST, lexer.getFile(), endPosition);
313309
}
314310

315311
/* MRI: block_append */

0 commit comments

Comments
 (0)