1 package de.fhdw.wtf.common.token;
2
3 import java.io.Serializable;
4
5 /**
6 * A token represents a scanned part of the input grammar. Each token-type defines a regular sequence of characters
7 * belonging to its type.
8 *
9 * This type is the interface describing all tokens.
10 */
11 public abstract class Token implements Serializable {
12
13 /**
14 * generated.
15 */
16 private static final long serialVersionUID = 6143241438846758818L;
17
18 /**
19 * The position is the begin position of this token in the original input document.
20 */
21 private final Position position;
22
23 /**
24 * Constructor initializing the position of this token.
25 *
26 * @param position
27 * begin position of this token
28 */
29 protected Token(final Position position) {
30 this.position = position;
31 }
32
33 /**
34 * @return Returns true, if and only if this token represents any keyword.
35 */
36 public boolean isKeywordToken() {
37 return false; // must be overwritten as implementation
38 }
39
40 /**
41 * @return Returns true, if and only if this token represents keyword: "class".
42 */
43 public boolean isClassToken() {
44 return false; // must be overwritten as implementation
45 }
46
47 /**
48 * @return Returns true, if and only if this token represents keyword: "group".
49 */
50 public boolean isGroupToken() {
51 return false; // must be overwritten as implementation
52 }
53
54 /**
55 * @return Returns true, if and only if this token represents any class modifier.
56 */
57 public boolean isClassModifierToken() {
58 return false; // must be overwritten as implementation
59 }
60
61 /**
62 * @return Returns true, if and only if this token represents keyword: "function".
63 */
64 public boolean isOperationToken() {
65 return false; // must be overwritten as implementation
66 }
67
68 /**
69 * @return Returns true, if and only if this token represents any operation modifier.
70 */
71 public boolean isOperationModifierToken() {
72 return false; // must be overwritten as implementation
73 }
74
75 /**
76 * @return Returns true, if and only if this token represents operation modifier: "abstract".
77 */
78 public boolean isAbstractOperationToken() {
79 return false; // must be overwritten as implementation
80 }
81
82 /**
83 * @return Returns true, if and only if this token represents class modifier: "abstract".
84 */
85 public boolean isAbstractModifierToken() {
86 return false; // must be overwritten as implementation
87 }
88
89 /**
90 * @return Returns true, if and only if this token represents any attribute modifier.
91 */
92 public boolean isAttributeModifierToken() {
93 return false; // must be overwritten as implementation
94 }
95
96 /**
97 * @return Returns true, if and only if this token represents attribute modifier: "prior".
98 */
99 public boolean isPriorModifierToken() {
100 return false; // must be overwritten as implementation
101 }
102
103 /**
104 * @return Returns true, if and only if this token represents a comment.
105 */
106 public boolean isCommentToken() {
107 return false; // must be overwritten as implementation
108 }
109
110 /**
111 * @return Returns true, if and only if this token represents an invalid token. The scanner uses this token to
112 * denote unrecognized characters or unfinished comments.
113 */
114 public boolean isInvalidToken() {
115 return false; // must be overwritten as implementation
116 }
117
118 /**
119 * @return Returns true, if and only if this token represents an identifier.
120 */
121 public boolean isIdentifierToken() {
122 return false; // must be overwritten as implementation
123 }
124
125 /**
126 * @return Returns true, if and only if this token represents the end of a stream. This token always is the last
127 * token in a stream.
128 */
129 public boolean isEndToken() {
130 return false; // must be overwritten as implementation
131 }
132
133 /**
134 * @return Returns true, if and only if this token represents any symbol.
135 */
136 public boolean isSymbolToken() {
137 return false; // must be overwritten as implementation
138 }
139
140 /**
141 * @return Returns true, if and only if this token represents symbol: "*".
142 */
143 public boolean isAsteriskToken() {
144 return false; // must be overwritten as implementation
145 }
146
147 /**
148 * @return Returns true, if and only if this token represents symbol: ";".
149 */
150 public boolean isSemicolonToken() {
151 return false; // must be overwritten as implementation
152 }
153
154 /**
155 * @return @return Returns true, if and only if this token represents symbol: "{".
156 */
157 public boolean isCurlyBracketOpenToken() {
158 return false; // must be overwritten as implementation
159 }
160
161 /**
162 * @return Returns true, if and only if this token represents symbol: "}".
163 */
164 public boolean isCurlyBracketCloseToken() {
165 return false; // must be overwritten as implementation
166 }
167
168 /**
169 * @return Returns true, if and only if this token represents symbol: "+".
170 */
171 public boolean isPlusSymbolToken() {
172 return false; // must be overwritten as implementation
173 }
174
175 /**
176 * @return Returns true, if and only if this token represents symbol: ":".
177 */
178 public boolean isColonToken() {
179 return false; // must be overwritten as implementation
180 }
181
182 /**
183 * @return Returns true, if and only if this token represents symbol: ">".
184 */
185 public boolean isGreaterSymbolToken() {
186 return false; // must be overwritten as implementation
187 }
188
189 /**
190 * @return Returns true, if and only if this token represents symbol: "=".
191 */
192 public boolean isEqualToken() {
193 return false; // must be overwritten as implementation
194 }
195
196 /**
197 * @return Returns true, if and only if this token represents symbol: "[".
198 */
199 public boolean isSquareBracketOpenToken() {
200 return false; // must be overwritten as implementation
201 }
202
203 /**
204 * @return Returns true, if and only if this token represents symbol: "]".
205 */
206 public boolean isSquareBracketCloseToken() {
207 return false; // must be overwritten as implementation
208 }
209
210 /**
211 * @return Returns true, if and only if this token represents whitespace.
212 */
213 public boolean isWhitespaceToken() {
214 return false; // must be overwritten as implementation
215 }
216
217 /**
218 * @return Returns true, if and only if this token represents an exception (keyword: "exception").
219 */
220 public boolean isExceptionToken() {
221 return false; // must be overwritten as implementation
222 }
223
224 /**
225 * @return Returns true, if and only if this token represents a FindableToken.
226 */
227 public boolean isFindableToken() {
228 return false; // must be overwritten as implementation
229 }
230
231 /**
232 * @return Returns true, if and only if this token represents a TransientToken.
233 */
234 public boolean isTransientToken() {
235 return false; // must be overwritten as implementation
236 }
237
238 /**
239 * @return Returns true, if and only if this token represents a ServiceToken.
240 */
241 public boolean isServiceToken() {
242 return false; // must be overwritten as implementation
243 }
244
245 /**
246 * @return Returns true, if and only if this token represents a HyphenToken.
247 */
248 public boolean isHyphenToken() {
249 return false; // must be overwritten as implementation
250 }
251
252 /**
253 * @return Returns true, if and only if this token represents a PipeToken.
254 */
255 public boolean isPipeToken() {
256 return false; // must be overwritten as implementation
257 }
258
259 /**
260 * @return Returns true, if and only if this token represents a BracketOpenToken.
261 */
262 public boolean isBracketOpenToken() {
263 return false; // must be overwritten as implementation
264 }
265
266 /**
267 * @return Returns true, if and only if this token represents a BracketCloseToken.
268 */
269 public boolean isBracketCloseToken() {
270 return false; // must be overwritten as implementation
271 }
272
273 /**
274 * @return Returns true, if and only if this token represents a CommaToken.
275 */
276 public boolean isCommaToken() {
277 return false; // must be overwritten as implementation
278 }
279
280 /**
281 * @return Returns true, if and only if this token represents a MutableToken.
282 */
283 public boolean isMutableToken() {
284 return false; // must be overwritten as implementation
285 }
286
287 /**
288 * @return Returns true, if and only if this token represents a VisitableToken.
289 */
290 public boolean isVisitableToken() {
291 return false; // must be overwritten as implementation
292 }
293
294 /**
295 * @return Returns true, if and only if this token represents a SymmetricToken.
296 */
297 public boolean isSymmetricToken() {
298 return false; // must be overwritten as implementation
299 }
300
301 /**
302 * @return Returns true, if and only if this token represents a DoubleSquareBracketOpenToken.
303 */
304 public boolean isDoubleSquareBracketOpenToken() {
305 return false; // must be overwritten as implementation
306 }
307
308 /**
309 * @return Returns true, if and only if this token represents a DoubleSquareBracketCloseToken.
310 */
311 public boolean isDoubleSquareBracketCloseToken() {
312 return false; // must be overwritten as implementation
313 }
314
315 /**
316 * @return Returns true, if and only if this token represents an ArrowToken.
317 */
318 public boolean isArrowToken() {
319 return false; // must be overwritten as implementation
320 }
321
322 /**
323 * @return Returns true, if and only if this token represents an ExclamationToken.
324 */
325 public boolean isExclamationToken() {
326 return false; // must be overwritten as implementation
327 }
328
329 /**
330 * @return Returns the Tokenrepresentation length, <br>
331 * e.g. A Token with the representation of "abc" returns 3.
332 */
333 public abstract int getLength();
334
335 @Override
336 public boolean equals(final Object o) {
337 if (o instanceof Token) {
338 final Token t = (Token) o;
339 return t.getPosition().equals(this.getPosition()) && this.equalsTemplate(t);
340 }
341 return false;
342 }
343
344 @Override
345 public int hashCode() {
346 return this.getPosition().hashCode() ^ this.hashCodeTemplate();
347 }
348
349 /**
350 * @param t
351 * equal comparison element
352 * @return Returns true if and only if the token t is equal to this<br>
353 * in terms of specialized attributes.
354 */
355 protected abstract boolean equalsTemplate(Token t);
356
357 /**
358 * Returns an hashCode-Value that depents on the equalsTemplate.
359 *
360 * @return hashCode
361 */
362 protected abstract int hashCodeTemplate();
363
364 /**
365 * Creates a string representation of this token the same way as it occurred in the original text.
366 *
367 * @return short string representation
368 */
369 public abstract String stringRepresentation();
370
371 @Override
372 /**
373 * Creates a string representation of this token, including the position.
374 */
375 public String toString() {
376 return this.getClass().getSimpleName() + "[" + this.stringRepresentation() + "]" + " at " + this.getPosition();
377 }
378
379 /**
380 * @return the position of this token.
381 */
382 public Position getPosition() {
383 return this.position;
384 }
385
386 }