View Javadoc

1   /*
2    * Copyright (c) 2001-2004,
3    * RedVerst Group, ISP RAS http://www.ispras.ru
4    * All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions are met:
8    *
9    * 1. Redistributions of source code must retain the above copyright notice, this
10   *    list of conditions and the following disclaimer.
11   *
12   * 2. Redistributions in binary form must reproduce the above copyright notice,
13   *    this list of conditions and the following disclaimer in the documentation
14   *    and/or other materials provided with the distribution.
15   *
16   * 3. The names "ATP", "TreeDL", "RedVerst", "ISP RAS"
17   *    may not be used to endorse or promote products derived from this software
18   *    without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
24   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  
32  package com.unitesk.atp.tree.tool.antlr;
33  
34  import antlr.Token;
35  import antlr.CommonToken;
36  
37  import com.unitesk.atp.tree.Node;
38  
39  /***
40   * Token linked with previous/next tokens and (optional) with tree node
41   *
42   * @author <A href="mailto:demakov@ispras.ru">Alexey Demakov</A>
43   * @version $Id: LinkedToken.java,v 1.3 2005/06/01 06:40:22 all-x Exp $
44   */
45  public class LinkedToken extends CommonToken
46  {
47      protected Node node;
48  
49      protected LinkedToken prev;
50      protected LinkedToken next;
51  
52      /***
53       * Overrides {@link CommonToken#getText()}
54       */
55      public String getText()
56      {
57          return getType() == Token.EOF_TYPE ? "<EOF>" : super.getText();  
58      }
59      
60      /***
61       * Link token with tree node.
62       *
63       * @param  node    tree node to link to
64       */
65      public void setNode( Node node )
66      {
67          this.node = node;
68      }
69  
70      /***
71       * Get linked tree node.
72       *
73       * @return         linked tree node or <code>null</code>
74       */
75      public Node getNode()
76      {
77          return node;
78      }
79  
80      /***
81       * Link to the previous token
82       *
83       * @param  prev    the previous token
84       */
85      public void setPrevToken( LinkedToken prev )
86      {
87          this.prev = prev;
88      }
89  
90      /***
91       * Get the previous token
92       *
93       * @return         the previous token
94       *                 (or <code>null</code> for the first token)
95       */
96      public LinkedToken getPrevToken()
97      {
98          return prev;
99      }
100 
101     /***
102      * Link to the next token
103      *
104      * @param  next    the next token
105      */
106     public void setNextToken( LinkedToken next )
107     {
108         this.next = next;
109     }
110 
111     /***
112      * Get the next token
113      *
114      * @return         the next token
115      *                 (or <code>null</code> for the first token)
116      */
117     public LinkedToken getNextToken()
118     {
119         return next;
120     }
121 
122     /***
123      * Insert token before this token.
124      *
125      * @param  before  the text of token to be inserted
126      */
127     public void insertBefore( String before )
128     {
129         LinkedToken t = new LinkedToken();
130 
131         t.setText( before );
132 
133         if( prev != null )
134         {
135             prev.setNextToken( t );
136         }
137 
138         t.setPrevToken( prev );
139         t.setNextToken( this );
140         setPrevToken( t );
141     }
142 
143     /***
144      * Insert token after this token.
145      *
146      * @param  after  the text of token to be inserted
147      */
148     public void insertAfter( String after )
149     {
150         LinkedToken t = new LinkedToken();
151 
152         t.setText( after );
153 
154         if( next != null )
155         {
156             next.setPrevToken( t );
157         }
158 
159         t.setNextToken( next );
160         t.setPrevToken( this );
161         setNextToken( t );
162     }
163 
164     /***
165      * Insert tokens before and after this token.
166      *
167      * @param  before  the text of token to be inserted before
168      * @param  after  the text of token to be inserted after
169      *
170      * @see #insertBefore(String) insertBefore( before )
171      * @see #insertAfter(String) insertAfter( after  )
172      */
173     public void wrap( String before, String after )
174     {
175         insertBefore( before );
176         insertAfter( after );
177     }
178 }