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.tree; 293447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 303447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinimport org.antlr.runtime.*; 313447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 323447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein/** A node representing erroneous token range in token stream */ 333447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinpublic class CommonErrorNode extends CommonTree { 343447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public IntStream input; 353447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public Token start; 363447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public Token stop; 373447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public RecognitionException trappedException; 383447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 393447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public CommonErrorNode(TokenStream input, Token start, Token stop, 403447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein RecognitionException e) 413447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein { 423447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein //System.out.println("start: "+start+", stop: "+stop); 433447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( stop==null || 443447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein (stop.getTokenIndex() < start.getTokenIndex() && 453447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein stop.getType()!=Token.EOF) ) 463447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein { 473447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein // sometimes resync does not consume a token (when LT(1) is 483447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein // in follow set. So, stop will be 1 to left to start. adjust. 493447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein // Also handle case where start is the first token and no token 503447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein // is consumed during recovery; LT(-1) will return null. 513447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein stop = start; 523447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 533447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.input = input; 543447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.start = start; 553447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.stop = stop; 563447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein this.trappedException = e; 573447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 583447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 593447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public boolean isNil() { 603447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return false; 613447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 623447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 633447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public int getType() { 643447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return Token.INVALID_TOKEN_TYPE; 653447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 663447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 673447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public String getText() { 683447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein String badText = null; 693447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( start instanceof Token ) { 703447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein int i = ((Token)start).getTokenIndex(); 713447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein int j = ((Token)stop).getTokenIndex(); 723447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( ((Token)stop).getType() == Token.EOF ) { 733447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein j = ((TokenStream)input).size(); 743447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 753447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein badText = ((TokenStream)input).toString(i, j); 763447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 773447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein else if ( start instanceof Tree ) { 783447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein badText = ((TreeNodeStream)input).toString(start, stop); 793447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 803447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein else { 813447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein // people should subclass if they alter the tree type so this 823447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein // next one is for sure correct. 833447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein badText = "<unknown>"; 843447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 853447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return badText; 863447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 873447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 883447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein public String toString() { 893447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein if ( trappedException instanceof MissingTokenException ) { 903447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return "<missing type: "+ 913447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein ((MissingTokenException)trappedException).getMissingType()+ 923447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein ">"; 933447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 943447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein else if ( trappedException instanceof UnwantedTokenException ) { 953447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return "<extraneous: "+ 963447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein ((UnwantedTokenException)trappedException).getUnexpectedToken()+ 973447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein ", resync="+getText()+">"; 983447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 993447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein else if ( trappedException instanceof MismatchedTokenException ) { 1003447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return "<mismatched token: "+trappedException.token+", resync="+getText()+">"; 1013447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1023447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein else if ( trappedException instanceof NoViableAltException ) { 1033447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return "<unexpected: "+trappedException.token+ 1043447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein ", resync="+getText()+">"; 1053447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1063447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein return "<error: "+getText()+">"; 1073447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein } 1083447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein} 109