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 {
29
30
31
32public class CommonToken implements Token {
33	protected var _type:int;
34	protected var _line:int;
35	protected var _charPositionInLine:int = -1; // set to invalid position
36	protected var _channel:int = TokenConstants.DEFAULT_CHANNEL;
37	protected var _input:CharStream;
38
39	/** We need to be able to change the text once in a while.  If
40	 *  this is non-null, then getText should return this.  Note that
41	 *  start/stop are not affected by changing this.
42	 */
43	protected var _text:String;
44
45	/** What token number is this from 0..n-1 tokens; < 0 implies invalid index */
46	protected var _index:int = -1;
47
48	/** The char position into the input buffer where this token starts */
49	protected var _start:int;
50
51	/** The char position into the input buffer where this token stops */
52	protected var _stop:int;
53
54	public function CommonToken(type:int, text:String = null) {
55		this._type = type;
56		this._text = text;
57	}
58
59	public static function createFromStream(input:CharStream, type:int, channel:int, start:int, stop:int):CommonToken {
60		var token:CommonToken = new CommonToken(type);
61		token._input = input;
62		token._channel = channel;
63		token._start = start;
64		token._stop = stop;
65		return token;
66	}
67
68	public static function cloneToken(oldToken:Token):CommonToken {
69		var token:CommonToken = new CommonToken(oldToken.type, oldToken.text);
70		token._line = oldToken.line;
71		token._index = oldToken.tokenIndex;
72		token._charPositionInLine = oldToken.charPositionInLine;
73		token._channel = oldToken.channel;
74		if ( oldToken is CommonToken ) {
75			token._start = CommonToken(oldToken).startIndex;
76			token._stop = CommonToken(oldToken).stopIndex;
77		}
78		return token;
79	}
80
81	public function get type():int {
82		return _type;
83	}
84
85	public function set line(line:int):void {
86		_line = line;
87	}
88
89	public function get text():String {
90		if ( _text!=null ) {
91			return _text;
92		}
93		if ( _input==null ) {
94			return null;
95		}
96		_text = _input.substring(_start, _stop);
97		return _text;
98	}
99
100	/** Override the text for this token.  getText() will return this text
101	 *  rather than pulling from the buffer.  Note that this does not mean
102	 *  that start/stop indexes are not valid.  It means that that input
103	 *  was converted to a new string in the token object.
104	 */
105	public function set text(text:String):void {
106		_text = text;
107	}
108
109	public function get line():int {
110		return _line;
111	}
112
113	public function get charPositionInLine():int {
114		return _charPositionInLine;
115	}
116
117	public function set charPositionInLine(charPositionInLine:int):void {
118		_charPositionInLine = charPositionInLine;
119	}
120
121	public function get channel():int {
122		return _channel;
123	}
124
125	public function set channel(channel:int):void {
126		_channel = channel;
127	}
128
129	public function set type(type:int):void {
130		_type = type;
131	}
132
133	public function get startIndex():int {
134		return _start;
135	}
136
137	public function set startIndex(start:int):void {
138		_start = start;
139	}
140
141	public function get stopIndex():int {
142		return _stop;
143	}
144
145	public function set stopIndex(stop:int):void {
146		_stop = stop;
147	}
148
149	public function get tokenIndex():int {
150		return _index;
151	}
152
153	public function set tokenIndex(index:int):void {
154		_index = index;
155	}
156
157	public function get inputStream():CharStream {
158		return _input;
159	}
160
161	public function set inputStream(input:CharStream):void {
162		_input = input;
163	}
164
165	public function toString():String {
166		var channelStr:String = "";
167		if ( channel>0 ) {
168			channelStr=",channel="+channel;
169		}
170		var txt:String = text;
171		if ( txt!=null ) {
172			txt = txt.replace("\n", "\\\\n");
173			txt = txt.replace("\r", "\\\\r");
174			txt = txt.replace("\t", "\\\\t");
175		}
176		else {
177			txt = "<no text>";
178		}
179		return "[@"+tokenIndex+","+startIndex+":"+stopIndex+"='"+txt+"',<"+type+">"+channelStr+","+line+":"+charPositionInLine+"]";
180	}
181}
182
183}