KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > parsec > tokens > MyToken


1 /*****************************************************************************
2  * Copyright (C) Zephyr Business Solutions Corp. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  *****************************************************************************/

8 /*
9  * Created on Dec 8, 2004
10  *
11  * Author Ben Yu
12  */

13 package jfun.parsec.tokens;
14
15 import jfun.parsec.Tokenizer;
16
17 /**
18  * Used to differentiate different token kind by a kind id.
19  * It is to avoid creating multiple simple tokens.
20  * @author Ben Yu
21  *
22  * Dec 8, 2004
23  *
24  */

25 @Deprecated JavaDoc
26 public class MyToken extends TokenWord
27 implements java.io.Serializable JavaDoc{
28   private final int kind;
29   
30   public boolean equals(Object JavaDoc v) {
31     if(v instanceof MyToken){
32       final MyToken mt2 = (MyToken)v;
33       return kind == mt2.kind && getWord().equals(mt2.getWord());
34     }
35     else return false;
36   }
37   public String JavaDoc toString() {
38     return super.toString() + ":" + kind;
39   }
40   public int hashCode() {
41     return super.hashCode() * 31 + kind;
42   }
43   /**
44    * @param text the text this token represents.
45    * @param kind the token kind.
46    */

47   MyToken(final String JavaDoc text, int kind) {
48     super(text);
49     this.kind = kind;
50   }
51   /**
52    * Get the kind number.
53    * @return the kind number.
54    */

55   public final int getKind(){return kind;}
56   /**
57    * Create a tokenizer that's gonna return a MyToken object with a certain kind number.
58    * @param k the kind number.
59    * @return the Tokenizer object.
60    */

61   public static Tokenizer getTokenizer(final int k){
62     return new Tokenizer(){
63       public Object JavaDoc toToken(final CharSequence JavaDoc cs, final int from, final int len){
64         return new MyToken(cs.subSequence(from, from+len).toString(), k);
65       }
66     };
67   }
68 }
69
Popular Tags