View Javadoc
Minimize
Table

1   /*
2    * Copyright 2003-2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.groovy.ast;
17  
18  import org.codehaus.groovy.GroovyBugError;
19  import org.codehaus.groovy.util.ListHashMap;
20  
21  /**
22   * Base class for any AST node. This class supports basic information used in all
23   * nodes of the AST<ul>
24   * <li> line and column number information. Usually a node represents a certain
25   * area in a text file determined by a starting position and an ending position.
26   * For nodes that do not represent this, this information will be -1. A node can
27   * also be configured in its line/col information using another node through 
28   * setSourcePosition(otherNode).</li>
29   * <li> every node can store meta data. A phase operation or transform can use 
30   * this to transport arbitrary information to another phase operation or 
31   * transform. The only requirement is that the other phase operation or transform
32   * runs after the part storing the information. If the information transport is 
33   * done it is strongly recommended to remove that meta data.</li> 
34   * </ul>
35   * <li> a text representation of this node trough getText(). This was in the 
36   * past used for assertion messages. Since the usage of power asserts this 
37   * method will not be called for this purpose anymore and might be removed in
38   * future versions of Groovy</li>
39   * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
40   * @author <a href="maito:blackdrag@gmx.org>Jochen "blackdrag" Theodorou</a>
41   * @version $Revision: 21228 $
42   */
43  public class ASTNode {
44  
45      private int lineNumber = -1;
46      private int columnNumber = -1;
47      private int lastLineNumber = -1;
48      private int lastColumnNumber = -1;
49      private ListHashMap metaDataMap = new ListHashMap(); 
50  
51      public void visit(GroovyCodeVisitor visitor) {
52          throw new RuntimeException("No visit() method implemented for class: " + getClass().getName());
53      }
54  
55      public String getText() {
56          return "<not implemented yet for class: " + getClass().getName() + ">";
57      }
58  
59      public int getLineNumber() {
60          return lineNumber;
61      }
62  
63      public void setLineNumber(int lineNumber) {
64          this.lineNumber = lineNumber;
65      }
66  
67      public int getColumnNumber() {
68          return columnNumber;
69      }
70  
71      public void setColumnNumber(int columnNumber) {
72          this.columnNumber = columnNumber;
73      }
74  
75      public int getLastLineNumber() {
76          return lastLineNumber;
77      }
78  
79      public void setLastLineNumber(int lastLineNumber) {
80          this.lastLineNumber = lastLineNumber;
81      }
82  
83      public int getLastColumnNumber() {
84          return lastColumnNumber;
85      }
86  
87      public void setLastColumnNumber(int lastColumnNumber) {
88          this.lastColumnNumber = lastColumnNumber;
89      }
90      
91      /**
92       * Sets the source position using another ASTNode.
93       * The sourcePosition consists of a line/column pair for
94       * the start and a line/column pair for the end of the
95       * expression or statement 
96       * 
97       * @param node - the node used to configure the position information
98       */
99      public void setSourcePosition(ASTNode node) {
100         this.columnNumber = node.getColumnNumber();
101         this.lastLineNumber = node.getLastLineNumber();
102         this.lastColumnNumber = node.getLastColumnNumber();
103         this.lineNumber = node.getLineNumber();
104     }
105     
106     /**
107      * Gets the node meta data. 
108      * 
109      * @param key - the meta data key
110      * @return the node meta data value for this key
111      */
112     public Object getNodeMetaData(Object key) {
113         return metaDataMap.get(key);
114     }
115     
116     /**
117      * Copies all node meta data from one node to the other
118      * @param other - the other node
119      */
120     public void copyNodeMetaData(ASTNode other) {
121         metaDataMap.putAll(other.metaDataMap);
122     }
123     
124     /**
125      * Sets the node meta data. 
126      * 
127      * @param key - the meta data key
128      * @param value - the meta data value
129      * @throws GroovyBugError if key is null or there is already meta 
130      *                        data under that key
131      */
132     public void setNodeMetaData(Object key, Object value) {
133         if (key==null) throw new GroovyBugError("Tried to set meta data with null key on "+this+".");
134         Object old = metaDataMap.put(key,value);
135         if (old!=null) throw new GroovyBugError("Tried to overwrite existing meta data "+this+".");
136     }
137     
138     /**
139      * Removes a node meta data entry.
140      * 
141      * @param key - the meta data key
142      * @throws GroovyBugError if the key is null
143      */
144     public void removeNodeMetaData(Object key) {
145         if (key==null) throw new GroovyBugError("Tried to remove meta data with null key "+this+".");
146         metaDataMap.remove(key);
147         if (metaDataMap.size()==0) metaDataMap=null;
148     }
149 }