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