001: /*
002: * xtc - The eXTensible Compiler
003: * Copyright (C) 2006-2007 Robert Grimm
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public License
007: * version 2.1 as published by the Free Software Foundation.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
017: * USA.
018: */
019: package xtc.tree;
020:
021: /**
022: * A token. A token is an immutable tree node containing a source
023: * file symbol.
024: *
025: * @author Robert Grimm
026: * @version $Revision: 1.7 $
027: */
028: public final class Token extends Node {
029:
030: /** The text. */
031: private final String text;
032:
033: /**
034: * Create a new token.
035: *
036: * @param text The text.
037: */
038: public Token(String text) {
039: this .text = text;
040: }
041:
042: public boolean isToken() {
043: return true;
044: }
045:
046: public Token toToken() {
047: return this ;
048: }
049:
050: public String getTokenText() {
051: return text;
052: }
053:
054: public boolean hasTraversal() {
055: return true;
056: }
057:
058: public int size() {
059: return 1;
060: }
061:
062: public Object get(int index) {
063: if (0 == index) {
064: return text;
065: } else {
066: throw new IndexOutOfBoundsException("Index: " + index
067: + ", Size: 1");
068: }
069: }
070:
071: public Object set(int index, Object value) {
072: throw new IllegalStateException("Not modifiable");
073: }
074:
075: // ========================================================================
076:
077: /**
078: * Determine whether the specified object represents a string.
079: *
080: * @param o The object.
081: * @return <code>true</code> if the specifed object is a string or a
082: * possibly annotated token.
083: */
084: public static final boolean test(Object o) {
085: return ((o instanceof String) || ((o instanceof Node) && ((Node) o)
086: .strip().isToken()));
087: }
088:
089: /**
090: * Cast the specified object to a string. If the specified object
091: * is a string, this method simply returns the string. Otherwise,
092: * it casts the object to a node, strips all annotations, and then
093: * returns the resulting token's text.
094: *
095: * @see #test(Object)
096: *
097: * @param o The object.
098: * @return The corresponding string.
099: * @throws ClassCastException Signals that the object does not
100: * describe a string.
101: */
102: public static final String cast(Object o) {
103: if (null == o) {
104: return null;
105: } else if (o instanceof String) {
106: return (String) o;
107: } else {
108: return ((Node) o).getTokenText();
109: }
110: }
111:
112: }
|