KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > ast > Node


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2001-2002 Jan Arne Petersen <jpetersen@uni-bonn.de>
15  * Copyright (C) 2001-2002 Benoit Cerrina <b.cerrina@wanadoo.fr>
16  * Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
17  * Copyright (C) 2004 Thomas E Enebo <enebo@acm.org>
18  * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
19  * Copyright (C) 2006 Thomas Corbat <tcorbat@hsr.ch>
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * either of the GNU General Public License Version 2 or later (the "GPL"),
23  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
24  * in which case the provisions of the GPL or the LGPL are applicable instead
25  * of those above. If you wish to allow use of your version of this file only
26  * under the terms of either the GPL or the LGPL, and not to allow others to
27  * use your version of this file under the terms of the CPL, indicate your
28  * decision by deleting the provisions above and replace them with the notice
29  * and other provisions required by the GPL or the LGPL. If you do not delete
30  * the provisions above, a recipient may use your version of this file under
31  * the terms of any one of the CPL, the GPL or the LGPL.
32  ***** END LICENSE BLOCK *****/

33 package org.jruby.ast;
34
35 import java.io.Serializable JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.Collection JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.List JavaDoc;
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 /**
50  *
51  * @author jpetersen
52  */

53 public abstract class Node implements ISourcePositionHolder, InstructionContext, Serializable JavaDoc {
54     static final long serialVersionUID = -5962822607672530224L;
55     // We define an actual list to get around bug in java integration (1387115)
56
static final List JavaDoc EMPTY_LIST = new ArrayList JavaDoc();
57     
58     public final int nodeId;
59     
60     public InstructionBundle instruction;
61
62     private ISourcePosition position;
63     private ArrayList JavaDoc comments;
64
65     public Node(ISourcePosition position, int id) {
66         this.position = position;
67         this.nodeId = id;
68     }
69
70     /**
71      * Location of this node within the source
72      */

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 JavaDoc childNodes();
83
84     static void addNode(Node node, List JavaDoc list) {
85         if (node != null)
86             list.add(node);
87     }
88
89     //TODO: Change to variable parameter list method once we have Java 1.5
90
protected static List JavaDoc createList(Node node) {
91         List JavaDoc list = new ArrayList JavaDoc();
92         Node.addNode(node, list);
93         return list;
94     }
95
96     protected static List JavaDoc createList(Node node1, Node node2) {
97         List JavaDoc list = createList(node1);
98         Node.addNode(node2, list);
99         return list;
100     }
101
102     protected static List JavaDoc createList(Node node1, Node node2, Node node3) {
103         List JavaDoc list = createList(node1, node2);
104         Node.addNode(node3, list);
105         return list;
106     }
107
108     protected static List JavaDoc createList(Node node1, Node node2, Node node3, Node node4) {
109         List JavaDoc list = createList(node1, node2, node3);
110         Node.addNode(node4, list);
111         return list;
112     }
113     
114     public String JavaDoc toString() {
115         return getNodeName() + "[]";
116     }
117
118     protected String JavaDoc getNodeName() {
119         String JavaDoc name = getClass().getName();
120         int i = name.lastIndexOf('.');
121         String JavaDoc nodeType = name.substring(i + 1);
122         return nodeType;
123     }
124     
125     public void addComment(CommentNode comment) {
126         if(comments == null) {
127             comments = new ArrayList JavaDoc();
128         }
129         comments.add(comment);
130     }
131     
132     public void addComments(Collection JavaDoc comments) {
133         if(this.comments == null) {
134             this.comments = new ArrayList JavaDoc();
135         }
136         this.comments.addAll(comments);
137     }
138     
139     public Collection JavaDoc 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 JavaDoc 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 JavaDoc 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