001: /*
002: * TclToken.java --
003: *
004: * For each word of a command, and for each piece of a word such as a
005: * variable reference, a TclToken is used to describe the word.
006: *
007: * Note: TclToken is designed to be write-once with respect to
008: * setting the script and size variables. Failure to do this
009: * may lead to inconsistencies in calls to getTokenString().
010: *
011: * Copyright (c) 1997 by Sun Microsystems, Inc.
012: *
013: * See the file "license.terms" for information on usage and redistribution
014: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
015: *
016: * RCS: @(#) $Id: TclToken.java,v 1.5 2005/10/29 00:27:43 mdejong Exp $
017: */
018:
019: package tcl.lang;
020:
021: class TclToken {
022:
023: // Contains an array the references the script from where the
024: // token originates from and an index to the first character
025: // of the token inside the script.
026:
027: char[] script_array;
028: // FIXME: Might be better to rename this as start so that
029: // the Java impl looks more like the C impl in source code.
030: int script_index;
031:
032: // Number of bytes in token.
033:
034: int size;
035:
036: // Type of token, such as TCL_TOKEN_WORD; See Parse.java
037: // for valid types.
038:
039: int type;
040:
041: // If this token is composed of other tokens, this field
042: // tells how many of them there are (including components
043: // of components, etc.). The component tokens immediately
044: // follow this one.
045:
046: int numComponents;
047:
048: /*
049: *----------------------------------------------------------------------
050: *
051: * Tcl_Token -> TclToken
052: *
053: * All class variables are accessed directly for speed. The
054: * consrructor only needs to allocate memory for the script.
055: *
056: * Side effects:
057: * None.
058: *
059: *----------------------------------------------------------------------
060: */
061:
062: TclToken() {
063: script_array = null;
064: script_index = -1;
065: }
066:
067: /*
068: *----------------------------------------------------------------------
069: *
070: * getTokenString --
071: *
072: * The token string is a substring in the script. The beginning
073: * is deliminated by script_index, and the offset is the size
074: * variable. The first time this method is called the substring
075: * is cached in the str variable.
076: *
077: * Note: TclToken is designed to be write-once with respect to
078: * setting the script and size variables. Failure to do this
079: * may lead to inconsistencies in str cache.
080: *
081: * Results:
082: * The String representing the token.
083: *
084: * Side effects:
085: * None.
086: *
087: *----------------------------------------------------------------------
088: */
089:
090: String getTokenString() {
091: final boolean debug = false;
092:
093: if (debug && ((script_index + size) > script_array.length)) {
094: System.out.println();
095: System.out.println("Entered TclToken.getTokenString()");
096: System.out.println("hashCode() is " + hashCode());
097: System.out.println("script_array.length is "
098: + script_array.length);
099: System.out.println("script_index is " + script_index);
100: System.out.println("size is " + size);
101:
102: System.out.print("the string is \"");
103: for (int k = 0; k < script_array.length; k++) {
104: System.out.print(script_array[k]);
105: }
106: System.out.println("\"");
107: }
108:
109: return (new String(script_array, script_index, size));
110: }
111:
112: /*
113: *----------------------------------------------------------------------
114: *
115: * toString --
116: *
117: * Prints debug information about the TclToken.
118: *
119: * Results:
120: * A String containing the info.
121: *
122: * Side effects:
123: * None.
124: *
125: *----------------------------------------------------------------------
126: */
127:
128: public String toString() {
129: StringBuffer sbuf = new StringBuffer();
130: switch (type) {
131: case Parser.TCL_TOKEN_WORD: {
132: sbuf.append("\n Token Type: TCL_TOKEN_WORD");
133: break;
134: }
135: case Parser.TCL_TOKEN_SIMPLE_WORD: {
136: sbuf.append("\n Token Type: TCL_TOKEN_SIMPLE_WORD");
137: break;
138: }
139: case Parser.TCL_TOKEN_TEXT: {
140: sbuf.append("\n Token Type: TCL_TOKEN_TEXT");
141: break;
142: }
143: case Parser.TCL_TOKEN_BS: {
144: sbuf.append("\n Token Type: TCL_TOKEN_BS");
145: break;
146: }
147: case Parser.TCL_TOKEN_COMMAND: {
148: sbuf.append("\n Token Type: TCL_TOKEN_COMMAND");
149: break;
150: }
151: case Parser.TCL_TOKEN_VARIABLE: {
152: sbuf.append("\n Token Type: TCL_TOKEN_VARIABLE");
153: break;
154: }
155: case Parser.TCL_TOKEN_SUB_EXPR: {
156: sbuf.append("\n Token Type: TCL_TOKEN_SUB_EXPR");
157: break;
158: }
159: case Parser.TCL_TOKEN_OPERATOR: {
160: sbuf.append("\n Token Type: TCL_TOKEN_OPERATOR");
161: break;
162: }
163: }
164:
165: String ts = getTokenString();
166: sbuf.append("\n String: " + ts);
167: sbuf.append("\n String Size: " + ts.length());
168: sbuf.append("\n ScriptIndex: " + script_index);
169: sbuf.append("\n NumComponents: " + numComponents);
170: sbuf.append("\n Token Size: " + size);
171: return sbuf.toString();
172: }
173:
174: } // end TclToken
|