Skip to content

Commit

Permalink
Tests for new detailed source positions.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed Oct 26, 2014
1 parent 43c0df9 commit 1a93885
Show file tree
Hide file tree
Showing 21 changed files with 239 additions and 47 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ast/Node.java
Expand Up @@ -126,7 +126,7 @@ public String toString(boolean indent, int indentation) {
builder.append(":").append(((INameNode) this).getName());
}

builder.append(" ").append(getPosition().getStartLine());
builder.append(" ").append(getPosition().getLine());

if (!childNodes().isEmpty() && indent) {
builder.append("\n");
Expand Down
4 changes: 1 addition & 3 deletions core/src/main/java/org/jruby/ast/util/SexpMaker.java
@@ -1,11 +1,9 @@
package org.jruby.ast.util;

import java.io.File;
import java.nio.CharBuffer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicLong;

import org.jruby.ast.AliasNode;
Expand Down Expand Up @@ -223,7 +221,7 @@ private static void processMethod(Builder sb, String methodName, Node argsNode,
sb.append("(method ").append(methodName).append(' ');
// JRUBY-4301, include filename and line in sexp
sb.append("(file ").append(new File(body.getPosition().getFile()).getName()).append(") ");
sb.append("(line ").append(body.getPosition().getStartLine()).append(") ");
sb.append("(line ").append(body.getPosition().getLine()).append(") ");
process(sb, argsNode);
sb.append(' ');
process(sb, body);
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/common/RubyWarnings.java
Expand Up @@ -69,7 +69,7 @@ public boolean isVerbose() {
public void warn(ID id, ISourcePosition position, String message) {
if (!runtime.warningsEnabled()) return;

warn(id, position.getFile(), position.getStartLine() + 1, message);
warn(id, position.getFile(), position.getLine() + 1, message);
}

/**
Expand Down Expand Up @@ -168,7 +168,7 @@ public void warning(ID id, ISourcePosition position, String message) {
if (!isVerbose()) return;
if (!runtime.warningsEnabled()) return;

warning(id, position.getFile(), position.getStartLine() + 1, message);
warning(id, position.getFile(), position.getLine() + 1, message);
}

/**
Expand Down
Expand Up @@ -230,7 +230,7 @@ public String getFile() {

public int getLine() {
if (compiledMethod == null) initializeMethod();
return position.getStartLine();
return position.getLine();
}

public String[] getParameterList() {
Expand Down Expand Up @@ -306,7 +306,7 @@ public String getFile() {
}

public int getLine() {
return position.getStartLine();
return position.getLine();
}

public Object getScriptObject() {
Expand Down
Expand Up @@ -282,7 +282,7 @@ public DynamicMethod getCompiledMethod(
scope,
callConfig,
position.getFile(),
position.getStartLine(),
position.getLine(),
methodNodes);
generatedClass = endCallWithBytes(invokerBytes, invokerPath);
} catch (LinkageError le) {
Expand Down
Expand Up @@ -65,7 +65,7 @@ public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule claz
boolean isTrace = runtime.hasEventHooks();
try {
if (isTrace) {
runtime.callEventHooks(context, RubyEvent.CALL, position.getFile(), position.getStartLine(), name, getImplementationClass());
runtime.callEventHooks(context, RubyEvent.CALL, position.getFile(), position.getLine(), name, getImplementationClass());
}
return (IRubyObject)method.invoke(null, $scriptObject, context, self, args, block);
} finally {
Expand Down
28 changes: 14 additions & 14 deletions core/src/main/java/org/jruby/ir/IRBuilder.java
Expand Up @@ -467,7 +467,7 @@ public IRBuilder newIRBuilder(IRManager manager) {
public Node skipOverNewlines(IRScope s, Node n) {
if (n.getNodeType() == NodeType.NEWLINENODE) {
// Do not emit multiple line number instrs for the same line
int currLineNum = n.getPosition().getStartLine();
int currLineNum = n.getPosition().getLine();
if (currLineNum != _lastProcessedLineNum) {
if (RubyInstanceConfig.FULL_TRACE_ENABLED) {
addInstr(s, new TraceInstr(RubyEvent.LINE, methodNameFor(s), s.getFileName(), currLineNum));
Expand Down Expand Up @@ -504,7 +504,7 @@ public Operand build(Node node, IRScope s) {
}

public Operand buildLambda(LambdaNode node, IRScope s) {
IRClosure closure = new IRClosure(manager, s, node.getPosition().getStartLine(), node.getScope(), Arity.procArityOf(node.getArgs()), node.getArgumentType());
IRClosure closure = new IRClosure(manager, s, node.getPosition().getLine(), node.getScope(), Arity.procArityOf(node.getArgs()), node.getArgumentType());

// Create a new nested builder to ensure this gets its own IR builder state
// like the ensure block stack
Expand Down Expand Up @@ -990,7 +990,7 @@ public Operand buildCall(CallNode callNode, IRScope s) {
callInstr.setProcNew(true);
}

receiveBreakException(s, block, callInstr, callNode.getPosition().getStartLine());
receiveBreakException(s, block, callInstr, callNode.getPosition().getLine());
return callResult;
}

Expand Down Expand Up @@ -1107,7 +1107,7 @@ public Operand buildClass(ClassNode classNode, IRScope s) {
IRClassBody body = new IRClassBody(manager, s, className, classNode.getPosition().getLine(), classNode.getScope());
Variable tmpVar = addResultInstr(s, new DefineClassInstr(s.createTemporaryVariable(), body, container, superClass));

return buildModuleOrClassBody(s, tmpVar, body, classNode.getBodyNode(), classNode.getPosition().getStartLine());
return buildModuleOrClassBody(s, tmpVar, body, classNode.getBodyNode(), classNode.getPosition().getLine());
}

// class Foo; class << self; end; end
Expand All @@ -1118,7 +1118,7 @@ public Operand buildSClass(SClassNode sclassNode, IRScope s) {
IRModuleBody body = new IRMetaClassBody(manager, s, manager.getMetaClassName(), sclassNode.getPosition().getLine(), sclassNode.getScope());
Variable tmpVar = addResultInstr(s, new DefineMetaClassInstr(s.createTemporaryVariable(), receiver, body));

return buildModuleOrClassBody(s, tmpVar, body, sclassNode.getBodyNode(), sclassNode.getPosition().getStartLine());
return buildModuleOrClassBody(s, tmpVar, body, sclassNode.getBodyNode(), sclassNode.getPosition().getLine());
}

// @@c
Expand Down Expand Up @@ -2243,7 +2243,7 @@ public Operand buildFCall(FCallNode fcallNode, IRScope s) {
Operand block = setupCallClosure(fcallNode.getIterNode(), s);
Variable callResult = s.createTemporaryVariable();
CallInstr callInstr = CallInstr.create(CallType.FUNCTIONAL, callResult, new MethAddr(fcallNode.getName()), s.getSelf(), args.toArray(new Operand[args.size()]), block);
receiveBreakException(s, block, callInstr, fcallNode.getPosition().getStartLine());
receiveBreakException(s, block, callInstr, fcallNode.getPosition().getLine());
return callResult;
}

Expand Down Expand Up @@ -2360,14 +2360,14 @@ public Operand buildFor(ForNode forNode, IRScope s) {
Operand receiver = build(forNode.getIterNode(), s);
Operand forBlock = buildForIter(forNode, s);
CallInstr callInstr = new CallInstr(CallType.NORMAL, result, new MethAddr("each"), receiver, NO_ARGS, forBlock);
receiveBreakException(s, forBlock, callInstr, forNode.getPosition().getStartLine());
receiveBreakException(s, forBlock, callInstr, forNode.getPosition().getLine());

return result;
}

public Operand buildForIter(final ForNode forNode, IRScope s) {
// Create a new closure context
IRClosure closure = new IRFor(manager, s, forNode.getPosition().getStartLine(), forNode.getScope(), Arity.procArityOf(forNode.getVarNode()), forNode.getArgumentType());
IRClosure closure = new IRFor(manager, s, forNode.getPosition().getLine(), forNode.getScope(), Arity.procArityOf(forNode.getVarNode()), forNode.getArgumentType());

// Create a new nested builder to ensure this gets its own IR builder state
// like the ensure block stack
Expand Down Expand Up @@ -2518,7 +2518,7 @@ public Operand buildInstVar(InstVarNode node, IRScope s) {
}

public Operand buildIter(final IterNode iterNode, IRScope s) {
IRClosure closure = new IRClosure(manager, s, iterNode.getPosition().getStartLine(), iterNode.getScope(), Arity.procArityOf(iterNode.getVarNode()), iterNode.getArgumentType());
IRClosure closure = new IRClosure(manager, s, iterNode.getPosition().getLine(), iterNode.getScope(), Arity.procArityOf(iterNode.getVarNode()), iterNode.getArgumentType());

// Create a new nested builder to ensure this gets its own IR builder state
// like the ensure block stack
Expand Down Expand Up @@ -2660,7 +2660,7 @@ public Operand buildModule(ModuleNode moduleNode, IRScope s) {
IRModuleBody body = new IRModuleBody(manager, s, moduleName, moduleNode.getPosition().getLine(), moduleNode.getScope());
Variable tmpVar = addResultInstr(s, new DefineModuleInstr(s.createTemporaryVariable(), body, container));

return buildModuleOrClassBody(s, tmpVar, body, moduleNode.getBodyNode(), moduleNode.getPosition().getStartLine());
return buildModuleOrClassBody(s, tmpVar, body, moduleNode.getBodyNode(), moduleNode.getPosition().getLine());
}

public Operand buildMultipleAsgn(MultipleAsgnNode multipleAsgnNode, IRScope s) {
Expand Down Expand Up @@ -2949,7 +2949,7 @@ public Operand buildPostExe(PostExeNode postExeNode, IRScope s) {
IRScope topLevel = s.getTopLevelScope();
IRScope nearestLVarScope = s.getNearestTopLocalVariableScope();

IRClosure endClosure = new IRClosure(manager, s, postExeNode.getPosition().getStartLine(), nearestLVarScope.getStaticScope(), Arity.procArityOf(postExeNode.getVarNode()), postExeNode.getArgumentType(), "_END_", true);
IRClosure endClosure = new IRClosure(manager, s, postExeNode.getPosition().getLine(), nearestLVarScope.getStaticScope(), Arity.procArityOf(postExeNode.getVarNode()), postExeNode.getArgumentType(), "_END_", true);
// Create a new nested builder to ensure this gets its own IR builder state
// like the ensure block stack
IRBuilder closureBuilder = newIRBuilder(manager);
Expand All @@ -2969,7 +2969,7 @@ public Operand buildPostExe(PostExeNode postExeNode, IRScope s) {
}

public Operand buildPreExe(PreExeNode preExeNode, IRScope s) {
IRClosure beginClosure = new IRFor(manager, s, preExeNode.getPosition().getStartLine(), s.getTopLevelScope().getStaticScope(), Arity.procArityOf(preExeNode.getVarNode()), preExeNode.getArgumentType(), "_BEGIN_");
IRClosure beginClosure = new IRFor(manager, s, preExeNode.getPosition().getLine(), s.getTopLevelScope().getStaticScope(), Arity.procArityOf(preExeNode.getVarNode()), preExeNode.getArgumentType(), "_BEGIN_");
// Create a new nested builder to ensure this gets its own IR builder state
// like the ensure block stack
IRBuilder closureBuilder = newIRBuilder(manager);
Expand Down Expand Up @@ -3304,7 +3304,7 @@ public Operand buildSuper(SuperNode superNode, IRScope s) {
List<Operand> args = setupCallArgs(superNode.getArgsNode(), s);
Operand block = setupCallClosure(superNode.getIterNode(), s);
if (block == null) block = getImplicitBlockArg(s);
return buildSuperInstr(s, block, args.toArray(new Operand[args.size()]), superNode.getPosition().getStartLine());
return buildSuperInstr(s, block, args.toArray(new Operand[args.size()]), superNode.getPosition().getLine());
}

private Operand buildSuperInScriptBody(IRScope s) {
Expand Down Expand Up @@ -3495,7 +3495,7 @@ public Operand buildZSuper(ZSuperNode zsuperNode, IRScope s) {
Operand block = setupCallClosure(zsuperNode.getIterNode(), s);
if (block == null) block = getImplicitBlockArg(s);

int linenumber = zsuperNode.getPosition().getStartLine();
int linenumber = zsuperNode.getPosition().getLine();

// Enebo:ZSuper in for (or nested for) can be statically resolved like method but it needs
// to fixup depth.
Expand Down
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved. This
* code is released under a tri EPL/GPL/LGPL license. You can use it,
* redistribute it and/or modify it under the terms of the:
*
* Eclipse Public License version 1.0
* GNU General Public License version 2
* GNU Lesser General Public License version 2.1
*/
package org.jruby.lexer.yacc;

public class DetailedSourcePosition extends SimpleSourcePosition {

final int offset;
final int length;

public DetailedSourcePosition(String filename, int line, int offset, int length) {
super(filename, line);
this.offset = offset;
this.length = length;
}

public int getOffset() {
return offset;
}

public int getLength() {
return length;
}

@Override
public String toString() {
return String.format("%s:%d:%d:%d", getFile(), getLine() + 1, offset, length);
}

}
Expand Up @@ -50,6 +50,6 @@ public interface ISourcePosition extends PositionAware {
* Which is the first(start) line that this source position occurs on (zero-based)
* @return
*/
public int getStartLine();
public int getLine();

}
Expand Up @@ -36,11 +36,8 @@ public String getFile() {
return "dummy";
}

public int getStartLine() {
return -1;
}

public int getLine() {
return -1;
}

}
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/lexer/yacc/RubyLexer.java
Expand Up @@ -67,8 +67,8 @@
import org.jruby.util.StringSupport;
import org.jruby.util.cli.Options;


/** This is a port of the MRI lexer to Java it is compatible to Ruby 1.8.1.
/*
* This is a port of the MRI lexer to Java.
*/
public class RubyLexer {
public static final Encoding UTF8_ENCODING = UTF8Encoding.INSTANCE;
Expand Down
Expand Up @@ -41,16 +41,13 @@ public String getFile() {
return filename;
}

public int getStartLine() {
return line;
}

public int getLine() {
return line;
}

@Override
public String toString() {
return getFile() + ":" + (getStartLine() + 1);
return String.format("%s:%d", getFile(), getLine() + 1);
}

}
Expand Up @@ -51,7 +51,7 @@ public ISourcePosition getPosition(ISourcePosition startPosition) {
public ISourcePosition getPosition() {
// Only give new position if we are at least one char past \n of previous line so that last tokens
// of previous line will not get associated with the next line.
if (lastPosition.getStartLine() == source.getVirtualLine() || source.lastWasBeginOfLine()) return lastPosition;
if (lastPosition.getLine() == source.getVirtualLine() || source.lastWasBeginOfLine()) return lastPosition;

lastPosition = new SimpleSourcePosition(source.getFilename(), source.getVirtualLine());

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/parser/Parser.java
Expand Up @@ -129,7 +129,7 @@ public Node parse(String file, LexerSource lexerSource, DynamicScope blockScope,
default:
StringBuilder buffer = new StringBuilder(100);
buffer.append(e.getPosition().getFile()).append(':');
buffer.append(e.getPosition().getStartLine() + 1).append(": ");
buffer.append(e.getPosition().getLine() + 1).append(": ");
buffer.append(e.getMessage());

throw runtime.newSyntaxError(buffer.toString());
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/parser/ParserSupport.java
Expand Up @@ -168,7 +168,7 @@ protected void getterIdentifierError(ISourcePosition position, String identifier
public Node newline_node(Node node, ISourcePosition position) {
if (node == null) return null;

configuration.coverLine(position.getStartLine());
configuration.coverLine(position.getLine());

return node instanceof NewlineNode ? node : new NewlineNode(position, node);
}
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/org/jruby/parser/RubyParser.java
Expand Up @@ -44,7 +44,6 @@
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 @@ -4615,7 +4614,7 @@ public Object yyparse (RubyLexer yyLex) throws java.io.IOException {
};
states[514] = new ParserState() {
@Override public Object execute(ParserSupport support, RubyLexer lexer, Object yyVal, Object[] yyVals, int yyTop) {
yyVal = new FixnumNode(lexer.getPosition(), lexer.tokline.getStartLine()+1);
yyVal = new FixnumNode(lexer.getPosition(), lexer.tokline.getLine()+1);
return yyVal;
}
};
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/org/jruby/runtime/ThreadContext.java
Expand Up @@ -44,7 +44,6 @@
import org.jruby.RubyString;
import org.jruby.RubyThread;
import org.jruby.ast.executable.RuntimeCache;
import org.jruby.exceptions.JumpException.ReturnJump;
import org.jruby.ext.fiber.ThreadFiber;
import org.jruby.internal.runtime.methods.DynamicMethod;
import org.jruby.lexer.yacc.ISourcePosition;
Expand Down Expand Up @@ -582,7 +581,7 @@ public void setFileAndLine(String file, int line) {
public void setFileAndLine(ISourcePosition position) {
BacktraceElement b = backtrace[backtraceIndex];
b.filename = position.getFile();
b.line = position.getStartLine();
b.line = position.getLine();
}

public Visibility getCurrentVisibility() {
Expand Down
Expand Up @@ -1337,7 +1337,7 @@ public RubyNode visitMultipleAsgnNode(org.jruby.ast.MultipleAsgn19Node node) {
RubyNode rhsTranslated;

if (rhs == null) {
context.getRuntime().getWarnings().warn(IRubyWarnings.ID.TRUFFLE, node.getPosition().getFile(), node.getPosition().getStartLine(), "no RHS for multiple assignment - using nil");
context.getRuntime().getWarnings().warn(IRubyWarnings.ID.TRUFFLE, node.getPosition().getFile(), node.getPosition().getLine(), "no RHS for multiple assignment - using nil");
rhsTranslated = new ObjectLiteralNode(context, sourceSection, context.getCoreLibrary().getNilObject());
} else {
rhsTranslated = rhs.accept(this);
Expand Down Expand Up @@ -1560,7 +1560,7 @@ public RubyNode visitMultipleAsgnNode(org.jruby.ast.MultipleAsgn19Node node) {

return SequenceNode.sequence(context, sourceSection, sequence);
} else {
context.getRuntime().getWarnings().warn(IRubyWarnings.ID.TRUFFLE, node.getPosition().getFile(), node.getPosition().getStartLine(), node + " unknown form of multiple assignment");
context.getRuntime().getWarnings().warn(IRubyWarnings.ID.TRUFFLE, node.getPosition().getFile(), node.getPosition().getLine(), node + " unknown form of multiple assignment");
return new ObjectLiteralNode(context, sourceSection, context.getCoreLibrary().getNilObject());
}
}
Expand Down Expand Up @@ -2154,7 +2154,7 @@ protected RubyNode defaultVisit(Node node) {
}

protected RubyNode unimplemented(Node node) {
context.getRuntime().getWarnings().warn(IRubyWarnings.ID.TRUFFLE, node.getPosition().getFile(), node.getPosition().getStartLine(), node + " does nothing - translating as nil");
context.getRuntime().getWarnings().warn(IRubyWarnings.ID.TRUFFLE, node.getPosition().getFile(), node.getPosition().getLine(), node + " does nothing - translating as nil");
return new ObjectLiteralNode(context, translate(node.getPosition()), context.getCoreLibrary().getNilObject());
}

Expand Down
Expand Up @@ -43,7 +43,7 @@ public SourceSection translate(Source source, org.jruby.lexer.yacc.ISourcePositi
}
} else if (Options.TRUFFLE_ALLOW_SIMPLE_SOURCE_SECTIONS.load()) {
// If we didn't run with -X+T, so maybe we're using truffelize, we might still get simple source sections
return source.createSection(getIdentifier(), sourcePosition.getStartLine() + 1);
return source.createSection(getIdentifier(), sourcePosition.getLine() + 1);
} else {
throw new UnsupportedOperationException("Truffle needs detailed source positions unless you know what you are doing and set truffle.allow_simple_source_sections - got " + sourcePosition.getClass());
}
Expand Down

0 comments on commit 1a93885

Please sign in to comment.