001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.util.sexp;
038:
039: /**
040: * Provides a common namespace for the token classes.
041: */
042: public interface Tokens {
043:
044: /** These tokens are designed to be compared using the == operator for (, ), ", and \. Otherwise,
045: * the tokens may be compared using the .equals() method. This class is concrete only for testing
046: * purposes.
047: */
048: /* abstract */class SExpToken {
049: protected String _rep;
050:
051: /** @param rep The string representation of this token */
052: public SExpToken(String rep) {
053: _rep = rep;
054: }
055:
056: /** @return the string representation of this token */
057: public String getText() {
058: return _rep;
059: }
060:
061: public boolean equals(Object o) {
062: return (o != null && o.getClass() == getClass() && ((SExpToken) o)._rep
063: .equals(_rep));
064: }
065:
066: public int hashCode() {
067: return _rep.hashCode();
068: }
069:
070: public String toString() {
071: return _rep;
072: }
073: }
074:
075: ////////////// Symbol Tokens ///////////////////
076:
077: class LeftParenToken extends SExpToken {
078: public static final LeftParenToken ONLY = new LeftParenToken();
079:
080: private LeftParenToken() {
081: super ("(");
082: }
083: }
084:
085: class RightParenToken extends SExpToken {
086: public static final RightParenToken ONLY = new RightParenToken();
087:
088: private RightParenToken() {
089: super (")");
090: }
091: }
092:
093: class BackSlashToken extends SExpToken {
094: public static final BackSlashToken ONLY = new BackSlashToken();
095:
096: private BackSlashToken() {
097: super ("\\");
098: }
099: }
100:
101: ////////////// General Tokens //////////////////
102:
103: /** Words include any text (including symbols) that is not a number, a backslash, or a quote character. */
104: class WordToken extends SExpToken {
105: public WordToken(String word) {
106: super (word);
107: }
108: }
109:
110: /** This token is handled as a unit by the lexer. Any text between the pair of quotes is given
111: * in this QuotedTextToken.
112: */
113: class QuotedTextToken extends SExpToken {
114: // private String _txt;
115: public QuotedTextToken(String txt) {
116: super (txt);
117: }
118:
119: public String getFullText() {
120: return "\"" + _rep + "\"";
121: }
122: }
123:
124: /**
125: * Words include any text (including symbols) that
126: * is not a number, a backslash, or a quote character
127: */
128: class BooleanToken extends SExpToken {
129: public static final BooleanToken TRUE = new BooleanToken(true);
130: public static final BooleanToken FALSE = new BooleanToken(false);
131:
132: private boolean _bool;
133:
134: private BooleanToken(boolean bool) {
135: super ("" + bool);
136: _bool = bool;
137: }
138:
139: public boolean getValue() {
140: return _bool;
141: }
142: }
143:
144: /**
145: * Numbers are string s of only digits (0-9)
146: */
147: class NumberToken extends SExpToken {
148: private double _num;
149:
150: public NumberToken(double num) {
151: // If it is a whole number, don't include
152: // the decimal in the string representation
153: super ((num % 1 == 0) ? "" + (int) num : "" + num);
154: _num = num;
155: }
156:
157: public double getValue() {
158: return _num;
159: }
160: }
161:
162: }
|