Home | History | Annotate | Download | only in runtime
      1 /*
      2  [The "BSD license"]
      3  Copyright (c) 2005-2009 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  */
     28 package org.antlr.runtime;
     29 
     30 /** A Token object like we'd use in ANTLR 2.x; has an actual string created
     31  *  and associated with this object.  These objects are needed for imaginary
     32  *  tree nodes that have payload objects.  We need to create a Token object
     33  *  that has a string; the tree node will point at this token.  CommonToken
     34  *  has indexes into a char stream and hence cannot be used to introduce
     35  *  new strings.
     36  */
     37 public class ClassicToken implements Token {
     38 	protected String text;
     39 	protected int type;
     40 	protected int line;
     41 	protected int charPositionInLine;
     42 	protected int channel=DEFAULT_CHANNEL;
     43 
     44 	/** What token number is this from 0..n-1 tokens */
     45 	protected int index;
     46 
     47 	public ClassicToken(int type) {
     48 		this.type = type;
     49 	}
     50 
     51 	public ClassicToken(Token oldToken) {
     52 		text = oldToken.getText();
     53 		type = oldToken.getType();
     54 		line = oldToken.getLine();
     55 		charPositionInLine = oldToken.getCharPositionInLine();
     56 		channel = oldToken.getChannel();
     57 	}
     58 
     59 	public ClassicToken(int type, String text) {
     60 		this.type = type;
     61 		this.text = text;
     62 	}
     63 
     64 	public ClassicToken(int type, String text, int channel) {
     65 		this.type = type;
     66 		this.text = text;
     67 		this.channel = channel;
     68 	}
     69 
     70 	public int getType() {
     71 		return type;
     72 	}
     73 
     74 	public void setLine(int line) {
     75 		this.line = line;
     76 	}
     77 
     78 	public String getText() {
     79 		return text;
     80 	}
     81 
     82 	public void setText(String text) {
     83 		this.text = text;
     84 	}
     85 
     86 	public int getLine() {
     87 		return line;
     88 	}
     89 
     90 	public int getCharPositionInLine() {
     91 		return charPositionInLine;
     92 	}
     93 
     94 	public void setCharPositionInLine(int charPositionInLine) {
     95 		this.charPositionInLine = charPositionInLine;
     96 	}
     97 
     98 	public int getChannel() {
     99 		return channel;
    100 	}
    101 
    102 	public void setChannel(int channel) {
    103 		this.channel = channel;
    104 	}
    105 
    106 	public void setType(int type) {
    107 		this.type = type;
    108 	}
    109 
    110 	public int getTokenIndex() {
    111 		return index;
    112 	}
    113 
    114 	public void setTokenIndex(int index) {
    115 		this.index = index;
    116 	}
    117 
    118 	public CharStream getInputStream() {
    119 		return null;
    120 	}
    121 
    122 	public void setInputStream(CharStream input) {
    123 	}
    124 
    125 	public String toString() {
    126 		String channelStr = "";
    127 		if ( channel>0 ) {
    128 			channelStr=",channel="+channel;
    129 		}
    130 		String txt = getText();
    131 		if ( txt!=null ) {
    132 			txt = txt.replaceAll("\n","\\\\n");
    133 			txt = txt.replaceAll("\r","\\\\r");
    134 			txt = txt.replaceAll("\t","\\\\t");
    135 		}
    136 		else {
    137 			txt = "<no text>";
    138 		}
    139 		return "[@"+getTokenIndex()+",'"+txt+"',<"+type+">"+channelStr+","+line+":"+getCharPositionInLine()+"]";
    140 	}
    141 }
    142