1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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 }