1/*
2 [The "BSD licence"]
3 Copyright (c) 2005-2006 Terence Parr
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9 1. Redistributions of source code must retain the above copyright
10    notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12    notice, this list of conditions and the following disclaimer in the
13    documentation and/or other materials provided with the distribution.
14 3. The name of the author may not be used to endorse or promote products
15    derived from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28package org.antlr.runtime.tree {
29	import org.antlr.runtime.Token;
30	import org.antlr.runtime.TokenConstants;
31
32	/** A tree node that is wrapper for a Token object.  After 3.0 release
33	 *  while building tree rewrite stuff, it became clear that computing
34	 *  parent and child index is very difficult and cumbersome.  Better to
35	 *  spend the space in every tree node.  If you don't want these extra
36	 *  fields, it's easy to cut them out in your own BaseTree subclass.
37	 */
38	public class CommonTree extends BaseTree {
39		/** A single token is the payload */
40		protected var _token:Token;
41
42		/** What token indexes bracket all tokens associated with this node
43		 *  and below?
44		 */
45		public var startIndex:int=-1, stopIndex:int=-1;
46
47		/** Who is the parent node of this node; if null, implies node is root */
48		protected var _parent:CommonTree;
49
50		/** What index is this node in the child list? Range: 0..n-1 */
51		protected var _childIndex:int = -1;
52
53		public function CommonTree(node:CommonTree = null) {
54			if (node != null) {
55				super(node);
56				this._token = node._token;
57				this.startIndex = node.startIndex;
58				this.stopIndex = node.stopIndex;
59			}
60		}
61
62		public static function createFromToken(t:Token):CommonTree {
63			var ct:CommonTree = new CommonTree();
64			ct._token = t;
65			return ct;
66		}
67
68		public function get token():Token {
69			return _token;
70		}
71
72		public override function dupNode():Tree {
73			return new CommonTree(this);
74		}
75
76		public override function get isNil():Boolean {
77			return _token==null;
78		}
79
80		public override function get type():int {
81			if ( _token==null ) {
82				return TokenConstants.INVALID_TOKEN_TYPE;
83			}
84			return _token.type;
85		}
86
87		public override function get text():String {
88			if ( _token==null ) {
89				return null;
90			}
91			return _token.text;
92		}
93
94		public override function get line():int {
95			if ( _token==null || _token.line==0 ) {
96				if ( childCount >0 ) {
97					return getChild(0).line;
98				}
99				return 0;
100			}
101			return _token.line;
102		}
103
104		public override function get charPositionInLine():int {
105			if ( _token==null || _token.charPositionInLine==-1 ) {
106				if ( childCount>0 ) {
107					return getChild(0).charPositionInLine;
108				}
109				return 0;
110			}
111			return _token.charPositionInLine;
112		}
113
114		public override function get tokenStartIndex():int {
115			if ( startIndex==-1 && _token!=null ) {
116				return _token.tokenIndex;
117			}
118			return startIndex;
119		}
120
121		public override function set tokenStartIndex(index:int):void {
122			startIndex = index;
123		}
124
125		public override function get tokenStopIndex():int {
126			if ( stopIndex==-1 && _token!=null ) {
127				return _token.tokenIndex;
128			}
129			return stopIndex;
130		}
131
132		public override function set tokenStopIndex(index:int):void {
133			stopIndex = index;
134		}
135
136		public override function get childIndex():int {
137			return _childIndex;
138		}
139
140		public override function get parent():Tree {
141			return _parent;
142		}
143
144		public override function set parent(t:Tree):void {
145			this._parent = CommonTree(t);
146		}
147
148		public override function set childIndex(index:int):void {
149			this._childIndex = index;
150		}
151
152		public function toString():String {
153			if ( isNil ) {
154				return "nil";
155			}
156			if ( type==TokenConstants.INVALID_TOKEN_TYPE ) {
157    			return "<errornode>";
158    		}
159    		if ( token==null ) {
160    			return null;
161    		}
162			return _token.text;
163		}
164	}
165
166}
167