001: //
002: // Generated by JTB 1.2.1
003: //
004:
005: package oscript.syntaxtree;
006:
007: import java.util.*;
008:
009: import oscript.data.OString;
010: import oscript.data.Value;
011: import oscript.parser.Token;
012:
013: /**
014: * Represents a single token in the grammar. If the "-tk" option
015: * is used, also contains a Vector of preceding special tokens.
016: */
017: public class NodeToken implements Node {
018:
019: public NodeToken(String s) {
020: this (s, null, null, null);
021: }
022:
023: public NodeToken(OString os) {
024: this (null, os, null, null);
025: }
026:
027: public NodeToken(String s, OString os, Token t, String desc) {
028: tokenImage = s;
029: otokenImage = os;
030: specialTokens = null;
031:
032: if (t != null) {
033: this .kind = t.kind;
034: this .beginLine = t.beginLine;
035: this .beginColumn = t.beginColumn;
036: this .beginOffset = t.beginOffset;
037: this .endLine = t.endLine;
038: this .endColumn = t.endColumn;
039: this .endOffset = t.endOffset;
040: }
041:
042: this .desc = desc;
043:
044: if (this .desc == null) {
045: this .desc = "";
046: }
047: }
048:
049: public NodeToken getSpecialAt(int i) {
050: if (specialTokens == null)
051: throw new NoSuchElementException("No specials in token");
052: return (NodeToken) specialTokens.elementAt(i);
053: }
054:
055: public int numSpecials() {
056: if (specialTokens == null)
057: return 0;
058: return specialTokens.size();
059: }
060:
061: public void addSpecial(NodeToken s) {
062: if (specialTokens == null)
063: specialTokens = new Vector();
064: specialTokens.addElement(s);
065: }
066:
067: public void trimSpecials() {
068: if (specialTokens == null)
069: return;
070: specialTokens.trimToSize();
071: }
072:
073: public String toString() {
074: return tokenImage;
075: }
076:
077: public String withSpecials() {
078: if (specialTokens == null)
079: return tokenImage;
080:
081: StringBuffer buf = new StringBuffer();
082:
083: for (Enumeration e = specialTokens.elements(); e
084: .hasMoreElements();)
085: buf.append(e.nextElement().toString());
086:
087: buf.append(tokenImage);
088: return buf.toString();
089: }
090:
091: public void accept(oscript.visitor.Visitor v) {
092: v.visit(this );
093: }
094:
095: public Object accept(oscript.visitor.ObjectVisitor v, Object argu) {
096: return v.visit(this , argu);
097: }
098:
099: public String tokenImage;
100: public OString otokenImage;
101:
102: public Value cachedValue;
103:
104: // Stores a list of NodeTokens
105: public Vector specialTokens;
106:
107: // offset into document stream
108: public int off;
109:
110: // -1 for these ints means no position info is available.
111: public int beginLine = -1, beginColumn = -1, beginOffset = -1,
112: endLine = -1, endColumn = -1, endOffset = -1;
113:
114: // begin offset including specials
115: private int actualBeginOffset = -1;
116:
117: public int getActualBeginOffset() {
118: if (actualBeginOffset == -1) {
119: if ((specialTokens == null) || (specialTokens.size() == 0)) {
120: actualBeginOffset = beginOffset;
121: } else {
122: actualBeginOffset = getSpecialAt(0).beginOffset;
123: }
124: }
125: return actualBeginOffset;
126: }
127:
128: public String desc;
129:
130: // Equal to the JavaCC token "kind" integer.
131: // -1 if not available.
132: public int kind;
133: }
|