1
2
3 package net.sourceforge.pmd.ast;
4
5 /**
6 * Describes the input token stream.
7 */
8
9 public class Token {
10
11 /**
12 * An integer that describes the kind of this token. This numbering
13 * system is determined by JavaCCParser, and a table of these numbers is
14 * stored in the file ...Constants.java.
15 */
16 public int kind;
17
18 /** The line number of the first character of this Token. */
19 public int beginLine;
20 /** The column number of the first character of this Token. */
21 public int beginColumn;
22 /** The line number of the last character of this Token. */
23 public int endLine;
24 /** The column number of the last character of this Token. */
25 public int endColumn;
26
27 /**
28 * The string image of the token.
29 */
30 public String image;
31
32 /**
33 * A reference to the next regular (non-special) token from the input
34 * stream. If this is the last token from the input stream, or if the
35 * token manager has not read tokens beyond this one, this field is
36 * set to null. This is true only if this token is also a regular
37 * token. Otherwise, see below for a description of the contents of
38 * this field.
39 */
40 public Token next;
41
42 /**
43 * This field is used to access special tokens that occur prior to this
44 * token, but after the immediately preceding regular (non-special) token.
45 * If there are no such special tokens, this field is set to null.
46 * When there are more than one such special token, this field refers
47 * to the last of these special tokens, which in turn refers to the next
48 * previous special token through its specialToken field, and so on
49 * until the first special token (whose specialToken field is null).
50 * The next fields of special tokens refer to other special tokens that
51 * immediately follow it (without an intervening regular token). If there
52 * is no such token, this field is null.
53 */
54 public Token specialToken;
55
56 /**
57 * An optional attribute value of the Token.
58 * Tokens which are not used as syntactic sugar will often contain
59 * meaningful values that will be used later on by the compiler or
60 * interpreter. This attribute value is often different from the image.
61 * Any subclass of Token that actually wants to return a non-null value can
62 * override this method as appropriate.
63 */
64 public Object getValue() {
65 return null;
66 }
67
68 /**
69 * No-argument constructor
70 */
71 public Token() {}
72
73 /**
74 * Constructs a new token for the specified Image.
75 */
76 public Token(int kind)
77 {
78 this(kind, null);
79 }
80
81 /**
82 * Constructs a new token for the specified Image and Kind.
83 */
84 public Token(int kind, String image)
85 {
86 this.kind = kind;
87 this.image = image;
88 }
89
90 /**
91 * Returns the image.
92 */
93 public String toString()
94 {
95 return image;
96 }
97
98 /**
99 * Returns a new Token object, by default. However, if you want, you
100 * can create and return subclass objects based on the value of ofKind.
101 * Simply add the cases to the switch for all those special cases.
102 * For example, if you have a subclass of Token called IDToken that
103 * you want to create if ofKind is ID, simply add something like :
104 *
105 * case MyParserConstants.ID : return new IDToken(ofKind, image);
106 *
107 * to the following switch statement. Then you can cast matchedToken
108 * variable to the appropriate type and use sit in your lexical actions.
109 */
110 public static Token newToken(int ofKind, String image)
111 {
112 switch(ofKind)
113 {
114 case JavaParserConstants.RUNSIGNEDSHIFT :
115 case JavaParserConstants.RSIGNEDSHIFT :
116 case JavaParserConstants.GT:
117 return new GTToken(ofKind, image);
118 default : return new Token(ofKind, image);
119 }
120 }
121
122 public static final class GTToken extends Token {
123 public int realKind = JavaParserConstants.GT;
124 public GTToken(int ofKind, String image) {
125 super(ofKind, image);
126 }
127 }
128
129 public static Token newToken(int ofKind)
130 {
131 return newToken(ofKind, null);
132 }
133
134 }
135