13447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein/*
23447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein [The "BSD license"]
33447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein Copyright (c) 2005-2009 Terence Parr
43447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein All rights reserved.
53447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
63447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein Redistribution and use in source and binary forms, with or without
73447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein modification, are permitted provided that the following conditions
83447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein are met:
93447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 1. Redistributions of source code must retain the above copyright
103447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein     notice, this list of conditions and the following disclaimer.
113447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 2. Redistributions in binary form must reproduce the above copyright
123447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein     notice, this list of conditions and the following disclaimer in the
133447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein     documentation and/or other materials provided with the distribution.
143447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 3. The name of the author may not be used to endorse or promote products
153447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein     derived from this software without specific prior written permission.
163447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
173447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
183447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
193447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
203447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
213447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
223447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
233447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
243447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
253447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
263447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
273447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */
283447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinpackage org.antlr.runtime;
293447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
303447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinimport java.io.Serializable;
313447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
323447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinpublic class CommonToken implements Token, Serializable {
333447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	protected int type;
343447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	protected int line;
353447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	protected int charPositionInLine = -1; // set to invalid position
363447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	protected int channel=DEFAULT_CHANNEL;
373447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	protected transient CharStream input;
383447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
393447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** We need to be able to change the text once in a while.  If
403447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  this is non-null, then getText should return this.  Note that
413447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  start/stop are not affected by changing this.
423447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	  */
433447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	protected String text;
443447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
453447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** What token number is this from 0..n-1 tokens; < 0 implies invalid index */
463447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	protected int index = -1;
473447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
483447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** The char position into the input buffer where this token starts */
493447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	protected int start;
503447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
513447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** The char position into the input buffer where this token stops */
523447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	protected int stop;
533447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
543447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public CommonToken(int type) {
553447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		this.type = type;
563447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
573447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
583447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public CommonToken(CharStream input, int type, int channel, int start, int stop) {
593447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		this.input = input;
603447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		this.type = type;
613447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		this.channel = channel;
623447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		this.start = start;
633447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		this.stop = stop;
643447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
653447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
663447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public CommonToken(int type, String text) {
673447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		this.type = type;
683447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		this.channel = DEFAULT_CHANNEL;
693447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		this.text = text;
703447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
713447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
723447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public CommonToken(Token oldToken) {
733447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		text = oldToken.getText();
743447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		type = oldToken.getType();
753447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		line = oldToken.getLine();
763447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		index = oldToken.getTokenIndex();
773447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		charPositionInLine = oldToken.getCharPositionInLine();
783447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		channel = oldToken.getChannel();
793447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein        input = oldToken.getInputStream();
803447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		if ( oldToken instanceof CommonToken ) {
813447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein			start = ((CommonToken)oldToken).start;
823447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein			stop = ((CommonToken)oldToken).stop;
833447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		}
843447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
853447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
863447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public int getType() {
873447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		return type;
883447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
893447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
903447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public void setLine(int line) {
913447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		this.line = line;
923447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
933447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
943447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public String getText() {
953447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		if ( text!=null ) {
963447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein			return text;
973447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		}
983447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		if ( input==null ) {
993447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein			return null;
1003447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		}
101324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver		int n = input.size();
102324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver		if ( start<n && stop<n) {
103324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver			return input.substring(start,stop);
104324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver		}
105324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver		else {
106324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver			return "<EOF>";
107324c4644fee44b9898524c09511bd33c3f12e2dfBen Gruver		}
1083447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
1093447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1103447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	/** Override the text for this token.  getText() will return this text
1113447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  rather than pulling from the buffer.  Note that this does not mean
1123447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  that start/stop indexes are not valid.  It means that that input
1133447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 *  was converted to a new string in the token object.
1143447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	 */
1153447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public void setText(String text) {
1163447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		this.text = text;
1173447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
1183447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1193447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public int getLine() {
1203447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		return line;
1213447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
1223447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1233447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public int getCharPositionInLine() {
1243447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		return charPositionInLine;
1253447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
1263447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1273447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public void setCharPositionInLine(int charPositionInLine) {
1283447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		this.charPositionInLine = charPositionInLine;
1293447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
1303447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1313447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public int getChannel() {
1323447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		return channel;
1333447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
1343447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1353447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public void setChannel(int channel) {
1363447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		this.channel = channel;
1373447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
1383447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1393447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public void setType(int type) {
1403447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		this.type = type;
1413447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
1423447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1433447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public int getStartIndex() {
1443447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		return start;
1453447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
1463447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1473447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public void setStartIndex(int start) {
1483447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		this.start = start;
1493447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
1503447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1513447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public int getStopIndex() {
1523447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		return stop;
1533447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
1543447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1553447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public void setStopIndex(int stop) {
1563447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		this.stop = stop;
1573447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
1583447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1593447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public int getTokenIndex() {
1603447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		return index;
1613447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
1623447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1633447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public void setTokenIndex(int index) {
1643447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		this.index = index;
1653447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
1663447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1673447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public CharStream getInputStream() {
1683447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		return input;
1693447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
1703447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1713447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public void setInputStream(CharStream input) {
1723447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		this.input = input;
1733447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
1743447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
1753447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public String toString() {
1763447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		String channelStr = "";
1773447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		if ( channel>0 ) {
1783447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein			channelStr=",channel="+channel;
1793447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		}
1803447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		String txt = getText();
1813447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		if ( txt!=null ) {
1823447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein			txt = txt.replaceAll("\n","\\\\n");
1833447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein			txt = txt.replaceAll("\r","\\\\r");
1843447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein			txt = txt.replaceAll("\t","\\\\t");
1853447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		}
1863447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		else {
1873447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein			txt = "<no text>";
1883447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		}
1893447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		return "[@"+getTokenIndex()+","+start+":"+stop+"='"+txt+"',<"+type+">"+channelStr+","+line+":"+getCharPositionInLine()+"]";
1903447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
1913447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein}
192