KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > util > sexp > Tokens


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project:
4  * http://sourceforge.net/projects/drjava/ or http://www.drjava.org/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2003 JavaPLT group at Rice University (javaplt@rice.edu)
9  * All rights reserved.
10  *
11  * Developed by: Java Programming Languages Team
12  * Rice University
13  * http://www.cs.rice.edu/~javaplt/
14  *
15  * Permission is hereby granted, free of charge, to any person obtaining a
16  * copy of this software and associated documentation files (the "Software"),
17  * to deal with the Software without restriction, including without
18  * limitation the rights to use, copy, modify, merge, publish, distribute,
19  * sublicense, and/or sell copies of the Software, and to permit persons to
20  * whom the Software is furnished to do so, subject to the following
21  * conditions:
22  *
23  * - Redistributions of source code must retain the above copyright
24  * notice, this list of conditions and the following disclaimers.
25  * - Redistributions in binary form must reproduce the above copyright
26  * notice, this list of conditions and the following disclaimers in the
27  * documentation and/or other materials provided with the distribution.
28  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the
29  * names of its contributors may be used to endorse or promote products
30  * derived from this Software without specific prior written permission.
31  * - Products derived from this software may not be called "DrJava" nor
32  * use the term "DrJava" as part of their names without prior written
33  * permission from the JavaPLT group. For permission, write to
34  * javaplt@rice.edu.
35  *
36  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
37  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
39  * THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
40  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
41  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
42  * OTHER DEALINGS WITH THE SOFTWARE.
43  *
44 END_COPYRIGHT_BLOCK*/

45
46 package edu.rice.cs.util.sexp;
47
48 /**
49  * Provides a common namespace for the token classes.
50  */

51 public interface Tokens {
52   
53   /** These tokens are designed to be compared using the == operator for (, ), ", and \. Otherwise,
54   * the tokens may be compared using the .equals() method. This class is concrete only for testing
55   * purposes.
56   */

57   /* abstract */ class SExpToken {
58     protected String JavaDoc _rep;
59     
60     /** @param rep The string representation of this token */
61     public SExpToken(String JavaDoc rep) { _rep = rep; }
62     
63     /** @return the string representation of this token */
64     public String JavaDoc getText() { return _rep; }
65     
66     public boolean equals(Object JavaDoc o) {
67       return (o!=null && o.getClass() == getClass() && ((SExpToken)o)._rep.equals(_rep));
68     }
69     
70     public int hashCode() { return _rep.hashCode(); }
71     
72     public String JavaDoc toString() { return _rep; }
73   }
74   
75   ////////////// Symbol Tokens ///////////////////
76

77   class LeftParenToken extends SExpToken {
78     public static final LeftParenToken ONLY = new LeftParenToken();
79     private LeftParenToken(){ super("("); }
80   }
81   
82   class RightParenToken extends SExpToken {
83     public static final RightParenToken ONLY = new RightParenToken();
84     private RightParenToken(){ super(")"); }
85   }
86   
87   class BackSlashToken extends SExpToken {
88     public static final BackSlashToken ONLY = new BackSlashToken();
89     private BackSlashToken(){ super("\\"); }
90   }
91   
92   ////////////// General Tokens //////////////////
93

94   /** Words include any text (including symbols) that is not a number, a backslash, or a quote character. */
95   class WordToken extends SExpToken { public WordToken(String JavaDoc word) { super(word); } }
96   
97   /** This token is handled as a unit by the lexer. Any text between the pair of quotes is given
98   * in this QuotedTextToken.
99   */

100   class QuotedTextToken extends SExpToken {
101     // private String _txt;
102
public QuotedTextToken(String JavaDoc txt) { super(txt); }
103     public String JavaDoc getFullText() { return "\"" + _rep + "\""; }
104   }
105   
106   /**
107   * Words include any text (including symbols) that
108    * is not a number, a backslash, or a quote character
109    */

110   class BooleanToken extends SExpToken {
111     public static final BooleanToken TRUE = new BooleanToken(true);
112     public static final BooleanToken FALSE = new BooleanToken(false);
113     
114     private boolean _bool;
115     private BooleanToken(boolean bool){
116       super(""+bool);
117       _bool = bool;
118     }
119     public boolean getValue() { return _bool; }
120   }
121   
122   /**
123   * Numbers are string s of only digits (0-9)
124    */

125   class NumberToken extends SExpToken {
126     private double _num;
127     public NumberToken(double num){
128       // If it is a whole number, don't include
129
// the decimal in the string representation
130
super((num % 1 == 0) ? "" + (int)num : "" + num);
131       _num = num;
132     }
133     public double getValue() { return _num; }
134   }
135   
136 }
137
Popular Tags