Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Need to update to enebo branch for this
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.codehaus.org/jruby/branches/enebo_node@2359 961051c9-f516-0410-bf72-c9f7e237a7b7
enebo committed Oct 19, 2006
1 parent 0b8a560 commit 835e7ce
Showing 25 changed files with 7,967 additions and 7,357 deletions.
4 changes: 4 additions & 0 deletions src/org/jruby/ast/ArrayNode.java
Original file line number Diff line number Diff line change
@@ -44,6 +44,10 @@
*/
public class ArrayNode extends ListNode implements ILiteralNode {
static final long serialVersionUID = 6279246130032958596L;

public ArrayNode(ISourcePosition position, Node firstNode) {
super(position, NodeTypes.ARRAYNODE, firstNode);
}

public ArrayNode(ISourcePosition position) {
super(position, NodeTypes.ARRAYNODE);
19 changes: 15 additions & 4 deletions src/org/jruby/ast/BlockArgNode.java
Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@

import java.util.List;

import org.jruby.ast.types.INameNode;
import org.jruby.ast.visitor.NodeVisitor;
import org.jruby.evaluator.Instruction;
import org.jruby.lexer.yacc.ISourcePosition;
@@ -45,14 +46,16 @@
* in this example c is a BlockArgNode
* @author jpetersen
*/
public class BlockArgNode extends Node {
public class BlockArgNode extends Node implements INameNode {
static final long serialVersionUID = 8374824536805365398L;

private final int count;
private String name;

public BlockArgNode(ISourcePosition position, int count) {
public BlockArgNode(ISourcePosition position, int count, String name) {
super(position, NodeTypes.BLOCKARGNODE);
this.count = count;
this.name = name;
}

/**
@@ -70,9 +73,17 @@ public Instruction accept(NodeVisitor iVisitor) {
public int getCount() {
return count;
}


/**
* Get the name of this block argument
*
* @return it's name
*/
public String getName() {
return name;
}

public List childNodes() {
return EMPTY_LIST;
}

}
3 changes: 2 additions & 1 deletion src/org/jruby/ast/CallNode.java
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@
import java.io.IOException;
import java.util.List;

import org.jruby.ast.types.INameNode;
import org.jruby.ast.visitor.NodeVisitor;
import org.jruby.evaluator.Instruction;
import org.jruby.lexer.yacc.ISourcePosition;
@@ -43,7 +44,7 @@
*
* @author jpetersen
*/
public final class CallNode extends Node {
public final class CallNode extends Node implements INameNode {
static final long serialVersionUID = -1993752395320088525L;

private final Node receiverNode;
67 changes: 67 additions & 0 deletions src/org/jruby/ast/CommentNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/***** BEGIN LICENSE BLOCK *****
* Version: CPL 1.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Common Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/cpl-v10.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Copyright (C) 2006 Thomas E Enebo <enebo@acm.org>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the CPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the CPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/
package org.jruby.ast;

import java.util.List;

import org.jruby.ast.visitor.NodeVisitor;
import org.jruby.evaluator.Instruction;
import org.jruby.lexer.yacc.ISourcePosition;

/**
* Representation of a comment. Note that comments are not part of evaluation so you do
* not get the ability to visit this node as part of evaluation. In theory we could add
* this if we envisioned some wacky annotation system, but we have no crazy ideas yet.
*
*/
public class CommentNode extends Node {
private static final long serialVersionUID = -8304070370230933044L;

// Textual data (this is missing newline as a single line comment and =begin..=end
// comments do contain the intermediary newlines.
String content;

public CommentNode(ISourcePosition position, String content) {
super(position, NodeTypes.COMMENTNODE);

this.content = content;
}

public Instruction accept(NodeVisitor visitor) {
return null;
}

public String getContent() {
return content;
}

public List childNodes() {
return EMPTY_LIST;
}

}
3 changes: 2 additions & 1 deletion src/org/jruby/ast/FCallNode.java
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@
import java.io.IOException;
import java.util.List;

import org.jruby.ast.types.INameNode;
import org.jruby.ast.visitor.NodeVisitor;
import org.jruby.evaluator.Instruction;
import org.jruby.lexer.yacc.ISourcePosition;
@@ -42,7 +43,7 @@
*
* @author jpetersen
*/
public class FCallNode extends Node {
public class FCallNode extends Node implements INameNode {
static final long serialVersionUID = 3590332973770104094L;

private String name;
22 changes: 18 additions & 4 deletions src/org/jruby/ast/ListNode.java
Original file line number Diff line number Diff line change
@@ -48,6 +48,18 @@ public class ListNode extends Node {

private List list = null;

/**
* Create a new ListNode.
*
* @param id type of listnode
* @param firstNode first element of the list
*/
public ListNode(ISourcePosition position, int id, Node firstNode) {
this(position, id);

add(firstNode);
}

public ListNode(ISourcePosition position, int id) {
super(position, id);
}
@@ -57,10 +69,12 @@ public ListNode(ISourcePosition position) {
}

public ListNode add(Node node) {
if (list == null) {
list = new ArrayList();
}
if (list == null) list = new ArrayList();
// Ruby Grammar productions return plenty of nulls.
if (node == null) return this;

list.add(node);
setPosition(getPosition().union(node.getPosition()));
return this;
}

@@ -105,7 +119,7 @@ public String toString() {
}

public List childNodes() {
return list;
return list == null ? EMPTY_LIST : list;
}

public Instruction accept(NodeVisitor visitor) {
11 changes: 9 additions & 2 deletions src/org/jruby/ast/LocalVarNode.java
Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@

import java.util.List;

import org.jruby.ast.types.INameNode;
import org.jruby.ast.visitor.NodeVisitor;
import org.jruby.evaluator.Instruction;
import org.jruby.lexer.yacc.ISourcePosition;
@@ -40,14 +41,16 @@
*
* @author jpetersen
*/
public class LocalVarNode extends Node {
public class LocalVarNode extends Node implements INameNode {
static final long serialVersionUID = 8562701804939317217L;

private final int count;
private final String name;

public LocalVarNode(ISourcePosition position, int count) {
public LocalVarNode(ISourcePosition position, int count, String name) {
super(position, NodeTypes.LOCALVARNODE);
this.count = count;
this.name = name;
}

/**
@@ -66,6 +69,10 @@ public int getCount() {
return count;
}

public String getName() {
return name;
}

public List childNodes() {
return EMPTY_LIST;
}
213 changes: 107 additions & 106 deletions src/org/jruby/ast/NodeTypes.java
Original file line number Diff line number Diff line change
@@ -1,106 +1,107 @@
package org.jruby.ast;

public final class NodeTypes {
public static final int ALIASNODE = 0;
public static final int ANDNODE = 1;
public static final int ARGSCATNODE = 2;
public static final int ARGSNODE = 3;
public static final int ARGUMENTNODE = 4;
public static final int ARRAYNODE = 5;
public static final int ASSIGNABLENODE = 6;
public static final int BACKREFNODE = 7;
public static final int BEGINNODE = 8;
public static final int BIGNUMNODE = 9;
public static final int BINARYOPERATORNODE = 10;
public static final int BLOCKARGNODE = 11;
public static final int BLOCKNODE = 12;
public static final int BLOCKPASSNODE = 13;
public static final int BREAKNODE = 14;
public static final int CALLNODE = 15;
public static final int CASENODE = 16;
public static final int CLASSNODE = 17;
public static final int CLASSVARASGNNODE = 18;
public static final int CLASSVARDECLNODE = 19;
public static final int CLASSVARNODE = 20;
public static final int COLON2NODE = 21;
public static final int COLON3NODE = 22;
public static final int CONSTDECLNODE = 23;
public static final int CONSTNODE = 24;
public static final int DASGNNODE = 25;
public static final int DEFINEDNODE = 26;
public static final int DEFNNODE = 27;
public static final int DEFSNODE = 28;
public static final int DOTNODE = 29;
public static final int DREGEXPNODE = 30;
public static final int DSTRNODE = 31;
public static final int DSYMBOLNODE = 32;
public static final int DVARNODE = 33;
public static final int DXSTRNODE = 34;
public static final int ENSURENODE = 35;
public static final int EVSTRNODE = 36;
public static final int FALSENODE = 37;
public static final int FCALLNODE = 38;
public static final int FIXNUMNODE = 39;
public static final int FLIPNODE = 40;
public static final int FLOATNODE = 41;
public static final int FORNODE = 42;
public static final int GLOBALASGNNODE = 43;
public static final int GLOBALVARNODE = 44;
public static final int HASHNODE = 45;
public static final int IFNODE = 46;
public static final int INSTASGNNODE = 47;
public static final int INSTVARNODE = 48;
public static final int ISCOPINGNODE = 49;
public static final int ITERNODE = 50;
public static final int LISTNODE = 51;
public static final int LOCALASGNNODE = 52;
public static final int LOCALVARNODE = 53;
public static final int MATCH2NODE = 54;
public static final int MATCH3NODE = 55;
public static final int MATCHNODE = 56;
public static final int MODULENODE = 57;
public static final int MULTIPLEASGNNODE = 58;
public static final int NEWLINENODE = 59;
public static final int NEXTNODE = 60;
public static final int NILNODE = 61;
public static final int NODETYPES = 62;
public static final int NOTNODE = 63;
public static final int NTHREFNODE = 64;
public static final int OPASGNANDNODE = 65;
public static final int OPASGNNODE = 66;
public static final int OPASGNORNODE = 67;
public static final int OPELEMENTASGNNODE = 68;
public static final int OPTNNODE = 69;
public static final int ORNODE = 70;
public static final int POSTEXENODE = 71;
public static final int REDONODE = 72;
public static final int REGEXPNODE = 73;
public static final int RESCUEBODYNODE = 74;
public static final int RESCUENODE = 75;
public static final int RETRYNODE = 76;
public static final int RETURNNODE = 77;
public static final int SCLASSNODE = 78;
public static final int SCOPENODE = 79;
public static final int SELFNODE = 80;
public static final int SPLATNODE = 81;
public static final int STARNODE = 82;
public static final int STRNODE = 83;
public static final int SUPERNODE = 84;
public static final int SVALUENODE = 85;
public static final int SYMBOLNODE = 86;
public static final int TOARYNODE = 87;
public static final int TRUENODE = 88;
public static final int UNDEFNODE = 89;
public static final int UNTILNODE = 90;
public static final int VALIASNODE = 91;
public static final int VCALLNODE = 92;
public static final int WHENNODE = 93;
public static final int WHILENODE = 94;
public static final int XSTRNODE = 95;
public static final int YIELDNODE = 96;
public static final int ZARRAYNODE = 97;
public static final int ZEROARGNODE = 98;
public static final int ZSUPERNODE = 99;

private NodeTypes() {}
}
package org.jruby.ast;

public final class NodeTypes {
public static final int ALIASNODE = 0;
public static final int ANDNODE = 1;
public static final int ARGSCATNODE = 2;
public static final int ARGSNODE = 3;
public static final int ARGUMENTNODE = 4;
public static final int ARRAYNODE = 5;
public static final int ASSIGNABLENODE = 6;
public static final int BACKREFNODE = 7;
public static final int BEGINNODE = 8;
public static final int BIGNUMNODE = 9;
public static final int BINARYOPERATORNODE = 10;
public static final int BLOCKARGNODE = 11;
public static final int BLOCKNODE = 12;
public static final int BLOCKPASSNODE = 13;
public static final int BREAKNODE = 14;
public static final int CALLNODE = 15;
public static final int CASENODE = 16;
public static final int CLASSNODE = 17;
public static final int CLASSVARASGNNODE = 18;
public static final int CLASSVARDECLNODE = 19;
public static final int CLASSVARNODE = 20;
public static final int COLON2NODE = 21;
public static final int COLON3NODE = 22;
public static final int CONSTDECLNODE = 23;
public static final int CONSTNODE = 24;
public static final int DASGNNODE = 25;
public static final int DEFINEDNODE = 26;
public static final int DEFNNODE = 27;
public static final int DEFSNODE = 28;
public static final int DOTNODE = 29;
public static final int DREGEXPNODE = 30;
public static final int DSTRNODE = 31;
public static final int DSYMBOLNODE = 32;
public static final int DVARNODE = 33;
public static final int DXSTRNODE = 34;
public static final int ENSURENODE = 35;
public static final int EVSTRNODE = 36;
public static final int FALSENODE = 37;
public static final int FCALLNODE = 38;
public static final int FIXNUMNODE = 39;
public static final int FLIPNODE = 40;
public static final int FLOATNODE = 41;
public static final int FORNODE = 42;
public static final int GLOBALASGNNODE = 43;
public static final int GLOBALVARNODE = 44;
public static final int HASHNODE = 45;
public static final int IFNODE = 46;
public static final int INSTASGNNODE = 47;
public static final int INSTVARNODE = 48;
public static final int ISCOPINGNODE = 49;
public static final int ITERNODE = 50;
public static final int LISTNODE = 51;
public static final int LOCALASGNNODE = 52;
public static final int LOCALVARNODE = 53;
public static final int MATCH2NODE = 54;
public static final int MATCH3NODE = 55;
public static final int MATCHNODE = 56;
public static final int MODULENODE = 57;
public static final int MULTIPLEASGNNODE = 58;
public static final int NEWLINENODE = 59;
public static final int NEXTNODE = 60;
public static final int NILNODE = 61;
public static final int NODETYPES = 62;
public static final int NOTNODE = 63;
public static final int NTHREFNODE = 64;
public static final int OPASGNANDNODE = 65;
public static final int OPASGNNODE = 66;
public static final int OPASGNORNODE = 67;
public static final int OPELEMENTASGNNODE = 68;
public static final int OPTNNODE = 69;
public static final int ORNODE = 70;
public static final int POSTEXENODE = 71;
public static final int REDONODE = 72;
public static final int REGEXPNODE = 73;
public static final int RESCUEBODYNODE = 74;
public static final int RESCUENODE = 75;
public static final int RETRYNODE = 76;
public static final int RETURNNODE = 77;
public static final int SCLASSNODE = 78;
public static final int SCOPENODE = 79;
public static final int SELFNODE = 80;
public static final int SPLATNODE = 81;
public static final int STARNODE = 82;
public static final int STRNODE = 83;
public static final int SUPERNODE = 84;
public static final int SVALUENODE = 85;
public static final int SYMBOLNODE = 86;
public static final int TOARYNODE = 87;
public static final int TRUENODE = 88;
public static final int UNDEFNODE = 89;
public static final int UNTILNODE = 90;
public static final int VALIASNODE = 91;
public static final int VCALLNODE = 92;
public static final int WHENNODE = 93;
public static final int WHILENODE = 94;
public static final int XSTRNODE = 95;
public static final int YIELDNODE = 96;
public static final int ZARRAYNODE = 97;
public static final int ZEROARGNODE = 98;
public static final int ZSUPERNODE = 99;
public static final int COMMENTNODE = 100;

private NodeTypes() {}
}
5 changes: 3 additions & 2 deletions src/org/jruby/ast/VCallNode.java
Original file line number Diff line number Diff line change
@@ -34,6 +34,7 @@
import java.io.IOException;
import java.util.List;

import org.jruby.ast.types.INameNode;
import org.jruby.ast.visitor.NodeVisitor;
import org.jruby.evaluator.Instruction;
import org.jruby.lexer.yacc.ISourcePosition;
@@ -43,7 +44,7 @@
*
* @author jpetersen
*/
public class VCallNode extends Node {
public class VCallNode extends Node implements INameNode {
static final long serialVersionUID = -7678578490000574578L;

private String name;
@@ -72,7 +73,7 @@ public Instruction accept(NodeVisitor iVisitor) {
* Gets the methodName.
* @return Returns a String
*/
public String getMethodName() {
public String getName() {
return name;
}

2 changes: 1 addition & 1 deletion src/org/jruby/evaluator/DefinedVisitor.java
Original file line number Diff line number Diff line change
@@ -195,7 +195,7 @@ public Instruction visitFCallNode(FCallNode iVisited) {
* @see AbstractVisitor#visitVCallNode(VCallNode)
*/
public Instruction visitVCallNode(VCallNode iVisited) {
if (runtime.getCurrentContext().getFrameSelf().getMetaClass().isMethodBound(iVisited.getMethodName(), false)) {
if (runtime.getCurrentContext().getFrameSelf().getMetaClass().isMethodBound(iVisited.getName(), false)) {
definition = "method";
}
return null;
5 changes: 3 additions & 2 deletions src/org/jruby/evaluator/EvaluationState.java
Original file line number Diff line number Diff line change
@@ -1107,7 +1107,8 @@ public static IRubyObject eval(ThreadContext context, Node node, IRubyObject sel

// If no exception is thrown execute else block
if (iVisited.getElseNode() != null) {
if (iVisited.getRescueNode() == null) {
if (iVisited.getRescueNode() == null ||
iVisited.getRescueNode().getExceptionNodes().childNodes().isEmpty()) {
runtime.getWarnings().warn(iVisited.getElseNode().getPosition(), "else without rescue is useless");
}
result = eval(context, iVisited.getElseNode(), self);
@@ -1315,7 +1316,7 @@ public static IRubyObject eval(ThreadContext context, Node node, IRubyObject sel
}
case NodeTypes.VCALLNODE: {
VCallNode iVisited = (VCallNode) node;
return self.callMethod(iVisited.getMethodName(), IRubyObject.NULL_ARRAY,
return self.callMethod(iVisited.getName(), IRubyObject.NULL_ARRAY,
CallType.VARIABLE);
}
case NodeTypes.WHENNODE:
7 changes: 5 additions & 2 deletions src/org/jruby/lexer/yacc/HeredocTerm.java
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@
***** END LICENSE BLOCK *****/
package org.jruby.lexer.yacc;

import org.jruby.ast.StrNode;
import org.jruby.parser.Tokens;


@@ -89,8 +90,10 @@ public int parseString(RubyYaccLexer lexer, LexerSource src) {
case '$':
case '@':
src.unread(c);
lexer.setValue(new Token("#" + c, lexer.getPosition()));
return Tokens.tSTRING_DVAR;
case '{':
lexer.setValue(new Token("#" + c, lexer.getPosition()));
return Tokens.tSTRING_DBEG;
}
buffer.append('#');
@@ -103,7 +106,7 @@ public int parseString(RubyYaccLexer lexer, LexerSource src) {
throw new SyntaxException(src.getPosition(), "can't find string \"" + eos + "\" anywhere before EOF");
}
if (c != '\n') {
lexer.yaccValue = new Token(buffer.toString(), lexer.getPosition(null, false));
lexer.yaccValue = new StrNode(lexer.getPosition(), buffer.toString());
return Tokens.tSTRING_CONTENT;
}
buffer.append(src.read());
@@ -119,7 +122,7 @@ public int parseString(RubyYaccLexer lexer, LexerSource src) {

src.unreadMany(lastLine);
lexer.setStrTerm(new StringTerm(-1, '\0', '\0'));
lexer.yaccValue = new Token(str.toString(), lexer.getPosition(null, false));
lexer.yaccValue = new StrNode(lexer.getPosition(), str.toString());
return Tokens.tSTRING_CONTENT;
}
}
56 changes: 53 additions & 3 deletions src/org/jruby/lexer/yacc/ISourcePosition.java
Original file line number Diff line number Diff line change
@@ -28,15 +28,65 @@
package org.jruby.lexer.yacc;

/**
* This interface is the combination of two needs:
* 1) A Ruby interpreter position (for warnings and errors).
* 2) A IDE position (offsets for showing ranges of grammatical elements).
* This interface is the combination of two needs: 1) A Ruby interpreter position (for warnings
* and errors). The interpreter only cares about filename and endLine. 2) A IDE position (offsets
* for showing ranges of grammatical elements).
*
* The offsets represent positions 'between' characters. So a variable 'abc' has a startOffset
* of 0 and an endOffset of 3 ( 0a1b2c3 ).
*
*/
public interface ISourcePosition {
/**
* Which file does this source position live in?
*
* @return name of the source file.
*/
public String getFile();

/**
* Which is the first(start) line that this source position occurs on (zero-based)
* @return
*/
public int getStartLine();

/**
* Which is the last(end) line that this source position occurs on (zero-based)
*
* @return the line
*/
public int getEndLine();

/**
* Modify startOffset by a relativeValue. At times our grammar and lexer do not give us
* the exact positions we need so we need to manually tweak position. The bummer of this
* is this requires ISourcePosition implementations are mutable.
*
* @param relativeValue to nudge startOffset up or down
*/
public void adjustStartOffset(int relativeValue);

/**
* Get offset (relative to beginning of source file) immediately before first character
* represented by this source position.
*
* @return the offset
*/
public int getStartOffset();

/**
* Get offset (relative to beginning of source file) immediately after the last character
* represented by this source position
*
* @return the offset
*/
public int getEndOffset();

/**
* Calculates the logical union of the two positions and creates a new resulting position
*
* @param position to be unioned against this position
* @return a new position
*/
public ISourcePosition union(ISourcePosition position);
}
4 changes: 0 additions & 4 deletions src/org/jruby/lexer/yacc/LexerSource.java
Original file line number Diff line number Diff line change
@@ -240,10 +240,6 @@ public ISourcePosition getPosition() {
return positionFactory.getPosition(null, false);
}

public ISourcePosition getDummyPosition() {
return positionFactory.getDummyPosition();
}

public ISourcePositionFactory getPositionFactory() {
return positionFactory;
}
508 changes: 267 additions & 241 deletions src/org/jruby/lexer/yacc/RubyYaccLexer.java

Large diffs are not rendered by default.

45 changes: 37 additions & 8 deletions src/org/jruby/lexer/yacc/SourcePosition.java
Original file line number Diff line number Diff line change
@@ -38,18 +38,20 @@
* error/warning information. An IDE using this may need it though. This is
* trivially added if need be.
*
* @see org.jruby.lexer.yacc.ISourcePosition
*/
public class SourcePosition implements ISourcePosition, Serializable {
private static final long serialVersionUID = 3762529027281400377L;

// The file of the source
private final String file;

// The row of the source
// The state/end rows of the source
private final int startLine;
private final int endLine;

private final int startOffset;
// The start/end offsets of the source
private int startOffset;
private final int endOffset;

/**
@@ -94,21 +96,23 @@ public SourcePosition(String file, int startLine, int endLine, int startOffset,
}

/**
* @return the file of the source
* @see org.jruby.lexer.yacc.ISourcePosition#getFile()
*/
public String getFile() {
return file;
}

/**
* @return the line a token starts within the source
* Not used in interpreter
*
* @see org.jruby.lexer.yacc.ISourcePosition#getStartLine()
*/
public int getStartLine() {
return startLine;
}

/**
* @return the line within the source
* @see org.jruby.lexer.yacc.ISourcePosition#getEndLine()
*/
public int getEndLine() {
return endLine;
@@ -132,31 +136,56 @@ public boolean equals(Object object) {
}

/**
* @return something we can use for identity in hashing, etc...
* Something we can use for identity in hashing, etc...
*
* @see java.lang.Object#hashCode()
*/
public int hashCode() {
return file.hashCode() ^ endLine;
}

/**
* @return simple Object.toString() implementation
* @see java.lang.Object#toString()
*/
public String toString() {
return file + ":[" + startLine + "," + endLine + "]:[" +
getStartOffset() + "," + getEndOffset() + "]";
}

/**
* Not used in interpreter
* @see org.jruby.lexer.yacc.ISourcePosition#adjustStartOffset(int)
*/
public void adjustStartOffset(int relativeValue) {
startOffset += relativeValue;
}

/**
* Not used in interpreter
*
* @see org.jruby.lexer.yacc.ISourcePosition#getStartOffset()
*/
public int getStartOffset() {
return startOffset;
}

/**
* Not used in interpreter
*
* @see org.jruby.lexer.yacc.ISourcePosition#getEndOffset()
*/
public int getEndOffset() {
return endOffset;
}

/**
* @see org.jruby.lexer.yacc.ISourcePosition#union(ISourcePosition)
*/
public ISourcePosition union(ISourcePosition other) {
int startLine = Math.min(getStartLine(), other.getStartLine());
int endLine = Math.max(getEndLine(), other.getEndLine());
int startOffset = Math.min(getStartOffset(), other.getStartOffset());
int endOffset = Math.max(getEndOffset(), other.getEndOffset());

return new SourcePosition(file, startLine, endLine, startOffset, endOffset);
}
}
36 changes: 18 additions & 18 deletions src/org/jruby/lexer/yacc/StringTerm.java
Original file line number Diff line number Diff line change
@@ -28,6 +28,7 @@
package org.jruby.lexer.yacc;

import org.jruby.ast.RegexpNode;
import org.jruby.ast.StrNode;
import org.jruby.parser.ReOptions;
import org.jruby.parser.Tokens;

@@ -54,8 +55,10 @@ public int parseString(final RubyYaccLexer lexer, LexerSource src) {
int space = 0;
StringBuffer buffer = new StringBuffer(100);

if (func == -1)
if (func == -1) {
lexer.setValue(new Token("\"", lexer.getPosition()));
return Tokens.tSTRING_END;
}

c = src.read();
if ((func & RubyYaccLexer.STR_FUNC_QWORDS) != 0
@@ -69,17 +72,19 @@ public int parseString(final RubyYaccLexer lexer, LexerSource src) {
if (c == term && nest == 0) {
if ((func & RubyYaccLexer.STR_FUNC_QWORDS) != 0) {
func = -1;
lexer.getPosition();
return ' ';
}
if ((func & RubyYaccLexer.STR_FUNC_REGEXP) != 0) {
lexer.yaccValue = new RegexpNode(src.getPosition(),
buffer.toString(), parseRegexpFlags(src));
lexer.setValue(new RegexpNode(src.getPosition(), buffer.toString(), parseRegexpFlags(src)));
return Tokens.tREGEXP_END;
}
lexer.setValue(new Token("\"", lexer.getPosition()));
return Tokens.tSTRING_END;
}
if (space != 0) {
src.unread(c);
lexer.getPosition();
return ' ';
}
if ((func & RubyYaccLexer.STR_FUNC_EXPAND) != 0 && c == '#') {
@@ -88,8 +93,10 @@ public int parseString(final RubyYaccLexer lexer, LexerSource src) {
case '$':
case '@':
src.unread(c);
lexer.setValue(new Token("#" + c, lexer.getPosition()));
return Tokens.tSTRING_DVAR;
case '{':
lexer.setValue(new Token("#" + c, lexer.getPosition()));
return Tokens.tSTRING_DBEG;
}
buffer.append('#');
@@ -99,7 +106,7 @@ public int parseString(final RubyYaccLexer lexer, LexerSource src) {
throw new SyntaxException(src.getPosition(), "unterminated string meets end of file");
}

lexer.yaccValue = new Token(buffer.toString(), lexer.getPosition(null, false));
lexer.setValue(new StrNode(lexer.getPosition(), buffer.toString()));
return Tokens.tSTRING_CONTENT;
}

@@ -162,8 +169,7 @@ public char parseStringIntoBuffer(LexerSource src, StringBuffer buffer) {
break;
}
nest--;
} else if ((func & RubyYaccLexer.STR_FUNC_EXPAND) != 0 && c == '#'
&& !src.peek('\n')) {
} else if ((func & RubyYaccLexer.STR_FUNC_EXPAND) != 0 && c == '#' && !src.peek('\n')) {
char c2 = src.read();

if (c2 == '$' || c2 == '@' || c2 == '{') {
@@ -231,8 +237,7 @@ private void escaped(LexerSource src, StringBuffer buffer) {
parseEscapeIntoBuffer(src, buffer);
break;
case RubyYaccLexer.EOF:
throw new SyntaxException(src.getPosition(),
"Invalid escape character syntax");
throw new SyntaxException(src.getPosition(), "Invalid escape character syntax");
default:
buffer.append(c);
}
@@ -257,8 +262,7 @@ private void parseEscapeIntoBuffer(LexerSource src, StringBuffer buffer) {
for (int i = 0; i < 2; i++) {
c = src.read();
if (c == RubyYaccLexer.EOF) {
throw new SyntaxException(src.getPosition(),
"Invalid escape character syntax");
throw new SyntaxException(src.getPosition(), "Invalid escape character syntax");
}
if (!RubyYaccLexer.isOctChar(c)) {
src.unread(c);
@@ -272,8 +276,7 @@ private void parseEscapeIntoBuffer(LexerSource src, StringBuffer buffer) {
buffer.append(c);
c = src.read();
if (!RubyYaccLexer.isHexChar(c)) {
throw new SyntaxException(src.getPosition(),
"Invalid escape character syntax");
throw new SyntaxException(src.getPosition(), "Invalid escape character syntax");
}
buffer.append(c);
c = src.read();
@@ -285,16 +288,14 @@ private void parseEscapeIntoBuffer(LexerSource src, StringBuffer buffer) {
break;
case 'M':
if ((c = src.read()) != '-') {
throw new SyntaxException(src.getPosition(),
"Invalid escape character syntax");
throw new SyntaxException(src.getPosition(), "Invalid escape character syntax");
}
buffer.append("\\M-");
escaped(src, buffer);
break;
case 'C':
if ((c = src.read()) != '-') {
throw new SyntaxException(src.getPosition(),
"Invalid escape character syntax");
throw new SyntaxException(src.getPosition(), "Invalid escape character syntax");
}
buffer.append("\\C-");
escaped(src, buffer);
@@ -304,8 +305,7 @@ private void parseEscapeIntoBuffer(LexerSource src, StringBuffer buffer) {
escaped(src, buffer);
break;
case 0:
throw new SyntaxException(src.getPosition(),
"Invalid escape character syntax");
throw new SyntaxException(src.getPosition(), "Invalid escape character syntax");
default:
if (c != '\\' || c != term) {
buffer.append('\\');
3,631 changes: 1,680 additions & 1,951 deletions src/org/jruby/parser/DefaultRubyParser.java

Large diffs are not rendered by default.

2,570 changes: 1,153 additions & 1,417 deletions src/org/jruby/parser/DefaultRubyParser.y

Large diffs are not rendered by default.

311 changes: 133 additions & 178 deletions src/org/jruby/parser/ParserSupport.java

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions src/org/jruby/parser/RubyParserResult.java
Original file line number Diff line number Diff line change
@@ -35,6 +35,7 @@
import java.util.List;

import org.jruby.ast.BlockNode;
import org.jruby.ast.CommentNode;
import org.jruby.ast.Node;
import org.jruby.lexer.yacc.ISourcePosition;

@@ -48,6 +49,7 @@ public class RubyParserResult {
private List localVariables;
private InputStream afterEndStream;
private boolean endSeen;
private List commentNodes = new ArrayList();

/**
* Constructor for RubyParserResult.
@@ -80,6 +82,10 @@ public InputStream getAfterEndStream() {
return afterEndStream;
}

public List getCommentNodes() {
return commentNodes;
}

public Node getAST() {
return ast;
}
@@ -115,6 +121,10 @@ public void setLocalVariables(List localVariables) {
this.localVariables = localVariables;
}

public void addComment(CommentNode node) {
commentNodes.add(node);
}

public void addBeginNode(Node node) {
beginNodes.add(node);
}
5 changes: 4 additions & 1 deletion src/org/jruby/parser/Tokens.java
Original file line number Diff line number Diff line change
@@ -128,8 +128,10 @@ public interface Tokens {
int tASSOC = DefaultRubyParser.tASSOC;
int tLPAREN = DefaultRubyParser.tLPAREN;
int tLPAREN2 = DefaultRubyParser.tLPAREN2;
int tRPAREN = DefaultRubyParser.tRPAREN;
int tLPAREN_ARG = DefaultRubyParser.tLPAREN_ARG;
int tLBRACK = DefaultRubyParser.tLBRACK;
int tRBRACK = DefaultRubyParser.tRBRACK;
int tLBRACE = DefaultRubyParser.tLBRACE;
int tLBRACE_ARG = DefaultRubyParser.tLBRACE_ARG;
int tSTAR = DefaultRubyParser.tSTAR;
@@ -146,7 +148,8 @@ public interface Tokens {
int tGT = DefaultRubyParser.tGT;
int tCARET = DefaultRubyParser.tCARET;
int tBANG = DefaultRubyParser.tBANG;
int tLCURLY = DefaultRubyParser.tLCURLY;
int tLCURLY = DefaultRubyParser.tLCURLY;
int tRCURLY = DefaultRubyParser.tRCURLY;
int tPIPE = DefaultRubyParser.tPIPE;

String[] operators = {"+@", "-@", "**", "<=>", "==", "===", "!=", ">=", "<=", "&&",
6,636 changes: 3,350 additions & 3,286 deletions src/org/jruby/parser/YyTables.java

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions src/org/jruby/util/IdUtil.java
Original file line number Diff line number Diff line change
@@ -28,6 +28,33 @@
package org.jruby.util;

public final class IdUtil {
public static final int CONSTANT = 0;
public static final int INSTANCE_VAR = 1;
public static final int CLASS_VAR = 2;
public static final int GLOBAL_VAR = 4;
public static final int LOCAL_VAR = 8;

/**
* Get type of variable based on Ruby naming conventions. This is useful when you know
* you are going to want to know what type it is. It should in theory be cheaper than
* calling all the isFoo methods seperately. It also should be faster than isLocal.
*
* @param id the name to determine its type from
* @return value representing the type.
*/
public static int getVarType(String id) {
char c = id.charAt(0);

switch (c) {
case '@':
return id.charAt(1) == '@' ? CLASS_VAR : INSTANCE_VAR;
case '$':
return GLOBAL_VAR;
}

return Character.isUpperCase(c) ? CONSTANT : LOCAL_VAR;
}

public static boolean isConstant(String id) {
return Character.isUpperCase(id.charAt(0));
}
1,124 changes: 999 additions & 125 deletions test/testPositions.rb

Large diffs are not rendered by default.

0 comments on commit 835e7ce

Please sign in to comment.