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 Sapperstein/** A Token object like we'd use in ANTLR 2.x; has an actual string created 313447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * and associated with this object. These objects are needed for imaginary 323447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * tree nodes that have payload objects. We need to create a Token object 333447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * that has a string; the tree node will point at this token. CommonToken 343447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * has indexes into a char stream and hence cannot be used to introduce 353447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein * new strings. 363447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */ 373447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinpublic class ClassicToken implements Token { 383447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein protected String text; 393447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein protected int type; 403447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein protected int line; 413447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein protected int charPositionInLine; 423447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein protected int channel=DEFAULT_CHANNEL; 433447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 443447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein /** What token number is this from 0..n-1 tokens */ 453447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein protected int index; 463447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 473447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public ClassicToken(int type) { 483447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.type = type; 493447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 503447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 513447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public ClassicToken(Token oldToken) { 523447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein text = oldToken.getText(); 533447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein type = oldToken.getType(); 543447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein line = oldToken.getLine(); 553447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein charPositionInLine = oldToken.getCharPositionInLine(); 563447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein channel = oldToken.getChannel(); 573447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 583447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 593447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public ClassicToken(int type, String text) { 603447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.type = type; 613447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.text = text; 623447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 633447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 643447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public ClassicToken(int type, String text, int channel) { 653447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.type = type; 663447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.text = text; 673447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.channel = channel; 683447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 693447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 703447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public int getType() { 713447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return type; 723447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 733447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 743447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public void setLine(int line) { 753447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.line = line; 763447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 773447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 783447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public String getText() { 793447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return text; 803447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 813447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 823447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public void setText(String text) { 833447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.text = text; 843447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 853447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 863447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public int getLine() { 873447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return line; 883447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 893447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 903447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public int getCharPositionInLine() { 913447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return charPositionInLine; 923447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 933447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 943447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public void setCharPositionInLine(int charPositionInLine) { 953447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.charPositionInLine = charPositionInLine; 963447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 973447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 983447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public int getChannel() { 993447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return channel; 1003447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1013447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 1023447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public void setChannel(int channel) { 1033447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.channel = channel; 1043447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1053447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 1063447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public void setType(int type) { 1073447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.type = type; 1083447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1093447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 1103447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public int getTokenIndex() { 1113447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return index; 1123447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1133447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 1143447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public void setTokenIndex(int index) { 1153447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.index = index; 1163447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1173447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 1183447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public CharStream getInputStream() { 1193447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return null; 1203447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1213447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 1223447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public void setInputStream(CharStream input) { 1233447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1243447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 1253447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public String toString() { 1263447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein String channelStr = ""; 1273447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( channel>0 ) { 1283447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein channelStr=",channel="+channel; 1293447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1303447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein String txt = getText(); 1313447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( txt!=null ) { 1323447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein txt = txt.replaceAll("\n","\\\\n"); 1333447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein txt = txt.replaceAll("\r","\\\\r"); 1343447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein txt = txt.replaceAll("\t","\\\\t"); 1353447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1363447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein else { 1373447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein txt = "<no text>"; 1383447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1393447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return "[@"+getTokenIndex()+",'"+txt+"',<"+type+">"+channelStr+","+line+":"+getCharPositionInLine()+"]"; 1403447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1413447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein} 142