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  
17  package org.codehaus.groovy.syntax;
18  
19  import org.codehaus.groovy.GroovyBugError;
20  
21  
22  /**
23   *  A <code>CSTNode</code> produced by the <code>Lexer</code>.
24   *
25   *  @see antlr.Parser
26   *  @see antlr.Token
27   *  @see Reduction
28   *  @see Types
29   *
30   *  @author <a href="mailto:bob@werken.com">bob mcwhirter</a>
31   *  @author <a href="mailto:cpoirier@dreaming.org">Chris Poirier</a>
32   *
33   *  @version $Id: Token.java 6778 2007-07-02 10:40:33Z glaforge $
34   */
35  
36  public class Token extends CSTNode
37  {
38      public static final Token NULL = new Token();
39      public static final Token EOF  = new Token( Types.EOF, "", -1, -1 );
40  
41  
42    //---------------------------------------------------------------------------
43    // TOKEN INITIALIZATION AND SUCH
44  
45      private int type        = Types.UNKNOWN;  // the actual type identified by the lexer
46      private int meaning     = Types.UNKNOWN;  // an interpretation applied to the token after the fact
47  
48      private String     text = "";             // the text of the token
49      private int   startLine = -1;             // the source line on which the token begins
50      private int startColumn = -1;             // the source column on which the token begins
51  
52  
53     /**
54      *  Initializes the Token with the specified information.
55      */
56  
57      public Token( int type, String text, int startLine, int startColumn )
58      {
59          this.type        = type;
60          this.meaning     = type;
61          this.text        = text;
62          this.startLine   = startLine;
63          this.startColumn = startColumn;
64      }
65  
66  
67     /**
68      *  Initializes the NULL Token.
69      */
70  
71      private Token() { }
72  
73  
74  
75     /**
76      *  Returns a copy of this Token.
77      */
78  
79      public Token dup()
80      {
81          Token token = new Token( this.type, this.text, this.startLine, this.startColumn );
82          token.setMeaning( this.meaning );
83  
84          return token;
85      }
86  
87  
88  
89  
90    //---------------------------------------------------------------------------
91    // NODE IDENTIFICATION AND MEANING
92  
93  
94     /**
95      *  Returns the meaning of this node.  If the node isEmpty(), returns
96      *  the type of Token.NULL.
97      */
98  
99      public int getMeaning()
100     {
101         return meaning;
102     }
103 
104 
105 
106    /**
107     *  Sets the meaning for this node (and it's root Token).  Not
108     *  valid if the node isEmpty().  Returns this token, for
109     *  convenience.
110     */
111 
112     public CSTNode setMeaning( int meaning )
113     {
114         this.meaning = meaning;
115         return this;
116     }
117 
118 
119 
120    /**
121     *  Returns the actual type of the node.  If the node isEmpty(), returns
122     *  the type of Token.NULL.
123     */
124 
125     public int getType()
126     {
127         return type;
128     }
129 
130 
131 
132 
133   //---------------------------------------------------------------------------
134   // MEMBER ACCESS
135 
136 
137    /**
138     *  Returns the number of elements in the node (including root).
139     */
140 
141     public int size()
142     {
143         return 1;
144     }
145 
146 
147 
148    /**
149     *  Returns the specified element, or null.
150     */
151 
152     public CSTNode get( int index )
153     {
154         if( index > 0 )
155         {
156             throw new GroovyBugError( "attempt to access Token element other than root" );
157         }
158 
159         return this;
160     }
161 
162 
163 
164    /**
165     *  Returns the root of the node.  By convention, all nodes have
166     *  a Token as the first element (or root), which indicates the type
167     *  of the node.  May return null if the node <code>isEmpty()</code>.
168     */
169 
170     public Token getRoot()
171     {
172         return this;
173     }
174 
175 
176 
177    /**
178     *  Returns the text of the root node.  Uses <code>getRoot(true)</code>
179     *  to get the root, so you will only receive null in return if the
180     *  root token returns it.
181     */
182 
183     public String getRootText()
184     {
185         return text;
186     }
187 
188 
189 
190    /**
191     *  Returns the text of the token.  Equivalent to
192     *  <code>getRootText()</code> when called directly.
193     */
194 
195     public String getText()
196     {
197         return text;
198     }
199 
200 
201 
202    /**
203     *  Not advisable, but if you need to adjust the token's text, this
204     *  will do it.
205     */
206 
207     public void setText( String text )
208     {
209         this.text = text;
210     }
211 
212 
213 
214    /**
215     *  Returns the starting line of the node.  Returns -1
216     *  if not known.
217     */
218 
219     public int getStartLine()
220     {
221         return startLine;
222     }
223 
224 
225 
226    /**
227     *  Returns the starting column of the node.  Returns -1
228     *  if not known.
229     */
230 
231     public int getStartColumn()
232     {
233         return startColumn;
234     }
235 
236 
237 
238 
239   //---------------------------------------------------------------------------
240   // OPERATIONS
241 
242 
243    /**
244     *  Creates a <code>Reduction</code> from this token.  Returns self if the
245     *  node is already a <code>Reduction</code>.
246     */
247 
248     public Reduction asReduction()
249     {
250         return new Reduction( this );
251     }
252 
253 
254 
255    /**
256     *  Creates a <code>Reduction</code> from this token, adding the supplied
257     *  node as the second element.
258     */
259 
260     public Reduction asReduction( CSTNode second )
261     {
262         Reduction created = asReduction();
263         created.add( second );
264         return created;
265     }
266 
267 
268 
269    /**
270     *  Creates a <code>Reduction</code> from this token, adding the supplied
271     *  nodes as the second and third element, respectively.
272     */
273 
274     public Reduction asReduction( CSTNode second, CSTNode third )
275     {
276         Reduction created = asReduction( second );
277         created.add( third );
278         return created;
279     }
280 
281 
282 
283    /**
284     *  Creates a <code>Reduction</code> from this token, adding the supplied
285     *  nodes as the second, third, and fourth element, respectively.
286     */
287 
288     public Reduction asReduction( CSTNode second, CSTNode third, CSTNode fourth )
289     {
290         Reduction created = asReduction( second, third );
291         created.add( fourth );
292         return created;
293     }
294 
295 
296 
297 
298   //---------------------------------------------------------------------------
299   // TOKEN FACTORIES
300 
301 
302    /**
303     *  Creates a token that represents a keyword.  Returns null if the
304     *  specified text isn't a keyword.
305     */
306 
307     public static Token newKeyword( String text, int startLine, int startColumn )
308     {
309 
310         int type = Types.lookupKeyword( text );
311         if( type != Types.UNKNOWN )
312         {
313             return new Token( type, text, startLine, startColumn );
314         }
315 
316         return null;
317 
318     }
319 
320 
321    /**
322     *  Creates a token that represents a double-quoted string.
323     */
324 
325     public static Token newString( String text, int startLine, int startColumn )
326     {
327         return new Token( Types.STRING, text, startLine, startColumn );
328     }
329 
330 
331    /**
332     *  Creates a token that represents an identifier.
333     */
334 
335     public static Token newIdentifier( String text, int startLine, int startColumn )
336     {
337         return new Token( Types.IDENTIFIER, text, startLine, startColumn );
338     }
339 
340 
341    /**
342     *  Creates a token that represents an integer.
343     */
344 
345     public static Token newInteger( String text, int startLine, int startColumn )
346     {
347         return new Token( Types.INTEGER_NUMBER, text, startLine, startColumn );
348     }
349 
350 
351    /**
352     *  Creates a token that represents a decimal number.
353     */
354 
355     public static Token newDecimal( String text, int startLine, int startColumn )
356     {
357         return new Token( Types.DECIMAL_NUMBER, text, startLine, startColumn );
358     }
359 
360 
361    /**
362     *  Creates a token that represents a symbol, using a library for the text.
363     */
364 
365     public static Token newSymbol( int type, int startLine, int startColumn )
366     {
367         return new Token( type, Types.getText(type), startLine, startColumn );
368     }
369 
370 
371    /**
372     *  Creates a token that represents a symbol, using a library for the type.
373     */
374 
375     public static Token newSymbol( String type, int startLine, int startColumn )
376     {
377         return new Token( Types.lookupSymbol(type), type, startLine, startColumn );
378     }
379 
380 
381    /**
382     *  Creates a token with the specified meaning.
383     */
384 
385     public static Token newPlaceholder( int type )
386     {
387         Token token = new Token( Types.UNKNOWN, "", -1, -1 );
388         token.setMeaning( type );
389 
390         return token;
391     }
392 
393 }