Skip to content

Commit 39c9ca5

Browse files
committed
Try to fix return and block passing mixup after it has been parsed - fixes #305.
1 parent 1c35453 commit 39c9ca5

3 files changed

Lines changed: 25 additions & 8 deletions

File tree

‎core/src/main/java/org/jruby/parser/RubyParser.java‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.jruby.ast.BlockNode;
4545
import org.jruby.ast.BlockPassNode;
4646
import org.jruby.ast.BreakNode;
47+
import org.jruby.ast.CallNode;
4748
import org.jruby.ast.ClassNode;
4849
import org.jruby.ast.ClassVarNode;
4950
import org.jruby.ast.ClassVarAsgnNode;
@@ -146,7 +147,7 @@ public void setWarnings(IRubyWarnings warnings) {
146147
support.setWarnings(warnings);
147148
lexer.setWarnings(warnings);
148149
}
149-
// line 150 "-"
150+
// line 151 "-"
150151
// %token constants
151152
public static final int kCLASS = 257;
152153
public static final int kMODULE = 258;
@@ -4048,8 +4049,16 @@ public Object yyparse (RubyLexer yyLex) throws java.io.IOException {
40484049
if (((Node)yyVals[-1+yyTop]) instanceof BlockAcceptingNode && ((BlockAcceptingNode)yyVals[-1+yyTop]).getIterNode() instanceof BlockPassNode) {
40494050
throw new SyntaxException(PID.BLOCK_ARG_AND_BLOCK_GIVEN, ((Node)yyVals[-1+yyTop]).getPosition(), lexer.getCurrentLine(), "Both block arg and actual block given.");
40504051
}
4051-
if (((Node)yyVals[-1+yyTop]) instanceof NonLocalControlFlowNode) {
4052-
yyVal = ((BlockAcceptingNode) ((NonLocalControlFlowNode)yyVals[-1+yyTop]).getValueNode()).setIterNode(((IterNode)yyVals[0+yyTop]));
4052+
if (yyVal instanceof IterNode
4053+
&& ((Node)yyVals[-1+yyTop]) instanceof ReturnNode
4054+
&& ((ReturnNode)yyVals[-1+yyTop]).getValueNode() instanceof BlockAcceptingNode) {
4055+
final IterNode iterNode = ((IterNode)yyVal);
4056+
final ReturnNode returnNode = ((ReturnNode)yyVals[-1+yyTop]);
4057+
final BlockAcceptingNode blockAcceptingNode = (BlockAcceptingNode) returnNode.getValueNode();
4058+
blockAcceptingNode.setIterNode(iterNode);
4059+
yyVal = returnNode;
4060+
} else if (((Node)yyVals[-1+yyTop]) instanceof NonLocalControlFlowNode) {
4061+
yyVal = ((BlockAcceptingNode) ((NonLocalControlFlowNode)yyVals[-1+yyTop]).getValueNode()).setIterNode(((IterNode)yyVals[0+yyTop]));
40534062
} else {
40544063
yyVal = ((BlockAcceptingNode)yyVals[-1+yyTop]).setIterNode(((IterNode)yyVals[0+yyTop]));
40554064
}
@@ -5198,7 +5207,7 @@ public Object yyparse (RubyLexer yyLex) throws java.io.IOException {
51985207
}
51995208
};
52005209
}
5201-
// line 2480 "RubyParser.y"
5210+
// line 2489 "RubyParser.y"
52025211

52035212
/** The parse method use an lexer stream and parse it to an AST node
52045213
* structure
@@ -5217,4 +5226,4 @@ public RubyParserResult parse(ParserConfiguration configuration, LexerSource sou
52175226
return support.getResult();
52185227
}
52195228
}
5220-
// line 9614 "-"
5229+
// line 9623 "-"

‎core/src/main/java/org/jruby/parser/RubyParser.y‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import org.jruby.ast.BlockArgNode;
4141
import org.jruby.ast.BlockNode;
4242
import org.jruby.ast.BlockPassNode;
4343
import org.jruby.ast.BreakNode;
44+
import org.jruby.ast.CallNode;
4445
import org.jruby.ast.ClassNode;
4546
import org.jruby.ast.ClassVarNode;
4647
import org.jruby.ast.ClassVarAsgnNode;
@@ -1730,8 +1731,16 @@ block_call : command do_block {
17301731
if ($1 instanceof BlockAcceptingNode && $<BlockAcceptingNode>1.getIterNode() instanceof BlockPassNode) {
17311732
throw new SyntaxException(PID.BLOCK_ARG_AND_BLOCK_GIVEN, $1.getPosition(), lexer.getCurrentLine(), "Both block arg and actual block given.");
17321733
}
1733-
if ($1 instanceof NonLocalControlFlowNode) {
1734-
$$ = ((BlockAcceptingNode) $<NonLocalControlFlowNode>1.getValueNode()).setIterNode($2);
1734+
if ($$ instanceof IterNode
1735+
&& $1 instanceof ReturnNode
1736+
&& $<ReturnNode>1.getValueNode() instanceof BlockAcceptingNode) {
1737+
final IterNode iterNode = $<IterNode>$;
1738+
final ReturnNode returnNode = $<ReturnNode>1;
1739+
final BlockAcceptingNode blockAcceptingNode = (BlockAcceptingNode) returnNode.getValueNode();
1740+
blockAcceptingNode.setIterNode(iterNode);
1741+
$$ = returnNode;
1742+
} else if ($1 instanceof NonLocalControlFlowNode) {
1743+
$$ = ((BlockAcceptingNode) $<NonLocalControlFlowNode>1.getValueNode()).setIterNode($2);
17351744
} else {
17361745
$$ = $<BlockAcceptingNode>1.setIterNode($2);
17371746
}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
fails:The return keyword within define_method stops at the method when the return is used directly
2-
fails:The return keyword invoked with a method call without parentheses with a block returns the value returned from the method call

0 commit comments

Comments
 (0)