Skip to content

Commit

Permalink
Fix position info for rootnode and highest block node in presence of …
Browse files Browse the repository at this point in the history
…only BEGIN nodes
  • Loading branch information
enebo committed Dec 5, 2014
1 parent 8ef9a88 commit 3e4e479
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
15 changes: 10 additions & 5 deletions core/src/main/java/org/jruby/parser/ParserSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,20 @@ public Node newline_node(Node node, ISourcePosition position) {
return node instanceof NewlineNode ? node : new NewlineNode(position, node);
}

public Node addRootNode(Node topOfAST, ISourcePosition position) {
position = topOfAST != null ? topOfAST.getPosition() : position;

public Node addRootNode(Node topOfAST) {
if (result.getBeginNodes().isEmpty()) {
if (topOfAST == null) topOfAST = NilImplicitNode.NIL;
ISourcePosition position;
if (topOfAST == null) {
topOfAST = NilImplicitNode.NIL;
position = lexer.getPosition();
} else {
position = topOfAST.getPosition();
}

return new RootNode(position, result.getScope(), topOfAST);
}


ISourcePosition position = topOfAST != null ? topOfAST.getPosition() : result.getBeginNodes().get(0).getPosition();
BlockNode newTopOfAST = new BlockNode(position);
for (Node beginNode: result.getBeginNodes()) {
appendToBlock(newTopOfAST, beginNode);
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/org/jruby/parser/RubyParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.jruby.ast.BlockNode;
import org.jruby.ast.BlockPassNode;
import org.jruby.ast.BreakNode;
import org.jruby.ast.CallNode;
import org.jruby.ast.ClassNode;
import org.jruby.ast.ClassVarNode;
import org.jruby.ast.ClassVarAsgnNode;
Expand Down Expand Up @@ -1694,7 +1695,7 @@ public Object yyparse (RubyLexer yyLex) throws java.io.IOException {
support.checkUselessStatement(((Node)yyVals[0+yyTop]));
}
}
support.getResult().setAST(support.addRootNode(((Node)yyVals[0+yyTop]), support.getPosition(((Node)yyVals[0+yyTop]))));
support.getResult().setAST(support.addRootNode(((Node)yyVals[0+yyTop])));
return yyVal;
}
};
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/parser/RubyParser.y
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ program : {
support.checkUselessStatement($2);
}
}
support.getResult().setAST(support.addRootNode($2, support.getPosition($2)));
support.getResult().setAST(support.addRootNode($2));
}

top_compstmt : top_stmts opt_terms {
Expand Down Expand Up @@ -2099,7 +2099,7 @@ var_ref : /*mri:user_variable*/ tIDENTIFIER {
support.getConfiguration().getRuntime().getEncodingService().getLocaleEncoding()));
}
| k__LINE__ {
$$ = new FixnumNode(lexer.getPosition(), lexer.tokline.getStartLine()+1);
$$ = new FixnumNode(lexer.getPosition(), lexer.tokline.getLine()+1);
}
| k__ENCODING__ {
$$ = new EncodingNode(lexer.getPosition(), lexer.getEncoding());
Expand Down

0 comments on commit 3e4e479

Please sign in to comment.