1 33 package org.jruby.ast; 34 35 import java.io.Serializable ; 36 import java.util.ArrayList ; 37 import java.util.Collection ; 38 import java.util.Iterator ; 39 import java.util.List ; 40 41 import org.jruby.ast.visitor.NodeVisitor; 42 import org.jruby.evaluator.Instruction; 43 import org.jruby.evaluator.InstructionBundle; 44 import org.jruby.evaluator.InstructionContext; 45 import org.jruby.lexer.yacc.ISourcePosition; 46 import org.jruby.lexer.yacc.ISourcePositionHolder; 47 import org.jruby.lexer.yacc.SourcePosition; 48 49 53 public abstract class Node implements ISourcePositionHolder, InstructionContext, Serializable { 54 static final long serialVersionUID = -5962822607672530224L; 55 static final List EMPTY_LIST = new ArrayList (); 57 58 public final int nodeId; 59 60 public InstructionBundle instruction; 61 62 private ISourcePosition position; 63 private ArrayList comments; 64 65 public Node(ISourcePosition position, int id) { 66 this.position = position; 67 this.nodeId = id; 68 } 69 70 73 public ISourcePosition getPosition() { 74 return position; 75 } 76 77 public void setPosition(ISourcePosition position) { 78 this.position = position; 79 } 80 81 public abstract Instruction accept(NodeVisitor visitor); 82 public abstract List childNodes(); 83 84 static void addNode(Node node, List list) { 85 if (node != null) 86 list.add(node); 87 } 88 89 protected static List createList(Node node) { 91 List list = new ArrayList (); 92 Node.addNode(node, list); 93 return list; 94 } 95 96 protected static List createList(Node node1, Node node2) { 97 List list = createList(node1); 98 Node.addNode(node2, list); 99 return list; 100 } 101 102 protected static List createList(Node node1, Node node2, Node node3) { 103 List list = createList(node1, node2); 104 Node.addNode(node3, list); 105 return list; 106 } 107 108 protected static List createList(Node node1, Node node2, Node node3, Node node4) { 109 List list = createList(node1, node2, node3); 110 Node.addNode(node4, list); 111 return list; 112 } 113 114 public String toString() { 115 return getNodeName() + "[]"; 116 } 117 118 protected String getNodeName() { 119 String name = getClass().getName(); 120 int i = name.lastIndexOf('.'); 121 String nodeType = name.substring(i + 1); 122 return nodeType; 123 } 124 125 public void addComment(CommentNode comment) { 126 if(comments == null) { 127 comments = new ArrayList (); 128 } 129 comments.add(comment); 130 } 131 132 public void addComments(Collection comments) { 133 if(this.comments == null) { 134 this.comments = new ArrayList (); 135 } 136 this.comments.addAll(comments); 137 } 138 139 public Collection getComments() { 140 if(comments == null) { 141 return EMPTY_LIST; 142 } 143 return comments; 144 } 145 146 public boolean hasComments() { 147 return comments != null && !comments.isEmpty(); 148 } 149 150 public ISourcePosition getPositionIncludingComments() { 151 if(position == null || !hasComments()) { 152 return position; 153 } 154 155 String fileName = position.getFile(); 156 int startOffset = position.getStartOffset(); 157 int endOffset = position.getEndOffset(); 158 int startLine = position.getStartLine(); 159 int endLine = position.getEndLine(); 160 161 ISourcePosition commentIncludingPos = new SourcePosition(fileName, startLine, endLine, startOffset, endOffset); 162 163 Iterator commentItr = comments.iterator(); 164 while(commentItr.hasNext()) { 165 ISourcePosition currentPos = ((CommentNode)commentItr.next()).getPosition(); 166 commentIncludingPos = commentIncludingPos.union(currentPos); 167 } 168 169 return commentIncludingPos; 170 } 171 172 } 173 | Popular Tags |