01: /*
02: * Word.java
03: *
04: * Copyright (c) 1997 Cornell University.
05: * Copyright (c) 1997 Sun Microsystems, Inc.
06: *
07: * See the file "license.terms" for information on usage and
08: * redistribution of this file, and for a DISCLAIMER OF ALL
09: * WARRANTIES.
10: *
11: * RCS: @(#) $Id: Word.java,v 1.1.1.1 1998/10/14 21:09:21 cvsadmin Exp $
12: *
13: */
14:
15: package tcl.lang;
16:
17: import java.util.*;
18:
19: /**
20: * This class is used to store a word during the parsing of Tcl commands.
21: */
22: class Word {
23: StringBuffer sbuf;
24: TclObject obj;
25:
26: /**
27: * Number of objects that have been concatenated into this word.
28: */
29: int objCount;
30:
31: Word() {
32: sbuf = null;
33: obj = null;
34: objCount = 0;
35: }
36:
37: void append(TclObject o) {
38: /*
39: * The object inside a word must be preserved. Otherwise code like
40: * the following will fail (prints out 2020 instead of 1020):
41: *
42: * set a 10
43: * puts $a[set a 20]
44: */
45:
46: if (sbuf != null) {
47: sbuf.append(o.toString());
48: } else if (obj != null) {
49: sbuf = new StringBuffer(obj.toString());
50: obj.release();
51: obj = null;
52: sbuf.append(o.toString());
53: } else {
54: obj = o;
55: obj.preserve();
56: }
57:
58: objCount++;
59: }
60:
61: void append(char c) {
62: if (sbuf != null) {
63: sbuf.append(c);
64: } else if (obj != null) {
65: sbuf = new StringBuffer(obj.toString());
66: obj.release();
67: obj = null;
68: sbuf.append(c);
69: } else {
70: sbuf = new StringBuffer();
71: sbuf.append(c);
72: }
73:
74: objCount++;
75: }
76:
77: TclObject getTclObject() {
78: if (sbuf != null) {
79: obj = TclString.newInstance(sbuf.toString());
80: obj.preserve();
81: return obj;
82: } else if (obj != null) {
83: return obj;
84: } else {
85: obj = TclString.newInstance("");
86: obj.preserve();
87: return obj;
88: }
89: }
90: }
|