00001 /* 00002 [The "BSD licence"] 00003 Copyright (c) 2005-2008 Terence Parr 00004 All rights reserved. 00005 00006 Redistribution and use in source and binary forms, with or without 00007 modification, are permitted provided that the following conditions 00008 are met: 00009 1. Redistributions of source code must retain the above copyright 00010 notice, this list of conditions and the following disclaimer. 00011 2. Redistributions in binary form must reproduce the above copyright 00012 notice, this list of conditions and the following disclaimer in the 00013 documentation and/or other materials provided with the distribution. 00014 3. The name of the author may not be used to endorse or promote products 00015 derived from this software without specific prior written permission. 00016 00017 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00018 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00019 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 00020 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 00021 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 00022 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00023 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00024 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00025 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 00026 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 */ 00028 package org.antlr.runtime.tree; 00029 00030 import org.antlr.runtime.*; 00031 00033 public class CommonErrorNode extends CommonTree { 00034 public IntStream input; 00035 public Token start; 00036 public Token stop; 00037 public RecognitionException trappedException; 00038 00039 public CommonErrorNode(TokenStream input, Token start, Token stop, 00040 RecognitionException e) 00041 { 00042 //System.out.println("start: "+start+", stop: "+stop); 00043 if ( stop==null || 00044 (stop.getTokenIndex() < start.getTokenIndex() && 00045 stop.getType()!=Token.EOF) ) 00046 { 00047 // sometimes resync does not consume a token (when LT(1) is 00048 // in follow set. So, stop will be 1 to left to start. adjust. 00049 // Also handle case where start is the first token and no token 00050 // is consumed during recovery; LT(-1) will return null. 00051 stop = start; 00052 } 00053 this.input = input; 00054 this.start = start; 00055 this.stop = stop; 00056 this.trappedException = e; 00057 } 00058 00059 public boolean isNil() { 00060 return false; 00061 } 00062 00063 public int getType() { 00064 return Token.INVALID_TOKEN_TYPE; 00065 } 00066 00067 public String getText() { 00068 String badText = null; 00069 if ( start instanceof Token ) { 00070 int i = ((Token)start).getTokenIndex(); 00071 int j = ((Token)stop).getTokenIndex(); 00072 if ( ((Token)stop).getType() == Token.EOF ) { 00073 j = ((TokenStream)input).size(); 00074 } 00075 badText = ((TokenStream)input).toString(i, j); 00076 } 00077 else if ( start instanceof Tree ) { 00078 badText = ((TreeNodeStream)input).toString(start, stop); 00079 } 00080 else { 00081 // people should subclass if they alter the tree type so this 00082 // next one is for sure correct. 00083 badText = "<unknown>"; 00084 } 00085 return badText; 00086 } 00087 00088 public String toString() { 00089 if ( trappedException instanceof MissingTokenException ) { 00090 return "<missing type: "+ 00091 ((MissingTokenException)trappedException).getMissingType()+ 00092 ">"; 00093 } 00094 else if ( trappedException instanceof UnwantedTokenException ) { 00095 return "<extraneous: "+ 00096 ((UnwantedTokenException)trappedException).getUnexpectedToken()+ 00097 ", resync="+getText()+">"; 00098 } 00099 else if ( trappedException instanceof MismatchedTokenException ) { 00100 return "<mismatched token: "+trappedException.token+", resync="+getText()+">"; 00101 } 00102 else if ( trappedException instanceof NoViableAltException ) { 00103 return "<unexpected: "+trappedException.token+ 00104 ", resync="+getText()+">"; 00105 } 00106 return "<error: "+getText()+">"; 00107 } 00108 }
1.5.5