001: /*
002: [The "BSD licence"]
003: Copyright (c) 2005-2006 Terence Parr
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011: 2. Redistributions in binary form must reproduce the above copyright
012: notice, this list of conditions and the following disclaimer in the
013: documentation and/or other materials provided with the distribution.
014: 3. The name of the author may not be used to endorse or promote products
015: derived from this software without specific prior written permission.
016:
017: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
018: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
019: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
020: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
021: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: */
028: package org.antlr.runtime.tree;
029:
030: import org.antlr.runtime.Token;
031:
032: /** A tree node that is wrapper for a Token object. */
033: public class CommonTree extends BaseTree {
034: /** What token indexes bracket all tokens associated with this node
035: * and below?
036: */
037: public int startIndex = -1, stopIndex = -1;
038:
039: /** A single token is the payload */
040: public Token token;
041:
042: public CommonTree() {
043: }
044:
045: public CommonTree(CommonTree node) {
046: super (node);
047: this .token = node.token;
048: }
049:
050: public CommonTree(Token t) {
051: this .token = t;
052: }
053:
054: public Token getToken() {
055: return token;
056: }
057:
058: public Tree dupNode() {
059: return new CommonTree(this );
060: }
061:
062: public boolean isNil() {
063: return token == null;
064: }
065:
066: public int getType() {
067: if (token == null) {
068: return 0;
069: }
070: return token.getType();
071: }
072:
073: public String getText() {
074: if (token == null) {
075: return null;
076: }
077: return token.getText();
078: }
079:
080: public int getLine() {
081: if (token == null || token.getLine() == 0) {
082: if (getChildCount() > 0) {
083: return getChild(0).getLine();
084: }
085: return 0;
086: }
087: return token.getLine();
088: }
089:
090: public int getCharPositionInLine() {
091: if (token == null || token.getCharPositionInLine() == -1) {
092: if (getChildCount() > 0) {
093: return getChild(0).getCharPositionInLine();
094: }
095: return 0;
096: }
097: return token.getCharPositionInLine();
098: }
099:
100: public int getTokenStartIndex() {
101: if (startIndex == -1 && token != null) {
102: return token.getTokenIndex();
103: }
104: return startIndex;
105: }
106:
107: public void setTokenStartIndex(int index) {
108: startIndex = index;
109: }
110:
111: public int getTokenStopIndex() {
112: if (stopIndex == -1 && token != null) {
113: return token.getTokenIndex();
114: }
115: return stopIndex;
116: }
117:
118: public void setTokenStopIndex(int index) {
119: stopIndex = index;
120: }
121:
122: public String toString() {
123: if (isNil()) {
124: return "nil";
125: }
126: return token.getText();
127: }
128: }
|