Skip to content

Commit

Permalink
Showing 2 changed files with 22 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -148,7 +148,7 @@ public BlockDefinitionNode compileBlockNode(SourceSection sourceSection, String
}

// Procs
final RubyNode bodyProc = new CatchForProcNode(context, enclosing(sourceSection, body.getEncapsulatingSourceSection()), composeBody(preludeProc, NodeUtil.cloneNode(body)));
final RubyNode bodyProc = new CatchForProcNode(context, enclosing(sourceSection, body), composeBody(preludeProc, NodeUtil.cloneNode(body)));

final RubyRootNode newRootNodeForProcs = new RubyRootNode(context, considerExtendingMethodToCoverEnd(bodyProc.getEncapsulatingSourceSection()), environment.getFrameDescriptor(), environment.getSharedMethodInfo(),
bodyProc, environment.needsDeclarationFrame());
@@ -200,7 +200,7 @@ private boolean shouldConsiderDestructuringArrayArg(Arity arity) {
}

private RubyNode composeBody(RubyNode prelude, RubyNode body) {
final SourceSection sourceSection = enclosing(prelude.getSourceSection(), body.getSourceSection());
final SourceSection sourceSection = enclosing(prelude.getSourceSection(), body);

body = sequence(context, sourceSection, Arrays.asList(prelude, body));

Original file line number Diff line number Diff line change
@@ -48,7 +48,7 @@ public Translator(Node currentNode, RubyContext context, Source source) {
}

public static RubyNode sequence(RubyContext context, SourceSection sourceSection, List<RubyNode> sequence) {
final List<RubyNode> flattened = flatten(context, sequence, true);
final List<RubyNode> flattened = flatten(sequence, true);

if (flattened.isEmpty()) {
return new NilLiteralNode(context, sourceSection, true);
@@ -60,57 +60,21 @@ public static RubyNode sequence(RubyContext context, SourceSection sourceSection
}
}

public static SourceSection enclosing(SourceSection base, SourceSection... sourceSections) {
for (SourceSection sourceSection : sourceSections) {
if (base == null) {
base = sourceSection;
} else {
break;
}
}

if (base == null) {
return null;
}

if (base.getSource() == null) {
return base;
}

if (sourceSections.length == 0) {
public static SourceSection enclosing(SourceSection base, RubyNode... sequence) {
if (base == null || base.getSource() == null) {
return base;
}

int startLine = base.getStartLine();
int endLine = safeGetEndLine(base);

int endLine;

try {
endLine = base.getEndLine();
} catch (IllegalArgumentException e) {
endLine = startLine;
}

for (SourceSection sourceSection : sourceSections) {
if (sourceSection == null) {
continue;
}

startLine = Math.min(startLine, sourceSection.getStartLine());
for (RubyNode node : sequence) {
final SourceSection sourceSection = node.getEncapsulatingSourceSection();

int nodeEndLine;

if (sourceSection.getSource() == null) {
nodeEndLine = sourceSection.getStartLine();
} else {
try {
nodeEndLine = sourceSection.getEndLine();
} catch (IllegalArgumentException e) {
nodeEndLine = sourceSection.getStartLine();
}
if (sourceSection != null) {
startLine = Integer.min(startLine, sourceSection.getStartLine());
endLine = Integer.max(endLine, safeGetEndLine(sourceSection));
}

endLine = Math.max(endLine, nodeEndLine);
}

final int index = base.getSource().getLineStartOffset(startLine);
@@ -128,17 +92,19 @@ public static SourceSection enclosing(SourceSection base, SourceSection... sourc
return base.getSource().createSection("(identifier)", index, length);
}

public static SourceSection enclosing(SourceSection base, RubyNode[] sequence) {
final SourceSection[] sourceSections = new SourceSection[sequence.length];

for (int n = 0; n < sequence.length; n++) {
sourceSections[n] = sequence[n].getEncapsulatingSourceSection();
private static int safeGetEndLine(SourceSection sourceSection) {
if (sourceSection.getSource() == null) {
return sourceSection.getStartLine();
} else {
try {
return sourceSection.getEndLine();
} catch (IllegalArgumentException e) {
return sourceSection.getStartLine();
}
}

return enclosing(base, sourceSections);
}

private static List<RubyNode> flatten(RubyContext context, List<RubyNode> sequence, boolean allowTrailingNil) {
private static List<RubyNode> flatten(List<RubyNode> sequence, boolean allowTrailingNil) {
final List<RubyNode> flattened = new ArrayList<>();

for (int n = 0; n < sequence.size(); n++) {
@@ -150,7 +116,7 @@ private static List<RubyNode> flatten(RubyContext context, List<RubyNode> sequen
flattened.add(node);
}
} else if (node instanceof SequenceNode) {
flattened.addAll(flatten(context, Arrays.asList(((SequenceNode) node).getSequence()), lastNode));
flattened.addAll(flatten(Arrays.asList(((SequenceNode) node).getSequence()), lastNode));
} else {
flattened.add(node);
}

0 comments on commit d25398a

Please sign in to comment.