package rebelsky.compiler.lexer; /** * Tokens with names. Intended primarily as a class that can be * overriden for the various kinds of tokens (e.g., String, Identifier). * * @author Samuel A. Rebelsky * @version 1.1 of December 2002 */ public class NamedToken extends Token { // +--------+------------------------------------------------------------ // | Fields | // +--------+ /** The string associated with this token. */ String name; // +--------------+------------------------------------------------------ // | Constructors | // +--------------+ /** * Create a new token with a particular name and type. */ public NamedToken(String name, int tokType) { super(tokType); this.name = name; } // Keyword(String) // +-----------+--------------------------------------------------------- // | Observers | // +-----------+ /** * Convert the token to a string (e.g., for printing). */ public String toString() { return this.name; } // toString(void) /** * Determine if the token is the same as another token. You * may want to override this method (e.g., to do direct * pointer comparison). */ public boolean equals(Token other) { try { return equals((NamedToken) other); } // try catch (ClassCastException cce) { return false; } // catch } // equals(Token) /** * Determine if this named token is the same as another named * token. To be equal, the two tokens must share the same * type, name, and class. */ public boolean equals(NamedToken other) { return this == other // Pointer comparison is fast || ( (this.getType() == other.getType()) // Same type && other.name.equals(this.name) // Same name && other.getClass().equals(this.getClass()) ); // Same class } // equals(NamedToken) /** * Get the name of the token. */ public String getName() { return this.name; } // getName() } // class NamedToken