View Javadoc
Minimize
Table

Bug Overview

linebug prioritybugbug descriptionexisting sinceauthor
222mediumIL_INFINITE_LOOPDiese Schleife terminiert nicht. Bitte Code überprüfen! Falls dies beabsichtigt ist und der Fehler soll ignoriert werden, dann //NOBUG verwenden. 18.06.2012-10:31Unbekannt

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  
17  package org.codehaus.groovy.syntax;
18  
19  import org.codehaus.groovy.GroovyBugError;
20  
21  import java.util.List;
22  import java.util.ArrayList;
23  import java.util.Collections;
24  
25  
26  /** 
27   *  A syntax reduction, produced by the <code>Parser</code>.
28   *
29   *  @see antlr.Parser
30   *  @see Token
31   *  @see CSTNode
32   *  @see Types
33   *
34   *  @author <a href="mailto:bob@werken.com">bob mcwhirter</a>
35   *  @author <a href="mailto:cpoirier@dreaming.org">Chris Poirier</a>
36   *
37   *  @version $Id: Reduction.java 7922 2007-09-04 01:42:57Z paulk $
38   */
39  
40  public class Reduction extends CSTNode
41  {
42      public static final Reduction EMPTY = new Reduction();
43  
44  
45    //---------------------------------------------------------------------------
46    // INITIALIZATION AND SUCH
47  
48      private List    elements  = null;    // The set of child nodes   
49      private boolean marked    = false;   // Used for completion marking by some parts of the parser
50  
51  
52     /**
53      *  Initializes the <code>Reduction</code> with the specified root.
54      */
55  
56      public Reduction( Token root ) 
57      {
58          elements = new ArrayList();
59          set( 0, root );
60      }
61  
62  
63     /**
64      *  Initializes the <code>Reduction</code> to empty.
65      */
66  
67      private Reduction() 
68      {
69          elements = Collections.EMPTY_LIST;
70      }
71  
72  
73     /**
74      *  Creates a new <code>Reduction</code> with <code>Token.NULL</code>
75      *  as it's root.
76      */
77  
78      public static Reduction newContainer() 
79      {
80          return new Reduction( Token.NULL );
81      }
82  
83  
84  
85  
86    //---------------------------------------------------------------------------
87    // MEMBER ACCESS
88  
89  
90     /**
91      *  Returns true if the node is completely empty (no root, even).
92      */
93  
94      public boolean isEmpty() 
95      {
96          return size() == 0;
97      }
98  
99  
100 
101    /**
102     *  Returns the number of elements in the node.
103     */
104 
105     public int size() 
106     {
107         return elements.size();
108     }
109 
110 
111 
112    /**
113     *  Returns the specified element, or null.
114     */
115 
116     public CSTNode get( int index ) 
117     {
118         CSTNode element = null;
119 
120         if( index < size() ) 
121         {
122             element = (CSTNode)elements.get( index );
123         }
124 
125         return element;
126     }
127 
128 
129 
130    /**
131     *  Returns the root of the node, the Token that indicates it's
132     *  type.  Returns null if there is no root (usually only if the
133     *  node is a placeholder of some kind -- see isEmpty()).
134     */
135 
136     public Token getRoot() 
137     {
138         if( size() > 0 )
139         {
140             return (Token)elements.get(0);
141         }
142         else
143         {
144             return null;
145         }
146     }
147 
148 
149 
150    /**
151     *  Marks the node a complete expression.
152     */
153 
154     public void markAsExpression() 
155     {
156         marked = true;
157     }
158 
159 
160 
161    /**
162     *  Returns true if the node is a complete expression.
163     */
164 
165     public boolean isAnExpression() 
166     {
167         if( isA(Types.COMPLEX_EXPRESSION) ) 
168         {
169             return true;
170         }
171 
172         return marked;
173     }
174 
175 
176 
177 
178   //---------------------------------------------------------------------------
179   // OPERATIONS
180 
181 
182    /**
183     *  Adds an element to the node.
184     */
185 
186     public CSTNode add( CSTNode element ) 
187     {
188         return set( size(), element );
189     }
190 
191 
192 
193    /**
194     *  Sets an element in at the specified index.
195     */
196 
197     public CSTNode set( int index, CSTNode element ) 
198     {
199         
200         if( elements == null ) 
201         {
202             throw new GroovyBugError( "attempt to set() on a EMPTY Reduction" );
203         }
204 
205         if( index == 0 && !(element instanceof Token) ) 
206         {
207 
208             //
209             // It's not the greatest of design that the interface allows this, but it
210             // is a tradeoff with convenience, and the convenience is more important.
211 
212             throw new GroovyBugError( "attempt to set() a non-Token as root of a Reduction" );
213         }
214 
215 
216         //
217         // Fill slots with nulls, if necessary.
218 
219         int count = elements.size();
220         if( index >= count ) 
221         {
222 bug overview               for( int i = count; i <= index; i++ )   HEALTH4J >>  :  IL_INFINITE_LOOP 
223             {
224                 elements.add( null );
225             }
226         }
227 
228         //
229         // Then set in the element.
230 
231         elements.set( index, element );
232 
233         return element;
234     }
235 
236 
237 
238    /**
239     *  Removes a node from the <code>Reduction</code>.  You cannot remove 
240     *  the root node (index 0).
241     */
242 
243     public CSTNode remove( int index )
244     {
245         if( index < 1 ) 
246         {
247             throw new GroovyBugError( "attempt to remove() root node of Reduction" );
248         }
249 
250         return (CSTNode)elements.remove( index );
251     }
252 
253 
254 
255    /**
256     *  Creates a <code>Reduction</code> from this node.  Returns self if the
257     *  node is already a <code>Reduction</code>.
258     */
259 
260     public Reduction asReduction() 
261     {
262         return this;
263     }
264 
265 }
266