View Javadoc

1   /*
2    * Copyright (c) 2001-2006,
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  package com.unitesk.atp.tree.tool.antlr;
32  
33  import antlr.Token;
34  
35  import com.unitesk.atp.text.location.OffsetPosition;
36  import com.unitesk.atp.text.location.OffsetRange;
37  import com.unitesk.atp.text.location.Position;
38  
39  /***
40   * Create positions from token.
41   *
42   * @author <A href="mailto:demakov@ispras.ru">Alexey Demakov</A>
43   * @version $Id: PositionFactory.java,v 1.2 2006/02/13 15:04:04 all-x Exp $
44   * @since 3.6.1
45  */
46  public abstract class PositionFactory 
47  {
48      /***
49       * Get position of token.
50       * 
51       * @param t        Token to get position.
52       * @return         Position of given token.
53       */
54      public static Position getPosition( Token t )
55      {
56          return new Position( t.getLine(), t.getColumn() );
57      }
58      
59      /***
60       * Get end position of one-line token.
61       * Column of end position points after token, not at the last char.
62       * 
63       * @param t        Token to get end position.
64       * @return         End position of given token.
65       */
66      public static Position getEndPosition( Token t )
67      {
68          return new Position( t.getLine(), t.getColumn() + t.getText().length() );
69      }
70      
71      /***
72       * Get offset position. 
73       * @param t must be of type {@link OffsetToken}
74       * @return offset position of the given token.
75       * @exception ClassCastException if t is not of type {@link OffsetPosition}
76       * @since 3.6.4, 4.0.1
77       */
78      public static OffsetPosition getOffsetPosition( Token t )
79      {
80          return ((OffsetToken)t).getPosition();
81      }
82      
83      /***
84       * Get range of the given one-line token.
85       * @param t offset token
86       * @return range
87       * @exception ClassCastException if t is not of type {@link OffsetPosition}
88       * @since 3.6.4, 4.0.1
89       */
90      public static OffsetRange getRange( Token t )
91      {
92          OffsetPosition start = getOffsetPosition( t );
93          int length = t.getText().length();
94          return new OffsetRange( start, new OffsetPosition( start.getLine()
95                                                           , start.getColumn() + length 
96                                                           , start.getOffset() + length
97                                                           )
98                                );
99      }
100 }